Coverage Report

Created: 2025-06-12 06:51

/src/opencv/3rdparty/libtiff/tif_dirread.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 *
28
 * Directory Read Support Routines.
29
 */
30
31
/* Suggested pending improvements:
32
 * - add a field 'field_info' to the TIFFDirEntry structure, and set that with
33
 *   the pointer to the appropriate TIFFField structure early on in
34
 *   TIFFReadDirectory, so as to eliminate current possibly repetitive lookup.
35
 */
36
37
#include "tiffconf.h"
38
#include "tiffiop.h"
39
#include <float.h>
40
#include <limits.h>
41
#include <stdlib.h>
42
#include <string.h>
43
44
453k
#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
12.6k
{
281
12.6k
    UInt64Aligned_t result;
282
283
12.6k
    result.c[0] = value[0];
284
12.6k
    result.c[1] = value[1];
285
12.6k
    result.c[2] = value[2];
286
12.6k
    result.c[3] = value[3];
287
12.6k
    result.c[4] = value[4];
288
12.6k
    result.c[5] = value[5];
289
12.6k
    result.c[6] = value[6];
290
12.6k
    result.c[7] = value[7];
291
292
12.6k
    return result.l;
293
12.6k
}
294
295
static enum TIFFReadDirEntryErr
296
TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
297
7
{
298
7
    enum TIFFReadDirEntryErr err;
299
7
    if (direntry->tdir_count != 1)
300
7
        return (TIFFReadDirEntryErrCount);
301
0
    switch (direntry->tdir_type)
302
0
    {
303
0
        case TIFF_BYTE:
304
0
        case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
305
                                field_readcount==1 */
306
0
            TIFFReadDirEntryCheckedByte(tif, direntry, value);
307
0
            return (TIFFReadDirEntryErrOk);
308
0
        case TIFF_SBYTE:
309
0
        {
310
0
            int8_t m;
311
0
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
312
0
            err = TIFFReadDirEntryCheckRangeByteSbyte(m);
313
0
            if (err != TIFFReadDirEntryErrOk)
314
0
                return (err);
315
0
            *value = (uint8_t)m;
316
0
            return (TIFFReadDirEntryErrOk);
317
0
        }
318
0
        case TIFF_SHORT:
319
0
        {
320
0
            uint16_t m;
321
0
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
322
0
            err = TIFFReadDirEntryCheckRangeByteShort(m);
323
0
            if (err != TIFFReadDirEntryErrOk)
324
0
                return (err);
325
0
            *value = (uint8_t)m;
326
0
            return (TIFFReadDirEntryErrOk);
327
0
        }
328
0
        case TIFF_SSHORT:
329
0
        {
330
0
            int16_t m;
331
0
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
332
0
            err = TIFFReadDirEntryCheckRangeByteSshort(m);
333
0
            if (err != TIFFReadDirEntryErrOk)
334
0
                return (err);
335
0
            *value = (uint8_t)m;
336
0
            return (TIFFReadDirEntryErrOk);
337
0
        }
338
0
        case TIFF_LONG:
339
0
        {
340
0
            uint32_t m;
341
0
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
342
0
            err = TIFFReadDirEntryCheckRangeByteLong(m);
343
0
            if (err != TIFFReadDirEntryErrOk)
344
0
                return (err);
345
0
            *value = (uint8_t)m;
346
0
            return (TIFFReadDirEntryErrOk);
347
0
        }
348
0
        case TIFF_SLONG:
349
0
        {
350
0
            int32_t m;
351
0
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
352
0
            err = TIFFReadDirEntryCheckRangeByteSlong(m);
353
0
            if (err != TIFFReadDirEntryErrOk)
354
0
                return (err);
355
0
            *value = (uint8_t)m;
356
0
            return (TIFFReadDirEntryErrOk);
357
0
        }
358
0
        case TIFF_LONG8:
359
0
        {
360
0
            uint64_t m;
361
0
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
362
0
            if (err != TIFFReadDirEntryErrOk)
363
0
                return (err);
364
0
            err = TIFFReadDirEntryCheckRangeByteLong8(m);
365
0
            if (err != TIFFReadDirEntryErrOk)
366
0
                return (err);
367
0
            *value = (uint8_t)m;
368
0
            return (TIFFReadDirEntryErrOk);
369
0
        }
370
0
        case TIFF_SLONG8:
371
0
        {
372
0
            int64_t m;
373
0
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
374
0
            if (err != TIFFReadDirEntryErrOk)
375
0
                return (err);
376
0
            err = TIFFReadDirEntryCheckRangeByteSlong8(m);
377
0
            if (err != TIFFReadDirEntryErrOk)
378
0
                return (err);
379
0
            *value = (uint8_t)m;
380
0
            return (TIFFReadDirEntryErrOk);
381
0
        }
382
0
        default:
383
0
            return (TIFFReadDirEntryErrType);
384
0
    }
385
0
}
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
6.96k
{
484
6.96k
    enum TIFFReadDirEntryErr err;
485
6.96k
    if (direntry->tdir_count != 1)
486
737
        return (TIFFReadDirEntryErrCount);
487
6.22k
    switch (direntry->tdir_type)
488
6.22k
    {
489
95
        case TIFF_BYTE:
490
95
        {
491
95
            uint8_t m;
492
95
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
493
95
            *value = (uint16_t)m;
494
95
            return (TIFFReadDirEntryErrOk);
495
0
        }
496
65
        case TIFF_SBYTE:
497
65
        {
498
65
            int8_t m;
499
65
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
500
65
            err = TIFFReadDirEntryCheckRangeShortSbyte(m);
501
65
            if (err != TIFFReadDirEntryErrOk)
502
0
                return (err);
503
65
            *value = (uint16_t)m;
504
65
            return (TIFFReadDirEntryErrOk);
505
65
        }
506
5.95k
        case TIFF_SHORT:
507
5.95k
            TIFFReadDirEntryCheckedShort(tif, direntry, value);
508
5.95k
            return (TIFFReadDirEntryErrOk);
509
29
        case TIFF_SSHORT:
510
29
        {
511
29
            int16_t m;
512
29
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
513
29
            err = TIFFReadDirEntryCheckRangeShortSshort(m);
514
29
            if (err != TIFFReadDirEntryErrOk)
515
0
                return (err);
516
29
            *value = (uint16_t)m;
517
29
            return (TIFFReadDirEntryErrOk);
518
29
        }
519
11
        case TIFF_LONG:
520
11
        {
521
11
            uint32_t m;
522
11
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
523
11
            err = TIFFReadDirEntryCheckRangeShortLong(m);
524
11
            if (err != TIFFReadDirEntryErrOk)
525
0
                return (err);
526
11
            *value = (uint16_t)m;
527
11
            return (TIFFReadDirEntryErrOk);
528
11
        }
529
3
        case TIFF_SLONG:
530
3
        {
531
3
            int32_t m;
532
3
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
533
3
            err = TIFFReadDirEntryCheckRangeShortSlong(m);
534
3
            if (err != TIFFReadDirEntryErrOk)
535
0
                return (err);
536
3
            *value = (uint16_t)m;
537
3
            return (TIFFReadDirEntryErrOk);
538
3
        }
539
0
        case TIFF_LONG8:
540
0
        {
541
0
            uint64_t m;
542
0
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
543
0
            if (err != TIFFReadDirEntryErrOk)
544
0
                return (err);
545
0
            err = TIFFReadDirEntryCheckRangeShortLong8(m);
546
0
            if (err != TIFFReadDirEntryErrOk)
547
0
                return (err);
548
0
            *value = (uint16_t)m;
549
0
            return (TIFFReadDirEntryErrOk);
550
0
        }
551
0
        case TIFF_SLONG8:
552
0
        {
553
0
            int64_t m;
554
0
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
555
0
            if (err != TIFFReadDirEntryErrOk)
556
0
                return (err);
557
0
            err = TIFFReadDirEntryCheckRangeShortSlong8(m);
558
0
            if (err != TIFFReadDirEntryErrOk)
559
0
                return (err);
560
0
            *value = (uint16_t)m;
561
0
            return (TIFFReadDirEntryErrOk);
562
0
        }
563
67
        default:
564
67
            return (TIFFReadDirEntryErrType);
565
6.22k
    }
566
6.22k
} /*-- 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
5.20k
{
655
5.20k
    enum TIFFReadDirEntryErr err;
656
5.20k
    if (direntry->tdir_count != 1)
657
327
        return (TIFFReadDirEntryErrCount);
658
4.87k
    switch (direntry->tdir_type)
659
4.87k
    {
660
382
        case TIFF_BYTE:
661
382
        {
662
382
            uint8_t m;
663
382
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
664
382
            *value = (uint32_t)m;
665
382
            return (TIFFReadDirEntryErrOk);
666
0
        }
667
10
        case TIFF_SBYTE:
668
10
        {
669
10
            int8_t m;
670
10
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
671
10
            err = TIFFReadDirEntryCheckRangeLongSbyte(m);
672
10
            if (err != TIFFReadDirEntryErrOk)
673
0
                return (err);
674
10
            *value = (uint32_t)m;
675
10
            return (TIFFReadDirEntryErrOk);
676
10
        }
677
4.05k
        case TIFF_SHORT:
678
4.05k
        {
679
4.05k
            uint16_t m;
680
4.05k
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
681
4.05k
            *value = (uint32_t)m;
682
4.05k
            return (TIFFReadDirEntryErrOk);
683
10
        }
684
3
        case TIFF_SSHORT:
685
3
        {
686
3
            int16_t m;
687
3
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
688
3
            err = TIFFReadDirEntryCheckRangeLongSshort(m);
689
3
            if (err != TIFFReadDirEntryErrOk)
690
0
                return (err);
691
3
            *value = (uint32_t)m;
692
3
            return (TIFFReadDirEntryErrOk);
693
3
        }
694
391
        case TIFF_LONG:
695
391
            TIFFReadDirEntryCheckedLong(tif, direntry, value);
696
391
            return (TIFFReadDirEntryErrOk);
697
7
        case TIFF_SLONG:
698
7
        {
699
7
            int32_t m;
700
7
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
701
7
            err = TIFFReadDirEntryCheckRangeLongSlong(m);
702
7
            if (err != TIFFReadDirEntryErrOk)
703
0
                return (err);
704
7
            *value = (uint32_t)m;
705
7
            return (TIFFReadDirEntryErrOk);
706
7
        }
707
16
        case TIFF_LONG8:
708
16
        {
709
16
            uint64_t m;
710
16
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
711
16
            if (err != TIFFReadDirEntryErrOk)
712
0
                return (err);
713
16
            err = TIFFReadDirEntryCheckRangeLongLong8(m);
714
16
            if (err != TIFFReadDirEntryErrOk)
715
1
                return (err);
716
15
            *value = (uint32_t)m;
717
15
            return (TIFFReadDirEntryErrOk);
718
16
        }
719
4
        case TIFF_SLONG8:
720
4
        {
721
4
            int64_t m;
722
4
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
723
4
            if (err != TIFFReadDirEntryErrOk)
724
3
                return (err);
725
1
            err = TIFFReadDirEntryCheckRangeLongSlong8(m);
726
1
            if (err != TIFFReadDirEntryErrOk)
727
1
                return (err);
728
0
            *value = (uint32_t)m;
729
0
            return (TIFFReadDirEntryErrOk);
730
1
        }
731
8
        default:
732
8
            return (TIFFReadDirEntryErrType);
733
4.87k
    }
734
4.87k
} /*-- 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
612
{
966
612
    enum TIFFReadDirEntryErr err;
967
612
    if (direntry->tdir_count != 1)
968
233
        return (TIFFReadDirEntryErrCount);
969
379
    switch (direntry->tdir_type)
970
379
    {
971
2
        case TIFF_BYTE:
972
2
        {
973
2
            uint8_t m;
974
2
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
975
2
            *value = (float)m;
976
2
            return (TIFFReadDirEntryErrOk);
977
0
        }
978
0
        case TIFF_SBYTE:
979
0
        {
980
0
            int8_t m;
981
0
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
982
0
            *value = (float)m;
983
0
            return (TIFFReadDirEntryErrOk);
984
0
        }
985
58
        case TIFF_SHORT:
986
58
        {
987
58
            uint16_t m;
988
58
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
989
58
            *value = (float)m;
990
58
            return (TIFFReadDirEntryErrOk);
991
0
        }
992
0
        case TIFF_SSHORT:
993
0
        {
994
0
            int16_t m;
995
0
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
996
0
            *value = (float)m;
997
0
            return (TIFFReadDirEntryErrOk);
998
0
        }
999
31
        case TIFF_LONG:
1000
31
        {
1001
31
            uint32_t m;
1002
31
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1003
31
            *value = (float)m;
1004
31
            return (TIFFReadDirEntryErrOk);
1005
0
        }
1006
14
        case TIFF_SLONG:
1007
14
        {
1008
14
            int32_t m;
1009
14
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1010
14
            *value = (float)m;
1011
14
            return (TIFFReadDirEntryErrOk);
1012
0
        }
1013
0
        case TIFF_LONG8:
1014
0
        {
1015
0
            uint64_t m;
1016
0
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1017
0
            if (err != TIFFReadDirEntryErrOk)
1018
0
                return (err);
1019
#if defined(__WIN32__) && (_MSC_VER < 1500)
1020
            /*
1021
             * XXX: MSVC 6.0 does not support conversion
1022
             * of 64-bit integers into floating point
1023
             * values.
1024
             */
1025
            *value = _TIFFUInt64ToFloat(m);
1026
#else
1027
0
            *value = (float)m;
1028
0
#endif
1029
0
            return (TIFFReadDirEntryErrOk);
1030
0
        }
1031
11
        case TIFF_SLONG8:
1032
11
        {
1033
11
            int64_t m;
1034
11
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1035
11
            if (err != TIFFReadDirEntryErrOk)
1036
8
                return (err);
1037
3
            *value = (float)m;
1038
3
            return (TIFFReadDirEntryErrOk);
1039
11
        }
1040
189
        case TIFF_RATIONAL:
1041
189
        {
1042
189
            double m;
1043
189
            err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
1044
189
            if (err != TIFFReadDirEntryErrOk)
1045
10
                return (err);
1046
179
            *value = (float)m;
1047
179
            return (TIFFReadDirEntryErrOk);
1048
189
        }
1049
17
        case TIFF_SRATIONAL:
1050
17
        {
1051
17
            double m;
1052
17
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
1053
17
            if (err != TIFFReadDirEntryErrOk)
1054
1
                return (err);
1055
16
            *value = (float)m;
1056
16
            return (TIFFReadDirEntryErrOk);
1057
17
        }
1058
0
        case TIFF_FLOAT:
1059
0
            TIFFReadDirEntryCheckedFloat(tif, direntry, value);
1060
0
            return (TIFFReadDirEntryErrOk);
1061
5
        case TIFF_DOUBLE:
1062
5
        {
1063
5
            double m;
1064
5
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
1065
5
            if (err != TIFFReadDirEntryErrOk)
1066
3
                return (err);
1067
2
            if ((m > FLT_MAX) || (m < -FLT_MAX))
1068
2
                return (TIFFReadDirEntryErrRange);
1069
0
            *value = (float)m;
1070
0
            return (TIFFReadDirEntryErrOk);
1071
2
        }
1072
52
        default:
1073
52
            return (TIFFReadDirEntryErrType);
1074
379
    }
1075
379
}
1076
1077
static enum TIFFReadDirEntryErr
1078
TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
1079
1
{
1080
1
    enum TIFFReadDirEntryErr err;
1081
1
    if (direntry->tdir_count != 1)
1082
0
        return (TIFFReadDirEntryErrCount);
1083
1
    switch (direntry->tdir_type)
1084
1
    {
1085
0
        case TIFF_BYTE:
1086
0
        {
1087
0
            uint8_t m;
1088
0
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
1089
0
            *value = (double)m;
1090
0
            return (TIFFReadDirEntryErrOk);
1091
0
        }
1092
0
        case TIFF_SBYTE:
1093
0
        {
1094
0
            int8_t m;
1095
0
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
1096
0
            *value = (double)m;
1097
0
            return (TIFFReadDirEntryErrOk);
1098
0
        }
1099
0
        case TIFF_SHORT:
1100
0
        {
1101
0
            uint16_t m;
1102
0
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
1103
0
            *value = (double)m;
1104
0
            return (TIFFReadDirEntryErrOk);
1105
0
        }
1106
0
        case TIFF_SSHORT:
1107
0
        {
1108
0
            int16_t m;
1109
0
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
1110
0
            *value = (double)m;
1111
0
            return (TIFFReadDirEntryErrOk);
1112
0
        }
1113
0
        case TIFF_LONG:
1114
0
        {
1115
0
            uint32_t m;
1116
0
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1117
0
            *value = (double)m;
1118
0
            return (TIFFReadDirEntryErrOk);
1119
0
        }
1120
1
        case TIFF_SLONG:
1121
1
        {
1122
1
            int32_t m;
1123
1
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
1124
1
            *value = (double)m;
1125
1
            return (TIFFReadDirEntryErrOk);
1126
0
        }
1127
0
        case TIFF_LONG8:
1128
0
        {
1129
0
            uint64_t m;
1130
0
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
1131
0
            if (err != TIFFReadDirEntryErrOk)
1132
0
                return (err);
1133
#if defined(__WIN32__) && (_MSC_VER < 1500)
1134
            /*
1135
             * XXX: MSVC 6.0 does not support conversion
1136
             * of 64-bit integers into floating point
1137
             * values.
1138
             */
1139
            *value = _TIFFUInt64ToDouble(m);
1140
#else
1141
0
            *value = (double)m;
1142
0
#endif
1143
0
            return (TIFFReadDirEntryErrOk);
1144
0
        }
1145
0
        case TIFF_SLONG8:
1146
0
        {
1147
0
            int64_t m;
1148
0
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
1149
0
            if (err != TIFFReadDirEntryErrOk)
1150
0
                return (err);
1151
0
            *value = (double)m;
1152
0
            return (TIFFReadDirEntryErrOk);
1153
0
        }
1154
0
        case TIFF_RATIONAL:
1155
0
            err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
1156
0
            return (err);
1157
0
        case TIFF_SRATIONAL:
1158
0
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
1159
0
            return (err);
1160
0
        case TIFF_FLOAT:
1161
0
        {
1162
0
            float m;
1163
0
            TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
1164
0
            *value = (double)m;
1165
0
            return (TIFFReadDirEntryErrOk);
1166
0
        }
1167
0
        case TIFF_DOUBLE:
1168
0
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
1169
0
            return (err);
1170
0
        default:
1171
0
            return (TIFFReadDirEntryErrType);
1172
1
    }
1173
1
}
1174
1175
static enum TIFFReadDirEntryErr
1176
TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
1177
83
{
1178
83
    enum TIFFReadDirEntryErr err;
1179
83
    if (direntry->tdir_count != 1)
1180
29
        return (TIFFReadDirEntryErrCount);
1181
54
    switch (direntry->tdir_type)
1182
54
    {
1183
53
        case TIFF_LONG:
1184
53
        case TIFF_IFD:
1185
53
        {
1186
53
            uint32_t m;
1187
53
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1188
53
            *value = (uint64_t)m;
1189
53
            return (TIFFReadDirEntryErrOk);
1190
53
        }
1191
1
        case TIFF_LONG8:
1192
1
        case TIFF_IFD8:
1193
1
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
1194
1
            return (err);
1195
0
        default:
1196
0
            return (TIFFReadDirEntryErrType);
1197
54
    }
1198
54
}
1199
1200
0
#define INITIAL_THRESHOLD (1024 * 1024)
1201
0
#define THRESHOLD_MULTIPLIER 10
1202
#define MAX_THRESHOLD                                                          \
1203
0
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
1204
0
     INITIAL_THRESHOLD)
1205
1206
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
1207
                                                               uint64_t offset,
1208
                                                               tmsize_t size,
1209
                                                               void **pdest)
1210
0
{
1211
0
#if SIZEOF_SIZE_T == 8
1212
0
    tmsize_t threshold = INITIAL_THRESHOLD;
1213
0
#endif
1214
0
    tmsize_t already_read = 0;
1215
1216
0
    assert(!isMapped(tif));
1217
1218
0
    if (!SeekOK(tif, offset))
1219
0
        return (TIFFReadDirEntryErrIo);
1220
1221
    /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
1222
    /* so as to avoid allocating too much memory in case the file is too */
1223
    /* short. We could ask for the file size, but this might be */
1224
    /* expensive with some I/O layers (think of reading a gzipped file) */
1225
    /* Restrict to 64 bit processes, so as to avoid reallocs() */
1226
    /* on 32 bit processes where virtual memory is scarce.  */
1227
0
    while (already_read < size)
1228
0
    {
1229
0
        void *new_dest;
1230
0
        tmsize_t bytes_read;
1231
0
        tmsize_t to_read = size - already_read;
1232
0
#if SIZEOF_SIZE_T == 8
1233
0
        if (to_read >= threshold && threshold < MAX_THRESHOLD)
1234
0
        {
1235
0
            to_read = threshold;
1236
0
            threshold *= THRESHOLD_MULTIPLIER;
1237
0
        }
1238
0
#endif
1239
1240
0
        new_dest =
1241
0
            (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
1242
0
        if (new_dest == NULL)
1243
0
        {
1244
0
            TIFFErrorExtR(tif, tif->tif_name,
1245
0
                          "Failed to allocate memory for %s "
1246
0
                          "(%" TIFF_SSIZE_FORMAT
1247
0
                          " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1248
0
                          "TIFFReadDirEntryArray", (tmsize_t)1,
1249
0
                          already_read + to_read);
1250
0
            return TIFFReadDirEntryErrAlloc;
1251
0
        }
1252
0
        *pdest = new_dest;
1253
1254
0
        bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
1255
0
        already_read += bytes_read;
1256
0
        if (bytes_read != to_read)
1257
0
        {
1258
0
            return TIFFReadDirEntryErrIo;
1259
0
        }
1260
0
    }
1261
0
    return TIFFReadDirEntryErrOk;
1262
0
}
1263
1264
/* Caution: if raising that value, make sure int32 / uint32 overflows can't
1265
 * occur elsewhere */
1266
48.1k
#define MAX_SIZE_TAG_DATA 2147483647U
1267
1268
static enum TIFFReadDirEntryErr
1269
TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
1270
                               uint32_t *count, uint32_t desttypesize,
1271
                               void **value, uint64_t maxcount)
1272
25.6k
{
1273
25.6k
    int typesize;
1274
25.6k
    uint32_t datasize;
1275
25.6k
    void *data;
1276
25.6k
    uint64_t target_count64;
1277
25.6k
    int original_datasize_clamped;
1278
25.6k
    typesize = TIFFDataWidth(direntry->tdir_type);
1279
1280
25.6k
    target_count64 =
1281
25.6k
        (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
1282
1283
25.6k
    if ((target_count64 == 0) || (typesize == 0))
1284
778
    {
1285
778
        *value = 0;
1286
778
        return (TIFFReadDirEntryErrOk);
1287
778
    }
1288
24.8k
    (void)desttypesize;
1289
1290
    /* We just want to know if the original tag size is more than 4 bytes
1291
     * (classic TIFF) or 8 bytes (BigTIFF)
1292
     */
1293
24.8k
    original_datasize_clamped =
1294
24.8k
        ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
1295
24.8k
        typesize;
1296
1297
    /*
1298
     * As a sanity check, make sure we have no more than a 2GB tag array
1299
     * in either the current data type or the dest data type.  This also
1300
     * avoids problems with overflow of tmsize_t on 32bit systems.
1301
     */
1302
24.8k
    if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
1303
1.65k
        return (TIFFReadDirEntryErrSizesan);
1304
23.2k
    if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
1305
0
        return (TIFFReadDirEntryErrSizesan);
1306
1307
23.2k
    *count = (uint32_t)target_count64;
1308
23.2k
    datasize = (*count) * typesize;
1309
23.2k
    assert((tmsize_t)datasize > 0);
1310
1311
23.2k
    if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
1312
12.6k
        return TIFFReadDirEntryErrIo;
1313
1314
10.5k
    if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
1315
0
                           (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
1316
0
    {
1317
0
        data = NULL;
1318
0
    }
1319
10.5k
    else
1320
10.5k
    {
1321
10.5k
        data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
1322
10.5k
        if (data == 0)
1323
0
            return (TIFFReadDirEntryErrAlloc);
1324
10.5k
    }
1325
10.5k
    if (!(tif->tif_flags & TIFF_BIGTIFF))
1326
10.5k
    {
1327
        /* Only the condition on original_datasize_clamped. The second
1328
         * one is implied, but Coverity Scan cannot see it. */
1329
10.5k
        if (original_datasize_clamped <= 4 && datasize <= 4)
1330
2.97k
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1331
7.53k
        else
1332
7.53k
        {
1333
7.53k
            enum TIFFReadDirEntryErr err;
1334
7.53k
            uint32_t offset = direntry->tdir_offset.toff_long;
1335
7.53k
            if (tif->tif_flags & TIFF_SWAB)
1336
857
                TIFFSwabLong(&offset);
1337
7.53k
            if (isMapped(tif))
1338
7.53k
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1339
7.53k
                                           (tmsize_t)datasize, data);
1340
0
            else
1341
0
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1342
0
                                                     (tmsize_t)datasize, &data);
1343
7.53k
            if (err != TIFFReadDirEntryErrOk)
1344
1.61k
            {
1345
1.61k
                _TIFFfreeExt(tif, data);
1346
1.61k
                return (err);
1347
1.61k
            }
1348
7.53k
        }
1349
10.5k
    }
1350
22
    else
1351
22
    {
1352
        /* See above comment for the Classic TIFF case */
1353
22
        if (original_datasize_clamped <= 8 && datasize <= 8)
1354
7
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1355
15
        else
1356
15
        {
1357
15
            enum TIFFReadDirEntryErr err;
1358
15
            uint64_t offset = direntry->tdir_offset.toff_long8;
1359
15
            if (tif->tif_flags & TIFF_SWAB)
1360
5
                TIFFSwabLong8(&offset);
1361
15
            if (isMapped(tif))
1362
15
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
1363
15
                                           (tmsize_t)datasize, data);
1364
0
            else
1365
0
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1366
0
                                                     (tmsize_t)datasize, &data);
1367
15
            if (err != TIFFReadDirEntryErrOk)
1368
10
            {
1369
10
                _TIFFfreeExt(tif, data);
1370
10
                return (err);
1371
10
            }
1372
15
        }
1373
22
    }
1374
8.91k
    *value = data;
1375
8.91k
    return (TIFFReadDirEntryErrOk);
1376
10.5k
}
1377
1378
static enum TIFFReadDirEntryErr
1379
TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
1380
                      uint32_t desttypesize, void **value)
1381
22.7k
{
1382
22.7k
    return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
1383
22.7k
                                          value, ~((uint64_t)0));
1384
22.7k
}
1385
1386
static enum TIFFReadDirEntryErr
1387
TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
1388
8.89k
{
1389
8.89k
    enum TIFFReadDirEntryErr err;
1390
8.89k
    uint32_t count;
1391
8.89k
    void *origdata;
1392
8.89k
    uint8_t *data;
1393
8.89k
    switch (direntry->tdir_type)
1394
8.89k
    {
1395
4.51k
        case TIFF_ASCII:
1396
4.72k
        case TIFF_UNDEFINED:
1397
8.43k
        case TIFF_BYTE:
1398
8.44k
        case TIFF_SBYTE:
1399
8.53k
        case TIFF_SHORT:
1400
8.54k
        case TIFF_SSHORT:
1401
8.55k
        case TIFF_LONG:
1402
8.55k
        case TIFF_SLONG:
1403
8.56k
        case TIFF_LONG8:
1404
8.56k
        case TIFF_SLONG8:
1405
8.56k
            break;
1406
334
        default:
1407
334
            return (TIFFReadDirEntryErrType);
1408
8.89k
    }
1409
8.56k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1410
8.56k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1411
6.37k
    {
1412
6.37k
        *value = 0;
1413
6.37k
        return (err);
1414
6.37k
    }
1415
2.19k
    switch (direntry->tdir_type)
1416
2.19k
    {
1417
1.57k
        case TIFF_ASCII:
1418
1.69k
        case TIFF_UNDEFINED:
1419
2.10k
        case TIFF_BYTE:
1420
2.10k
            *value = (uint8_t *)origdata;
1421
2.10k
            return (TIFFReadDirEntryErrOk);
1422
4
        case TIFF_SBYTE:
1423
4
        {
1424
4
            int8_t *m;
1425
4
            uint32_t n;
1426
4
            m = (int8_t *)origdata;
1427
32
            for (n = 0; n < count; n++)
1428
28
            {
1429
28
                err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
1430
28
                if (err != TIFFReadDirEntryErrOk)
1431
0
                {
1432
0
                    _TIFFfreeExt(tif, origdata);
1433
0
                    return (err);
1434
0
                }
1435
28
                m++;
1436
28
            }
1437
4
            *value = (uint8_t *)origdata;
1438
4
            return (TIFFReadDirEntryErrOk);
1439
4
        }
1440
2.19k
    }
1441
89
    data = (uint8_t *)_TIFFmallocExt(tif, count);
1442
89
    if (data == 0)
1443
0
    {
1444
0
        _TIFFfreeExt(tif, origdata);
1445
0
        return (TIFFReadDirEntryErrAlloc);
1446
0
    }
1447
89
    switch (direntry->tdir_type)
1448
89
    {
1449
71
        case TIFF_SHORT:
1450
71
        {
1451
71
            uint16_t *ma;
1452
71
            uint8_t *mb;
1453
71
            uint32_t n;
1454
71
            ma = (uint16_t *)origdata;
1455
71
            mb = data;
1456
141
            for (n = 0; n < count; n++)
1457
85
            {
1458
85
                if (tif->tif_flags & TIFF_SWAB)
1459
0
                    TIFFSwabShort(ma);
1460
85
                err = TIFFReadDirEntryCheckRangeByteShort(*ma);
1461
85
                if (err != TIFFReadDirEntryErrOk)
1462
15
                    break;
1463
70
                *mb++ = (uint8_t)(*ma++);
1464
70
            }
1465
71
        }
1466
71
        break;
1467
2
        case TIFF_SSHORT:
1468
2
        {
1469
2
            int16_t *ma;
1470
2
            uint8_t *mb;
1471
2
            uint32_t n;
1472
2
            ma = (int16_t *)origdata;
1473
2
            mb = data;
1474
47
            for (n = 0; n < count; n++)
1475
47
            {
1476
47
                if (tif->tif_flags & TIFF_SWAB)
1477
46
                    TIFFSwabShort((uint16_t *)ma);
1478
47
                err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
1479
47
                if (err != TIFFReadDirEntryErrOk)
1480
2
                    break;
1481
45
                *mb++ = (uint8_t)(*ma++);
1482
45
            }
1483
2
        }
1484
2
        break;
1485
12
        case TIFF_LONG:
1486
12
        {
1487
12
            uint32_t *ma;
1488
12
            uint8_t *mb;
1489
12
            uint32_t n;
1490
12
            ma = (uint32_t *)origdata;
1491
12
            mb = data;
1492
252
            for (n = 0; n < count; n++)
1493
252
            {
1494
252
                if (tif->tif_flags & TIFF_SWAB)
1495
240
                    TIFFSwabLong(ma);
1496
252
                err = TIFFReadDirEntryCheckRangeByteLong(*ma);
1497
252
                if (err != TIFFReadDirEntryErrOk)
1498
12
                    break;
1499
240
                *mb++ = (uint8_t)(*ma++);
1500
240
            }
1501
12
        }
1502
12
        break;
1503
1
        case TIFF_SLONG:
1504
1
        {
1505
1
            int32_t *ma;
1506
1
            uint8_t *mb;
1507
1
            uint32_t n;
1508
1
            ma = (int32_t *)origdata;
1509
1
            mb = data;
1510
21
            for (n = 0; n < count; n++)
1511
21
            {
1512
21
                if (tif->tif_flags & TIFF_SWAB)
1513
0
                    TIFFSwabLong((uint32_t *)ma);
1514
21
                err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
1515
21
                if (err != TIFFReadDirEntryErrOk)
1516
1
                    break;
1517
20
                *mb++ = (uint8_t)(*ma++);
1518
20
            }
1519
1
        }
1520
1
        break;
1521
1
        case TIFF_LONG8:
1522
1
        {
1523
1
            uint64_t *ma;
1524
1
            uint8_t *mb;
1525
1
            uint32_t n;
1526
1
            ma = (uint64_t *)origdata;
1527
1
            mb = data;
1528
5
            for (n = 0; n < count; n++)
1529
5
            {
1530
5
                if (tif->tif_flags & TIFF_SWAB)
1531
0
                    TIFFSwabLong8(ma);
1532
5
                err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
1533
5
                if (err != TIFFReadDirEntryErrOk)
1534
1
                    break;
1535
4
                *mb++ = (uint8_t)(*ma++);
1536
4
            }
1537
1
        }
1538
1
        break;
1539
2
        case TIFF_SLONG8:
1540
2
        {
1541
2
            int64_t *ma;
1542
2
            uint8_t *mb;
1543
2
            uint32_t n;
1544
2
            ma = (int64_t *)origdata;
1545
2
            mb = data;
1546
2
            for (n = 0; n < count; n++)
1547
2
            {
1548
2
                if (tif->tif_flags & TIFF_SWAB)
1549
0
                    TIFFSwabLong8((uint64_t *)ma);
1550
2
                err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
1551
2
                if (err != TIFFReadDirEntryErrOk)
1552
2
                    break;
1553
0
                *mb++ = (uint8_t)(*ma++);
1554
0
            }
1555
2
        }
1556
2
        break;
1557
89
    }
1558
89
    _TIFFfreeExt(tif, origdata);
1559
89
    if (err != TIFFReadDirEntryErrOk)
1560
33
    {
1561
33
        _TIFFfreeExt(tif, data);
1562
33
        return (err);
1563
33
    }
1564
56
    *value = data;
1565
56
    return (TIFFReadDirEntryErrOk);
1566
89
}
1567
1568
static enum TIFFReadDirEntryErr
1569
TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
1570
361
{
1571
361
    enum TIFFReadDirEntryErr err;
1572
361
    uint32_t count;
1573
361
    void *origdata;
1574
361
    int8_t *data;
1575
361
    switch (direntry->tdir_type)
1576
361
    {
1577
0
        case TIFF_UNDEFINED:
1578
0
        case TIFF_BYTE:
1579
361
        case TIFF_SBYTE:
1580
361
        case TIFF_SHORT:
1581
361
        case TIFF_SSHORT:
1582
361
        case TIFF_LONG:
1583
361
        case TIFF_SLONG:
1584
361
        case TIFF_LONG8:
1585
361
        case TIFF_SLONG8:
1586
361
            break;
1587
0
        default:
1588
0
            return (TIFFReadDirEntryErrType);
1589
361
    }
1590
361
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
1591
361
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1592
310
    {
1593
310
        *value = 0;
1594
310
        return (err);
1595
310
    }
1596
51
    switch (direntry->tdir_type)
1597
51
    {
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
51
        case TIFF_SBYTE:
1618
51
            *value = (int8_t *)origdata;
1619
51
            return (TIFFReadDirEntryErrOk);
1620
51
    }
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
6.89k
{
1751
6.89k
    enum TIFFReadDirEntryErr err;
1752
6.89k
    uint32_t count;
1753
6.89k
    void *origdata;
1754
6.89k
    uint16_t *data;
1755
6.89k
    switch (direntry->tdir_type)
1756
6.89k
    {
1757
104
        case TIFF_BYTE:
1758
104
        case TIFF_SBYTE:
1759
6.76k
        case TIFF_SHORT:
1760
6.76k
        case TIFF_SSHORT:
1761
6.77k
        case TIFF_LONG:
1762
6.78k
        case TIFF_SLONG:
1763
6.80k
        case TIFF_LONG8:
1764
6.80k
        case TIFF_SLONG8:
1765
6.80k
            break;
1766
82
        default:
1767
82
            return (TIFFReadDirEntryErrType);
1768
6.89k
    }
1769
6.80k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1770
6.80k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1771
3.93k
    {
1772
3.93k
        *value = 0;
1773
3.93k
        return (err);
1774
3.93k
    }
1775
2.87k
    switch (direntry->tdir_type)
1776
2.87k
    {
1777
2.83k
        case TIFF_SHORT:
1778
2.83k
            *value = (uint16_t *)origdata;
1779
2.83k
            if (tif->tif_flags & TIFF_SWAB)
1780
258
                TIFFSwabArrayOfShort(*value, count);
1781
2.83k
            return (TIFFReadDirEntryErrOk);
1782
1
        case TIFF_SSHORT:
1783
1
        {
1784
1
            int16_t *m;
1785
1
            uint32_t n;
1786
1
            m = (int16_t *)origdata;
1787
2
            for (n = 0; n < count; n++)
1788
1
            {
1789
1
                if (tif->tif_flags & TIFF_SWAB)
1790
0
                    TIFFSwabShort((uint16_t *)m);
1791
1
                err = TIFFReadDirEntryCheckRangeShortSshort(*m);
1792
1
                if (err != TIFFReadDirEntryErrOk)
1793
0
                {
1794
0
                    _TIFFfreeExt(tif, origdata);
1795
0
                    return (err);
1796
0
                }
1797
1
                m++;
1798
1
            }
1799
1
            *value = (uint16_t *)origdata;
1800
1
            return (TIFFReadDirEntryErrOk);
1801
1
        }
1802
2.87k
    }
1803
39
    data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
1804
39
    if (data == 0)
1805
0
    {
1806
0
        _TIFFfreeExt(tif, origdata);
1807
0
        return (TIFFReadDirEntryErrAlloc);
1808
0
    }
1809
39
    switch (direntry->tdir_type)
1810
39
    {
1811
11
        case TIFF_BYTE:
1812
11
        {
1813
11
            uint8_t *ma;
1814
11
            uint16_t *mb;
1815
11
            uint32_t n;
1816
11
            ma = (uint8_t *)origdata;
1817
11
            mb = data;
1818
3.35k
            for (n = 0; n < count; n++)
1819
3.34k
                *mb++ = (uint16_t)(*ma++);
1820
11
        }
1821
11
        break;
1822
0
        case TIFF_SBYTE:
1823
0
        {
1824
0
            int8_t *ma;
1825
0
            uint16_t *mb;
1826
0
            uint32_t n;
1827
0
            ma = (int8_t *)origdata;
1828
0
            mb = data;
1829
0
            for (n = 0; n < count; n++)
1830
0
            {
1831
0
                err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
1832
0
                if (err != TIFFReadDirEntryErrOk)
1833
0
                    break;
1834
0
                *mb++ = (uint16_t)(*ma++);
1835
0
            }
1836
0
        }
1837
0
        break;
1838
4
        case TIFF_LONG:
1839
4
        {
1840
4
            uint32_t *ma;
1841
4
            uint16_t *mb;
1842
4
            uint32_t n;
1843
4
            ma = (uint32_t *)origdata;
1844
4
            mb = data;
1845
8
            for (n = 0; n < count; n++)
1846
6
            {
1847
6
                if (tif->tif_flags & TIFF_SWAB)
1848
2
                    TIFFSwabLong(ma);
1849
6
                err = TIFFReadDirEntryCheckRangeShortLong(*ma);
1850
6
                if (err != TIFFReadDirEntryErrOk)
1851
2
                    break;
1852
4
                *mb++ = (uint16_t)(*ma++);
1853
4
            }
1854
4
        }
1855
4
        break;
1856
0
        case TIFF_SLONG:
1857
0
        {
1858
0
            int32_t *ma;
1859
0
            uint16_t *mb;
1860
0
            uint32_t n;
1861
0
            ma = (int32_t *)origdata;
1862
0
            mb = data;
1863
0
            for (n = 0; n < count; n++)
1864
0
            {
1865
0
                if (tif->tif_flags & TIFF_SWAB)
1866
0
                    TIFFSwabLong((uint32_t *)ma);
1867
0
                err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
1868
0
                if (err != TIFFReadDirEntryErrOk)
1869
0
                    break;
1870
0
                *mb++ = (uint16_t)(*ma++);
1871
0
            }
1872
0
        }
1873
0
        break;
1874
19
        case TIFF_LONG8:
1875
19
        {
1876
19
            uint64_t *ma;
1877
19
            uint16_t *mb;
1878
19
            uint32_t n;
1879
19
            ma = (uint64_t *)origdata;
1880
19
            mb = data;
1881
46
            for (n = 0; n < count; n++)
1882
37
            {
1883
37
                if (tif->tif_flags & TIFF_SWAB)
1884
0
                    TIFFSwabLong8(ma);
1885
37
                err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
1886
37
                if (err != TIFFReadDirEntryErrOk)
1887
10
                    break;
1888
27
                *mb++ = (uint16_t)(*ma++);
1889
27
            }
1890
19
        }
1891
19
        break;
1892
5
        case TIFF_SLONG8:
1893
5
        {
1894
5
            int64_t *ma;
1895
5
            uint16_t *mb;
1896
5
            uint32_t n;
1897
5
            ma = (int64_t *)origdata;
1898
5
            mb = data;
1899
335
            for (n = 0; n < count; n++)
1900
335
            {
1901
335
                if (tif->tif_flags & TIFF_SWAB)
1902
206
                    TIFFSwabLong8((uint64_t *)ma);
1903
335
                err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
1904
335
                if (err != TIFFReadDirEntryErrOk)
1905
5
                    break;
1906
330
                *mb++ = (uint16_t)(*ma++);
1907
330
            }
1908
5
        }
1909
5
        break;
1910
39
    }
1911
39
    _TIFFfreeExt(tif, origdata);
1912
39
    if (err != TIFFReadDirEntryErrOk)
1913
17
    {
1914
17
        _TIFFfreeExt(tif, data);
1915
17
        return (err);
1916
17
    }
1917
22
    *value = data;
1918
22
    return (TIFFReadDirEntryErrOk);
1919
39
}
1920
1921
static enum TIFFReadDirEntryErr
1922
TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
1923
907
{
1924
907
    enum TIFFReadDirEntryErr err;
1925
907
    uint32_t count;
1926
907
    void *origdata;
1927
907
    int16_t *data;
1928
907
    switch (direntry->tdir_type)
1929
907
    {
1930
0
        case TIFF_BYTE:
1931
0
        case TIFF_SBYTE:
1932
0
        case TIFF_SHORT:
1933
907
        case TIFF_SSHORT:
1934
907
        case TIFF_LONG:
1935
907
        case TIFF_SLONG:
1936
907
        case TIFF_LONG8:
1937
907
        case TIFF_SLONG8:
1938
907
            break;
1939
0
        default:
1940
0
            return (TIFFReadDirEntryErrType);
1941
907
    }
1942
907
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
1943
907
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1944
771
    {
1945
771
        *value = 0;
1946
771
        return (err);
1947
771
    }
1948
136
    switch (direntry->tdir_type)
1949
136
    {
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
136
        case TIFF_SSHORT:
1971
136
            *value = (int16_t *)origdata;
1972
136
            if (tif->tif_flags & TIFF_SWAB)
1973
2
                TIFFSwabArrayOfShort((uint16_t *)(*value), count);
1974
136
            return (TIFFReadDirEntryErrOk);
1975
136
    }
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
2.06k
{
2092
2.06k
    enum TIFFReadDirEntryErr err;
2093
2.06k
    uint32_t count;
2094
2.06k
    void *origdata;
2095
2.06k
    uint32_t *data;
2096
2.06k
    switch (direntry->tdir_type)
2097
2.06k
    {
2098
0
        case TIFF_BYTE:
2099
14
        case TIFF_SBYTE:
2100
14
        case TIFF_SHORT:
2101
14
        case TIFF_SSHORT:
2102
2.04k
        case TIFF_LONG:
2103
2.04k
        case TIFF_SLONG:
2104
2.05k
        case TIFF_LONG8:
2105
2.06k
        case TIFF_SLONG8:
2106
2.06k
            break;
2107
0
        default:
2108
0
            return (TIFFReadDirEntryErrType);
2109
2.06k
    }
2110
2.06k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2111
2.06k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2112
1.88k
    {
2113
1.88k
        *value = 0;
2114
1.88k
        return (err);
2115
1.88k
    }
2116
174
    switch (direntry->tdir_type)
2117
174
    {
2118
155
        case TIFF_LONG:
2119
155
            *value = (uint32_t *)origdata;
2120
155
            if (tif->tif_flags & TIFF_SWAB)
2121
30
                TIFFSwabArrayOfLong(*value, count);
2122
155
            return (TIFFReadDirEntryErrOk);
2123
0
        case TIFF_SLONG:
2124
0
        {
2125
0
            int32_t *m;
2126
0
            uint32_t n;
2127
0
            m = (int32_t *)origdata;
2128
0
            for (n = 0; n < count; n++)
2129
0
            {
2130
0
                if (tif->tif_flags & TIFF_SWAB)
2131
0
                    TIFFSwabLong((uint32_t *)m);
2132
0
                err = TIFFReadDirEntryCheckRangeLongSlong(*m);
2133
0
                if (err != TIFFReadDirEntryErrOk)
2134
0
                {
2135
0
                    _TIFFfreeExt(tif, origdata);
2136
0
                    return (err);
2137
0
                }
2138
0
                m++;
2139
0
            }
2140
0
            *value = (uint32_t *)origdata;
2141
0
            return (TIFFReadDirEntryErrOk);
2142
0
        }
2143
174
    }
2144
19
    data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
2145
19
    if (data == 0)
2146
0
    {
2147
0
        _TIFFfreeExt(tif, origdata);
2148
0
        return (TIFFReadDirEntryErrAlloc);
2149
0
    }
2150
19
    switch (direntry->tdir_type)
2151
19
    {
2152
0
        case TIFF_BYTE:
2153
0
        {
2154
0
            uint8_t *ma;
2155
0
            uint32_t *mb;
2156
0
            uint32_t n;
2157
0
            ma = (uint8_t *)origdata;
2158
0
            mb = data;
2159
0
            for (n = 0; n < count; n++)
2160
0
                *mb++ = (uint32_t)(*ma++);
2161
0
        }
2162
0
        break;
2163
9
        case TIFF_SBYTE:
2164
9
        {
2165
9
            int8_t *ma;
2166
9
            uint32_t *mb;
2167
9
            uint32_t n;
2168
9
            ma = (int8_t *)origdata;
2169
9
            mb = data;
2170
95
            for (n = 0; n < count; n++)
2171
90
            {
2172
90
                err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
2173
90
                if (err != TIFFReadDirEntryErrOk)
2174
4
                    break;
2175
86
                *mb++ = (uint32_t)(*ma++);
2176
86
            }
2177
9
        }
2178
9
        break;
2179
0
        case TIFF_SHORT:
2180
0
        {
2181
0
            uint16_t *ma;
2182
0
            uint32_t *mb;
2183
0
            uint32_t n;
2184
0
            ma = (uint16_t *)origdata;
2185
0
            mb = data;
2186
0
            for (n = 0; n < count; n++)
2187
0
            {
2188
0
                if (tif->tif_flags & TIFF_SWAB)
2189
0
                    TIFFSwabShort(ma);
2190
0
                *mb++ = (uint32_t)(*ma++);
2191
0
            }
2192
0
        }
2193
0
        break;
2194
0
        case TIFF_SSHORT:
2195
0
        {
2196
0
            int16_t *ma;
2197
0
            uint32_t *mb;
2198
0
            uint32_t n;
2199
0
            ma = (int16_t *)origdata;
2200
0
            mb = data;
2201
0
            for (n = 0; n < count; n++)
2202
0
            {
2203
0
                if (tif->tif_flags & TIFF_SWAB)
2204
0
                    TIFFSwabShort((uint16_t *)ma);
2205
0
                err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
2206
0
                if (err != TIFFReadDirEntryErrOk)
2207
0
                    break;
2208
0
                *mb++ = (uint32_t)(*ma++);
2209
0
            }
2210
0
        }
2211
0
        break;
2212
7
        case TIFF_LONG8:
2213
7
        {
2214
7
            uint64_t *ma;
2215
7
            uint32_t *mb;
2216
7
            uint32_t n;
2217
7
            ma = (uint64_t *)origdata;
2218
7
            mb = data;
2219
89
            for (n = 0; n < count; n++)
2220
89
            {
2221
89
                if (tif->tif_flags & TIFF_SWAB)
2222
86
                    TIFFSwabLong8(ma);
2223
89
                err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
2224
89
                if (err != TIFFReadDirEntryErrOk)
2225
7
                    break;
2226
82
                *mb++ = (uint32_t)(*ma++);
2227
82
            }
2228
7
        }
2229
7
        break;
2230
3
        case TIFF_SLONG8:
2231
3
        {
2232
3
            int64_t *ma;
2233
3
            uint32_t *mb;
2234
3
            uint32_t n;
2235
3
            ma = (int64_t *)origdata;
2236
3
            mb = data;
2237
16
            for (n = 0; n < count; n++)
2238
16
            {
2239
16
                if (tif->tif_flags & TIFF_SWAB)
2240
0
                    TIFFSwabLong8((uint64_t *)ma);
2241
16
                err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
2242
16
                if (err != TIFFReadDirEntryErrOk)
2243
3
                    break;
2244
13
                *mb++ = (uint32_t)(*ma++);
2245
13
            }
2246
3
        }
2247
3
        break;
2248
19
    }
2249
19
    _TIFFfreeExt(tif, origdata);
2250
19
    if (err != TIFFReadDirEntryErrOk)
2251
14
    {
2252
14
        _TIFFfreeExt(tif, data);
2253
14
        return (err);
2254
14
    }
2255
5
    *value = data;
2256
5
    return (TIFFReadDirEntryErrOk);
2257
19
}
2258
2259
static enum TIFFReadDirEntryErr
2260
TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
2261
586
{
2262
586
    enum TIFFReadDirEntryErr err;
2263
586
    uint32_t count;
2264
586
    void *origdata;
2265
586
    int32_t *data;
2266
586
    switch (direntry->tdir_type)
2267
586
    {
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
586
        case TIFF_SLONG:
2274
586
        case TIFF_LONG8:
2275
586
        case TIFF_SLONG8:
2276
586
            break;
2277
0
        default:
2278
0
            return (TIFFReadDirEntryErrType);
2279
586
    }
2280
586
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2281
586
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2282
556
    {
2283
556
        *value = 0;
2284
556
        return (err);
2285
556
    }
2286
30
    switch (direntry->tdir_type)
2287
30
    {
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
30
        case TIFF_SLONG:
2309
30
            *value = (int32_t *)origdata;
2310
30
            if (tif->tif_flags & TIFF_SWAB)
2311
14
                TIFFSwabArrayOfLong((uint32_t *)(*value), count);
2312
30
            return (TIFFReadDirEntryErrOk);
2313
30
    }
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
2.96k
{
2425
2.96k
    enum TIFFReadDirEntryErr err;
2426
2.96k
    uint32_t count;
2427
2.96k
    void *origdata;
2428
2.96k
    uint64_t *data;
2429
2.96k
    switch (direntry->tdir_type)
2430
2.96k
    {
2431
695
        case TIFF_BYTE:
2432
701
        case TIFF_SBYTE:
2433
969
        case TIFF_SHORT:
2434
975
        case TIFF_SSHORT:
2435
2.10k
        case TIFF_LONG:
2436
2.10k
        case TIFF_SLONG:
2437
2.94k
        case TIFF_LONG8:
2438
2.94k
        case TIFF_SLONG8:
2439
2.94k
            break;
2440
15
        default:
2441
15
            return (TIFFReadDirEntryErrType);
2442
2.96k
    }
2443
2.94k
    err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
2444
2.94k
                                         maxcount);
2445
2.94k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2446
305
    {
2447
305
        *value = 0;
2448
305
        return (err);
2449
305
    }
2450
2.64k
    switch (direntry->tdir_type)
2451
2.64k
    {
2452
574
        case TIFF_LONG8:
2453
574
            *value = (uint64_t *)origdata;
2454
574
            if (tif->tif_flags & TIFF_SWAB)
2455
130
                TIFFSwabArrayOfLong8(*value, count);
2456
574
            return (TIFFReadDirEntryErrOk);
2457
1
        case TIFF_SLONG8:
2458
1
        {
2459
1
            int64_t *m;
2460
1
            uint32_t n;
2461
1
            m = (int64_t *)origdata;
2462
10
            for (n = 0; n < count; n++)
2463
9
            {
2464
9
                if (tif->tif_flags & TIFF_SWAB)
2465
0
                    TIFFSwabLong8((uint64_t *)m);
2466
9
                err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
2467
9
                if (err != TIFFReadDirEntryErrOk)
2468
0
                {
2469
0
                    _TIFFfreeExt(tif, origdata);
2470
0
                    return (err);
2471
0
                }
2472
9
                m++;
2473
9
            }
2474
1
            *value = (uint64_t *)origdata;
2475
1
            return (TIFFReadDirEntryErrOk);
2476
1
        }
2477
2.64k
    }
2478
2.06k
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
2479
2.06k
    if (data == 0)
2480
0
    {
2481
0
        _TIFFfreeExt(tif, origdata);
2482
0
        return (TIFFReadDirEntryErrAlloc);
2483
0
    }
2484
2.06k
    switch (direntry->tdir_type)
2485
2.06k
    {
2486
678
        case TIFF_BYTE:
2487
678
        {
2488
678
            uint8_t *ma;
2489
678
            uint64_t *mb;
2490
678
            uint32_t n;
2491
678
            ma = (uint8_t *)origdata;
2492
678
            mb = data;
2493
1.69M
            for (n = 0; n < count; n++)
2494
1.69M
                *mb++ = (uint64_t)(*ma++);
2495
678
        }
2496
678
        break;
2497
6
        case TIFF_SBYTE:
2498
6
        {
2499
6
            int8_t *ma;
2500
6
            uint64_t *mb;
2501
6
            uint32_t n;
2502
6
            ma = (int8_t *)origdata;
2503
6
            mb = data;
2504
12
            for (n = 0; n < count; n++)
2505
6
            {
2506
6
                err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
2507
6
                if (err != TIFFReadDirEntryErrOk)
2508
0
                    break;
2509
6
                *mb++ = (uint64_t)(*ma++);
2510
6
            }
2511
6
        }
2512
6
        break;
2513
258
        case TIFF_SHORT:
2514
258
        {
2515
258
            uint16_t *ma;
2516
258
            uint64_t *mb;
2517
258
            uint32_t n;
2518
258
            ma = (uint16_t *)origdata;
2519
258
            mb = data;
2520
1.67M
            for (n = 0; n < count; n++)
2521
1.67M
            {
2522
1.67M
                if (tif->tif_flags & TIFF_SWAB)
2523
9
                    TIFFSwabShort(ma);
2524
1.67M
                *mb++ = (uint64_t)(*ma++);
2525
1.67M
            }
2526
258
        }
2527
258
        break;
2528
2
        case TIFF_SSHORT:
2529
2
        {
2530
2
            int16_t *ma;
2531
2
            uint64_t *mb;
2532
2
            uint32_t n;
2533
2
            ma = (int16_t *)origdata;
2534
2
            mb = data;
2535
221
            for (n = 0; n < count; n++)
2536
221
            {
2537
221
                if (tif->tif_flags & TIFF_SWAB)
2538
0
                    TIFFSwabShort((uint16_t *)ma);
2539
221
                err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
2540
221
                if (err != TIFFReadDirEntryErrOk)
2541
2
                    break;
2542
219
                *mb++ = (uint64_t)(*ma++);
2543
219
            }
2544
2
        }
2545
2
        break;
2546
1.12k
        case TIFF_LONG:
2547
1.12k
        {
2548
1.12k
            uint32_t *ma;
2549
1.12k
            uint64_t *mb;
2550
1.12k
            uint32_t n;
2551
1.12k
            ma = (uint32_t *)origdata;
2552
1.12k
            mb = data;
2553
54.9k
            for (n = 0; n < count; n++)
2554
53.7k
            {
2555
53.7k
                if (tif->tif_flags & TIFF_SWAB)
2556
3.82k
                    TIFFSwabLong(ma);
2557
53.7k
                *mb++ = (uint64_t)(*ma++);
2558
53.7k
            }
2559
1.12k
        }
2560
1.12k
        break;
2561
0
        case TIFF_SLONG:
2562
0
        {
2563
0
            int32_t *ma;
2564
0
            uint64_t *mb;
2565
0
            uint32_t n;
2566
0
            ma = (int32_t *)origdata;
2567
0
            mb = data;
2568
0
            for (n = 0; n < count; n++)
2569
0
            {
2570
0
                if (tif->tif_flags & TIFF_SWAB)
2571
0
                    TIFFSwabLong((uint32_t *)ma);
2572
0
                err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
2573
0
                if (err != TIFFReadDirEntryErrOk)
2574
0
                    break;
2575
0
                *mb++ = (uint64_t)(*ma++);
2576
0
            }
2577
0
        }
2578
0
        break;
2579
2.06k
    }
2580
2.06k
    _TIFFfreeExt(tif, origdata);
2581
2.06k
    if (err != TIFFReadDirEntryErrOk)
2582
2
    {
2583
2
        _TIFFfreeExt(tif, data);
2584
2
        return (err);
2585
2
    }
2586
2.06k
    *value = data;
2587
2.06k
    return (TIFFReadDirEntryErrOk);
2588
2.06k
}
2589
2590
static enum TIFFReadDirEntryErr
2591
TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
2592
270
{
2593
270
    return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
2594
270
                                               ~((uint64_t)0));
2595
270
}
2596
2597
static enum TIFFReadDirEntryErr
2598
TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
2599
162
{
2600
162
    enum TIFFReadDirEntryErr err;
2601
162
    uint32_t count;
2602
162
    void *origdata;
2603
162
    int64_t *data;
2604
162
    switch (direntry->tdir_type)
2605
162
    {
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
162
        case TIFF_SLONG8:
2614
162
            break;
2615
0
        default:
2616
0
            return (TIFFReadDirEntryErrType);
2617
162
    }
2618
162
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
2619
162
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2620
97
    {
2621
97
        *value = 0;
2622
97
        return (err);
2623
97
    }
2624
65
    switch (direntry->tdir_type)
2625
65
    {
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
65
        case TIFF_SLONG8:
2647
65
            *value = (int64_t *)origdata;
2648
65
            if (tif->tif_flags & TIFF_SWAB)
2649
4
                TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
2650
65
            return (TIFFReadDirEntryErrOk);
2651
65
    }
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
2.73k
{
2751
2.73k
    enum TIFFReadDirEntryErr err;
2752
2.73k
    uint32_t count;
2753
2.73k
    void *origdata;
2754
2.73k
    float *data;
2755
2.73k
    switch (direntry->tdir_type)
2756
2.73k
    {
2757
2
        case TIFF_BYTE:
2758
14
        case TIFF_SBYTE:
2759
14
        case TIFF_SHORT:
2760
14
        case TIFF_SSHORT:
2761
26
        case TIFF_LONG:
2762
39
        case TIFF_SLONG:
2763
47
        case TIFF_LONG8:
2764
48
        case TIFF_SLONG8:
2765
2.08k
        case TIFF_RATIONAL:
2766
2.43k
        case TIFF_SRATIONAL:
2767
2.67k
        case TIFF_FLOAT:
2768
2.69k
        case TIFF_DOUBLE:
2769
2.69k
            break;
2770
41
        default:
2771
41
            return (TIFFReadDirEntryErrType);
2772
2.73k
    }
2773
2.69k
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
2774
2.69k
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
2775
2.13k
    {
2776
2.13k
        *value = 0;
2777
2.13k
        return (err);
2778
2.13k
    }
2779
557
    switch (direntry->tdir_type)
2780
557
    {
2781
44
        case TIFF_FLOAT:
2782
44
            if (tif->tif_flags & TIFF_SWAB)
2783
2
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
2784
44
            TIFFCvtIEEEDoubleToNative(tif, count, (float *)origdata);
2785
44
            *value = (float *)origdata;
2786
44
            return (TIFFReadDirEntryErrOk);
2787
557
    }
2788
513
    data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
2789
513
    if (data == 0)
2790
0
    {
2791
0
        _TIFFfreeExt(tif, origdata);
2792
0
        return (TIFFReadDirEntryErrAlloc);
2793
0
    }
2794
513
    switch (direntry->tdir_type)
2795
513
    {
2796
1
        case TIFF_BYTE:
2797
1
        {
2798
1
            uint8_t *ma;
2799
1
            float *mb;
2800
1
            uint32_t n;
2801
1
            ma = (uint8_t *)origdata;
2802
1
            mb = data;
2803
5
            for (n = 0; n < count; n++)
2804
4
                *mb++ = (float)(*ma++);
2805
1
        }
2806
1
        break;
2807
11
        case TIFF_SBYTE:
2808
11
        {
2809
11
            int8_t *ma;
2810
11
            float *mb;
2811
11
            uint32_t n;
2812
11
            ma = (int8_t *)origdata;
2813
11
            mb = data;
2814
59
            for (n = 0; n < count; n++)
2815
48
                *mb++ = (float)(*ma++);
2816
11
        }
2817
11
        break;
2818
0
        case TIFF_SHORT:
2819
0
        {
2820
0
            uint16_t *ma;
2821
0
            float *mb;
2822
0
            uint32_t n;
2823
0
            ma = (uint16_t *)origdata;
2824
0
            mb = data;
2825
0
            for (n = 0; n < count; n++)
2826
0
            {
2827
0
                if (tif->tif_flags & TIFF_SWAB)
2828
0
                    TIFFSwabShort(ma);
2829
0
                *mb++ = (float)(*ma++);
2830
0
            }
2831
0
        }
2832
0
        break;
2833
0
        case TIFF_SSHORT:
2834
0
        {
2835
0
            int16_t *ma;
2836
0
            float *mb;
2837
0
            uint32_t n;
2838
0
            ma = (int16_t *)origdata;
2839
0
            mb = data;
2840
0
            for (n = 0; n < count; n++)
2841
0
            {
2842
0
                if (tif->tif_flags & TIFF_SWAB)
2843
0
                    TIFFSwabShort((uint16_t *)ma);
2844
0
                *mb++ = (float)(*ma++);
2845
0
            }
2846
0
        }
2847
0
        break;
2848
12
        case TIFF_LONG:
2849
12
        {
2850
12
            uint32_t *ma;
2851
12
            float *mb;
2852
12
            uint32_t n;
2853
12
            ma = (uint32_t *)origdata;
2854
12
            mb = data;
2855
186
            for (n = 0; n < count; n++)
2856
174
            {
2857
174
                if (tif->tif_flags & TIFF_SWAB)
2858
0
                    TIFFSwabLong(ma);
2859
174
                *mb++ = (float)(*ma++);
2860
174
            }
2861
12
        }
2862
12
        break;
2863
0
        case TIFF_SLONG:
2864
0
        {
2865
0
            int32_t *ma;
2866
0
            float *mb;
2867
0
            uint32_t n;
2868
0
            ma = (int32_t *)origdata;
2869
0
            mb = data;
2870
0
            for (n = 0; n < count; n++)
2871
0
            {
2872
0
                if (tif->tif_flags & TIFF_SWAB)
2873
0
                    TIFFSwabLong((uint32_t *)ma);
2874
0
                *mb++ = (float)(*ma++);
2875
0
            }
2876
0
        }
2877
0
        break;
2878
8
        case TIFF_LONG8:
2879
8
        {
2880
8
            uint64_t *ma;
2881
8
            float *mb;
2882
8
            uint32_t n;
2883
8
            ma = (uint64_t *)origdata;
2884
8
            mb = data;
2885
141
            for (n = 0; n < count; n++)
2886
133
            {
2887
133
                if (tif->tif_flags & TIFF_SWAB)
2888
0
                    TIFFSwabLong8(ma);
2889
#if defined(__WIN32__) && (_MSC_VER < 1500)
2890
                /*
2891
                 * XXX: MSVC 6.0 does not support
2892
                 * conversion of 64-bit integers into
2893
                 * floating point values.
2894
                 */
2895
                *mb++ = _TIFFUInt64ToFloat(*ma++);
2896
#else
2897
133
                *mb++ = (float)(*ma++);
2898
133
#endif
2899
133
            }
2900
8
        }
2901
8
        break;
2902
1
        case TIFF_SLONG8:
2903
1
        {
2904
1
            int64_t *ma;
2905
1
            float *mb;
2906
1
            uint32_t n;
2907
1
            ma = (int64_t *)origdata;
2908
1
            mb = data;
2909
252
            for (n = 0; n < count; n++)
2910
251
            {
2911
251
                if (tif->tif_flags & TIFF_SWAB)
2912
0
                    TIFFSwabLong8((uint64_t *)ma);
2913
251
                *mb++ = (float)(*ma++);
2914
251
            }
2915
1
        }
2916
1
        break;
2917
367
        case TIFF_RATIONAL:
2918
367
        {
2919
367
            uint32_t *ma;
2920
367
            uint32_t maa;
2921
367
            uint32_t mab;
2922
367
            float *mb;
2923
367
            uint32_t n;
2924
367
            ma = (uint32_t *)origdata;
2925
367
            mb = data;
2926
4.46k
            for (n = 0; n < count; n++)
2927
4.09k
            {
2928
4.09k
                if (tif->tif_flags & TIFF_SWAB)
2929
1.79k
                    TIFFSwabLong(ma);
2930
4.09k
                maa = *ma++;
2931
4.09k
                if (tif->tif_flags & TIFF_SWAB)
2932
1.79k
                    TIFFSwabLong(ma);
2933
4.09k
                mab = *ma++;
2934
4.09k
                if (mab == 0)
2935
430
                    *mb++ = 0.0;
2936
3.66k
                else
2937
3.66k
                    *mb++ = (float)maa / (float)mab;
2938
4.09k
            }
2939
367
        }
2940
367
        break;
2941
95
        case TIFF_SRATIONAL:
2942
95
        {
2943
95
            uint32_t *ma;
2944
95
            int32_t maa;
2945
95
            uint32_t mab;
2946
95
            float *mb;
2947
95
            uint32_t n;
2948
95
            ma = (uint32_t *)origdata;
2949
95
            mb = data;
2950
7.76k
            for (n = 0; n < count; n++)
2951
7.67k
            {
2952
7.67k
                if (tif->tif_flags & TIFF_SWAB)
2953
5
                    TIFFSwabLong(ma);
2954
7.67k
                maa = *(int32_t *)ma;
2955
7.67k
                ma++;
2956
7.67k
                if (tif->tif_flags & TIFF_SWAB)
2957
5
                    TIFFSwabLong(ma);
2958
7.67k
                mab = *ma++;
2959
7.67k
                if (mab == 0)
2960
1.39k
                    *mb++ = 0.0;
2961
6.27k
                else
2962
6.27k
                    *mb++ = (float)maa / (float)mab;
2963
7.67k
            }
2964
95
        }
2965
95
        break;
2966
18
        case TIFF_DOUBLE:
2967
18
        {
2968
18
            double *ma;
2969
18
            float *mb;
2970
18
            uint32_t n;
2971
18
            if (tif->tif_flags & TIFF_SWAB)
2972
0
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
2973
18
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
2974
18
            ma = (double *)origdata;
2975
18
            mb = data;
2976
2.14k
            for (n = 0; n < count; n++)
2977
2.12k
            {
2978
2.12k
                double val = *ma++;
2979
2.12k
                if (val > FLT_MAX)
2980
402
                    val = FLT_MAX;
2981
1.72k
                else if (val < -FLT_MAX)
2982
566
                    val = -FLT_MAX;
2983
2.12k
                *mb++ = (float)val;
2984
2.12k
            }
2985
18
        }
2986
18
        break;
2987
513
    }
2988
513
    _TIFFfreeExt(tif, origdata);
2989
513
    *value = data;
2990
513
    return (TIFFReadDirEntryErrOk);
2991
513
}
2992
2993
static enum TIFFReadDirEntryErr
2994
TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
2995
326
{
2996
326
    enum TIFFReadDirEntryErr err;
2997
326
    uint32_t count;
2998
326
    void *origdata;
2999
326
    double *data;
3000
326
    switch (direntry->tdir_type)
3001
326
    {
3002
0
        case TIFF_BYTE:
3003
0
        case TIFF_SBYTE:
3004
0
        case TIFF_SHORT:
3005
0
        case TIFF_SSHORT:
3006
0
        case TIFF_LONG:
3007
0
        case TIFF_SLONG:
3008
0
        case TIFF_LONG8:
3009
0
        case TIFF_SLONG8:
3010
0
        case TIFF_RATIONAL:
3011
2
        case TIFF_SRATIONAL:
3012
3
        case TIFF_FLOAT:
3013
326
        case TIFF_DOUBLE:
3014
326
            break;
3015
0
        default:
3016
0
            return (TIFFReadDirEntryErrType);
3017
326
    }
3018
326
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3019
326
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3020
190
    {
3021
190
        *value = 0;
3022
190
        return (err);
3023
190
    }
3024
136
    switch (direntry->tdir_type)
3025
136
    {
3026
133
        case TIFF_DOUBLE:
3027
133
            if (tif->tif_flags & TIFF_SWAB)
3028
8
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
3029
133
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3030
133
            *value = (double *)origdata;
3031
133
            return (TIFFReadDirEntryErrOk);
3032
136
    }
3033
3
    data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
3034
3
    if (data == 0)
3035
0
    {
3036
0
        _TIFFfreeExt(tif, origdata);
3037
0
        return (TIFFReadDirEntryErrAlloc);
3038
0
    }
3039
3
    switch (direntry->tdir_type)
3040
3
    {
3041
0
        case TIFF_BYTE:
3042
0
        {
3043
0
            uint8_t *ma;
3044
0
            double *mb;
3045
0
            uint32_t n;
3046
0
            ma = (uint8_t *)origdata;
3047
0
            mb = data;
3048
0
            for (n = 0; n < count; n++)
3049
0
                *mb++ = (double)(*ma++);
3050
0
        }
3051
0
        break;
3052
0
        case TIFF_SBYTE:
3053
0
        {
3054
0
            int8_t *ma;
3055
0
            double *mb;
3056
0
            uint32_t n;
3057
0
            ma = (int8_t *)origdata;
3058
0
            mb = data;
3059
0
            for (n = 0; n < count; n++)
3060
0
                *mb++ = (double)(*ma++);
3061
0
        }
3062
0
        break;
3063
0
        case TIFF_SHORT:
3064
0
        {
3065
0
            uint16_t *ma;
3066
0
            double *mb;
3067
0
            uint32_t n;
3068
0
            ma = (uint16_t *)origdata;
3069
0
            mb = data;
3070
0
            for (n = 0; n < count; n++)
3071
0
            {
3072
0
                if (tif->tif_flags & TIFF_SWAB)
3073
0
                    TIFFSwabShort(ma);
3074
0
                *mb++ = (double)(*ma++);
3075
0
            }
3076
0
        }
3077
0
        break;
3078
0
        case TIFF_SSHORT:
3079
0
        {
3080
0
            int16_t *ma;
3081
0
            double *mb;
3082
0
            uint32_t n;
3083
0
            ma = (int16_t *)origdata;
3084
0
            mb = data;
3085
0
            for (n = 0; n < count; n++)
3086
0
            {
3087
0
                if (tif->tif_flags & TIFF_SWAB)
3088
0
                    TIFFSwabShort((uint16_t *)ma);
3089
0
                *mb++ = (double)(*ma++);
3090
0
            }
3091
0
        }
3092
0
        break;
3093
0
        case TIFF_LONG:
3094
0
        {
3095
0
            uint32_t *ma;
3096
0
            double *mb;
3097
0
            uint32_t n;
3098
0
            ma = (uint32_t *)origdata;
3099
0
            mb = data;
3100
0
            for (n = 0; n < count; n++)
3101
0
            {
3102
0
                if (tif->tif_flags & TIFF_SWAB)
3103
0
                    TIFFSwabLong(ma);
3104
0
                *mb++ = (double)(*ma++);
3105
0
            }
3106
0
        }
3107
0
        break;
3108
0
        case TIFF_SLONG:
3109
0
        {
3110
0
            int32_t *ma;
3111
0
            double *mb;
3112
0
            uint32_t n;
3113
0
            ma = (int32_t *)origdata;
3114
0
            mb = data;
3115
0
            for (n = 0; n < count; n++)
3116
0
            {
3117
0
                if (tif->tif_flags & TIFF_SWAB)
3118
0
                    TIFFSwabLong((uint32_t *)ma);
3119
0
                *mb++ = (double)(*ma++);
3120
0
            }
3121
0
        }
3122
0
        break;
3123
0
        case TIFF_LONG8:
3124
0
        {
3125
0
            uint64_t *ma;
3126
0
            double *mb;
3127
0
            uint32_t n;
3128
0
            ma = (uint64_t *)origdata;
3129
0
            mb = data;
3130
0
            for (n = 0; n < count; n++)
3131
0
            {
3132
0
                if (tif->tif_flags & TIFF_SWAB)
3133
0
                    TIFFSwabLong8(ma);
3134
#if defined(__WIN32__) && (_MSC_VER < 1500)
3135
                /*
3136
                 * XXX: MSVC 6.0 does not support
3137
                 * conversion of 64-bit integers into
3138
                 * floating point values.
3139
                 */
3140
                *mb++ = _TIFFUInt64ToDouble(*ma++);
3141
#else
3142
0
                *mb++ = (double)(*ma++);
3143
0
#endif
3144
0
            }
3145
0
        }
3146
0
        break;
3147
0
        case TIFF_SLONG8:
3148
0
        {
3149
0
            int64_t *ma;
3150
0
            double *mb;
3151
0
            uint32_t n;
3152
0
            ma = (int64_t *)origdata;
3153
0
            mb = data;
3154
0
            for (n = 0; n < count; n++)
3155
0
            {
3156
0
                if (tif->tif_flags & TIFF_SWAB)
3157
0
                    TIFFSwabLong8((uint64_t *)ma);
3158
0
                *mb++ = (double)(*ma++);
3159
0
            }
3160
0
        }
3161
0
        break;
3162
0
        case TIFF_RATIONAL:
3163
0
        {
3164
0
            uint32_t *ma;
3165
0
            uint32_t maa;
3166
0
            uint32_t mab;
3167
0
            double *mb;
3168
0
            uint32_t n;
3169
0
            ma = (uint32_t *)origdata;
3170
0
            mb = data;
3171
0
            for (n = 0; n < count; n++)
3172
0
            {
3173
0
                if (tif->tif_flags & TIFF_SWAB)
3174
0
                    TIFFSwabLong(ma);
3175
0
                maa = *ma++;
3176
0
                if (tif->tif_flags & TIFF_SWAB)
3177
0
                    TIFFSwabLong(ma);
3178
0
                mab = *ma++;
3179
0
                if (mab == 0)
3180
0
                    *mb++ = 0.0;
3181
0
                else
3182
0
                    *mb++ = (double)maa / (double)mab;
3183
0
            }
3184
0
        }
3185
0
        break;
3186
2
        case TIFF_SRATIONAL:
3187
2
        {
3188
2
            uint32_t *ma;
3189
2
            int32_t maa;
3190
2
            uint32_t mab;
3191
2
            double *mb;
3192
2
            uint32_t n;
3193
2
            ma = (uint32_t *)origdata;
3194
2
            mb = data;
3195
514
            for (n = 0; n < count; n++)
3196
512
            {
3197
512
                if (tif->tif_flags & TIFF_SWAB)
3198
512
                    TIFFSwabLong(ma);
3199
512
                maa = *(int32_t *)ma;
3200
512
                ma++;
3201
512
                if (tif->tif_flags & TIFF_SWAB)
3202
512
                    TIFFSwabLong(ma);
3203
512
                mab = *ma++;
3204
512
                if (mab == 0)
3205
263
                    *mb++ = 0.0;
3206
249
                else
3207
249
                    *mb++ = (double)maa / (double)mab;
3208
512
            }
3209
2
        }
3210
2
        break;
3211
1
        case TIFF_FLOAT:
3212
1
        {
3213
1
            float *ma;
3214
1
            double *mb;
3215
1
            uint32_t n;
3216
1
            if (tif->tif_flags & TIFF_SWAB)
3217
1
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
3218
1
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3219
1
            ma = (float *)origdata;
3220
1
            mb = data;
3221
257
            for (n = 0; n < count; n++)
3222
256
                *mb++ = (double)(*ma++);
3223
1
        }
3224
1
        break;
3225
3
    }
3226
3
    _TIFFfreeExt(tif, origdata);
3227
3
    *value = data;
3228
3
    return (TIFFReadDirEntryErrOk);
3229
3
}
3230
3231
static enum TIFFReadDirEntryErr
3232
TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
3233
253
{
3234
253
    enum TIFFReadDirEntryErr err;
3235
253
    uint32_t count;
3236
253
    void *origdata;
3237
253
    uint64_t *data;
3238
253
    switch (direntry->tdir_type)
3239
253
    {
3240
2
        case TIFF_LONG:
3241
2
        case TIFF_LONG8:
3242
143
        case TIFF_IFD:
3243
251
        case TIFF_IFD8:
3244
251
            break;
3245
2
        default:
3246
2
            return (TIFFReadDirEntryErrType);
3247
253
    }
3248
251
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
3249
251
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
3250
201
    {
3251
201
        *value = 0;
3252
201
        return (err);
3253
201
    }
3254
50
    switch (direntry->tdir_type)
3255
50
    {
3256
0
        case TIFF_LONG8:
3257
9
        case TIFF_IFD8:
3258
9
            *value = (uint64_t *)origdata;
3259
9
            if (tif->tif_flags & TIFF_SWAB)
3260
5
                TIFFSwabArrayOfLong8(*value, count);
3261
9
            return (TIFFReadDirEntryErrOk);
3262
50
    }
3263
41
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
3264
41
    if (data == 0)
3265
0
    {
3266
0
        _TIFFfreeExt(tif, origdata);
3267
0
        return (TIFFReadDirEntryErrAlloc);
3268
0
    }
3269
41
    switch (direntry->tdir_type)
3270
41
    {
3271
2
        case TIFF_LONG:
3272
41
        case TIFF_IFD:
3273
41
        {
3274
41
            uint32_t *ma;
3275
41
            uint64_t *mb;
3276
41
            uint32_t n;
3277
41
            ma = (uint32_t *)origdata;
3278
41
            mb = data;
3279
10.5k
            for (n = 0; n < count; n++)
3280
10.5k
            {
3281
10.5k
                if (tif->tif_flags & TIFF_SWAB)
3282
6
                    TIFFSwabLong(ma);
3283
10.5k
                *mb++ = (uint64_t)(*ma++);
3284
10.5k
            }
3285
41
        }
3286
41
        break;
3287
41
    }
3288
41
    _TIFFfreeExt(tif, origdata);
3289
41
    *value = data;
3290
41
    return (TIFFReadDirEntryErrOk);
3291
41
}
3292
3293
static enum TIFFReadDirEntryErr
3294
TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
3295
                               uint16_t *value)
3296
255
{
3297
255
    enum TIFFReadDirEntryErr err;
3298
255
    uint16_t *m;
3299
255
    uint16_t *na;
3300
255
    uint16_t nb;
3301
255
    if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel)
3302
3
        return (TIFFReadDirEntryErrCount);
3303
252
    err = TIFFReadDirEntryShortArray(tif, direntry, &m);
3304
252
    if (err != TIFFReadDirEntryErrOk || m == NULL)
3305
15
        return (err);
3306
237
    na = m;
3307
237
    nb = tif->tif_dir.td_samplesperpixel;
3308
237
    *value = *na++;
3309
237
    nb--;
3310
950
    while (nb > 0)
3311
713
    {
3312
713
        if (*na++ != *value)
3313
0
        {
3314
0
            err = TIFFReadDirEntryErrPsdif;
3315
0
            break;
3316
0
        }
3317
713
        nb--;
3318
713
    }
3319
237
    _TIFFfreeExt(tif, m);
3320
237
    return (err);
3321
252
}
3322
3323
static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
3324
                                        uint8_t *value)
3325
479
{
3326
479
    (void)tif;
3327
479
    *value = *(uint8_t *)(&direntry->tdir_offset);
3328
479
}
3329
3330
static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
3331
                                         int8_t *value)
3332
75
{
3333
75
    (void)tif;
3334
75
    *value = *(int8_t *)(&direntry->tdir_offset);
3335
75
}
3336
3337
static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
3338
                                         uint16_t *value)
3339
10.0k
{
3340
10.0k
    *value = direntry->tdir_offset.toff_short;
3341
    /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3342
10.0k
    if (tif->tif_flags & TIFF_SWAB)
3343
671
        TIFFSwabShort(value);
3344
10.0k
}
3345
3346
static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
3347
                                          int16_t *value)
3348
32
{
3349
32
    *value = *(int16_t *)(&direntry->tdir_offset);
3350
32
    if (tif->tif_flags & TIFF_SWAB)
3351
4
        TIFFSwabShort((uint16_t *)value);
3352
32
}
3353
3354
static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
3355
                                        uint32_t *value)
3356
486
{
3357
486
    *value = *(uint32_t *)(&direntry->tdir_offset);
3358
486
    if (tif->tif_flags & TIFF_SWAB)
3359
331
        TIFFSwabLong(value);
3360
486
}
3361
3362
static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
3363
                                         int32_t *value)
3364
25
{
3365
25
    *value = *(int32_t *)(&direntry->tdir_offset);
3366
25
    if (tif->tif_flags & TIFF_SWAB)
3367
6
        TIFFSwabLong((uint32_t *)value);
3368
25
}
3369
3370
static enum TIFFReadDirEntryErr
3371
TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
3372
17
{
3373
17
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3374
3
    {
3375
3
        enum TIFFReadDirEntryErr err;
3376
3
        uint32_t offset = direntry->tdir_offset.toff_long;
3377
3
        if (tif->tif_flags & TIFF_SWAB)
3378
0
            TIFFSwabLong(&offset);
3379
3
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3380
3
        if (err != TIFFReadDirEntryErrOk)
3381
0
            return (err);
3382
3
    }
3383
14
    else
3384
14
        *value = direntry->tdir_offset.toff_long8;
3385
17
    if (tif->tif_flags & TIFF_SWAB)
3386
0
        TIFFSwabLong8(value);
3387
17
    return (TIFFReadDirEntryErrOk);
3388
17
}
3389
3390
static enum TIFFReadDirEntryErr
3391
TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
3392
15
{
3393
15
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3394
15
    {
3395
15
        enum TIFFReadDirEntryErr err;
3396
15
        uint32_t offset = direntry->tdir_offset.toff_long;
3397
15
        if (tif->tif_flags & TIFF_SWAB)
3398
0
            TIFFSwabLong(&offset);
3399
15
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3400
15
        if (err != TIFFReadDirEntryErrOk)
3401
11
            return (err);
3402
15
    }
3403
0
    else
3404
0
        *value = *(int64_t *)(&direntry->tdir_offset);
3405
4
    if (tif->tif_flags & TIFF_SWAB)
3406
0
        TIFFSwabLong8((uint64_t *)value);
3407
4
    return (TIFFReadDirEntryErrOk);
3408
15
}
3409
3410
static enum TIFFReadDirEntryErr
3411
TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
3412
                                double *value)
3413
189
{
3414
189
    UInt64Aligned_t m;
3415
3416
189
    assert(sizeof(double) == 8);
3417
189
    assert(sizeof(uint64_t) == 8);
3418
189
    assert(sizeof(uint32_t) == 4);
3419
189
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3420
189
    {
3421
189
        enum TIFFReadDirEntryErr err;
3422
189
        uint32_t offset = direntry->tdir_offset.toff_long;
3423
189
        if (tif->tif_flags & TIFF_SWAB)
3424
24
            TIFFSwabLong(&offset);
3425
189
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3426
189
        if (err != TIFFReadDirEntryErrOk)
3427
10
            return (err);
3428
189
    }
3429
0
    else
3430
0
        m.l = direntry->tdir_offset.toff_long8;
3431
179
    if (tif->tif_flags & TIFF_SWAB)
3432
22
        TIFFSwabArrayOfLong(m.i, 2);
3433
    /* Not completely sure what we should do when m.i[1]==0, but some */
3434
    /* sanitizers do not like division by 0.0: */
3435
    /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3436
179
    if (m.i[0] == 0 || m.i[1] == 0)
3437
11
        *value = 0.0;
3438
168
    else
3439
168
        *value = (double)m.i[0] / (double)m.i[1];
3440
179
    return (TIFFReadDirEntryErrOk);
3441
189
}
3442
3443
static enum TIFFReadDirEntryErr
3444
TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
3445
                                 double *value)
3446
17
{
3447
17
    UInt64Aligned_t m;
3448
17
    assert(sizeof(double) == 8);
3449
17
    assert(sizeof(uint64_t) == 8);
3450
17
    assert(sizeof(int32_t) == 4);
3451
17
    assert(sizeof(uint32_t) == 4);
3452
17
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3453
17
    {
3454
17
        enum TIFFReadDirEntryErr err;
3455
17
        uint32_t offset = direntry->tdir_offset.toff_long;
3456
17
        if (tif->tif_flags & TIFF_SWAB)
3457
0
            TIFFSwabLong(&offset);
3458
17
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3459
17
        if (err != TIFFReadDirEntryErrOk)
3460
1
            return (err);
3461
17
    }
3462
0
    else
3463
0
        m.l = direntry->tdir_offset.toff_long8;
3464
16
    if (tif->tif_flags & TIFF_SWAB)
3465
0
        TIFFSwabArrayOfLong(m.i, 2);
3466
    /* Not completely sure what we should do when m.i[1]==0, but some */
3467
    /* sanitizers do not like division by 0.0: */
3468
    /* http://bugzilla.maptools.org/show_bug.cgi?id=2644 */
3469
16
    if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
3470
0
        *value = 0.0;
3471
16
    else
3472
16
        *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
3473
16
    return (TIFFReadDirEntryErrOk);
3474
17
}
3475
3476
#if 0
3477
static enum TIFFReadDirEntryErr
3478
TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
3479
                                      TIFFRational_t *value)
3480
{ /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
3481
     directly --*/
3482
    UInt64Aligned_t m;
3483
3484
    assert(sizeof(double) == 8);
3485
    assert(sizeof(uint64_t) == 8);
3486
    assert(sizeof(uint32_t) == 4);
3487
3488
    if (direntry->tdir_count != 1)
3489
        return (TIFFReadDirEntryErrCount);
3490
3491
    if (direntry->tdir_type != TIFF_RATIONAL &&
3492
        direntry->tdir_type != TIFF_SRATIONAL)
3493
        return (TIFFReadDirEntryErrType);
3494
3495
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3496
    {
3497
        enum TIFFReadDirEntryErr err;
3498
        uint32_t offset = direntry->tdir_offset.toff_long;
3499
        if (tif->tif_flags & TIFF_SWAB)
3500
            TIFFSwabLong(&offset);
3501
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
3502
        if (err != TIFFReadDirEntryErrOk)
3503
            return (err);
3504
    }
3505
    else
3506
    {
3507
        m.l = direntry->tdir_offset.toff_long8;
3508
    }
3509
3510
    if (tif->tif_flags & TIFF_SWAB)
3511
        TIFFSwabArrayOfLong(m.i, 2);
3512
3513
    value->uNum = m.i[0];
3514
    value->uDenom = m.i[1];
3515
    return (TIFFReadDirEntryErrOk);
3516
} /*-- TIFFReadDirEntryCheckedRationalDirect() --*/
3517
#endif
3518
3519
static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
3520
                                         float *value)
3521
0
{
3522
0
    union
3523
0
    {
3524
0
        float f;
3525
0
        uint32_t i;
3526
0
    } float_union;
3527
0
    assert(sizeof(float) == 4);
3528
0
    assert(sizeof(uint32_t) == 4);
3529
0
    assert(sizeof(float_union) == 4);
3530
0
    float_union.i = *(uint32_t *)(&direntry->tdir_offset);
3531
0
    *value = float_union.f;
3532
0
    if (tif->tif_flags & TIFF_SWAB)
3533
0
        TIFFSwabLong((uint32_t *)value);
3534
0
}
3535
3536
static enum TIFFReadDirEntryErr
3537
TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
3538
5
{
3539
5
    assert(sizeof(double) == 8);
3540
5
    assert(sizeof(uint64_t) == 8);
3541
5
    assert(sizeof(UInt64Aligned_t) == 8);
3542
5
    if (!(tif->tif_flags & TIFF_BIGTIFF))
3543
5
    {
3544
5
        enum TIFFReadDirEntryErr err;
3545
5
        uint32_t offset = direntry->tdir_offset.toff_long;
3546
5
        if (tif->tif_flags & TIFF_SWAB)
3547
0
            TIFFSwabLong(&offset);
3548
5
        err = TIFFReadDirEntryData(tif, offset, 8, value);
3549
5
        if (err != TIFFReadDirEntryErrOk)
3550
3
            return (err);
3551
5
    }
3552
0
    else
3553
0
    {
3554
0
        UInt64Aligned_t uint64_union;
3555
0
        uint64_union.l = direntry->tdir_offset.toff_long8;
3556
0
        *value = uint64_union.d;
3557
0
    }
3558
2
    if (tif->tif_flags & TIFF_SWAB)
3559
0
        TIFFSwabLong8((uint64_t *)value);
3560
2
    return (TIFFReadDirEntryErrOk);
3561
5
}
3562
3563
static enum TIFFReadDirEntryErr
3564
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
3565
28
{
3566
28
    if (value < 0)
3567
0
        return (TIFFReadDirEntryErrRange);
3568
28
    else
3569
28
        return (TIFFReadDirEntryErrOk);
3570
28
}
3571
3572
static enum TIFFReadDirEntryErr
3573
TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
3574
85
{
3575
85
    if (value > 0xFF)
3576
15
        return (TIFFReadDirEntryErrRange);
3577
70
    else
3578
70
        return (TIFFReadDirEntryErrOk);
3579
85
}
3580
3581
static enum TIFFReadDirEntryErr
3582
TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
3583
47
{
3584
47
    if ((value < 0) || (value > 0xFF))
3585
2
        return (TIFFReadDirEntryErrRange);
3586
45
    else
3587
45
        return (TIFFReadDirEntryErrOk);
3588
47
}
3589
3590
static enum TIFFReadDirEntryErr
3591
TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
3592
252
{
3593
252
    if (value > 0xFF)
3594
12
        return (TIFFReadDirEntryErrRange);
3595
240
    else
3596
240
        return (TIFFReadDirEntryErrOk);
3597
252
}
3598
3599
static enum TIFFReadDirEntryErr
3600
TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
3601
21
{
3602
21
    if ((value < 0) || (value > 0xFF))
3603
1
        return (TIFFReadDirEntryErrRange);
3604
20
    else
3605
20
        return (TIFFReadDirEntryErrOk);
3606
21
}
3607
3608
static enum TIFFReadDirEntryErr
3609
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
3610
5
{
3611
5
    if (value > 0xFF)
3612
1
        return (TIFFReadDirEntryErrRange);
3613
4
    else
3614
4
        return (TIFFReadDirEntryErrOk);
3615
5
}
3616
3617
static enum TIFFReadDirEntryErr
3618
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
3619
2
{
3620
2
    if ((value < 0) || (value > 0xFF))
3621
2
        return (TIFFReadDirEntryErrRange);
3622
0
    else
3623
0
        return (TIFFReadDirEntryErrOk);
3624
2
}
3625
3626
static enum TIFFReadDirEntryErr
3627
TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
3628
0
{
3629
0
    if (value > 0x7F)
3630
0
        return (TIFFReadDirEntryErrRange);
3631
0
    else
3632
0
        return (TIFFReadDirEntryErrOk);
3633
0
}
3634
3635
static enum TIFFReadDirEntryErr
3636
TIFFReadDirEntryCheckRangeSbyteShort(uint16_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
TIFFReadDirEntryCheckRangeSbyteSshort(int16_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
TIFFReadDirEntryCheckRangeSbyteLong(uint32_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
TIFFReadDirEntryCheckRangeSbyteSlong(int32_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
TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
3673
0
{
3674
0
    if (value > 0x7F)
3675
0
        return (TIFFReadDirEntryErrRange);
3676
0
    else
3677
0
        return (TIFFReadDirEntryErrOk);
3678
0
}
3679
3680
static enum TIFFReadDirEntryErr
3681
TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
3682
0
{
3683
0
    if ((value < -0x80) || (value > 0x7F))
3684
0
        return (TIFFReadDirEntryErrRange);
3685
0
    else
3686
0
        return (TIFFReadDirEntryErrOk);
3687
0
}
3688
3689
static enum TIFFReadDirEntryErr
3690
TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
3691
65
{
3692
65
    if (value < 0)
3693
0
        return (TIFFReadDirEntryErrRange);
3694
65
    else
3695
65
        return (TIFFReadDirEntryErrOk);
3696
65
}
3697
3698
static enum TIFFReadDirEntryErr
3699
TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
3700
30
{
3701
30
    if (value < 0)
3702
0
        return (TIFFReadDirEntryErrRange);
3703
30
    else
3704
30
        return (TIFFReadDirEntryErrOk);
3705
30
}
3706
3707
static enum TIFFReadDirEntryErr
3708
TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
3709
17
{
3710
17
    if (value > 0xFFFF)
3711
2
        return (TIFFReadDirEntryErrRange);
3712
15
    else
3713
15
        return (TIFFReadDirEntryErrOk);
3714
17
}
3715
3716
static enum TIFFReadDirEntryErr
3717
TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
3718
3
{
3719
3
    if ((value < 0) || (value > 0xFFFF))
3720
0
        return (TIFFReadDirEntryErrRange);
3721
3
    else
3722
3
        return (TIFFReadDirEntryErrOk);
3723
3
}
3724
3725
static enum TIFFReadDirEntryErr
3726
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
3727
37
{
3728
37
    if (value > 0xFFFF)
3729
10
        return (TIFFReadDirEntryErrRange);
3730
27
    else
3731
27
        return (TIFFReadDirEntryErrOk);
3732
37
}
3733
3734
static enum TIFFReadDirEntryErr
3735
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
3736
335
{
3737
335
    if ((value < 0) || (value > 0xFFFF))
3738
5
        return (TIFFReadDirEntryErrRange);
3739
330
    else
3740
330
        return (TIFFReadDirEntryErrOk);
3741
335
}
3742
3743
static enum TIFFReadDirEntryErr
3744
TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
3745
0
{
3746
0
    if (value > 0x7FFF)
3747
0
        return (TIFFReadDirEntryErrRange);
3748
0
    else
3749
0
        return (TIFFReadDirEntryErrOk);
3750
0
}
3751
3752
static enum TIFFReadDirEntryErr
3753
TIFFReadDirEntryCheckRangeSshortLong(uint32_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
TIFFReadDirEntryCheckRangeSshortSlong(int32_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
TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
3772
0
{
3773
0
    if (value > 0x7FFF)
3774
0
        return (TIFFReadDirEntryErrRange);
3775
0
    else
3776
0
        return (TIFFReadDirEntryErrOk);
3777
0
}
3778
3779
static enum TIFFReadDirEntryErr
3780
TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
3781
0
{
3782
0
    if ((value < -0x8000) || (value > 0x7FFF))
3783
0
        return (TIFFReadDirEntryErrRange);
3784
0
    else
3785
0
        return (TIFFReadDirEntryErrOk);
3786
0
}
3787
3788
static enum TIFFReadDirEntryErr
3789
TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
3790
100
{
3791
100
    if (value < 0)
3792
4
        return (TIFFReadDirEntryErrRange);
3793
96
    else
3794
96
        return (TIFFReadDirEntryErrOk);
3795
100
}
3796
3797
static enum TIFFReadDirEntryErr
3798
TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
3799
3
{
3800
3
    if (value < 0)
3801
0
        return (TIFFReadDirEntryErrRange);
3802
3
    else
3803
3
        return (TIFFReadDirEntryErrOk);
3804
3
}
3805
3806
static enum TIFFReadDirEntryErr
3807
TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
3808
7
{
3809
7
    if (value < 0)
3810
0
        return (TIFFReadDirEntryErrRange);
3811
7
    else
3812
7
        return (TIFFReadDirEntryErrOk);
3813
7
}
3814
3815
static enum TIFFReadDirEntryErr
3816
TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
3817
105
{
3818
105
    if (value > UINT32_MAX)
3819
8
        return (TIFFReadDirEntryErrRange);
3820
97
    else
3821
97
        return (TIFFReadDirEntryErrOk);
3822
105
}
3823
3824
static enum TIFFReadDirEntryErr
3825
TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
3826
17
{
3827
17
    if ((value < 0) || (value > (int64_t)UINT32_MAX))
3828
4
        return (TIFFReadDirEntryErrRange);
3829
13
    else
3830
13
        return (TIFFReadDirEntryErrOk);
3831
17
}
3832
3833
static enum TIFFReadDirEntryErr
3834
TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
3835
0
{
3836
0
    if (value > 0x7FFFFFFFUL)
3837
0
        return (TIFFReadDirEntryErrRange);
3838
0
    else
3839
0
        return (TIFFReadDirEntryErrOk);
3840
0
}
3841
3842
/* Check that the 8-byte unsigned value can fit in a 4-byte unsigned range */
3843
static enum TIFFReadDirEntryErr
3844
TIFFReadDirEntryCheckRangeSlongLong8(uint64_t value)
3845
0
{
3846
0
    if (value > 0x7FFFFFFF)
3847
0
        return (TIFFReadDirEntryErrRange);
3848
0
    else
3849
0
        return (TIFFReadDirEntryErrOk);
3850
0
}
3851
3852
/* Check that the 8-byte signed value can fit in a 4-byte signed range */
3853
static enum TIFFReadDirEntryErr
3854
TIFFReadDirEntryCheckRangeSlongSlong8(int64_t value)
3855
0
{
3856
0
    if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
3857
0
        return (TIFFReadDirEntryErrRange);
3858
0
    else
3859
0
        return (TIFFReadDirEntryErrOk);
3860
0
}
3861
3862
static enum TIFFReadDirEntryErr
3863
TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
3864
6
{
3865
6
    if (value < 0)
3866
0
        return (TIFFReadDirEntryErrRange);
3867
6
    else
3868
6
        return (TIFFReadDirEntryErrOk);
3869
6
}
3870
3871
static enum TIFFReadDirEntryErr
3872
TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
3873
221
{
3874
221
    if (value < 0)
3875
2
        return (TIFFReadDirEntryErrRange);
3876
219
    else
3877
219
        return (TIFFReadDirEntryErrOk);
3878
221
}
3879
3880
static enum TIFFReadDirEntryErr
3881
TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
3882
0
{
3883
0
    if (value < 0)
3884
0
        return (TIFFReadDirEntryErrRange);
3885
0
    else
3886
0
        return (TIFFReadDirEntryErrOk);
3887
0
}
3888
3889
static enum TIFFReadDirEntryErr
3890
TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
3891
9
{
3892
9
    if (value < 0)
3893
0
        return (TIFFReadDirEntryErrRange);
3894
9
    else
3895
9
        return (TIFFReadDirEntryErrOk);
3896
9
}
3897
3898
static enum TIFFReadDirEntryErr
3899
TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
3900
0
{
3901
0
    if (value > INT64_MAX)
3902
0
        return (TIFFReadDirEntryErrRange);
3903
0
    else
3904
0
        return (TIFFReadDirEntryErrOk);
3905
0
}
3906
3907
static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
3908
                                                     tmsize_t size, void *dest)
3909
7.77k
{
3910
7.77k
    assert(size > 0);
3911
7.77k
    if (!isMapped(tif))
3912
0
    {
3913
0
        if (!SeekOK(tif, offset))
3914
0
            return (TIFFReadDirEntryErrIo);
3915
0
        if (!ReadOK(tif, dest, size))
3916
0
            return (TIFFReadDirEntryErrIo);
3917
0
    }
3918
7.77k
    else
3919
7.77k
    {
3920
7.77k
        size_t ma, mb;
3921
7.77k
        ma = (size_t)offset;
3922
7.77k
        if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
3923
0
        {
3924
0
            return TIFFReadDirEntryErrIo;
3925
0
        }
3926
7.77k
        mb = ma + size;
3927
7.77k
        if (mb > (uint64_t)tif->tif_size)
3928
1.64k
            return (TIFFReadDirEntryErrIo);
3929
6.12k
        _TIFFmemcpy(dest, tif->tif_base + ma, size);
3930
6.12k
    }
3931
6.12k
    return (TIFFReadDirEntryErrOk);
3932
7.77k
}
3933
3934
static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
3935
                                      const char *module, const char *tagname,
3936
                                      int recover)
3937
18.4k
{
3938
18.4k
    if (!recover)
3939
85
    {
3940
85
        switch (err)
3941
85
        {
3942
12
            case TIFFReadDirEntryErrCount:
3943
12
                TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
3944
12
                              tagname);
3945
12
                break;
3946
25
            case TIFFReadDirEntryErrType:
3947
25
                TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
3948
25
                              tagname);
3949
25
                break;
3950
37
            case TIFFReadDirEntryErrIo:
3951
37
                TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
3952
37
                              tagname);
3953
37
                break;
3954
9
            case TIFFReadDirEntryErrRange:
3955
9
                TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
3956
9
                              tagname);
3957
9
                break;
3958
0
            case TIFFReadDirEntryErrPsdif:
3959
0
                TIFFErrorExtR(
3960
0
                    tif, module,
3961
0
                    "Cannot handle different values per sample for \"%s\"",
3962
0
                    tagname);
3963
0
                break;
3964
2
            case TIFFReadDirEntryErrSizesan:
3965
2
                TIFFErrorExtR(tif, module,
3966
2
                              "Sanity check on size of \"%s\" value failed",
3967
2
                              tagname);
3968
2
                break;
3969
0
            case TIFFReadDirEntryErrAlloc:
3970
0
                TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
3971
0
                              tagname);
3972
0
                break;
3973
0
            default:
3974
0
                assert(0); /* we should never get here */
3975
0
                break;
3976
85
        }
3977
85
    }
3978
18.3k
    else
3979
18.3k
    {
3980
18.3k
        switch (err)
3981
18.3k
        {
3982
1.76k
            case TIFFReadDirEntryErrCount:
3983
1.76k
                TIFFWarningExtR(tif, module,
3984
1.76k
                                "Incorrect count for \"%s\"; tag ignored",
3985
1.76k
                                tagname);
3986
1.76k
                break;
3987
576
            case TIFFReadDirEntryErrType:
3988
576
                TIFFWarningExtR(tif, module,
3989
576
                                "Incompatible type for \"%s\"; tag ignored",
3990
576
                                tagname);
3991
576
                break;
3992
14.3k
            case TIFFReadDirEntryErrIo:
3993
14.3k
                TIFFWarningExtR(
3994
14.3k
                    tif, module,
3995
14.3k
                    "IO error during reading of \"%s\"; tag ignored", tagname);
3996
14.3k
                break;
3997
61
            case TIFFReadDirEntryErrRange:
3998
61
                TIFFWarningExtR(tif, module,
3999
61
                                "Incorrect value for \"%s\"; tag ignored",
4000
61
                                tagname);
4001
61
                break;
4002
0
            case TIFFReadDirEntryErrPsdif:
4003
0
                TIFFWarningExtR(tif, module,
4004
0
                                "Cannot handle different values per sample for "
4005
0
                                "\"%s\"; tag ignored",
4006
0
                                tagname);
4007
0
                break;
4008
1.65k
            case TIFFReadDirEntryErrSizesan:
4009
1.65k
                TIFFWarningExtR(
4010
1.65k
                    tif, module,
4011
1.65k
                    "Sanity check on size of \"%s\" value failed; tag ignored",
4012
1.65k
                    tagname);
4013
1.65k
                break;
4014
0
            case TIFFReadDirEntryErrAlloc:
4015
0
                TIFFWarningExtR(tif, module,
4016
0
                                "Out of memory reading of \"%s\"; tag ignored",
4017
0
                                tagname);
4018
0
                break;
4019
0
            default:
4020
0
                assert(0); /* we should never get here */
4021
0
                break;
4022
18.3k
        }
4023
18.3k
    }
4024
18.4k
}
4025
4026
/*
4027
 * Return the maximum number of color channels specified for a given photometric
4028
 * type. 0 is returned if photometric type isn't supported or no default value
4029
 * is defined by the specification.
4030
 */
4031
static int _TIFFGetMaxColorChannels(uint16_t photometric)
4032
1.40k
{
4033
1.40k
    switch (photometric)
4034
1.40k
    {
4035
53
        case PHOTOMETRIC_PALETTE:
4036
167
        case PHOTOMETRIC_MINISWHITE:
4037
256
        case PHOTOMETRIC_MINISBLACK:
4038
256
            return 1;
4039
845
        case PHOTOMETRIC_YCBCR:
4040
1.02k
        case PHOTOMETRIC_RGB:
4041
1.04k
        case PHOTOMETRIC_CIELAB:
4042
1.05k
        case PHOTOMETRIC_LOGLUV:
4043
1.05k
        case PHOTOMETRIC_ITULAB:
4044
1.05k
        case PHOTOMETRIC_ICCLAB:
4045
1.05k
            return 3;
4046
77
        case PHOTOMETRIC_SEPARATED:
4047
78
        case PHOTOMETRIC_MASK:
4048
78
            return 4;
4049
3
        case PHOTOMETRIC_LOGL:
4050
3
        case PHOTOMETRIC_CFA:
4051
11
        default:
4052
11
            return 0;
4053
1.40k
    }
4054
1.40k
}
4055
4056
static int ByteCountLooksBad(TIFF *tif)
4057
216
{
4058
    /*
4059
     * Assume we have wrong StripByteCount value (in case
4060
     * of single strip) in following cases:
4061
     *   - it is equal to zero along with StripOffset;
4062
     *   - it is larger than file itself (in case of uncompressed
4063
     *     image);
4064
     *   - it is smaller than the size of the bytes per row
4065
     *     multiplied on the number of rows.  The last case should
4066
     *     not be checked in the case of writing new image,
4067
     *     because we may do not know the exact strip size
4068
     *     until the whole image will be written and directory
4069
     *     dumped out.
4070
     */
4071
216
    uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
4072
216
    uint64_t offset = TIFFGetStrileOffset(tif, 0);
4073
216
    uint64_t filesize;
4074
4075
216
    if (offset == 0)
4076
2
        return 0;
4077
214
    if (bytecount == 0)
4078
5
        return 1;
4079
209
    if (tif->tif_dir.td_compression != COMPRESSION_NONE)
4080
198
        return 0;
4081
11
    filesize = TIFFGetFileSize(tif);
4082
11
    if (offset <= filesize && bytecount > filesize - offset)
4083
5
        return 1;
4084
6
    if (tif->tif_mode == O_RDONLY)
4085
6
    {
4086
6
        uint64_t scanlinesize = TIFFScanlineSize64(tif);
4087
6
        if (tif->tif_dir.td_imagelength > 0 &&
4088
6
            scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
4089
0
        {
4090
0
            return 1;
4091
0
        }
4092
6
        if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
4093
2
            return 1;
4094
6
    }
4095
4
    return 0;
4096
6
}
4097
4098
/*
4099
 * Read the next TIFF directory from a file and convert it to the internal
4100
 * format. We read directories sequentially.
4101
 */
4102
int TIFFReadDirectory(TIFF *tif)
4103
1.54k
{
4104
1.54k
    static const char module[] = "TIFFReadDirectory";
4105
1.54k
    TIFFDirEntry *dir;
4106
1.54k
    uint16_t dircount;
4107
1.54k
    TIFFDirEntry *dp;
4108
1.54k
    uint16_t di;
4109
1.54k
    const TIFFField *fip;
4110
1.54k
    uint32_t fii = FAILED_FII;
4111
1.54k
    toff_t nextdiroff;
4112
1.54k
    int bitspersample_read = FALSE;
4113
1.54k
    int color_channels;
4114
4115
1.54k
    if (tif->tif_nextdiroff == 0)
4116
0
    {
4117
        /* In this special case, tif_diroff needs also to be set to 0.
4118
         * This is behind the last IFD, thus no checking or reading necessary.
4119
         */
4120
0
        tif->tif_diroff = tif->tif_nextdiroff;
4121
0
        return 0;
4122
0
    }
4123
4124
1.54k
    nextdiroff = tif->tif_nextdiroff;
4125
    /* tif_curdir++ and tif_nextdiroff should only be updated after SUCCESSFUL
4126
     * reading of the directory. Otherwise, invalid IFD offsets could corrupt
4127
     * the IFD list. */
4128
1.54k
    if (!_TIFFCheckDirNumberAndOffset(tif,
4129
1.54k
                                      tif->tif_curdir ==
4130
1.54k
                                              TIFF_NON_EXISTENT_DIR_NUMBER
4131
1.54k
                                          ? 0
4132
1.54k
                                          : tif->tif_curdir + 1,
4133
1.54k
                                      nextdiroff))
4134
0
    {
4135
0
        return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
4136
                     IFDs) */
4137
0
    }
4138
1.54k
    dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
4139
1.54k
    if (!dircount)
4140
0
    {
4141
0
        TIFFErrorExtR(tif, module,
4142
0
                      "Failed to read directory at offset %" PRIu64,
4143
0
                      nextdiroff);
4144
0
        return 0;
4145
0
    }
4146
    /* Set global values after a valid directory has been fetched.
4147
     * tif_diroff is already set to nextdiroff in TIFFFetchDirectory() in the
4148
     * beginning. */
4149
1.54k
    if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
4150
1.54k
        tif->tif_curdir = 0;
4151
0
    else
4152
0
        tif->tif_curdir++;
4153
1.54k
    (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
4154
4155
1.54k
    TIFFReadDirectoryCheckOrder(tif, dir, dircount);
4156
4157
    /*
4158
     * Mark duplicates of any tag to be ignored (bugzilla 1994)
4159
     * to avoid certain pathological problems.
4160
     */
4161
1.54k
    {
4162
1.54k
        TIFFDirEntry *ma;
4163
1.54k
        uint16_t mb;
4164
409k
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
4165
408k
        {
4166
408k
            TIFFDirEntry *na;
4167
408k
            uint16_t nb;
4168
326M
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
4169
326M
            {
4170
326M
                if (ma->tdir_tag == na->tdir_tag)
4171
23.0M
                {
4172
23.0M
                    na->tdir_ignore = TRUE;
4173
23.0M
                }
4174
326M
            }
4175
408k
        }
4176
1.54k
    }
4177
4178
1.54k
    tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
4179
1.54k
    tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
4180
1.54k
    tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
4181
4182
    /* free any old stuff and reinit */
4183
1.54k
    TIFFFreeDirectory(tif);
4184
1.54k
    TIFFDefaultDirectory(tif);
4185
    /*
4186
     * Electronic Arts writes gray-scale TIFF files
4187
     * without a PlanarConfiguration directory entry.
4188
     * Thus we setup a default value here, even though
4189
     * the TIFF spec says there is no default value.
4190
     * After PlanarConfiguration is preset in TIFFDefaultDirectory()
4191
     * the following setting is not needed, but does not harm either.
4192
     */
4193
1.54k
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
4194
    /*
4195
     * Setup default value and then make a pass over
4196
     * the fields to check type and tag information,
4197
     * and to extract info required to size data
4198
     * structures.  A second pass is made afterwards
4199
     * to read in everything not taken in the first pass.
4200
     * But we must process the Compression tag first
4201
     * in order to merge in codec-private tag definitions (otherwise
4202
     * we may get complaints about unknown tags).  However, the
4203
     * Compression tag may be dependent on the SamplesPerPixel
4204
     * tag value because older TIFF specs permitted Compression
4205
     * to be written as a SamplesPerPixel-count tag entry.
4206
     * Thus if we don't first figure out the correct SamplesPerPixel
4207
     * tag value then we may end up ignoring the Compression tag
4208
     * value because it has an incorrect count value (if the
4209
     * true value of SamplesPerPixel is not 1).
4210
     */
4211
1.54k
    dp =
4212
1.54k
        TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
4213
1.54k
    if (dp)
4214
1.31k
    {
4215
1.31k
        if (!TIFFFetchNormalTag(tif, dp, 0))
4216
0
            goto bad;
4217
1.31k
        dp->tdir_ignore = TRUE;
4218
1.31k
    }
4219
1.54k
    dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
4220
1.54k
    if (dp)
4221
1.18k
    {
4222
        /*
4223
         * The 5.0 spec says the Compression tag has one value, while
4224
         * earlier specs say it has one value per sample.  Because of
4225
         * this, we accept the tag if one value is supplied with either
4226
         * count.
4227
         */
4228
1.18k
        uint16_t value;
4229
1.18k
        enum TIFFReadDirEntryErr err;
4230
1.18k
        err = TIFFReadDirEntryShort(tif, dp, &value);
4231
1.18k
        if (err == TIFFReadDirEntryErrCount)
4232
14
            err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
4233
1.18k
        if (err != TIFFReadDirEntryErrOk)
4234
9
        {
4235
9
            TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
4236
9
            goto bad;
4237
9
        }
4238
1.17k
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
4239
0
            goto bad;
4240
1.17k
        dp->tdir_ignore = TRUE;
4241
1.17k
    }
4242
363
    else
4243
363
    {
4244
363
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
4245
0
            goto bad;
4246
363
    }
4247
    /*
4248
     * First real pass over the directory.
4249
     */
4250
403k
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4251
401k
    {
4252
401k
        if (!dp->tdir_ignore)
4253
158k
        {
4254
158k
            TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4255
158k
            if (fii == FAILED_FII)
4256
141k
            {
4257
141k
                TIFFWarningExtR(tif, module,
4258
141k
                                "Unknown field with tag %" PRIu16 " (0x%" PRIx16
4259
141k
                                ") encountered",
4260
141k
                                dp->tdir_tag, dp->tdir_tag);
4261
                /* the following knowingly leaks the
4262
                   anonymous field structure */
4263
141k
                if (!_TIFFMergeFields(
4264
141k
                        tif,
4265
141k
                        _TIFFCreateAnonField(tif, dp->tdir_tag,
4266
141k
                                             (TIFFDataType)dp->tdir_type),
4267
141k
                        1))
4268
0
                {
4269
0
                    TIFFWarningExtR(
4270
0
                        tif, module,
4271
0
                        "Registering anonymous field with tag %" PRIu16
4272
0
                        " (0x%" PRIx16 ") failed",
4273
0
                        dp->tdir_tag, dp->tdir_tag);
4274
0
                    dp->tdir_ignore = TRUE;
4275
0
                }
4276
141k
                else
4277
141k
                {
4278
141k
                    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
4279
141k
                    assert(fii != FAILED_FII);
4280
141k
                }
4281
141k
            }
4282
158k
        }
4283
401k
        if (!dp->tdir_ignore)
4284
158k
        {
4285
158k
            fip = tif->tif_fields[fii];
4286
158k
            if (fip->field_bit == FIELD_IGNORE)
4287
831
                dp->tdir_ignore = TRUE;
4288
157k
            else
4289
157k
            {
4290
157k
                switch (dp->tdir_tag)
4291
157k
                {
4292
1.01k
                    case TIFFTAG_STRIPOFFSETS:
4293
2.25k
                    case TIFFTAG_STRIPBYTECOUNTS:
4294
2.79k
                    case TIFFTAG_TILEOFFSETS:
4295
2.88k
                    case TIFFTAG_TILEBYTECOUNTS:
4296
2.88k
                        TIFFSetFieldBit(tif, fip->field_bit);
4297
2.88k
                        break;
4298
1.50k
                    case TIFFTAG_IMAGEWIDTH:
4299
2.91k
                    case TIFFTAG_IMAGELENGTH:
4300
2.92k
                    case TIFFTAG_IMAGEDEPTH:
4301
3.48k
                    case TIFFTAG_TILELENGTH:
4302
4.09k
                    case TIFFTAG_TILEWIDTH:
4303
4.09k
                    case TIFFTAG_TILEDEPTH:
4304
4.55k
                    case TIFFTAG_PLANARCONFIG:
4305
5.25k
                    case TIFFTAG_ROWSPERSTRIP:
4306
5.48k
                    case TIFFTAG_EXTRASAMPLES:
4307
5.48k
                        if (!TIFFFetchNormalTag(tif, dp, 0))
4308
10
                            goto bad;
4309
5.47k
                        dp->tdir_ignore = TRUE;
4310
5.47k
                        break;
4311
149k
                    default:
4312
149k
                        if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
4313
1.54k
                            dp->tdir_ignore = TRUE;
4314
149k
                        break;
4315
157k
                }
4316
157k
            }
4317
158k
        }
4318
401k
    }
4319
    /*
4320
     * XXX: OJPEG hack.
4321
     * If a) compression is OJPEG, b) planarconfig tag says it's separate,
4322
     * c) strip offsets/bytecounts tag are both present and
4323
     * d) both contain exactly one value, then we consistently find
4324
     * that the buggy implementation of the buggy compression scheme
4325
     * matches contig planarconfig best. So we 'fix-up' the tag here
4326
     */
4327
1.52k
    if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4328
1.52k
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
4329
5
    {
4330
5
        if (!_TIFFFillStriles(tif))
4331
0
            goto bad;
4332
5
        dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4333
5
                                        TIFFTAG_STRIPOFFSETS);
4334
5
        if ((dp != 0) && (dp->tdir_count == 1))
4335
5
        {
4336
5
            dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
4337
5
                                            TIFFTAG_STRIPBYTECOUNTS);
4338
5
            if ((dp != 0) && (dp->tdir_count == 1))
4339
0
            {
4340
0
                tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
4341
0
                TIFFWarningExtR(tif, module,
4342
0
                                "Planarconfig tag value assumed incorrect, "
4343
0
                                "assuming data is contig instead of chunky");
4344
0
            }
4345
5
        }
4346
5
    }
4347
    /*
4348
     * Allocate directory structure and setup defaults.
4349
     */
4350
1.52k
    if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
4351
6
    {
4352
6
        MissingRequired(tif, "ImageLength");
4353
6
        goto bad;
4354
6
    }
4355
4356
    /*
4357
     * Second pass: extract other information.
4358
     */
4359
398k
    for (di = 0, dp = dir; di < dircount; di++, dp++)
4360
396k
    {
4361
396k
        if (!dp->tdir_ignore)
4362
148k
        {
4363
148k
            switch (dp->tdir_tag)
4364
148k
            {
4365
7
                case TIFFTAG_MINSAMPLEVALUE:
4366
39
                case TIFFTAG_MAXSAMPLEVALUE:
4367
940
                case TIFFTAG_BITSPERSAMPLE:
4368
954
                case TIFFTAG_DATATYPE:
4369
1.07k
                case TIFFTAG_SAMPLEFORMAT:
4370
                    /*
4371
                     * The MinSampleValue, MaxSampleValue, BitsPerSample
4372
                     * DataType and SampleFormat tags are supposed to be
4373
                     * written as one value/sample, but some vendors
4374
                     * incorrectly write one value only -- so we accept
4375
                     * that as well (yuck). Other vendors write correct
4376
                     * value for NumberOfSamples, but incorrect one for
4377
                     * BitsPerSample and friends, and we will read this
4378
                     * too.
4379
                     */
4380
1.07k
                    {
4381
1.07k
                        uint16_t value;
4382
1.07k
                        enum TIFFReadDirEntryErr err;
4383
1.07k
                        err = TIFFReadDirEntryShort(tif, dp, &value);
4384
1.07k
                        if (err == TIFFReadDirEntryErrCount)
4385
241
                            err =
4386
241
                                TIFFReadDirEntryPersampleShort(tif, dp, &value);
4387
1.07k
                        if (err != TIFFReadDirEntryErrOk)
4388
9
                        {
4389
9
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4390
9
                            TIFFReadDirEntryOutputErr(
4391
9
                                tif, err, module,
4392
9
                                fip ? fip->field_name : "unknown tagname", 0);
4393
9
                            goto bad;
4394
9
                        }
4395
1.06k
                        if (!TIFFSetField(tif, dp->tdir_tag, value))
4396
0
                            goto bad;
4397
1.06k
                        if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
4398
896
                            bitspersample_read = TRUE;
4399
1.06k
                    }
4400
0
                    break;
4401
1
                case TIFFTAG_SMINSAMPLEVALUE:
4402
5
                case TIFFTAG_SMAXSAMPLEVALUE:
4403
5
                {
4404
4405
5
                    double *data = NULL;
4406
5
                    enum TIFFReadDirEntryErr err;
4407
5
                    uint32_t saved_flags;
4408
5
                    int m;
4409
5
                    if (dp->tdir_count !=
4410
5
                        (uint64_t)tif->tif_dir.td_samplesperpixel)
4411
2
                        err = TIFFReadDirEntryErrCount;
4412
3
                    else
4413
3
                        err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
4414
5
                    if (err != TIFFReadDirEntryErrOk)
4415
2
                    {
4416
2
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4417
2
                        TIFFReadDirEntryOutputErr(
4418
2
                            tif, err, module,
4419
2
                            fip ? fip->field_name : "unknown tagname", 0);
4420
2
                        goto bad;
4421
2
                    }
4422
3
                    saved_flags = tif->tif_flags;
4423
3
                    tif->tif_flags |= TIFF_PERSAMPLE;
4424
3
                    m = TIFFSetField(tif, dp->tdir_tag, data);
4425
3
                    tif->tif_flags = saved_flags;
4426
3
                    _TIFFfreeExt(tif, data);
4427
3
                    if (!m)
4428
0
                        goto bad;
4429
3
                }
4430
3
                break;
4431
1.00k
                case TIFFTAG_STRIPOFFSETS:
4432
1.54k
                case TIFFTAG_TILEOFFSETS:
4433
1.54k
                    switch (dp->tdir_type)
4434
1.54k
                    {
4435
237
                        case TIFF_SHORT:
4436
1.24k
                        case TIFF_LONG:
4437
1.24k
                        case TIFF_LONG8:
4438
1.24k
                            break;
4439
294
                        default:
4440
                            /* Warn except if directory typically created with
4441
                             * TIFFDeferStrileArrayWriting() */
4442
294
                            if (!(tif->tif_mode == O_RDWR &&
4443
294
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
4444
294
                                  dp->tdir_offset.toff_long8 == 0))
4445
294
                            {
4446
294
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4447
294
                                TIFFWarningExtR(
4448
294
                                    tif, module, "Invalid data type for tag %s",
4449
294
                                    fip ? fip->field_name : "unknown tagname");
4450
294
                            }
4451
294
                            break;
4452
1.54k
                    }
4453
1.54k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
4454
1.54k
                                sizeof(TIFFDirEntry));
4455
1.54k
                    break;
4456
1.23k
                case TIFFTAG_STRIPBYTECOUNTS:
4457
1.32k
                case TIFFTAG_TILEBYTECOUNTS:
4458
1.32k
                    switch (dp->tdir_type)
4459
1.32k
                    {
4460
70
                        case TIFF_SHORT:
4461
285
                        case TIFF_LONG:
4462
850
                        case TIFF_LONG8:
4463
850
                            break;
4464
478
                        default:
4465
                            /* Warn except if directory typically created with
4466
                             * TIFFDeferStrileArrayWriting() */
4467
478
                            if (!(tif->tif_mode == O_RDWR &&
4468
478
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
4469
478
                                  dp->tdir_offset.toff_long8 == 0))
4470
478
                            {
4471
478
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4472
478
                                TIFFWarningExtR(
4473
478
                                    tif, module, "Invalid data type for tag %s",
4474
478
                                    fip ? fip->field_name : "unknown tagname");
4475
478
                            }
4476
478
                            break;
4477
1.32k
                    }
4478
1.32k
                    _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
4479
1.32k
                                sizeof(TIFFDirEntry));
4480
1.32k
                    break;
4481
210
                case TIFFTAG_COLORMAP:
4482
246
                case TIFFTAG_TRANSFERFUNCTION:
4483
246
                {
4484
246
                    enum TIFFReadDirEntryErr err;
4485
246
                    uint32_t countpersample;
4486
246
                    uint32_t countrequired;
4487
246
                    uint32_t incrementpersample;
4488
246
                    uint16_t *value = NULL;
4489
                    /* It would be dangerous to instantiate those tag values */
4490
                    /* since if td_bitspersample has not yet been read (due to
4491
                     */
4492
                    /* unordered tags), it could be read afterwards with a */
4493
                    /* values greater than the default one (1), which may cause
4494
                     */
4495
                    /* crashes in user code */
4496
246
                    if (!bitspersample_read)
4497
55
                    {
4498
55
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4499
55
                        TIFFWarningExtR(
4500
55
                            tif, module,
4501
55
                            "Ignoring %s since BitsPerSample tag not found",
4502
55
                            fip ? fip->field_name : "unknown tagname");
4503
55
                        continue;
4504
55
                    }
4505
                    /* ColorMap or TransferFunction for high bit */
4506
                    /* depths do not make much sense and could be */
4507
                    /* used as a denial of service vector */
4508
191
                    if (tif->tif_dir.td_bitspersample > 24)
4509
5
                    {
4510
5
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4511
5
                        TIFFWarningExtR(
4512
5
                            tif, module,
4513
5
                            "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4514
5
                            fip ? fip->field_name : "unknown tagname",
4515
5
                            tif->tif_dir.td_bitspersample);
4516
5
                        continue;
4517
5
                    }
4518
186
                    countpersample = (1U << tif->tif_dir.td_bitspersample);
4519
186
                    if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
4520
186
                        (dp->tdir_count == (uint64_t)countpersample))
4521
8
                    {
4522
8
                        countrequired = countpersample;
4523
8
                        incrementpersample = 0;
4524
8
                    }
4525
178
                    else
4526
178
                    {
4527
178
                        countrequired = 3 * countpersample;
4528
178
                        incrementpersample = countpersample;
4529
178
                    }
4530
186
                    if (dp->tdir_count != (uint64_t)countrequired)
4531
60
                        err = TIFFReadDirEntryErrCount;
4532
126
                    else
4533
126
                        err = TIFFReadDirEntryShortArray(tif, dp, &value);
4534
186
                    if (err != TIFFReadDirEntryErrOk)
4535
157
                    {
4536
157
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4537
157
                        TIFFReadDirEntryOutputErr(
4538
157
                            tif, err, module,
4539
157
                            fip ? fip->field_name : "unknown tagname", 1);
4540
157
                    }
4541
29
                    else
4542
29
                    {
4543
29
                        TIFFSetField(tif, dp->tdir_tag, value,
4544
29
                                     value + incrementpersample,
4545
29
                                     value + 2 * incrementpersample);
4546
29
                        _TIFFfreeExt(tif, value);
4547
29
                    }
4548
186
                }
4549
0
                break;
4550
                    /* BEGIN REV 4.0 COMPATIBILITY */
4551
0
                case TIFFTAG_OSUBFILETYPE:
4552
0
                {
4553
0
                    uint16_t valueo;
4554
0
                    uint32_t value;
4555
0
                    if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
4556
0
                        TIFFReadDirEntryErrOk)
4557
0
                    {
4558
0
                        switch (valueo)
4559
0
                        {
4560
0
                            case OFILETYPE_REDUCEDIMAGE:
4561
0
                                value = FILETYPE_REDUCEDIMAGE;
4562
0
                                break;
4563
0
                            case OFILETYPE_PAGE:
4564
0
                                value = FILETYPE_PAGE;
4565
0
                                break;
4566
0
                            default:
4567
0
                                value = 0;
4568
0
                                break;
4569
0
                        }
4570
0
                        if (value != 0)
4571
0
                            TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
4572
0
                    }
4573
0
                }
4574
0
                break;
4575
                /* END REV 4.0 COMPATIBILITY */
4576
#if 0
4577
                case TIFFTAG_EP_BATTERYLEVEL:
4578
                    /* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
4579
                     * LibTiff defines it as ASCII and converts RATIONAL to an
4580
                     * ASCII string. */
4581
                    switch (dp->tdir_type)
4582
                    {
4583
                        case TIFF_RATIONAL:
4584
                        {
4585
                            /* Read rational and convert to ASCII*/
4586
                            enum TIFFReadDirEntryErr err;
4587
                            TIFFRational_t rValue;
4588
                            err = TIFFReadDirEntryCheckedRationalDirect(
4589
                                tif, dp, &rValue);
4590
                            if (err != TIFFReadDirEntryErrOk)
4591
                            {
4592
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4593
                                TIFFReadDirEntryOutputErr(
4594
                                    tif, err, module,
4595
                                    fip ? fip->field_name : "unknown tagname",
4596
                                    1);
4597
                            }
4598
                            else
4599
                            {
4600
                                char szAux[32];
4601
                                snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
4602
                                         rValue.uNum, rValue.uDenom);
4603
                                TIFFSetField(tif, dp->tdir_tag, szAux);
4604
                            }
4605
                        }
4606
                        break;
4607
                        case TIFF_ASCII:
4608
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
4609
                            break;
4610
                        default:
4611
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
4612
                            TIFFWarningExtR(tif, module,
4613
                                            "Invalid data type for tag %s. "
4614
                                            "ASCII or RATIONAL expected",
4615
                                            fip ? fip->field_name
4616
                                                : "unknown tagname");
4617
                            break;
4618
                    }
4619
                    break;
4620
#endif
4621
144k
                default:
4622
144k
                    (void)TIFFFetchNormalTag(tif, dp, TRUE);
4623
144k
                    break;
4624
148k
            }
4625
148k
        } /* -- if (!dp->tdir_ignore) */
4626
396k
    }     /* -- for-loop -- */
4627
4628
    /*
4629
     * OJPEG hack:
4630
     * - If a) compression is OJPEG, and b) photometric tag is missing,
4631
     * then we consistently find that photometric should be YCbCr
4632
     * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
4633
     * then we consistently find that the buggy implementation of the
4634
     * buggy compression scheme matches photometric YCbCr instead.
4635
     * - If a) compression is OJPEG, and b) bitspersample tag is missing,
4636
     * then we consistently find bitspersample should be 8.
4637
     * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4638
     * and c) photometric is RGB or YCbCr, then we consistently find
4639
     * samplesperpixel should be 3
4640
     * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
4641
     * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
4642
     * find samplesperpixel should be 3
4643
     */
4644
1.50k
    if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
4645
11
    {
4646
11
        if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
4647
5
        {
4648
5
            TIFFWarningExtR(
4649
5
                tif, module,
4650
5
                "Photometric tag is missing, assuming data is YCbCr");
4651
5
            if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
4652
0
                goto bad;
4653
5
        }
4654
6
        else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4655
3
        {
4656
3
            tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
4657
3
            TIFFWarningExtR(tif, module,
4658
3
                            "Photometric tag value assumed incorrect, "
4659
3
                            "assuming data is YCbCr instead of RGB");
4660
3
        }
4661
11
        if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
4662
3
        {
4663
3
            TIFFWarningExtR(
4664
3
                tif, module,
4665
3
                "BitsPerSample tag is missing, assuming 8 bits per sample");
4666
3
            if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
4667
0
                goto bad;
4668
3
        }
4669
11
        if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
4670
5
        {
4671
5
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
4672
0
            {
4673
0
                TIFFWarningExtR(tif, module,
4674
0
                                "SamplesPerPixel tag is missing, "
4675
0
                                "assuming correct SamplesPerPixel value is 3");
4676
0
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4677
0
                    goto bad;
4678
0
            }
4679
5
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
4680
5
            {
4681
5
                TIFFWarningExtR(tif, module,
4682
5
                                "SamplesPerPixel tag is missing, "
4683
5
                                "applying correct SamplesPerPixel value of 3");
4684
5
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
4685
0
                    goto bad;
4686
5
            }
4687
0
            else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
4688
0
                     (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
4689
0
            {
4690
                /*
4691
                 * SamplesPerPixel tag is missing, but is not required
4692
                 * by spec.  Assume correct SamplesPerPixel value of 1.
4693
                 */
4694
0
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
4695
0
                    goto bad;
4696
0
            }
4697
5
        }
4698
11
    }
4699
4700
    /*
4701
     * Setup appropriate structures (by strip or by tile)
4702
     * We do that only after the above OJPEG hack which alters SamplesPerPixel
4703
     * and thus influences the number of strips in the separate planarconfig.
4704
     */
4705
1.50k
    if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
4706
809
    {
4707
809
        tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
4708
809
        tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
4709
809
        tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
4710
809
        tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
4711
809
        tif->tif_flags &= ~TIFF_ISTILED;
4712
809
    }
4713
700
    else
4714
700
    {
4715
700
        tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
4716
700
        tif->tif_flags |= TIFF_ISTILED;
4717
700
    }
4718
1.50k
    if (!tif->tif_dir.td_nstrips)
4719
8
    {
4720
8
        TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
4721
8
                      isTiled(tif) ? "tiles" : "strips");
4722
8
        goto bad;
4723
8
    }
4724
1.50k
    tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
4725
1.50k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
4726
309
        tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
4727
1.50k
    if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
4728
46
    {
4729
#ifdef OJPEG_SUPPORT
4730
        if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
4731
            (isTiled(tif) == 0) && (tif->tif_dir.td_nstrips == 1))
4732
        {
4733
            /*
4734
             * XXX: OJPEG hack.
4735
             * If a) compression is OJPEG, b) it's not a tiled TIFF,
4736
             * and c) the number of strips is 1,
4737
             * then we tolerate the absence of stripoffsets tag,
4738
             * because, presumably, all required data is in the
4739
             * JpegInterchangeFormat stream.
4740
             */
4741
            TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
4742
        }
4743
        else
4744
#endif
4745
46
        {
4746
46
            MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
4747
46
            goto bad;
4748
46
        }
4749
46
    }
4750
4751
1.45k
    if (tif->tif_mode == O_RDWR &&
4752
1.45k
        tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
4753
1.45k
        tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
4754
1.45k
        tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
4755
1.45k
        tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
4756
1.45k
        tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
4757
1.45k
        tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
4758
1.45k
        tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
4759
1.45k
        tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
4760
0
    {
4761
        /* Directory typically created with TIFFDeferStrileArrayWriting() */
4762
0
        TIFFSetupStrips(tif);
4763
0
    }
4764
1.45k
    else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
4765
1.45k
    {
4766
1.45k
        if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
4767
1.45k
        {
4768
1.45k
            if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
4769
1.45k
                                     tif->tif_dir.td_nstrips,
4770
1.45k
                                     &tif->tif_dir.td_stripoffset_p))
4771
36
            {
4772
36
                goto bad;
4773
36
            }
4774
1.45k
        }
4775
1.41k
        if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
4776
1.23k
        {
4777
1.23k
            if (!TIFFFetchStripThing(
4778
1.23k
                    tif, &(tif->tif_dir.td_stripbytecount_entry),
4779
1.23k
                    tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
4780
19
            {
4781
19
                goto bad;
4782
19
            }
4783
1.23k
        }
4784
1.41k
    }
4785
4786
    /*
4787
     * Make sure all non-color channels are extrasamples.
4788
     * If it's not the case, define them as such.
4789
     */
4790
1.40k
    color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
4791
1.40k
    if (color_channels &&
4792
1.40k
        tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
4793
1.38k
            color_channels)
4794
131
    {
4795
131
        uint16_t old_extrasamples;
4796
131
        uint16_t *new_sampleinfo;
4797
4798
131
        TIFFWarningExtR(
4799
131
            tif, module,
4800
131
            "Sum of Photometric type-related "
4801
131
            "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4802
131
            "Defining non-color channels as ExtraSamples.");
4803
4804
131
        old_extrasamples = tif->tif_dir.td_extrasamples;
4805
131
        tif->tif_dir.td_extrasamples =
4806
131
            (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
4807
4808
        // sampleinfo should contain information relative to these new extra
4809
        // samples
4810
131
        new_sampleinfo = (uint16_t *)_TIFFcallocExt(
4811
131
            tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
4812
131
        if (!new_sampleinfo)
4813
0
        {
4814
0
            TIFFErrorExtR(tif, module,
4815
0
                          "Failed to allocate memory for "
4816
0
                          "temporary new sampleinfo array "
4817
0
                          "(%" PRIu16 " 16 bit elements)",
4818
0
                          tif->tif_dir.td_extrasamples);
4819
0
            goto bad;
4820
0
        }
4821
4822
131
        if (old_extrasamples > 0)
4823
11
            memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
4824
11
                   old_extrasamples * sizeof(uint16_t));
4825
131
        _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
4826
131
                              tif->tif_dir.td_extrasamples);
4827
131
        _TIFFfreeExt(tif, new_sampleinfo);
4828
131
    }
4829
4830
    /*
4831
     * Verify Palette image has a Colormap.
4832
     */
4833
1.40k
    if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
4834
1.40k
        !TIFFFieldSet(tif, FIELD_COLORMAP))
4835
31
    {
4836
31
        if (tif->tif_dir.td_bitspersample >= 8 &&
4837
31
            tif->tif_dir.td_samplesperpixel == 3)
4838
1
            tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
4839
30
        else if (tif->tif_dir.td_bitspersample >= 8)
4840
30
            tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
4841
0
        else
4842
0
        {
4843
0
            MissingRequired(tif, "Colormap");
4844
0
            goto bad;
4845
0
        }
4846
31
    }
4847
    /*
4848
     * OJPEG hack:
4849
     * We do no further messing with strip/tile offsets/bytecounts in OJPEG
4850
     * TIFFs
4851
     */
4852
1.40k
    if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
4853
1.39k
    {
4854
        /*
4855
         * Attempt to deal with a missing StripByteCounts tag.
4856
         */
4857
1.39k
        if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
4858
182
        {
4859
            /*
4860
             * Some manufacturers violate the spec by not giving
4861
             * the size of the strips.  In this case, assume there
4862
             * is one uncompressed strip of data.
4863
             */
4864
182
            if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4865
182
                 tif->tif_dir.td_nstrips > 1) ||
4866
182
                (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
4867
182
                 tif->tif_dir.td_nstrips !=
4868
3
                     (uint32_t)tif->tif_dir.td_samplesperpixel))
4869
0
            {
4870
0
                MissingRequired(tif, "StripByteCounts");
4871
0
                goto bad;
4872
0
            }
4873
182
            TIFFWarningExtR(
4874
182
                tif, module,
4875
182
                "TIFF directory is missing required "
4876
182
                "\"StripByteCounts\" field, calculating from imagelength");
4877
182
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4878
11
                goto bad;
4879
182
        }
4880
1.21k
        else if (tif->tif_dir.td_nstrips == 1 &&
4881
1.21k
                 !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
4882
12
        {
4883
            /*
4884
             * XXX: Plexus (and others) sometimes give a value of
4885
             * zero for a tag when they don't know what the
4886
             * correct value is!  Try and handle the simple case
4887
             * of estimating the size of a one strip image.
4888
             */
4889
12
            TIFFWarningExtR(tif, module,
4890
12
                            "Bogus \"StripByteCounts\" field, ignoring and "
4891
12
                            "calculating from imagelength");
4892
12
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4893
2
                goto bad;
4894
12
        }
4895
1.20k
        else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
4896
1.20k
                 tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4897
1.20k
                 tif->tif_dir.td_nstrips > 2 &&
4898
1.20k
                 tif->tif_dir.td_compression == COMPRESSION_NONE &&
4899
1.20k
                 TIFFGetStrileByteCount(tif, 0) !=
4900
161
                     TIFFGetStrileByteCount(tif, 1) &&
4901
1.20k
                 TIFFGetStrileByteCount(tif, 0) != 0 &&
4902
1.20k
                 TIFFGetStrileByteCount(tif, 1) != 0)
4903
99
        {
4904
            /*
4905
             * XXX: Some vendors fill StripByteCount array with
4906
             * absolutely wrong values (it can be equal to
4907
             * StripOffset array, for example). Catch this case
4908
             * here.
4909
             *
4910
             * We avoid this check if deferring strile loading
4911
             * as it would always force us to load the strip/tile
4912
             * information.
4913
             */
4914
99
            TIFFWarningExtR(tif, module,
4915
99
                            "Wrong \"StripByteCounts\" field, ignoring and "
4916
99
                            "calculating from imagelength");
4917
99
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
4918
0
                goto bad;
4919
99
        }
4920
1.39k
    }
4921
1.38k
    if (dir)
4922
1.38k
    {
4923
1.38k
        _TIFFfreeExt(tif, dir);
4924
1.38k
        dir = NULL;
4925
1.38k
    }
4926
1.38k
    if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
4927
1.35k
    {
4928
1.35k
        if (tif->tif_dir.td_bitspersample >= 16)
4929
176
            tif->tif_dir.td_maxsamplevalue = 0xFFFF;
4930
1.17k
        else
4931
1.17k
            tif->tif_dir.td_maxsamplevalue =
4932
1.17k
                (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
4933
1.35k
    }
4934
4935
#ifdef STRIPBYTECOUNTSORTED_UNUSED
4936
    /*
4937
     * XXX: We can optimize checking for the strip bounds using the sorted
4938
     * bytecounts array. See also comments for TIFFAppendToStrip()
4939
     * function in tif_write.c.
4940
     */
4941
    if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) && tif->tif_dir.td_nstrips > 1)
4942
    {
4943
        uint32_t strip;
4944
4945
        tif->tif_dir.td_stripbytecountsorted = 1;
4946
        for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
4947
        {
4948
            if (TIFFGetStrileOffset(tif, strip - 1) >
4949
                TIFFGetStrileOffset(tif, strip))
4950
            {
4951
                tif->tif_dir.td_stripbytecountsorted = 0;
4952
                break;
4953
            }
4954
        }
4955
    }
4956
#endif
4957
4958
    /*
4959
     * An opportunity for compression mode dependent tag fixup
4960
     */
4961
1.38k
    (*tif->tif_fixuptags)(tif);
4962
4963
    /*
4964
     * Some manufacturers make life difficult by writing
4965
     * large amounts of uncompressed data as a single strip.
4966
     * This is contrary to the recommendations of the spec.
4967
     * The following makes an attempt at breaking such images
4968
     * into strips closer to the recommended 8k bytes.  A
4969
     * side effect, however, is that the RowsPerStrip tag
4970
     * value may be changed.
4971
     */
4972
1.38k
    if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
4973
1.38k
        (tif->tif_dir.td_nstrips == 1) &&
4974
1.38k
        (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
4975
1.38k
        ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
4976
0
    {
4977
0
        ChopUpSingleUncompressedStrip(tif);
4978
0
    }
4979
4980
    /* There are also uncompressed striped files with strips larger than */
4981
    /* 2 GB, which make them unfriendly with a lot of code. If possible, */
4982
    /* try to expose smaller "virtual" strips. */
4983
1.38k
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
4984
1.38k
        tif->tif_dir.td_compression == COMPRESSION_NONE &&
4985
1.38k
        (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
4986
1.38k
        TIFFStripSize64(tif) > 0x7FFFFFFFUL)
4987
0
    {
4988
0
        TryChopUpUncompressedBigTiff(tif);
4989
0
    }
4990
4991
    /*
4992
     * Clear the dirty directory flag.
4993
     */
4994
1.38k
    tif->tif_flags &= ~TIFF_DIRTYDIRECT;
4995
1.38k
    tif->tif_flags &= ~TIFF_DIRTYSTRIP;
4996
4997
    /*
4998
     * Reinitialize i/o since we are starting on a new directory.
4999
     */
5000
1.38k
    tif->tif_row = (uint32_t)-1;
5001
1.38k
    tif->tif_curstrip = (uint32_t)-1;
5002
1.38k
    tif->tif_col = (uint32_t)-1;
5003
1.38k
    tif->tif_curtile = (uint32_t)-1;
5004
1.38k
    tif->tif_tilesize = (tmsize_t)-1;
5005
5006
1.38k
    tif->tif_scanlinesize = TIFFScanlineSize(tif);
5007
1.38k
    if (!tif->tif_scanlinesize)
5008
37
    {
5009
37
        TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
5010
37
        return (0);
5011
37
    }
5012
5013
1.35k
    if (isTiled(tif))
5014
662
    {
5015
662
        tif->tif_tilesize = TIFFTileSize(tif);
5016
662
        if (!tif->tif_tilesize)
5017
0
        {
5018
0
            TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
5019
0
            return (0);
5020
0
        }
5021
662
    }
5022
688
    else
5023
688
    {
5024
688
        if (!TIFFStripSize(tif))
5025
72
        {
5026
72
            TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
5027
72
            return (0);
5028
72
        }
5029
688
    }
5030
1.27k
    return (1);
5031
158
bad:
5032
158
    if (dir)
5033
158
        _TIFFfreeExt(tif, dir);
5034
158
    return (0);
5035
1.35k
}
5036
5037
static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
5038
                                        uint16_t dircount)
5039
1.54k
{
5040
1.54k
    static const char module[] = "TIFFReadDirectoryCheckOrder";
5041
1.54k
    uint32_t m;
5042
1.54k
    uint16_t n;
5043
1.54k
    TIFFDirEntry *o;
5044
1.54k
    m = 0;
5045
6.67k
    for (n = 0, o = dir; n < dircount; n++, o++)
5046
6.61k
    {
5047
6.61k
        if (o->tdir_tag < m)
5048
1.48k
        {
5049
1.48k
            TIFFWarningExtR(tif, module,
5050
1.48k
                            "Invalid TIFF directory; tags are not sorted in "
5051
1.48k
                            "ascending order");
5052
1.48k
            break;
5053
1.48k
        }
5054
5.12k
        m = o->tdir_tag + 1;
5055
5.12k
    }
5056
1.54k
}
5057
5058
static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
5059
                                                uint16_t dircount,
5060
                                                uint16_t tagid)
5061
3.10k
{
5062
3.10k
    TIFFDirEntry *m;
5063
3.10k
    uint16_t n;
5064
3.10k
    (void)tif;
5065
305k
    for (m = dir, n = 0; n < dircount; m++, n++)
5066
304k
    {
5067
304k
        if (m->tdir_tag == tagid)
5068
2.51k
            return (m);
5069
304k
    }
5070
590
    return (0);
5071
3.10k
}
5072
5073
static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
5074
                                           uint32_t *fii)
5075
451k
{
5076
451k
    int32_t ma, mb, mc;
5077
451k
    ma = -1;
5078
451k
    mc = (int32_t)tif->tif_nfields;
5079
3.71M
    while (1)
5080
3.71M
    {
5081
3.71M
        if (ma + 1 == mc)
5082
141k
        {
5083
141k
            *fii = FAILED_FII;
5084
141k
            return;
5085
141k
        }
5086
3.56M
        mb = (ma + mc) / 2;
5087
3.56M
        if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
5088
310k
            break;
5089
3.25M
        if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5090
1.70M
            ma = mb;
5091
1.55M
        else
5092
1.55M
            mc = mb;
5093
3.25M
    }
5094
310k
    while (1)
5095
310k
    {
5096
310k
        if (mb == 0)
5097
3.48k
            break;
5098
306k
        if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
5099
306k
            break;
5100
0
        mb--;
5101
0
    }
5102
310k
    *fii = mb;
5103
310k
}
5104
5105
/*
5106
 * Read custom directory from the arbitrary offset.
5107
 * The code is very similar to TIFFReadDirectory().
5108
 */
5109
int TIFFReadCustomDirectory(TIFF *tif, toff_t diroff,
5110
                            const TIFFFieldArray *infoarray)
5111
0
{
5112
0
    static const char module[] = "TIFFReadCustomDirectory";
5113
0
    TIFFDirEntry *dir;
5114
0
    uint16_t dircount;
5115
0
    TIFFDirEntry *dp;
5116
0
    uint16_t di;
5117
0
    const TIFFField *fip;
5118
0
    uint32_t fii;
5119
0
    (*tif->tif_cleanup)(tif); /* cleanup any previous compression state */
5120
0
    _TIFFSetupFields(tif, infoarray);
5121
0
    dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
5122
0
    if (!dircount)
5123
0
    {
5124
0
        TIFFErrorExtR(tif, module,
5125
0
                      "Failed to read custom directory at offset %" PRIu64,
5126
0
                      diroff);
5127
0
        return 0;
5128
0
    }
5129
0
    TIFFFreeDirectory(tif);
5130
0
    _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
5131
0
    TIFFReadDirectoryCheckOrder(tif, dir, dircount);
5132
0
    for (di = 0, dp = dir; di < dircount; di++, dp++)
5133
0
    {
5134
0
        TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5135
0
        if (fii == FAILED_FII)
5136
0
        {
5137
0
            TIFFWarningExtR(tif, module,
5138
0
                            "Unknown field with tag %" PRIu16 " (0x%" PRIx16
5139
0
                            ") encountered",
5140
0
                            dp->tdir_tag, dp->tdir_tag);
5141
0
            if (!_TIFFMergeFields(
5142
0
                    tif,
5143
0
                    _TIFFCreateAnonField(tif, dp->tdir_tag,
5144
0
                                         (TIFFDataType)dp->tdir_type),
5145
0
                    1))
5146
0
            {
5147
0
                TIFFWarningExtR(tif, module,
5148
0
                                "Registering anonymous field with tag %" PRIu16
5149
0
                                " (0x%" PRIx16 ") failed",
5150
0
                                dp->tdir_tag, dp->tdir_tag);
5151
0
                dp->tdir_ignore = TRUE;
5152
0
            }
5153
0
            else
5154
0
            {
5155
0
                TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5156
0
                assert(fii != FAILED_FII);
5157
0
            }
5158
0
        }
5159
0
        if (!dp->tdir_ignore)
5160
0
        {
5161
0
            fip = tif->tif_fields[fii];
5162
0
            if (fip->field_bit == FIELD_IGNORE)
5163
0
                dp->tdir_ignore = TRUE;
5164
0
            else
5165
0
            {
5166
                /* check data type */
5167
0
                while ((fip->field_type != TIFF_ANY) &&
5168
0
                       (fip->field_type != dp->tdir_type))
5169
0
                {
5170
0
                    fii++;
5171
0
                    if ((fii == tif->tif_nfields) ||
5172
0
                        (tif->tif_fields[fii]->field_tag !=
5173
0
                         (uint32_t)dp->tdir_tag))
5174
0
                    {
5175
0
                        fii = 0xFFFF;
5176
0
                        break;
5177
0
                    }
5178
0
                    fip = tif->tif_fields[fii];
5179
0
                }
5180
0
                if (fii == 0xFFFF)
5181
0
                {
5182
0
                    TIFFWarningExtR(tif, module,
5183
0
                                    "Wrong data type %" PRIu16
5184
0
                                    " for \"%s\"; tag ignored",
5185
0
                                    dp->tdir_type, fip->field_name);
5186
0
                    dp->tdir_ignore = TRUE;
5187
0
                }
5188
0
                else
5189
0
                {
5190
                    /* check count if known in advance */
5191
0
                    if ((fip->field_readcount != TIFF_VARIABLE) &&
5192
0
                        (fip->field_readcount != TIFF_VARIABLE2))
5193
0
                    {
5194
0
                        uint32_t expected;
5195
0
                        if (fip->field_readcount == TIFF_SPP)
5196
0
                            expected =
5197
0
                                (uint32_t)tif->tif_dir.td_samplesperpixel;
5198
0
                        else
5199
0
                            expected = (uint32_t)fip->field_readcount;
5200
0
                        if (!CheckDirCount(tif, dp, expected))
5201
0
                            dp->tdir_ignore = TRUE;
5202
0
                    }
5203
0
                }
5204
0
            }
5205
0
            if (!dp->tdir_ignore)
5206
0
            {
5207
0
                switch (dp->tdir_tag)
5208
0
                {
5209
0
                    case EXIFTAG_SUBJECTDISTANCE:
5210
0
                        if (!TIFFFieldIsAnonymous(fip))
5211
0
                        {
5212
                            /* should only be called on a Exif directory */
5213
                            /* when exifFields[] is active */
5214
0
                            (void)TIFFFetchSubjectDistance(tif, dp);
5215
0
                        }
5216
0
                        else
5217
0
                        {
5218
0
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
5219
0
                        }
5220
0
                        break;
5221
0
                    default:
5222
0
                        (void)TIFFFetchNormalTag(tif, dp, TRUE);
5223
0
                        break;
5224
0
                }
5225
0
            } /*-- if (!dp->tdir_ignore) */
5226
0
        }
5227
0
    }
5228
    /* To be able to return from SubIFD or custom-IFD to main-IFD */
5229
0
    tif->tif_setdirectory_force_absolute = TRUE;
5230
0
    if (dir)
5231
0
        _TIFFfreeExt(tif, dir);
5232
0
    return 1;
5233
0
}
5234
5235
/*
5236
 * EXIF is important special case of custom IFD, so we have a special
5237
 * function to read it.
5238
 */
5239
int TIFFReadEXIFDirectory(TIFF *tif, toff_t diroff)
5240
0
{
5241
0
    const TIFFFieldArray *exifFieldArray;
5242
0
    exifFieldArray = _TIFFGetExifFields();
5243
0
    return TIFFReadCustomDirectory(tif, diroff, exifFieldArray);
5244
0
}
5245
5246
/*
5247
 *--: EXIF-GPS custom directory reading as another special case of custom IFD.
5248
 */
5249
int TIFFReadGPSDirectory(TIFF *tif, toff_t diroff)
5250
0
{
5251
0
    const TIFFFieldArray *gpsFieldArray;
5252
0
    gpsFieldArray = _TIFFGetGpsFields();
5253
0
    return TIFFReadCustomDirectory(tif, diroff, gpsFieldArray);
5254
0
}
5255
5256
static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
5257
                                   uint16_t dircount)
5258
293
{
5259
293
    static const char module[] = "EstimateStripByteCounts";
5260
5261
293
    TIFFDirEntry *dp;
5262
293
    TIFFDirectory *td = &tif->tif_dir;
5263
293
    uint32_t strip;
5264
5265
    /* Do not try to load stripbytecount as we will compute it */
5266
293
    if (!_TIFFFillStrilesInternal(tif, 0))
5267
0
        return -1;
5268
5269
293
    if (td->td_stripbytecount_p)
5270
111
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
5271
293
    td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
5272
293
        tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
5273
293
    if (td->td_stripbytecount_p == NULL)
5274
0
        return -1;
5275
5276
293
    if (td->td_compression != COMPRESSION_NONE)
5277
137
    {
5278
137
        uint64_t space;
5279
137
        uint64_t filesize;
5280
137
        uint16_t n;
5281
137
        filesize = TIFFGetFileSize(tif);
5282
137
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5283
137
            space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
5284
0
        else
5285
0
            space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
5286
        /* calculate amount of space used by indirect values */
5287
1.61k
        for (dp = dir, n = dircount; n > 0; n--, dp++)
5288
1.48k
        {
5289
1.48k
            uint32_t typewidth;
5290
1.48k
            uint64_t datasize;
5291
1.48k
            typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
5292
1.48k
            if (typewidth == 0)
5293
13
            {
5294
13
                TIFFErrorExtR(
5295
13
                    tif, module,
5296
13
                    "Cannot determine size of unknown tag type %" PRIu16,
5297
13
                    dp->tdir_type);
5298
13
                return -1;
5299
13
            }
5300
1.47k
            if (dp->tdir_count > UINT64_MAX / typewidth)
5301
0
                return -1;
5302
1.47k
            datasize = (uint64_t)typewidth * dp->tdir_count;
5303
1.47k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
5304
1.47k
            {
5305
1.47k
                if (datasize <= 4)
5306
1.07k
                    datasize = 0;
5307
1.47k
            }
5308
0
            else
5309
0
            {
5310
0
                if (datasize <= 8)
5311
0
                    datasize = 0;
5312
0
            }
5313
1.47k
            if (space > UINT64_MAX - datasize)
5314
0
                return -1;
5315
1.47k
            space += datasize;
5316
1.47k
        }
5317
124
        if (filesize < space)
5318
            /* we should perhaps return in error ? */
5319
96
            space = filesize;
5320
28
        else
5321
28
            space = filesize - space;
5322
124
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
5323
0
            space /= td->td_samplesperpixel;
5324
248
        for (strip = 0; strip < td->td_nstrips; strip++)
5325
124
            td->td_stripbytecount_p[strip] = space;
5326
        /*
5327
         * This gross hack handles the case were the offset to
5328
         * the last strip is past the place where we think the strip
5329
         * should begin.  Since a strip of data must be contiguous,
5330
         * it's safe to assume that we've overestimated the amount
5331
         * of data in the strip and trim this number back accordingly.
5332
         */
5333
124
        strip--;
5334
124
        if (td->td_stripoffset_p[strip] >
5335
124
            UINT64_MAX - td->td_stripbytecount_p[strip])
5336
0
            return -1;
5337
124
        if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
5338
124
            filesize)
5339
95
        {
5340
95
            if (td->td_stripoffset_p[strip] >= filesize)
5341
0
            {
5342
                /* Not sure what we should in that case... */
5343
0
                td->td_stripbytecount_p[strip] = 0;
5344
0
            }
5345
95
            else
5346
95
            {
5347
95
                td->td_stripbytecount_p[strip] =
5348
95
                    filesize - td->td_stripoffset_p[strip];
5349
95
            }
5350
95
        }
5351
124
    }
5352
156
    else if (isTiled(tif))
5353
65
    {
5354
65
        uint64_t bytespertile = TIFFTileSize64(tif);
5355
5356
38.9k
        for (strip = 0; strip < td->td_nstrips; strip++)
5357
38.8k
            td->td_stripbytecount_p[strip] = bytespertile;
5358
65
    }
5359
91
    else
5360
91
    {
5361
91
        uint64_t rowbytes = TIFFScanlineSize64(tif);
5362
91
        uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
5363
3.83k
        for (strip = 0; strip < td->td_nstrips; strip++)
5364
3.74k
        {
5365
3.74k
            if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
5366
0
                return -1;
5367
3.74k
            td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
5368
3.74k
        }
5369
91
    }
5370
280
    TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
5371
280
    if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
5372
203
        td->td_rowsperstrip = td->td_imagelength;
5373
280
    return 1;
5374
293
}
5375
5376
static void MissingRequired(TIFF *tif, const char *tagname)
5377
52
{
5378
52
    static const char module[] = "MissingRequired";
5379
5380
52
    TIFFErrorExtR(tif, module,
5381
52
                  "TIFF directory is missing required \"%s\" field", tagname);
5382
52
}
5383
5384
static unsigned long hashFuncOffsetToNumber(const void *elt)
5385
10.2k
{
5386
10.2k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5387
10.2k
        (const TIFFOffsetAndDirNumber *)elt;
5388
10.2k
    const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
5389
10.2k
                          ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
5390
10.2k
    return hash;
5391
10.2k
}
5392
5393
static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
5394
2.37k
{
5395
2.37k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5396
2.37k
        (const TIFFOffsetAndDirNumber *)elt1;
5397
2.37k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5398
2.37k
        (const TIFFOffsetAndDirNumber *)elt2;
5399
2.37k
    return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
5400
2.37k
}
5401
5402
static unsigned long hashFuncNumberToOffset(const void *elt)
5403
7.92k
{
5404
7.92k
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
5405
7.92k
        (const TIFFOffsetAndDirNumber *)elt;
5406
7.92k
    return offsetAndDirNumber->dirNumber;
5407
7.92k
}
5408
5409
static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
5410
0
{
5411
0
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
5412
0
        (const TIFFOffsetAndDirNumber *)elt1;
5413
0
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
5414
0
        (const TIFFOffsetAndDirNumber *)elt2;
5415
0
    return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
5416
0
}
5417
5418
/*
5419
 * Check the directory number and offset against the list of already seen
5420
 * directory numbers and offsets. This is a trick to prevent IFD looping.
5421
 * The one can create TIFF file with looped directory pointers. We will
5422
 * maintain a list of already seen directories and check every IFD offset
5423
 * and its IFD number against that list. However, the offset of an IFD number
5424
 * can change - e.g. when writing updates to file.
5425
 * Returns 1 if all is ok; 0 if last directory or IFD loop is encountered,
5426
 * or an error has occurred.
5427
 */
5428
int _TIFFCheckDirNumberAndOffset(TIFF *tif, tdir_t dirn, uint64_t diroff)
5429
4.99k
{
5430
4.99k
    if (diroff == 0) /* no more directories */
5431
0
        return 0;
5432
5433
4.99k
    if (tif->tif_map_dir_offset_to_number == NULL)
5434
1.54k
    {
5435
1.54k
        tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
5436
1.54k
            hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5437
1.54k
        if (tif->tif_map_dir_offset_to_number == NULL)
5438
0
        {
5439
0
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5440
0
                          "Not enough memory");
5441
0
            return 1;
5442
0
        }
5443
1.54k
    }
5444
5445
4.99k
    if (tif->tif_map_dir_number_to_offset == NULL)
5446
1.54k
    {
5447
        /* No free callback for this map, as it shares the same items as
5448
         * tif->tif_map_dir_offset_to_number. */
5449
1.54k
        tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
5450
1.54k
            hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5451
1.54k
        if (tif->tif_map_dir_number_to_offset == NULL)
5452
0
        {
5453
0
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5454
0
                          "Not enough memory");
5455
0
            return 1;
5456
0
        }
5457
1.54k
    }
5458
5459
    /* Check if offset is already in the list:
5460
     * - yes: check, if offset is at the same IFD number - if not, it is an IFD
5461
     * loop
5462
     * -  no: add to list or update offset at that IFD number
5463
     */
5464
4.99k
    TIFFOffsetAndDirNumber entry;
5465
4.99k
    entry.offset = diroff;
5466
4.99k
    entry.dirNumber = dirn;
5467
5468
4.99k
    TIFFOffsetAndDirNumber *foundEntry =
5469
4.99k
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5470
4.99k
            tif->tif_map_dir_offset_to_number, &entry);
5471
4.99k
    if (foundEntry)
5472
2.35k
    {
5473
2.35k
        if (foundEntry->dirNumber == dirn)
5474
2.35k
        {
5475
2.35k
            return 1;
5476
2.35k
        }
5477
1
        else
5478
1
        {
5479
1
            TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
5480
1
                            "TIFF directory %d has IFD looping to directory %u "
5481
1
                            "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5482
1
                            (int)dirn - 1, foundEntry->dirNumber, diroff,
5483
1
                            diroff);
5484
1
            return 0;
5485
1
        }
5486
2.35k
    }
5487
5488
    /* Check if offset of an IFD has been changed and update offset of that IFD
5489
     * number. */
5490
2.64k
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5491
2.64k
        tif->tif_map_dir_number_to_offset, &entry);
5492
2.64k
    if (foundEntry)
5493
0
    {
5494
0
        if (foundEntry->offset != diroff)
5495
0
        {
5496
0
            TIFFOffsetAndDirNumber entryOld;
5497
0
            entryOld.offset = foundEntry->offset;
5498
0
            entryOld.dirNumber = dirn;
5499
            /* We must remove first from tif_map_dir_number_to_offset as the */
5500
            /* entry is owned (and thus freed) by */
5501
            /* tif_map_dir_offset_to_number */
5502
0
            TIFFOffsetAndDirNumber *foundEntryOld =
5503
0
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5504
0
                    tif->tif_map_dir_number_to_offset, &entryOld);
5505
0
            if (foundEntryOld)
5506
0
            {
5507
0
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5508
0
                                  foundEntryOld);
5509
0
            }
5510
0
            foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5511
0
                tif->tif_map_dir_offset_to_number, &entryOld);
5512
0
            if (foundEntryOld)
5513
0
            {
5514
0
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5515
0
                                  foundEntryOld);
5516
0
            }
5517
5518
0
            TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
5519
0
                sizeof(TIFFOffsetAndDirNumber));
5520
0
            if (entryPtr == NULL)
5521
0
            {
5522
0
                return 0;
5523
0
            }
5524
5525
            /* Add IFD offset and dirn to IFD directory list */
5526
0
            *entryPtr = entry;
5527
5528
0
            if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5529
0
            {
5530
0
                TIFFErrorExtR(
5531
0
                    tif, "_TIFFCheckDirNumberAndOffset",
5532
0
                    "Insertion in tif_map_dir_offset_to_number failed");
5533
0
                return 0;
5534
0
            }
5535
0
            if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5536
0
            {
5537
0
                TIFFErrorExtR(
5538
0
                    tif, "_TIFFCheckDirNumberAndOffset",
5539
0
                    "Insertion in tif_map_dir_number_to_offset failed");
5540
0
                return 0;
5541
0
            }
5542
0
        }
5543
0
        return 1;
5544
0
    }
5545
5546
    /* Arbitrary (hopefully big enough) limit */
5547
2.64k
    if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
5548
2.64k
        TIFF_MAX_DIR_COUNT)
5549
0
    {
5550
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5551
0
                      "Cannot handle more than %u TIFF directories",
5552
0
                      TIFF_MAX_DIR_COUNT);
5553
0
        return 0;
5554
0
    }
5555
5556
2.64k
    TIFFOffsetAndDirNumber *entryPtr =
5557
2.64k
        (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
5558
2.64k
    if (entryPtr == NULL)
5559
0
    {
5560
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5561
0
                      "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5562
0
        return 0;
5563
0
    }
5564
5565
    /* Add IFD offset and dirn to IFD directory list */
5566
2.64k
    *entryPtr = entry;
5567
5568
2.64k
    if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
5569
0
    {
5570
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5571
0
                      "Insertion in tif_map_dir_offset_to_number failed");
5572
0
        return 0;
5573
0
    }
5574
2.64k
    if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
5575
0
    {
5576
0
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
5577
0
                      "Insertion in tif_map_dir_number_to_offset failed");
5578
0
        return 0;
5579
0
    }
5580
5581
2.64k
    return 1;
5582
2.64k
} /* --- _TIFFCheckDirNumberAndOffset() ---*/
5583
5584
/*
5585
 * Retrieve the matching IFD directory number of a given IFD offset
5586
 * from the list of directories already seen.
5587
 * Returns 1 if the offset was in the list and the directory number
5588
 * can be returned.
5589
 * Otherwise returns 0 or if an error occurred.
5590
 */
5591
int _TIFFGetDirNumberFromOffset(TIFF *tif, uint64_t diroff, tdir_t *dirn)
5592
0
{
5593
0
    if (diroff == 0) /* no more directories */
5594
0
        return 0;
5595
5596
    /* Check if offset is already in the list and return matching directory
5597
     * number. Otherwise update IFD list using TIFFNumberOfDirectories() and
5598
     * search again in IFD list.
5599
     */
5600
0
    if (tif->tif_map_dir_offset_to_number == NULL)
5601
0
        return 0;
5602
0
    TIFFOffsetAndDirNumber entry;
5603
0
    entry.offset = diroff;
5604
0
    entry.dirNumber = 0; /* not used */
5605
5606
0
    TIFFOffsetAndDirNumber *foundEntry =
5607
0
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5608
0
            tif->tif_map_dir_offset_to_number, &entry);
5609
0
    if (foundEntry)
5610
0
    {
5611
0
        *dirn = foundEntry->dirNumber;
5612
0
        return 1;
5613
0
    }
5614
5615
    /* This updates the directory list for all main-IFDs in the file. */
5616
0
    TIFFNumberOfDirectories(tif);
5617
5618
0
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5619
0
        tif->tif_map_dir_offset_to_number, &entry);
5620
0
    if (foundEntry)
5621
0
    {
5622
0
        *dirn = foundEntry->dirNumber;
5623
0
        return 1;
5624
0
    }
5625
5626
0
    return 0;
5627
0
} /*--- _TIFFGetDirNumberFromOffset() ---*/
5628
5629
/*
5630
 * Retrieve the matching IFD directory offset of a given IFD number
5631
 * from the list of directories already seen.
5632
 * Returns 1 if the offset was in the list of already seen IFDs and the
5633
 * directory offset can be returned. The directory list is not updated.
5634
 * Otherwise returns 0 or if an error occurred.
5635
 */
5636
int _TIFFGetOffsetFromDirNumber(TIFF *tif, tdir_t dirn, uint64_t *diroff)
5637
0
{
5638
5639
0
    if (tif->tif_map_dir_number_to_offset == NULL)
5640
0
        return 0;
5641
0
    TIFFOffsetAndDirNumber entry;
5642
0
    entry.offset = 0; /* not used */
5643
0
    entry.dirNumber = dirn;
5644
5645
0
    TIFFOffsetAndDirNumber *foundEntry =
5646
0
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5647
0
            tif->tif_map_dir_number_to_offset, &entry);
5648
0
    if (foundEntry)
5649
0
    {
5650
0
        *diroff = foundEntry->offset;
5651
0
        return 1;
5652
0
    }
5653
5654
0
    return 0;
5655
0
} /*--- _TIFFGetOffsetFromDirNumber() ---*/
5656
5657
/*
5658
 * Remove an entry from the directory list of already seen directories
5659
 * by directory offset.
5660
 * If an entry is to be removed from the list, it is also okay if the entry
5661
 * is not in the list or the list does not exist.
5662
 */
5663
int _TIFFRemoveEntryFromDirectoryListByOffset(TIFF *tif, uint64_t diroff)
5664
0
{
5665
0
    if (tif->tif_map_dir_offset_to_number == NULL)
5666
0
        return 1;
5667
5668
0
    TIFFOffsetAndDirNumber entryOld;
5669
0
    entryOld.offset = diroff;
5670
0
    entryOld.dirNumber = 0;
5671
    /* We must remove first from tif_map_dir_number_to_offset as the
5672
     * entry is owned (and thus freed) by tif_map_dir_offset_to_number.
5673
     * However, we need firstly to find the directory number from offset. */
5674
5675
0
    TIFFOffsetAndDirNumber *foundEntryOldOff =
5676
0
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5677
0
            tif->tif_map_dir_offset_to_number, &entryOld);
5678
0
    if (foundEntryOldOff)
5679
0
    {
5680
0
        entryOld.dirNumber = foundEntryOldOff->dirNumber;
5681
0
        if (tif->tif_map_dir_number_to_offset != NULL)
5682
0
        {
5683
0
            TIFFOffsetAndDirNumber *foundEntryOldDir =
5684
0
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
5685
0
                    tif->tif_map_dir_number_to_offset, &entryOld);
5686
0
            if (foundEntryOldDir)
5687
0
            {
5688
0
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
5689
0
                                  foundEntryOldDir);
5690
0
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
5691
0
                                  foundEntryOldOff);
5692
0
                return 1;
5693
0
            }
5694
0
        }
5695
0
        else
5696
0
        {
5697
0
            TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
5698
0
                          "Unexpectedly tif_map_dir_number_to_offset is "
5699
0
                          "missing but tif_map_dir_offset_to_number exists.");
5700
0
            return 0;
5701
0
        }
5702
0
    }
5703
0
    return 1;
5704
0
} /*--- _TIFFRemoveEntryFromDirectoryListByOffset() ---*/
5705
5706
/*
5707
 * Check the count field of a directory entry against a known value.  The
5708
 * caller is expected to skip/ignore the tag if there is a mismatch.
5709
 */
5710
static int CheckDirCount(TIFF *tif, TIFFDirEntry *dir, uint32_t count)
5711
0
{
5712
0
    if ((uint64_t)count > dir->tdir_count)
5713
0
    {
5714
0
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5715
0
        TIFFWarningExtR(tif, tif->tif_name,
5716
0
                        "incorrect count for field \"%s\" (%" PRIu64
5717
0
                        ", expecting %" PRIu32 "); tag ignored",
5718
0
                        fip ? fip->field_name : "unknown tagname",
5719
0
                        dir->tdir_count, count);
5720
0
        return (0);
5721
0
    }
5722
0
    else if ((uint64_t)count < dir->tdir_count)
5723
0
    {
5724
0
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
5725
0
        TIFFWarningExtR(tif, tif->tif_name,
5726
0
                        "incorrect count for field \"%s\" (%" PRIu64
5727
0
                        ", expecting %" PRIu32 "); tag trimmed",
5728
0
                        fip ? fip->field_name : "unknown tagname",
5729
0
                        dir->tdir_count, count);
5730
0
        dir->tdir_count = count;
5731
0
        return (1);
5732
0
    }
5733
0
    return (1);
5734
0
}
5735
5736
/*
5737
 * Read IFD structure from the specified offset. If the pointer to
5738
 * nextdiroff variable has been specified, read it too. Function returns a
5739
 * number of fields in the directory or 0 if failed.
5740
 */
5741
static uint16_t TIFFFetchDirectory(TIFF *tif, uint64_t diroff,
5742
                                   TIFFDirEntry **pdir, uint64_t *nextdiroff)
5743
1.54k
{
5744
1.54k
    static const char module[] = "TIFFFetchDirectory";
5745
5746
1.54k
    void *origdir;
5747
1.54k
    uint16_t dircount16;
5748
1.54k
    uint32_t dirsize;
5749
1.54k
    TIFFDirEntry *dir;
5750
1.54k
    uint8_t *ma;
5751
1.54k
    TIFFDirEntry *mb;
5752
1.54k
    uint16_t n;
5753
5754
1.54k
    assert(pdir);
5755
5756
1.54k
    tif->tif_diroff = diroff;
5757
1.54k
    if (nextdiroff)
5758
1.54k
        *nextdiroff = 0;
5759
1.54k
    if (!isMapped(tif))
5760
0
    {
5761
0
        if (!SeekOK(tif, tif->tif_diroff))
5762
0
        {
5763
0
            TIFFErrorExtR(tif, module,
5764
0
                          "%s: Seek error accessing TIFF directory",
5765
0
                          tif->tif_name);
5766
0
            return 0;
5767
0
        }
5768
0
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5769
0
        {
5770
0
            if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
5771
0
            {
5772
0
                TIFFErrorExtR(tif, module,
5773
0
                              "%s: Can not read TIFF directory count",
5774
0
                              tif->tif_name);
5775
0
                return 0;
5776
0
            }
5777
0
            if (tif->tif_flags & TIFF_SWAB)
5778
0
                TIFFSwabShort(&dircount16);
5779
0
            if (dircount16 > 4096)
5780
0
            {
5781
0
                TIFFErrorExtR(tif, module,
5782
0
                              "Sanity check on directory count failed, this is "
5783
0
                              "probably not a valid IFD offset");
5784
0
                return 0;
5785
0
            }
5786
0
            dirsize = 12;
5787
0
        }
5788
0
        else
5789
0
        {
5790
0
            uint64_t dircount64;
5791
0
            if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
5792
0
            {
5793
0
                TIFFErrorExtR(tif, module,
5794
0
                              "%s: Can not read TIFF directory count",
5795
0
                              tif->tif_name);
5796
0
                return 0;
5797
0
            }
5798
0
            if (tif->tif_flags & TIFF_SWAB)
5799
0
                TIFFSwabLong8(&dircount64);
5800
0
            if (dircount64 > 4096)
5801
0
            {
5802
0
                TIFFErrorExtR(tif, module,
5803
0
                              "Sanity check on directory count failed, this is "
5804
0
                              "probably not a valid IFD offset");
5805
0
                return 0;
5806
0
            }
5807
0
            dircount16 = (uint16_t)dircount64;
5808
0
            dirsize = 20;
5809
0
        }
5810
0
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
5811
0
                                   "to read TIFF directory");
5812
0
        if (origdir == NULL)
5813
0
            return 0;
5814
0
        if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
5815
0
        {
5816
0
            TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
5817
0
                          tif->tif_name);
5818
0
            _TIFFfreeExt(tif, origdir);
5819
0
            return 0;
5820
0
        }
5821
        /*
5822
         * Read offset to next directory for sequential scans if
5823
         * needed.
5824
         */
5825
0
        if (nextdiroff)
5826
0
        {
5827
0
            if (!(tif->tif_flags & TIFF_BIGTIFF))
5828
0
            {
5829
0
                uint32_t nextdiroff32;
5830
0
                if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
5831
0
                    nextdiroff32 = 0;
5832
0
                if (tif->tif_flags & TIFF_SWAB)
5833
0
                    TIFFSwabLong(&nextdiroff32);
5834
0
                *nextdiroff = nextdiroff32;
5835
0
            }
5836
0
            else
5837
0
            {
5838
0
                if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
5839
0
                    *nextdiroff = 0;
5840
0
                if (tif->tif_flags & TIFF_SWAB)
5841
0
                    TIFFSwabLong8(nextdiroff);
5842
0
            }
5843
0
        }
5844
0
    }
5845
1.54k
    else
5846
1.54k
    {
5847
1.54k
        tmsize_t m;
5848
1.54k
        tmsize_t off;
5849
1.54k
        if (tif->tif_diroff > (uint64_t)INT64_MAX)
5850
0
        {
5851
0
            TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
5852
0
            return (0);
5853
0
        }
5854
1.54k
        off = (tmsize_t)tif->tif_diroff;
5855
5856
        /*
5857
         * Check for integer overflow when validating the dir_off,
5858
         * otherwise a very high offset may cause an OOB read and
5859
         * crash the client. Make two comparisons instead of
5860
         *
5861
         *  off + sizeof(uint16_t) > tif->tif_size
5862
         *
5863
         * to avoid overflow.
5864
         */
5865
1.54k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5866
1.52k
        {
5867
1.52k
            m = off + sizeof(uint16_t);
5868
1.52k
            if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
5869
1.52k
                (m > tif->tif_size))
5870
0
            {
5871
0
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
5872
0
                return 0;
5873
0
            }
5874
1.52k
            else
5875
1.52k
            {
5876
1.52k
                _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
5877
1.52k
            }
5878
1.52k
            off += sizeof(uint16_t);
5879
1.52k
            if (tif->tif_flags & TIFF_SWAB)
5880
192
                TIFFSwabShort(&dircount16);
5881
1.52k
            if (dircount16 > 4096)
5882
0
            {
5883
0
                TIFFErrorExtR(tif, module,
5884
0
                              "Sanity check on directory count failed, this is "
5885
0
                              "probably not a valid IFD offset");
5886
0
                return 0;
5887
0
            }
5888
1.52k
            dirsize = 12;
5889
1.52k
        }
5890
25
        else
5891
25
        {
5892
25
            uint64_t dircount64;
5893
25
            m = off + sizeof(uint64_t);
5894
25
            if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
5895
25
                (m > tif->tif_size))
5896
0
            {
5897
0
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
5898
0
                return 0;
5899
0
            }
5900
25
            else
5901
25
            {
5902
25
                _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
5903
25
            }
5904
25
            off += sizeof(uint64_t);
5905
25
            if (tif->tif_flags & TIFF_SWAB)
5906
15
                TIFFSwabLong8(&dircount64);
5907
25
            if (dircount64 > 4096)
5908
0
            {
5909
0
                TIFFErrorExtR(tif, module,
5910
0
                              "Sanity check on directory count failed, this is "
5911
0
                              "probably not a valid IFD offset");
5912
0
                return 0;
5913
0
            }
5914
25
            dircount16 = (uint16_t)dircount64;
5915
25
            dirsize = 20;
5916
25
        }
5917
1.54k
        if (dircount16 == 0)
5918
0
        {
5919
0
            TIFFErrorExtR(tif, module,
5920
0
                          "Sanity check on directory count failed, zero tag "
5921
0
                          "directories not supported");
5922
0
            return 0;
5923
0
        }
5924
1.54k
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
5925
1.54k
                                   "to read TIFF directory");
5926
1.54k
        if (origdir == NULL)
5927
0
            return 0;
5928
1.54k
        m = off + dircount16 * dirsize;
5929
1.54k
        if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
5930
1.54k
            (m > tif->tif_size))
5931
0
        {
5932
0
            TIFFErrorExtR(tif, module, "Can not read TIFF directory");
5933
0
            _TIFFfreeExt(tif, origdir);
5934
0
            return 0;
5935
0
        }
5936
1.54k
        else
5937
1.54k
        {
5938
1.54k
            _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
5939
1.54k
        }
5940
1.54k
        if (nextdiroff)
5941
1.54k
        {
5942
1.54k
            off += dircount16 * dirsize;
5943
1.54k
            if (!(tif->tif_flags & TIFF_BIGTIFF))
5944
1.52k
            {
5945
1.52k
                uint32_t nextdiroff32;
5946
1.52k
                m = off + sizeof(uint32_t);
5947
1.52k
                if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
5948
1.52k
                    (m > tif->tif_size))
5949
42
                    nextdiroff32 = 0;
5950
1.47k
                else
5951
1.47k
                    _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
5952
1.47k
                                sizeof(uint32_t));
5953
1.52k
                if (tif->tif_flags & TIFF_SWAB)
5954
192
                    TIFFSwabLong(&nextdiroff32);
5955
1.52k
                *nextdiroff = nextdiroff32;
5956
1.52k
            }
5957
25
            else
5958
25
            {
5959
25
                m = off + sizeof(uint64_t);
5960
25
                if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
5961
25
                    (m > tif->tif_size))
5962
0
                    *nextdiroff = 0;
5963
25
                else
5964
25
                    _TIFFmemcpy(nextdiroff, tif->tif_base + off,
5965
25
                                sizeof(uint64_t));
5966
25
                if (tif->tif_flags & TIFF_SWAB)
5967
15
                    TIFFSwabLong8(nextdiroff);
5968
25
            }
5969
1.54k
        }
5970
1.54k
    }
5971
1.54k
    dir = (TIFFDirEntry *)_TIFFCheckMalloc(
5972
1.54k
        tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
5973
1.54k
    if (dir == 0)
5974
0
    {
5975
0
        _TIFFfreeExt(tif, origdir);
5976
0
        return 0;
5977
0
    }
5978
1.54k
    ma = (uint8_t *)origdir;
5979
1.54k
    mb = dir;
5980
409k
    for (n = 0; n < dircount16; n++)
5981
408k
    {
5982
408k
        mb->tdir_ignore = FALSE;
5983
408k
        if (tif->tif_flags & TIFF_SWAB)
5984
21.9k
            TIFFSwabShort((uint16_t *)ma);
5985
408k
        mb->tdir_tag = *(uint16_t *)ma;
5986
408k
        ma += sizeof(uint16_t);
5987
408k
        if (tif->tif_flags & TIFF_SWAB)
5988
21.9k
            TIFFSwabShort((uint16_t *)ma);
5989
408k
        mb->tdir_type = *(uint16_t *)ma;
5990
408k
        ma += sizeof(uint16_t);
5991
408k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
5992
401k
        {
5993
401k
            if (tif->tif_flags & TIFF_SWAB)
5994
21.0k
                TIFFSwabLong((uint32_t *)ma);
5995
401k
            mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
5996
401k
            ma += sizeof(uint32_t);
5997
401k
            mb->tdir_offset.toff_long8 = 0;
5998
401k
            *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
5999
401k
            ma += sizeof(uint32_t);
6000
401k
        }
6001
6.34k
        else
6002
6.34k
        {
6003
6.34k
            if (tif->tif_flags & TIFF_SWAB)
6004
888
                TIFFSwabLong8((uint64_t *)ma);
6005
6.34k
            mb->tdir_count = TIFFReadUInt64(ma);
6006
6.34k
            ma += sizeof(uint64_t);
6007
6.34k
            mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
6008
6.34k
            ma += sizeof(uint64_t);
6009
6.34k
        }
6010
408k
        mb++;
6011
408k
    }
6012
1.54k
    _TIFFfreeExt(tif, origdir);
6013
1.54k
    *pdir = dir;
6014
1.54k
    return dircount16;
6015
1.54k
}
6016
6017
/*
6018
 * Fetch a tag that is not handled by special case code.
6019
 */
6020
static int TIFFFetchNormalTag(TIFF *tif, TIFFDirEntry *dp, int recover)
6021
151k
{
6022
151k
    static const char module[] = "TIFFFetchNormalTag";
6023
151k
    enum TIFFReadDirEntryErr err;
6024
151k
    uint32_t fii;
6025
151k
    const TIFFField *fip = NULL;
6026
151k
    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
6027
151k
    if (fii == FAILED_FII)
6028
0
    {
6029
0
        TIFFErrorExtR(tif, "TIFFFetchNormalTag",
6030
0
                      "No definition found for tag %" PRIu16, dp->tdir_tag);
6031
0
        return 0;
6032
0
    }
6033
151k
    fip = tif->tif_fields[fii];
6034
151k
    assert(fip != NULL); /* should not happen */
6035
151k
    assert(fip->set_field_type !=
6036
151k
           TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
6037
                                  this in specialized code */
6038
151k
    assert(fip->set_field_type !=
6039
151k
           TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
6040
                                the case for pseudo-tags */
6041
151k
    err = TIFFReadDirEntryErrOk;
6042
151k
    switch (fip->set_field_type)
6043
151k
    {
6044
117k
        case TIFF_SETGET_UNDEFINED:
6045
117k
            TIFFErrorExtR(
6046
117k
                tif, "TIFFFetchNormalTag",
6047
117k
                "Defined set_field_type of custom tag %u (%s) is "
6048
117k
                "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6049
117k
                fip->field_tag, fip->field_name);
6050
117k
            break;
6051
850
        case TIFF_SETGET_ASCII:
6052
850
        {
6053
850
            uint8_t *data;
6054
850
            assert(fip->field_passcount == 0);
6055
850
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
6056
850
            if (err == TIFFReadDirEntryErrOk)
6057
419
            {
6058
419
                size_t mb = 0;
6059
419
                int n;
6060
419
                if (data != NULL)
6061
419
                {
6062
419
                    if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
6063
155
                    {
6064
                        /* optimization: if data is known to be 0 terminated, we
6065
                         * can use strlen() */
6066
155
                        mb = strlen((const char *)data);
6067
155
                    }
6068
264
                    else
6069
264
                    {
6070
                        /* general case. equivalent to non-portable */
6071
                        /* mb = strnlen((const char*)data,
6072
                         * (uint32_t)dp->tdir_count); */
6073
264
                        uint8_t *ma = data;
6074
2.22k
                        while (mb < (uint32_t)dp->tdir_count)
6075
2.09k
                        {
6076
2.09k
                            if (*ma == 0)
6077
138
                                break;
6078
1.95k
                            ma++;
6079
1.95k
                            mb++;
6080
1.95k
                        }
6081
264
                    }
6082
419
                }
6083
419
                if (mb + 1 < (uint32_t)dp->tdir_count)
6084
221
                    TIFFWarningExtR(
6085
221
                        tif, module,
6086
221
                        "ASCII value for tag \"%s\" contains null byte in "
6087
221
                        "value; value incorrectly truncated during reading due "
6088
221
                        "to implementation limitations",
6089
221
                        fip->field_name);
6090
198
                else if (mb + 1 > (uint32_t)dp->tdir_count)
6091
126
                {
6092
126
                    uint8_t *o;
6093
126
                    TIFFWarningExtR(
6094
126
                        tif, module,
6095
126
                        "ASCII value for tag \"%s\" does not end in null byte",
6096
126
                        fip->field_name);
6097
                    /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6098
                     * larger than MAX_SIZE_TAG_DATA */
6099
126
                    assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
6100
126
                    o = _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
6101
126
                    if (o == NULL)
6102
0
                    {
6103
0
                        if (data != NULL)
6104
0
                            _TIFFfreeExt(tif, data);
6105
0
                        return (0);
6106
0
                    }
6107
126
                    if (dp->tdir_count > 0)
6108
126
                    {
6109
126
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
6110
126
                    }
6111
126
                    o[(uint32_t)dp->tdir_count] = 0;
6112
126
                    if (data != 0)
6113
126
                        _TIFFfreeExt(tif, data);
6114
126
                    data = o;
6115
126
                }
6116
419
                n = TIFFSetField(tif, dp->tdir_tag, data);
6117
419
                if (data != 0)
6118
419
                    _TIFFfreeExt(tif, data);
6119
419
                if (!n)
6120
0
                    return (0);
6121
419
            }
6122
850
        }
6123
850
        break;
6124
850
        case TIFF_SETGET_UINT8:
6125
7
        {
6126
7
            uint8_t data = 0;
6127
7
            assert(fip->field_readcount == 1);
6128
7
            assert(fip->field_passcount == 0);
6129
7
            err = TIFFReadDirEntryByte(tif, dp, &data);
6130
7
            if (err == TIFFReadDirEntryErrOk)
6131
0
            {
6132
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6133
0
                    return (0);
6134
0
            }
6135
7
        }
6136
7
        break;
6137
7
        case TIFF_SETGET_SINT8:
6138
0
        {
6139
0
            int8_t data = 0;
6140
0
            assert(fip->field_readcount == 1);
6141
0
            assert(fip->field_passcount == 0);
6142
0
            err = TIFFReadDirEntrySbyte(tif, dp, &data);
6143
0
            if (err == TIFFReadDirEntryErrOk)
6144
0
            {
6145
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6146
0
                    return (0);
6147
0
            }
6148
0
        }
6149
0
        break;
6150
4.70k
        case TIFF_SETGET_UINT16:
6151
4.70k
        {
6152
4.70k
            uint16_t data;
6153
4.70k
            assert(fip->field_readcount == 1);
6154
4.70k
            assert(fip->field_passcount == 0);
6155
4.70k
            err = TIFFReadDirEntryShort(tif, dp, &data);
6156
4.70k
            if (err == TIFFReadDirEntryErrOk)
6157
4.15k
            {
6158
4.15k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6159
66
                    return (0);
6160
4.15k
            }
6161
4.70k
        }
6162
4.63k
        break;
6163
4.63k
        case TIFF_SETGET_SINT16:
6164
0
        {
6165
0
            int16_t data;
6166
0
            assert(fip->field_readcount == 1);
6167
0
            assert(fip->field_passcount == 0);
6168
0
            err = TIFFReadDirEntrySshort(tif, dp, &data);
6169
0
            if (err == TIFFReadDirEntryErrOk)
6170
0
            {
6171
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6172
0
                    return (0);
6173
0
            }
6174
0
        }
6175
0
        break;
6176
5.20k
        case TIFF_SETGET_UINT32:
6177
5.20k
        {
6178
5.20k
            uint32_t data;
6179
5.20k
            assert(fip->field_readcount == 1);
6180
5.20k
            assert(fip->field_passcount == 0);
6181
5.20k
            err = TIFFReadDirEntryLong(tif, dp, &data);
6182
5.20k
            if (err == TIFFReadDirEntryErrOk)
6183
4.86k
            {
6184
4.86k
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6185
0
                    return (0);
6186
4.86k
            }
6187
5.20k
        }
6188
5.20k
        break;
6189
5.20k
        case TIFF_SETGET_SINT32:
6190
0
        {
6191
0
            int32_t data;
6192
0
            assert(fip->field_readcount == 1);
6193
0
            assert(fip->field_passcount == 0);
6194
0
            err = TIFFReadDirEntrySlong(tif, dp, &data);
6195
0
            if (err == TIFFReadDirEntryErrOk)
6196
0
            {
6197
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6198
0
                    return (0);
6199
0
            }
6200
0
        }
6201
0
        break;
6202
0
        case TIFF_SETGET_UINT64:
6203
0
        {
6204
0
            uint64_t data;
6205
0
            assert(fip->field_readcount == 1);
6206
0
            assert(fip->field_passcount == 0);
6207
0
            err = TIFFReadDirEntryLong8(tif, dp, &data);
6208
0
            if (err == TIFFReadDirEntryErrOk)
6209
0
            {
6210
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6211
0
                    return (0);
6212
0
            }
6213
0
        }
6214
0
        break;
6215
0
        case TIFF_SETGET_SINT64:
6216
0
        {
6217
0
            int64_t data;
6218
0
            assert(fip->field_readcount == 1);
6219
0
            assert(fip->field_passcount == 0);
6220
0
            err = TIFFReadDirEntrySlong8(tif, dp, &data);
6221
0
            if (err == TIFFReadDirEntryErrOk)
6222
0
            {
6223
0
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6224
0
                    return (0);
6225
0
            }
6226
0
        }
6227
0
        break;
6228
612
        case TIFF_SETGET_FLOAT:
6229
612
        {
6230
612
            float data;
6231
612
            assert(fip->field_readcount == 1);
6232
612
            assert(fip->field_passcount == 0);
6233
612
            err = TIFFReadDirEntryFloat(tif, dp, &data);
6234
612
            if (err == TIFFReadDirEntryErrOk)
6235
303
            {
6236
303
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6237
0
                    return (0);
6238
303
            }
6239
612
        }
6240
612
        break;
6241
612
        case TIFF_SETGET_DOUBLE:
6242
1
        {
6243
1
            double data;
6244
1
            assert(fip->field_readcount == 1);
6245
1
            assert(fip->field_passcount == 0);
6246
1
            err = TIFFReadDirEntryDouble(tif, dp, &data);
6247
1
            if (err == TIFFReadDirEntryErrOk)
6248
1
            {
6249
1
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6250
0
                    return (0);
6251
1
            }
6252
1
        }
6253
1
        break;
6254
83
        case TIFF_SETGET_IFD8:
6255
83
        {
6256
83
            uint64_t data;
6257
83
            assert(fip->field_readcount == 1);
6258
83
            assert(fip->field_passcount == 0);
6259
83
            err = TIFFReadDirEntryIfd8(tif, dp, &data);
6260
83
            if (err == TIFFReadDirEntryErrOk)
6261
54
            {
6262
54
                if (!TIFFSetField(tif, dp->tdir_tag, data))
6263
0
                    return (0);
6264
54
            }
6265
83
        }
6266
83
        break;
6267
479
        case TIFF_SETGET_UINT16_PAIR:
6268
479
        {
6269
479
            uint16_t *data;
6270
479
            assert(fip->field_readcount == 2);
6271
479
            assert(fip->field_passcount == 0);
6272
479
            if (dp->tdir_count != 2)
6273
147
            {
6274
147
                TIFFWarningExtR(tif, module,
6275
147
                                "incorrect count for field \"%s\", expected 2, "
6276
147
                                "got %" PRIu64,
6277
147
                                fip->field_name, dp->tdir_count);
6278
147
                return (0);
6279
147
            }
6280
332
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
6281
332
            if (err == TIFFReadDirEntryErrOk)
6282
257
            {
6283
257
                int m;
6284
257
                assert(data); /* avoid CLang static Analyzer false positive */
6285
257
                m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
6286
257
                _TIFFfreeExt(tif, data);
6287
257
                if (!m)
6288
0
                    return (0);
6289
257
            }
6290
332
        }
6291
332
        break;
6292
332
        case TIFF_SETGET_C0_UINT8:
6293
74
        {
6294
74
            uint8_t *data;
6295
74
            assert(fip->field_readcount >= 1);
6296
74
            assert(fip->field_passcount == 0);
6297
74
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6298
13
            {
6299
13
                TIFFWarningExtR(tif, module,
6300
13
                                "incorrect count for field \"%s\", expected "
6301
13
                                "%d, got %" PRIu64,
6302
13
                                fip->field_name, (int)fip->field_readcount,
6303
13
                                dp->tdir_count);
6304
13
                return (0);
6305
13
            }
6306
61
            else
6307
61
            {
6308
61
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6309
61
                if (err == TIFFReadDirEntryErrOk)
6310
43
                {
6311
43
                    int m;
6312
43
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6313
43
                    if (data != 0)
6314
43
                        _TIFFfreeExt(tif, data);
6315
43
                    if (!m)
6316
0
                        return (0);
6317
43
                }
6318
61
            }
6319
74
        }
6320
61
        break;
6321
61
        case TIFF_SETGET_C0_SINT8:
6322
0
        {
6323
0
            int8_t *data;
6324
0
            assert(fip->field_readcount >= 1);
6325
0
            assert(fip->field_passcount == 0);
6326
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6327
0
            {
6328
0
                TIFFWarningExtR(tif, module,
6329
0
                                "incorrect count for field \"%s\", expected "
6330
0
                                "%d, got %" PRIu64,
6331
0
                                fip->field_name, (int)fip->field_readcount,
6332
0
                                dp->tdir_count);
6333
0
                return (0);
6334
0
            }
6335
0
            else
6336
0
            {
6337
0
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6338
0
                if (err == TIFFReadDirEntryErrOk)
6339
0
                {
6340
0
                    int m;
6341
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6342
0
                    if (data != 0)
6343
0
                        _TIFFfreeExt(tif, data);
6344
0
                    if (!m)
6345
0
                        return (0);
6346
0
                }
6347
0
            }
6348
0
        }
6349
0
        break;
6350
1
        case TIFF_SETGET_C0_UINT16:
6351
1
        {
6352
1
            uint16_t *data;
6353
1
            assert(fip->field_readcount >= 1);
6354
1
            assert(fip->field_passcount == 0);
6355
1
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6356
1
            {
6357
1
                TIFFWarningExtR(tif, module,
6358
1
                                "incorrect count for field \"%s\", expected "
6359
1
                                "%d, got %" PRIu64,
6360
1
                                fip->field_name, (int)fip->field_readcount,
6361
1
                                dp->tdir_count);
6362
1
                return (0);
6363
1
            }
6364
0
            else
6365
0
            {
6366
0
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
6367
0
                if (err == TIFFReadDirEntryErrOk)
6368
0
                {
6369
0
                    int m;
6370
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6371
0
                    if (data != 0)
6372
0
                        _TIFFfreeExt(tif, data);
6373
0
                    if (!m)
6374
0
                        return (0);
6375
0
                }
6376
0
            }
6377
1
        }
6378
0
        break;
6379
0
        case TIFF_SETGET_C0_SINT16:
6380
0
        {
6381
0
            int16_t *data;
6382
0
            assert(fip->field_readcount >= 1);
6383
0
            assert(fip->field_passcount == 0);
6384
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6385
0
            {
6386
0
                TIFFWarningExtR(tif, module,
6387
0
                                "incorrect count for field \"%s\", expected "
6388
0
                                "%d, got %" PRIu64,
6389
0
                                fip->field_name, (int)fip->field_readcount,
6390
0
                                dp->tdir_count);
6391
0
                return (0);
6392
0
            }
6393
0
            else
6394
0
            {
6395
0
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6396
0
                if (err == TIFFReadDirEntryErrOk)
6397
0
                {
6398
0
                    int m;
6399
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6400
0
                    if (data != 0)
6401
0
                        _TIFFfreeExt(tif, data);
6402
0
                    if (!m)
6403
0
                        return (0);
6404
0
                }
6405
0
            }
6406
0
        }
6407
0
        break;
6408
14
        case TIFF_SETGET_C0_UINT32:
6409
14
        {
6410
14
            uint32_t *data;
6411
14
            assert(fip->field_readcount >= 1);
6412
14
            assert(fip->field_passcount == 0);
6413
14
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6414
9
            {
6415
9
                TIFFWarningExtR(tif, module,
6416
9
                                "incorrect count for field \"%s\", expected "
6417
9
                                "%d, got %" PRIu64,
6418
9
                                fip->field_name, (int)fip->field_readcount,
6419
9
                                dp->tdir_count);
6420
9
                return (0);
6421
9
            }
6422
5
            else
6423
5
            {
6424
5
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
6425
5
                if (err == TIFFReadDirEntryErrOk)
6426
2
                {
6427
2
                    int m;
6428
2
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6429
2
                    if (data != 0)
6430
2
                        _TIFFfreeExt(tif, data);
6431
2
                    if (!m)
6432
0
                        return (0);
6433
2
                }
6434
5
            }
6435
14
        }
6436
5
        break;
6437
5
        case TIFF_SETGET_C0_SINT32:
6438
0
        {
6439
0
            int32_t *data;
6440
0
            assert(fip->field_readcount >= 1);
6441
0
            assert(fip->field_passcount == 0);
6442
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6443
0
            {
6444
0
                TIFFWarningExtR(tif, module,
6445
0
                                "incorrect count for field \"%s\", expected "
6446
0
                                "%d, got %" PRIu64,
6447
0
                                fip->field_name, (int)fip->field_readcount,
6448
0
                                dp->tdir_count);
6449
0
                return (0);
6450
0
            }
6451
0
            else
6452
0
            {
6453
0
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
6454
0
                if (err == TIFFReadDirEntryErrOk)
6455
0
                {
6456
0
                    int m;
6457
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6458
0
                    if (data != 0)
6459
0
                        _TIFFfreeExt(tif, data);
6460
0
                    if (!m)
6461
0
                        return (0);
6462
0
                }
6463
0
            }
6464
0
        }
6465
0
        break;
6466
0
        case TIFF_SETGET_C0_UINT64:
6467
0
        {
6468
0
            uint64_t *data;
6469
0
            assert(fip->field_readcount >= 1);
6470
0
            assert(fip->field_passcount == 0);
6471
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6472
0
            {
6473
0
                TIFFWarningExtR(tif, module,
6474
0
                                "incorrect count for field \"%s\", expected "
6475
0
                                "%d, got %" PRIu64,
6476
0
                                fip->field_name, (int)fip->field_readcount,
6477
0
                                dp->tdir_count);
6478
0
                return (0);
6479
0
            }
6480
0
            else
6481
0
            {
6482
0
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6483
0
                if (err == TIFFReadDirEntryErrOk)
6484
0
                {
6485
0
                    int m;
6486
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6487
0
                    if (data != 0)
6488
0
                        _TIFFfreeExt(tif, data);
6489
0
                    if (!m)
6490
0
                        return (0);
6491
0
                }
6492
0
            }
6493
0
        }
6494
0
        break;
6495
0
        case TIFF_SETGET_C0_SINT64:
6496
0
        {
6497
0
            int64_t *data;
6498
0
            assert(fip->field_readcount >= 1);
6499
0
            assert(fip->field_passcount == 0);
6500
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6501
0
            {
6502
0
                TIFFWarningExtR(tif, module,
6503
0
                                "incorrect count for field \"%s\", expected "
6504
0
                                "%d, got %" PRIu64,
6505
0
                                fip->field_name, (int)fip->field_readcount,
6506
0
                                dp->tdir_count);
6507
0
                return (0);
6508
0
            }
6509
0
            else
6510
0
            {
6511
0
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
6512
0
                if (err == TIFFReadDirEntryErrOk)
6513
0
                {
6514
0
                    int m;
6515
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6516
0
                    if (data != 0)
6517
0
                        _TIFFfreeExt(tif, data);
6518
0
                    if (!m)
6519
0
                        return (0);
6520
0
                }
6521
0
            }
6522
0
        }
6523
0
        break;
6524
599
        case TIFF_SETGET_C0_FLOAT:
6525
599
        {
6526
599
            float *data;
6527
599
            assert(fip->field_readcount >= 1);
6528
599
            assert(fip->field_passcount == 0);
6529
599
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6530
229
            {
6531
229
                TIFFWarningExtR(tif, module,
6532
229
                                "incorrect count for field \"%s\", expected "
6533
229
                                "%d, got %" PRIu64,
6534
229
                                fip->field_name, (int)fip->field_readcount,
6535
229
                                dp->tdir_count);
6536
229
                return (0);
6537
229
            }
6538
370
            else
6539
370
            {
6540
370
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6541
370
                if (err == TIFFReadDirEntryErrOk)
6542
311
                {
6543
311
                    int m;
6544
311
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6545
311
                    if (data != 0)
6546
311
                        _TIFFfreeExt(tif, data);
6547
311
                    if (!m)
6548
0
                        return (0);
6549
311
                }
6550
370
            }
6551
599
        }
6552
370
        break;
6553
        /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6554
         * into Double-Arrays. */
6555
370
        case TIFF_SETGET_C0_DOUBLE:
6556
0
        {
6557
0
            double *data;
6558
0
            assert(fip->field_readcount >= 1);
6559
0
            assert(fip->field_passcount == 0);
6560
0
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
6561
0
            {
6562
0
                TIFFWarningExtR(tif, module,
6563
0
                                "incorrect count for field \"%s\", expected "
6564
0
                                "%d, got %" PRIu64,
6565
0
                                fip->field_name, (int)fip->field_readcount,
6566
0
                                dp->tdir_count);
6567
0
                return (0);
6568
0
            }
6569
0
            else
6570
0
            {
6571
0
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6572
0
                if (err == TIFFReadDirEntryErrOk)
6573
0
                {
6574
0
                    int m;
6575
0
                    m = TIFFSetField(tif, dp->tdir_tag, data);
6576
0
                    if (data != 0)
6577
0
                        _TIFFfreeExt(tif, data);
6578
0
                    if (!m)
6579
0
                        return (0);
6580
0
                }
6581
0
            }
6582
0
        }
6583
0
        break;
6584
130
        case TIFF_SETGET_C16_ASCII:
6585
130
        {
6586
130
            uint8_t *data;
6587
130
            assert(fip->field_readcount == TIFF_VARIABLE);
6588
130
            assert(fip->field_passcount == 1);
6589
130
            if (dp->tdir_count > 0xFFFF)
6590
102
                err = TIFFReadDirEntryErrCount;
6591
28
            else
6592
28
            {
6593
28
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6594
28
                if (err == TIFFReadDirEntryErrOk)
6595
18
                {
6596
18
                    int m;
6597
18
                    if (data != 0 && dp->tdir_count > 0 &&
6598
18
                        data[dp->tdir_count - 1] != '\0')
6599
18
                    {
6600
18
                        TIFFWarningExtR(
6601
18
                            tif, module,
6602
18
                            "ASCII value for tag \"%s\" does not end in null "
6603
18
                            "byte. Forcing it to be null",
6604
18
                            fip->field_name);
6605
18
                        data[dp->tdir_count - 1] = '\0';
6606
18
                    }
6607
18
                    m = TIFFSetField(tif, dp->tdir_tag,
6608
18
                                     (uint16_t)(dp->tdir_count), data);
6609
18
                    if (data != 0)
6610
18
                        _TIFFfreeExt(tif, data);
6611
18
                    if (!m)
6612
0
                        return (0);
6613
18
                }
6614
28
            }
6615
130
        }
6616
130
        break;
6617
130
        case TIFF_SETGET_C16_UINT8:
6618
24
        {
6619
24
            uint8_t *data;
6620
24
            assert(fip->field_readcount == TIFF_VARIABLE);
6621
24
            assert(fip->field_passcount == 1);
6622
24
            if (dp->tdir_count > 0xFFFF)
6623
6
                err = TIFFReadDirEntryErrCount;
6624
18
            else
6625
18
            {
6626
18
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6627
18
                if (err == TIFFReadDirEntryErrOk)
6628
13
                {
6629
13
                    int m;
6630
13
                    m = TIFFSetField(tif, dp->tdir_tag,
6631
13
                                     (uint16_t)(dp->tdir_count), data);
6632
13
                    if (data != 0)
6633
4
                        _TIFFfreeExt(tif, data);
6634
13
                    if (!m)
6635
9
                        return (0);
6636
13
                }
6637
18
            }
6638
24
        }
6639
15
        break;
6640
15
        case TIFF_SETGET_C16_SINT8:
6641
0
        {
6642
0
            int8_t *data;
6643
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6644
0
            assert(fip->field_passcount == 1);
6645
0
            if (dp->tdir_count > 0xFFFF)
6646
0
                err = TIFFReadDirEntryErrCount;
6647
0
            else
6648
0
            {
6649
0
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6650
0
                if (err == TIFFReadDirEntryErrOk)
6651
0
                {
6652
0
                    int m;
6653
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6654
0
                                     (uint16_t)(dp->tdir_count), data);
6655
0
                    if (data != 0)
6656
0
                        _TIFFfreeExt(tif, data);
6657
0
                    if (!m)
6658
0
                        return (0);
6659
0
                }
6660
0
            }
6661
0
        }
6662
0
        break;
6663
255
        case TIFF_SETGET_C16_UINT16:
6664
255
        {
6665
255
            uint16_t *data;
6666
255
            assert(fip->field_readcount == TIFF_VARIABLE);
6667
255
            assert(fip->field_passcount == 1);
6668
255
            if (dp->tdir_count > 0xFFFF)
6669
14
                err = TIFFReadDirEntryErrCount;
6670
241
            else
6671
241
            {
6672
241
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
6673
241
                if (err == TIFFReadDirEntryErrOk)
6674
227
                {
6675
227
                    int m;
6676
227
                    m = TIFFSetField(tif, dp->tdir_tag,
6677
227
                                     (uint16_t)(dp->tdir_count), data);
6678
227
                    if (data != 0)
6679
221
                        _TIFFfreeExt(tif, data);
6680
227
                    if (!m)
6681
4
                        return (0);
6682
227
                }
6683
241
            }
6684
255
        }
6685
251
        break;
6686
251
        case TIFF_SETGET_C16_SINT16:
6687
0
        {
6688
0
            int16_t *data;
6689
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6690
0
            assert(fip->field_passcount == 1);
6691
0
            if (dp->tdir_count > 0xFFFF)
6692
0
                err = TIFFReadDirEntryErrCount;
6693
0
            else
6694
0
            {
6695
0
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6696
0
                if (err == TIFFReadDirEntryErrOk)
6697
0
                {
6698
0
                    int m;
6699
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6700
0
                                     (uint16_t)(dp->tdir_count), data);
6701
0
                    if (data != 0)
6702
0
                        _TIFFfreeExt(tif, data);
6703
0
                    if (!m)
6704
0
                        return (0);
6705
0
                }
6706
0
            }
6707
0
        }
6708
0
        break;
6709
31
        case TIFF_SETGET_C16_UINT32:
6710
31
        {
6711
31
            uint32_t *data;
6712
31
            assert(fip->field_readcount == TIFF_VARIABLE);
6713
31
            assert(fip->field_passcount == 1);
6714
31
            if (dp->tdir_count > 0xFFFF)
6715
9
                err = TIFFReadDirEntryErrCount;
6716
22
            else
6717
22
            {
6718
22
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
6719
22
                if (err == TIFFReadDirEntryErrOk)
6720
3
                {
6721
3
                    int m;
6722
3
                    m = TIFFSetField(tif, dp->tdir_tag,
6723
3
                                     (uint16_t)(dp->tdir_count), data);
6724
3
                    if (data != 0)
6725
3
                        _TIFFfreeExt(tif, data);
6726
3
                    if (!m)
6727
0
                        return (0);
6728
3
                }
6729
22
            }
6730
31
        }
6731
31
        break;
6732
31
        case TIFF_SETGET_C16_SINT32:
6733
0
        {
6734
0
            int32_t *data;
6735
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6736
0
            assert(fip->field_passcount == 1);
6737
0
            if (dp->tdir_count > 0xFFFF)
6738
0
                err = TIFFReadDirEntryErrCount;
6739
0
            else
6740
0
            {
6741
0
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
6742
0
                if (err == TIFFReadDirEntryErrOk)
6743
0
                {
6744
0
                    int m;
6745
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6746
0
                                     (uint16_t)(dp->tdir_count), data);
6747
0
                    if (data != 0)
6748
0
                        _TIFFfreeExt(tif, data);
6749
0
                    if (!m)
6750
0
                        return (0);
6751
0
                }
6752
0
            }
6753
0
        }
6754
0
        break;
6755
0
        case TIFF_SETGET_C16_UINT64:
6756
0
        {
6757
0
            uint64_t *data;
6758
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6759
0
            assert(fip->field_passcount == 1);
6760
0
            if (dp->tdir_count > 0xFFFF)
6761
0
                err = TIFFReadDirEntryErrCount;
6762
0
            else
6763
0
            {
6764
0
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6765
0
                if (err == TIFFReadDirEntryErrOk)
6766
0
                {
6767
0
                    int m;
6768
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6769
0
                                     (uint16_t)(dp->tdir_count), data);
6770
0
                    if (data != 0)
6771
0
                        _TIFFfreeExt(tif, data);
6772
0
                    if (!m)
6773
0
                        return (0);
6774
0
                }
6775
0
            }
6776
0
        }
6777
0
        break;
6778
0
        case TIFF_SETGET_C16_SINT64:
6779
0
        {
6780
0
            int64_t *data;
6781
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6782
0
            assert(fip->field_passcount == 1);
6783
0
            if (dp->tdir_count > 0xFFFF)
6784
0
                err = TIFFReadDirEntryErrCount;
6785
0
            else
6786
0
            {
6787
0
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
6788
0
                if (err == TIFFReadDirEntryErrOk)
6789
0
                {
6790
0
                    int m;
6791
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6792
0
                                     (uint16_t)(dp->tdir_count), data);
6793
0
                    if (data != 0)
6794
0
                        _TIFFfreeExt(tif, data);
6795
0
                    if (!m)
6796
0
                        return (0);
6797
0
                }
6798
0
            }
6799
0
        }
6800
0
        break;
6801
219
        case TIFF_SETGET_C16_FLOAT:
6802
219
        {
6803
219
            float *data;
6804
219
            assert(fip->field_readcount == TIFF_VARIABLE);
6805
219
            assert(fip->field_passcount == 1);
6806
219
            if (dp->tdir_count > 0xFFFF)
6807
53
                err = TIFFReadDirEntryErrCount;
6808
166
            else
6809
166
            {
6810
166
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
6811
166
                if (err == TIFFReadDirEntryErrOk)
6812
138
                {
6813
138
                    int m;
6814
138
                    m = TIFFSetField(tif, dp->tdir_tag,
6815
138
                                     (uint16_t)(dp->tdir_count), data);
6816
138
                    if (data != 0)
6817
125
                        _TIFFfreeExt(tif, data);
6818
138
                    if (!m)
6819
13
                        return (0);
6820
138
                }
6821
166
            }
6822
219
        }
6823
206
        break;
6824
206
        case TIFF_SETGET_C16_DOUBLE:
6825
0
        {
6826
0
            double *data;
6827
0
            assert(fip->field_readcount == TIFF_VARIABLE);
6828
0
            assert(fip->field_passcount == 1);
6829
0
            if (dp->tdir_count > 0xFFFF)
6830
0
                err = TIFFReadDirEntryErrCount;
6831
0
            else
6832
0
            {
6833
0
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
6834
0
                if (err == TIFFReadDirEntryErrOk)
6835
0
                {
6836
0
                    int m;
6837
0
                    m = TIFFSetField(tif, dp->tdir_tag,
6838
0
                                     (uint16_t)(dp->tdir_count), data);
6839
0
                    if (data != 0)
6840
0
                        _TIFFfreeExt(tif, data);
6841
0
                    if (!m)
6842
0
                        return (0);
6843
0
                }
6844
0
            }
6845
0
        }
6846
0
        break;
6847
16
        case TIFF_SETGET_C16_IFD8:
6848
16
        {
6849
16
            uint64_t *data;
6850
16
            assert(fip->field_readcount == TIFF_VARIABLE);
6851
16
            assert(fip->field_passcount == 1);
6852
16
            if (dp->tdir_count > 0xFFFF)
6853
12
                err = TIFFReadDirEntryErrCount;
6854
4
            else
6855
4
            {
6856
4
                err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
6857
4
                if (err == TIFFReadDirEntryErrOk)
6858
2
                {
6859
2
                    int m;
6860
2
                    m = TIFFSetField(tif, dp->tdir_tag,
6861
2
                                     (uint16_t)(dp->tdir_count), data);
6862
2
                    if (data != 0)
6863
2
                        _TIFFfreeExt(tif, data);
6864
2
                    if (!m)
6865
0
                        return (0);
6866
2
                }
6867
4
            }
6868
16
        }
6869
16
        break;
6870
4.06k
        case TIFF_SETGET_C32_ASCII:
6871
4.06k
        {
6872
4.06k
            uint8_t *data;
6873
4.06k
            assert(fip->field_readcount == TIFF_VARIABLE2);
6874
4.06k
            assert(fip->field_passcount == 1);
6875
4.06k
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
6876
4.06k
            if (err == TIFFReadDirEntryErrOk)
6877
1.31k
            {
6878
1.31k
                int m;
6879
1.31k
                if (data != 0 && dp->tdir_count > 0 &&
6880
1.31k
                    data[dp->tdir_count - 1] != '\0')
6881
607
                {
6882
607
                    TIFFWarningExtR(tif, module,
6883
607
                                    "ASCII value for tag \"%s\" does not end "
6884
607
                                    "in null byte. Forcing it to be null",
6885
607
                                    fip->field_name);
6886
607
                    data[dp->tdir_count - 1] = '\0';
6887
607
                }
6888
1.31k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
6889
1.31k
                                 data);
6890
1.31k
                if (data != 0)
6891
1.21k
                    _TIFFfreeExt(tif, data);
6892
1.31k
                if (!m)
6893
0
                    return (0);
6894
1.31k
            }
6895
4.06k
        }
6896
4.06k
        break;
6897
4.06k
        case TIFF_SETGET_C32_UINT8:
6898
3.88k
        {
6899
3.88k
            uint8_t *data;
6900
3.88k
            uint32_t count = 0;
6901
3.88k
            assert(fip->field_readcount == TIFF_VARIABLE2);
6902
3.88k
            assert(fip->field_passcount == 1);
6903
3.88k
            if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
6904
3.88k
                dp->tdir_type == TIFF_LONG)
6905
3
            {
6906
                /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
6907
                 * data type LONG instead of UNDEFINED. Work around this
6908
                 * frequently found issue */
6909
3
                void *origdata;
6910
3
                err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
6911
3
                if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
6912
1
                {
6913
1
                    data = NULL;
6914
1
                }
6915
2
                else
6916
2
                {
6917
2
                    if (tif->tif_flags & TIFF_SWAB)
6918
0
                        TIFFSwabArrayOfLong((uint32_t *)origdata, count);
6919
2
                    data = (uint8_t *)origdata;
6920
2
                    count = (uint32_t)(count * 4);
6921
2
                }
6922
3
            }
6923
3.88k
            else
6924
3.88k
            {
6925
3.88k
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
6926
3.88k
                count = (uint32_t)(dp->tdir_count);
6927
3.88k
            }
6928
3.88k
            if (err == TIFFReadDirEntryErrOk)
6929
740
            {
6930
740
                int m;
6931
740
                m = TIFFSetField(tif, dp->tdir_tag, count, data);
6932
740
                if (data != 0)
6933
465
                    _TIFFfreeExt(tif, data);
6934
740
                if (!m)
6935
275
                    return (0);
6936
740
            }
6937
3.88k
        }
6938
3.60k
        break;
6939
3.60k
        case TIFF_SETGET_C32_SINT8:
6940
361
        {
6941
361
            int8_t *data = NULL;
6942
361
            assert(fip->field_readcount == TIFF_VARIABLE2);
6943
361
            assert(fip->field_passcount == 1);
6944
361
            err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
6945
361
            if (err == TIFFReadDirEntryErrOk)
6946
75
            {
6947
75
                int m;
6948
75
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
6949
75
                                 data);
6950
75
                if (data != 0)
6951
51
                    _TIFFfreeExt(tif, data);
6952
75
                if (!m)
6953
24
                    return (0);
6954
75
            }
6955
361
        }
6956
337
        break;
6957
5.93k
        case TIFF_SETGET_C32_UINT16:
6958
5.93k
        {
6959
5.93k
            uint16_t *data;
6960
5.93k
            assert(fip->field_readcount == TIFF_VARIABLE2);
6961
5.93k
            assert(fip->field_passcount == 1);
6962
5.93k
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
6963
5.93k
            if (err == TIFFReadDirEntryErrOk)
6964
2.15k
            {
6965
2.15k
                int m;
6966
2.15k
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
6967
2.15k
                                 data);
6968
2.15k
                if (data != 0)
6969
2.11k
                    _TIFFfreeExt(tif, data);
6970
2.15k
                if (!m)
6971
39
                    return (0);
6972
2.15k
            }
6973
5.93k
        }
6974
5.90k
        break;
6975
5.90k
        case TIFF_SETGET_C32_SINT16:
6976
907
        {
6977
907
            int16_t *data = NULL;
6978
907
            assert(fip->field_readcount == TIFF_VARIABLE2);
6979
907
            assert(fip->field_passcount == 1);
6980
907
            err = TIFFReadDirEntrySshortArray(tif, dp, &data);
6981
907
            if (err == TIFFReadDirEntryErrOk)
6982
158
            {
6983
158
                int m;
6984
158
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
6985
158
                                 data);
6986
158
                if (data != 0)
6987
136
                    _TIFFfreeExt(tif, data);
6988
158
                if (!m)
6989
22
                    return (0);
6990
158
            }
6991
907
        }
6992
885
        break;
6993
2.03k
        case TIFF_SETGET_C32_UINT32:
6994
2.03k
        {
6995
2.03k
            uint32_t *data;
6996
2.03k
            assert(fip->field_readcount == TIFF_VARIABLE2);
6997
2.03k
            assert(fip->field_passcount == 1);
6998
2.03k
            err = TIFFReadDirEntryLongArray(tif, dp, &data);
6999
2.03k
            if (err == TIFFReadDirEntryErrOk)
7000
179
            {
7001
179
                int m;
7002
179
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7003
179
                                 data);
7004
179
                if (data != 0)
7005
155
                    _TIFFfreeExt(tif, data);
7006
179
                if (!m)
7007
24
                    return (0);
7008
179
            }
7009
2.03k
        }
7010
2.00k
        break;
7011
2.00k
        case TIFF_SETGET_C32_SINT32:
7012
586
        {
7013
586
            int32_t *data = NULL;
7014
586
            assert(fip->field_readcount == TIFF_VARIABLE2);
7015
586
            assert(fip->field_passcount == 1);
7016
586
            err = TIFFReadDirEntrySlongArray(tif, dp, &data);
7017
586
            if (err == TIFFReadDirEntryErrOk)
7018
50
            {
7019
50
                int m;
7020
50
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7021
50
                                 data);
7022
50
                if (data != 0)
7023
30
                    _TIFFfreeExt(tif, data);
7024
50
                if (!m)
7025
20
                    return (0);
7026
50
            }
7027
586
        }
7028
566
        break;
7029
566
        case TIFF_SETGET_C32_UINT64:
7030
270
        {
7031
270
            uint64_t *data;
7032
270
            assert(fip->field_readcount == TIFF_VARIABLE2);
7033
270
            assert(fip->field_passcount == 1);
7034
270
            err = TIFFReadDirEntryLong8Array(tif, dp, &data);
7035
270
            if (err == TIFFReadDirEntryErrOk)
7036
81
            {
7037
81
                int m;
7038
81
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7039
81
                                 data);
7040
81
                if (data != 0)
7041
12
                    _TIFFfreeExt(tif, data);
7042
81
                if (!m)
7043
81
                    return (0);
7044
81
            }
7045
270
        }
7046
189
        break;
7047
189
        case TIFF_SETGET_C32_SINT64:
7048
162
        {
7049
162
            int64_t *data = NULL;
7050
162
            assert(fip->field_readcount == TIFF_VARIABLE2);
7051
162
            assert(fip->field_passcount == 1);
7052
162
            err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
7053
162
            if (err == TIFFReadDirEntryErrOk)
7054
78
            {
7055
78
                int m;
7056
78
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7057
78
                                 data);
7058
78
                if (data != 0)
7059
65
                    _TIFFfreeExt(tif, data);
7060
78
                if (!m)
7061
72
                    return (0);
7062
78
            }
7063
162
        }
7064
90
        break;
7065
2.19k
        case TIFF_SETGET_C32_FLOAT:
7066
2.19k
        {
7067
2.19k
            float *data;
7068
2.19k
            assert(fip->field_readcount == TIFF_VARIABLE2);
7069
2.19k
            assert(fip->field_passcount == 1);
7070
2.19k
            err = TIFFReadDirEntryFloatArray(tif, dp, &data);
7071
2.19k
            if (err == TIFFReadDirEntryErrOk)
7072
235
            {
7073
235
                int m;
7074
235
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7075
235
                                 data);
7076
235
                if (data != 0)
7077
121
                    _TIFFfreeExt(tif, data);
7078
235
                if (!m)
7079
114
                    return (0);
7080
235
            }
7081
2.19k
        }
7082
2.08k
        break;
7083
2.08k
        case TIFF_SETGET_C32_DOUBLE:
7084
323
        {
7085
323
            double *data;
7086
323
            assert(fip->field_readcount == TIFF_VARIABLE2);
7087
323
            assert(fip->field_passcount == 1);
7088
323
            err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
7089
323
            if (err == TIFFReadDirEntryErrOk)
7090
147
            {
7091
147
                int m;
7092
147
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7093
147
                                 data);
7094
147
                if (data != 0)
7095
133
                    _TIFFfreeExt(tif, data);
7096
147
                if (!m)
7097
14
                    return (0);
7098
147
            }
7099
323
        }
7100
309
        break;
7101
309
        case TIFF_SETGET_C32_IFD8:
7102
249
        {
7103
249
            uint64_t *data;
7104
249
            assert(fip->field_readcount == TIFF_VARIABLE2);
7105
249
            assert(fip->field_passcount == 1);
7106
249
            err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
7107
249
            if (err == TIFFReadDirEntryErrOk)
7108
69
            {
7109
69
                int m;
7110
69
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
7111
69
                                 data);
7112
69
                if (data != 0)
7113
48
                    _TIFFfreeExt(tif, data);
7114
69
                if (!m)
7115
21
                    return (0);
7116
69
            }
7117
249
        }
7118
228
        break;
7119
228
        default:
7120
0
            assert(0); /* we should never get here */
7121
0
            break;
7122
151k
    }
7123
150k
    if (err != TIFFReadDirEntryErrOk)
7124
17.7k
    {
7125
17.7k
        TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
7126
17.7k
        return (0);
7127
17.7k
    }
7128
132k
    return (1);
7129
150k
}
7130
7131
/*
7132
 * Fetch a set of offsets or lengths.
7133
 * While this routine says "strips", in fact it's also used for tiles.
7134
 */
7135
static int TIFFFetchStripThing(TIFF *tif, TIFFDirEntry *dir, uint32_t nstrips,
7136
                               uint64_t **lpp)
7137
2.69k
{
7138
2.69k
    static const char module[] = "TIFFFetchStripThing";
7139
2.69k
    enum TIFFReadDirEntryErr err;
7140
2.69k
    uint64_t *data;
7141
2.69k
    err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
7142
2.69k
    if (err != TIFFReadDirEntryErrOk)
7143
54
    {
7144
54
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7145
54
        TIFFReadDirEntryOutputErr(tif, err, module,
7146
54
                                  fip ? fip->field_name : "unknown tagname", 0);
7147
54
        return (0);
7148
54
    }
7149
2.63k
    if (dir->tdir_count < (uint64_t)nstrips)
7150
434
    {
7151
434
        uint64_t *resizeddata;
7152
434
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
7153
434
        const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
7154
434
        uint32_t max_nstrips = 1000000;
7155
434
        if (pszMax)
7156
0
            max_nstrips = (uint32_t)atoi(pszMax);
7157
434
        TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
7158
434
                                  fip ? fip->field_name : "unknown tagname",
7159
434
                                  (nstrips <= max_nstrips));
7160
7161
434
        if (nstrips > max_nstrips)
7162
1
        {
7163
1
            _TIFFfreeExt(tif, data);
7164
1
            return (0);
7165
1
        }
7166
7167
433
        resizeddata = (uint64_t *)_TIFFCheckMalloc(
7168
433
            tif, nstrips, sizeof(uint64_t), "for strip array");
7169
433
        if (resizeddata == 0)
7170
0
        {
7171
0
            _TIFFfreeExt(tif, data);
7172
0
            return (0);
7173
0
        }
7174
433
        if (dir->tdir_count)
7175
423
            _TIFFmemcpy(resizeddata, data,
7176
423
                        (uint32_t)dir->tdir_count * sizeof(uint64_t));
7177
433
        _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
7178
433
                    (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
7179
433
        _TIFFfreeExt(tif, data);
7180
433
        data = resizeddata;
7181
433
    }
7182
2.63k
    *lpp = data;
7183
2.63k
    return (1);
7184
2.63k
}
7185
7186
/*
7187
 * Fetch and set the SubjectDistance EXIF tag.
7188
 */
7189
static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
7190
0
{
7191
0
    static const char module[] = "TIFFFetchSubjectDistance";
7192
0
    enum TIFFReadDirEntryErr err;
7193
0
    UInt64Aligned_t m;
7194
0
    m.l = 0;
7195
0
    assert(sizeof(double) == 8);
7196
0
    assert(sizeof(uint64_t) == 8);
7197
0
    assert(sizeof(uint32_t) == 4);
7198
0
    if (dir->tdir_count != 1)
7199
0
        err = TIFFReadDirEntryErrCount;
7200
0
    else if (dir->tdir_type != TIFF_RATIONAL)
7201
0
        err = TIFFReadDirEntryErrType;
7202
0
    else
7203
0
    {
7204
0
        if (!(tif->tif_flags & TIFF_BIGTIFF))
7205
0
        {
7206
0
            uint32_t offset;
7207
0
            offset = *(uint32_t *)(&dir->tdir_offset);
7208
0
            if (tif->tif_flags & TIFF_SWAB)
7209
0
                TIFFSwabLong(&offset);
7210
0
            err = TIFFReadDirEntryData(tif, offset, 8, m.i);
7211
0
        }
7212
0
        else
7213
0
        {
7214
0
            m.l = dir->tdir_offset.toff_long8;
7215
0
            err = TIFFReadDirEntryErrOk;
7216
0
        }
7217
0
    }
7218
0
    if (err == TIFFReadDirEntryErrOk)
7219
0
    {
7220
0
        double n;
7221
0
        if (tif->tif_flags & TIFF_SWAB)
7222
0
            TIFFSwabArrayOfLong(m.i, 2);
7223
0
        if (m.i[0] == 0)
7224
0
            n = 0.0;
7225
0
        else if (m.i[0] == 0xFFFFFFFF || m.i[1] == 0)
7226
            /*
7227
             * XXX: Numerator 0xFFFFFFFF means that we have infinite
7228
             * distance. Indicate that with a negative floating point
7229
             * SubjectDistance value.
7230
             */
7231
0
            n = -1.0;
7232
0
        else
7233
0
            n = (double)m.i[0] / (double)m.i[1];
7234
0
        return (TIFFSetField(tif, dir->tdir_tag, n));
7235
0
    }
7236
0
    else
7237
0
    {
7238
0
        TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
7239
0
        return (0);
7240
0
    }
7241
0
}
7242
7243
static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
7244
                                      uint64_t stripbytes,
7245
                                      uint32_t rowsperstrip)
7246
0
{
7247
0
    TIFFDirectory *td = &tif->tif_dir;
7248
0
    uint64_t bytecount;
7249
0
    uint64_t offset;
7250
0
    uint64_t last_offset;
7251
0
    uint64_t last_bytecount;
7252
0
    uint32_t i;
7253
0
    uint64_t *newcounts;
7254
0
    uint64_t *newoffsets;
7255
7256
0
    offset = TIFFGetStrileOffset(tif, 0);
7257
0
    last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7258
0
    last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7259
0
    if (last_offset > UINT64_MAX - last_bytecount ||
7260
0
        last_offset + last_bytecount < offset)
7261
0
    {
7262
0
        return;
7263
0
    }
7264
0
    bytecount = last_offset + last_bytecount - offset;
7265
7266
0
    newcounts =
7267
0
        (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
7268
0
                                     "for chopped \"StripByteCounts\" array");
7269
0
    newoffsets = (uint64_t *)_TIFFCheckMalloc(
7270
0
        tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7271
0
    if (newcounts == NULL || newoffsets == NULL)
7272
0
    {
7273
        /*
7274
         * Unable to allocate new strip information, give up and use
7275
         * the original one strip information.
7276
         */
7277
0
        if (newcounts != NULL)
7278
0
            _TIFFfreeExt(tif, newcounts);
7279
0
        if (newoffsets != NULL)
7280
0
            _TIFFfreeExt(tif, newoffsets);
7281
0
        return;
7282
0
    }
7283
7284
    /*
7285
     * Fill the strip information arrays with new bytecounts and offsets
7286
     * that reflect the broken-up format.
7287
     */
7288
0
    for (i = 0; i < nstrips; i++)
7289
0
    {
7290
0
        if (stripbytes > bytecount)
7291
0
            stripbytes = bytecount;
7292
0
        newcounts[i] = stripbytes;
7293
0
        newoffsets[i] = stripbytes ? offset : 0;
7294
0
        offset += stripbytes;
7295
0
        bytecount -= stripbytes;
7296
0
    }
7297
7298
    /*
7299
     * Replace old single strip info with multi-strip info.
7300
     */
7301
0
    td->td_stripsperimage = td->td_nstrips = nstrips;
7302
0
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
7303
7304
0
    _TIFFfreeExt(tif, td->td_stripbytecount_p);
7305
0
    _TIFFfreeExt(tif, td->td_stripoffset_p);
7306
0
    td->td_stripbytecount_p = newcounts;
7307
0
    td->td_stripoffset_p = newoffsets;
7308
#ifdef STRIPBYTECOUNTSORTED_UNUSED
7309
    td->td_stripbytecountsorted = 1;
7310
#endif
7311
0
    tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
7312
0
}
7313
7314
/*
7315
 * Replace a single strip (tile) of uncompressed data by multiple strips
7316
 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
7317
 * dealing with large images or for dealing with machines with a limited
7318
 * amount memory.
7319
 */
7320
static void ChopUpSingleUncompressedStrip(TIFF *tif)
7321
0
{
7322
0
    register TIFFDirectory *td = &tif->tif_dir;
7323
0
    uint64_t bytecount;
7324
0
    uint64_t offset;
7325
0
    uint32_t rowblock;
7326
0
    uint64_t rowblockbytes;
7327
0
    uint64_t stripbytes;
7328
0
    uint32_t nstrips;
7329
0
    uint32_t rowsperstrip;
7330
7331
0
    bytecount = TIFFGetStrileByteCount(tif, 0);
7332
    /* On a newly created file, just re-opened to be filled, we */
7333
    /* don't want strip chop to trigger as it is going to cause issues */
7334
    /* later ( StripOffsets and StripByteCounts improperly filled) . */
7335
0
    if (bytecount == 0 && tif->tif_mode != O_RDONLY)
7336
0
        return;
7337
0
    offset = TIFFGetStrileByteCount(tif, 0);
7338
0
    assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
7339
0
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7340
0
        rowblock = td->td_ycbcrsubsampling[1];
7341
0
    else
7342
0
        rowblock = 1;
7343
0
    rowblockbytes = TIFFVTileSize64(tif, rowblock);
7344
    /*
7345
     * Make the rows hold at least one scanline, but fill specified amount
7346
     * of data if possible.
7347
     */
7348
0
    if (rowblockbytes > STRIP_SIZE_DEFAULT)
7349
0
    {
7350
0
        stripbytes = rowblockbytes;
7351
0
        rowsperstrip = rowblock;
7352
0
    }
7353
0
    else if (rowblockbytes > 0)
7354
0
    {
7355
0
        uint32_t rowblocksperstrip;
7356
0
        rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
7357
0
        rowsperstrip = rowblocksperstrip * rowblock;
7358
0
        stripbytes = rowblocksperstrip * rowblockbytes;
7359
0
    }
7360
0
    else
7361
0
        return;
7362
7363
    /*
7364
     * never increase the number of rows per strip
7365
     */
7366
0
    if (rowsperstrip >= td->td_rowsperstrip)
7367
0
        return;
7368
0
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
7369
0
    if (nstrips == 0)
7370
0
        return;
7371
7372
    /* If we are going to allocate a lot of memory, make sure that the */
7373
    /* file is as big as needed */
7374
0
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
7375
0
        (offset >= TIFFGetFileSize(tif) ||
7376
0
         stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
7377
0
    {
7378
0
        return;
7379
0
    }
7380
7381
0
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
7382
0
}
7383
7384
/*
7385
 * Replace a file with contiguous strips > 2 GB of uncompressed data by
7386
 * multiple smaller strips. This is useful for
7387
 * dealing with large images or for dealing with machines with a limited
7388
 * amount memory.
7389
 */
7390
static void TryChopUpUncompressedBigTiff(TIFF *tif)
7391
0
{
7392
0
    TIFFDirectory *td = &tif->tif_dir;
7393
0
    uint32_t rowblock;
7394
0
    uint64_t rowblockbytes;
7395
0
    uint32_t i;
7396
0
    uint64_t stripsize;
7397
0
    uint32_t rowblocksperstrip;
7398
0
    uint32_t rowsperstrip;
7399
0
    uint64_t stripbytes;
7400
0
    uint32_t nstrips;
7401
7402
0
    stripsize = TIFFStripSize64(tif);
7403
7404
0
    assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
7405
0
    assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
7406
0
    assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
7407
0
           TIFF_STRIPCHOP);
7408
0
    assert(stripsize > 0x7FFFFFFFUL);
7409
7410
    /* On a newly created file, just re-opened to be filled, we */
7411
    /* don't want strip chop to trigger as it is going to cause issues */
7412
    /* later ( StripOffsets and StripByteCounts improperly filled) . */
7413
0
    if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
7414
0
        return;
7415
7416
0
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
7417
0
        rowblock = td->td_ycbcrsubsampling[1];
7418
0
    else
7419
0
        rowblock = 1;
7420
0
    rowblockbytes = TIFFVStripSize64(tif, rowblock);
7421
0
    if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
7422
0
    {
7423
        /* In case of file with gigantic width */
7424
0
        return;
7425
0
    }
7426
7427
    /* Check that the strips are contiguous and of the expected size */
7428
0
    for (i = 0; i < td->td_nstrips; i++)
7429
0
    {
7430
0
        if (i == td->td_nstrips - 1)
7431
0
        {
7432
0
            if (TIFFGetStrileByteCount(tif, i) <
7433
0
                TIFFVStripSize64(tif,
7434
0
                                 td->td_imagelength - i * td->td_rowsperstrip))
7435
0
            {
7436
0
                return;
7437
0
            }
7438
0
        }
7439
0
        else
7440
0
        {
7441
0
            if (TIFFGetStrileByteCount(tif, i) != stripsize)
7442
0
            {
7443
0
                return;
7444
0
            }
7445
0
            if (i > 0 && TIFFGetStrileOffset(tif, i) !=
7446
0
                             TIFFGetStrileOffset(tif, i - 1) +
7447
0
                                 TIFFGetStrileByteCount(tif, i - 1))
7448
0
            {
7449
0
                return;
7450
0
            }
7451
0
        }
7452
0
    }
7453
7454
    /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
7455
0
    rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
7456
0
    if (rowblocksperstrip == 0)
7457
0
        rowblocksperstrip = 1;
7458
0
    rowsperstrip = rowblocksperstrip * rowblock;
7459
0
    stripbytes = rowblocksperstrip * rowblockbytes;
7460
0
    assert(stripbytes <= 0x7FFFFFFFUL);
7461
7462
0
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
7463
0
    if (nstrips == 0)
7464
0
        return;
7465
7466
    /* If we are going to allocate a lot of memory, make sure that the */
7467
    /* file is as big as needed */
7468
0
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
7469
0
    {
7470
0
        uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
7471
0
        uint64_t filesize = TIFFGetFileSize(tif);
7472
0
        uint64_t last_bytecount =
7473
0
            TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
7474
0
        if (last_offset > filesize || last_bytecount > filesize - last_offset)
7475
0
        {
7476
0
            return;
7477
0
        }
7478
0
    }
7479
7480
0
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
7481
0
}
7482
7483
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
7484
static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
7485
0
{
7486
0
    return a + b;
7487
0
}
7488
7489
/* Read the value of [Strip|Tile]Offset or [Strip|Tile]ByteCount around
7490
 * strip/tile of number strile. Also fetch the neighbouring values using a
7491
 * 4096 byte page size.
7492
 */
7493
static int _TIFFPartialReadStripArray(TIFF *tif, TIFFDirEntry *dirent,
7494
                                      int strile, uint64_t *panVals)
7495
0
{
7496
0
    static const char module[] = "_TIFFPartialReadStripArray";
7497
0
#define IO_CACHE_PAGE_SIZE 4096
7498
7499
0
    size_t sizeofval;
7500
0
    const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
7501
0
    int sizeofvalint;
7502
0
    uint64_t nBaseOffset;
7503
0
    uint64_t nOffset;
7504
0
    uint64_t nOffsetStartPage;
7505
0
    uint64_t nOffsetEndPage;
7506
0
    tmsize_t nToRead;
7507
0
    tmsize_t nRead;
7508
0
    uint64_t nLastStripOffset;
7509
0
    int iStartBefore;
7510
0
    int i;
7511
0
    const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
7512
0
    unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
7513
7514
0
    assert(dirent->tdir_count > 4);
7515
7516
0
    if (dirent->tdir_type == TIFF_SHORT)
7517
0
    {
7518
0
        sizeofval = sizeof(uint16_t);
7519
0
    }
7520
0
    else if (dirent->tdir_type == TIFF_LONG)
7521
0
    {
7522
0
        sizeofval = sizeof(uint32_t);
7523
0
    }
7524
0
    else if (dirent->tdir_type == TIFF_LONG8)
7525
0
    {
7526
0
        sizeofval = sizeof(uint64_t);
7527
0
    }
7528
0
    else if (dirent->tdir_type == TIFF_SLONG8)
7529
0
    {
7530
        /* Non conformant but used by some images as in */
7531
        /* https://github.com/OSGeo/gdal/issues/2165 */
7532
0
        sizeofval = sizeof(int64_t);
7533
0
    }
7534
0
    else
7535
0
    {
7536
0
        TIFFErrorExtR(tif, module,
7537
0
                      "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
7538
0
        panVals[strile] = 0;
7539
0
        return 0;
7540
0
    }
7541
0
    sizeofvalint = (int)(sizeofval);
7542
7543
0
    if (tif->tif_flags & TIFF_BIGTIFF)
7544
0
    {
7545
0
        uint64_t offset = dirent->tdir_offset.toff_long8;
7546
0
        if (bSwab)
7547
0
            TIFFSwabLong8(&offset);
7548
0
        nBaseOffset = offset;
7549
0
    }
7550
0
    else
7551
0
    {
7552
0
        uint32_t offset = dirent->tdir_offset.toff_long;
7553
0
        if (bSwab)
7554
0
            TIFFSwabLong(&offset);
7555
0
        nBaseOffset = offset;
7556
0
    }
7557
    /* To avoid later unsigned integer overflows */
7558
0
    if (nBaseOffset > (uint64_t)INT64_MAX)
7559
0
    {
7560
0
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
7561
0
                      strile);
7562
0
        panVals[strile] = 0;
7563
0
        return 0;
7564
0
    }
7565
0
    nOffset = nBaseOffset + sizeofval * strile;
7566
0
    nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
7567
0
    nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
7568
7569
0
    if (nOffset + sizeofval > nOffsetEndPage)
7570
0
        nOffsetEndPage += IO_CACHE_PAGE_SIZE;
7571
0
#undef IO_CACHE_PAGE_SIZE
7572
7573
0
    nLastStripOffset = nBaseOffset + arraySize * sizeofval;
7574
0
    if (nLastStripOffset < nOffsetEndPage)
7575
0
        nOffsetEndPage = nLastStripOffset;
7576
0
    if (nOffsetStartPage >= nOffsetEndPage)
7577
0
    {
7578
0
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
7579
0
                      strile);
7580
0
        panVals[strile] = 0;
7581
0
        return 0;
7582
0
    }
7583
0
    if (!SeekOK(tif, nOffsetStartPage))
7584
0
    {
7585
0
        panVals[strile] = 0;
7586
0
        return 0;
7587
0
    }
7588
7589
0
    nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
7590
0
    nRead = TIFFReadFile(tif, buffer, nToRead);
7591
0
    if (nRead < nToRead)
7592
0
    {
7593
0
        TIFFErrorExtR(tif, module,
7594
0
                      "Cannot read offset/size for strile around ~%d", strile);
7595
0
        return 0;
7596
0
    }
7597
0
    iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
7598
0
    if (strile + iStartBefore < 0)
7599
0
        iStartBefore = -strile;
7600
0
    for (i = iStartBefore;
7601
0
         (uint32_t)(strile + i) < arraySize &&
7602
0
         _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
7603
0
             nOffsetEndPage;
7604
0
         ++i)
7605
0
    {
7606
0
        if (dirent->tdir_type == TIFF_SHORT)
7607
0
        {
7608
0
            uint16_t val;
7609
0
            memcpy(&val,
7610
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
7611
0
                   sizeof(val));
7612
0
            if (bSwab)
7613
0
                TIFFSwabShort(&val);
7614
0
            panVals[strile + i] = val;
7615
0
        }
7616
0
        else if (dirent->tdir_type == TIFF_LONG)
7617
0
        {
7618
0
            uint32_t val;
7619
0
            memcpy(&val,
7620
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
7621
0
                   sizeof(val));
7622
0
            if (bSwab)
7623
0
                TIFFSwabLong(&val);
7624
0
            panVals[strile + i] = val;
7625
0
        }
7626
0
        else if (dirent->tdir_type == TIFF_LONG8)
7627
0
        {
7628
0
            uint64_t val;
7629
0
            memcpy(&val,
7630
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
7631
0
                   sizeof(val));
7632
0
            if (bSwab)
7633
0
                TIFFSwabLong8(&val);
7634
0
            panVals[strile + i] = val;
7635
0
        }
7636
0
        else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
7637
0
        {
7638
            /* Non conformant data type */
7639
0
            int64_t val;
7640
0
            memcpy(&val,
7641
0
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
7642
0
                   sizeof(val));
7643
0
            if (bSwab)
7644
0
                TIFFSwabLong8((uint64_t *)&val);
7645
0
            panVals[strile + i] = (uint64_t)val;
7646
0
        }
7647
0
    }
7648
0
    return 1;
7649
0
}
7650
7651
static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
7652
                                 TIFFDirEntry *dirent, uint64_t **parray)
7653
0
{
7654
0
    static const char module[] = "_TIFFFetchStrileValue";
7655
0
    TIFFDirectory *td = &tif->tif_dir;
7656
0
    if (strile >= dirent->tdir_count)
7657
0
    {
7658
0
        return 0;
7659
0
    }
7660
0
    if (strile >= td->td_stripoffsetbyteallocsize)
7661
0
    {
7662
0
        uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
7663
0
        uint32_t nStripArrayAllocNew;
7664
0
        uint64_t nArraySize64;
7665
0
        size_t nArraySize;
7666
0
        uint64_t *offsetArray;
7667
0
        uint64_t *bytecountArray;
7668
7669
0
        if (strile > 1000000)
7670
0
        {
7671
0
            uint64_t filesize = TIFFGetFileSize(tif);
7672
            /* Avoid excessive memory allocation attempt */
7673
            /* For such a big blockid we need at least a TIFF_LONG per strile */
7674
            /* for the offset array. */
7675
0
            if (strile > filesize / sizeof(uint32_t))
7676
0
            {
7677
0
                TIFFErrorExtR(tif, module, "File too short");
7678
0
                return 0;
7679
0
            }
7680
0
        }
7681
7682
0
        if (td->td_stripoffsetbyteallocsize == 0 &&
7683
0
            td->td_nstrips < 1024 * 1024)
7684
0
        {
7685
0
            nStripArrayAllocNew = td->td_nstrips;
7686
0
        }
7687
0
        else
7688
0
        {
7689
0
#define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
7690
0
#define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
7691
0
            nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
7692
0
            if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
7693
0
                nStripArrayAllocNew *= 2;
7694
0
            nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
7695
0
        }
7696
0
        assert(strile < nStripArrayAllocNew);
7697
0
        nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
7698
0
        nArraySize = (size_t)(nArraySize64);
7699
#if SIZEOF_SIZE_T == 4
7700
        if (nArraySize != nArraySize64)
7701
        {
7702
            TIFFErrorExtR(tif, module,
7703
                          "Cannot allocate strip offset and bytecount arrays");
7704
            return 0;
7705
        }
7706
#endif
7707
0
        offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
7708
0
                                                   nArraySize));
7709
0
        bytecountArray = (uint64_t *)(_TIFFreallocExt(
7710
0
            tif, td->td_stripbytecount_p, nArraySize));
7711
0
        if (offsetArray)
7712
0
            td->td_stripoffset_p = offsetArray;
7713
0
        if (bytecountArray)
7714
0
            td->td_stripbytecount_p = bytecountArray;
7715
0
        if (offsetArray && bytecountArray)
7716
0
        {
7717
0
            td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
7718
            /* Initialize new entries to ~0 / -1 */
7719
            /* coverity[overrun-buffer-arg] */
7720
0
            memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
7721
0
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
7722
0
                       sizeof(uint64_t));
7723
            /* coverity[overrun-buffer-arg] */
7724
0
            memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
7725
0
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
7726
0
                       sizeof(uint64_t));
7727
0
        }
7728
0
        else
7729
0
        {
7730
0
            TIFFErrorExtR(tif, module,
7731
0
                          "Cannot allocate strip offset and bytecount arrays");
7732
0
            _TIFFfreeExt(tif, td->td_stripoffset_p);
7733
0
            td->td_stripoffset_p = NULL;
7734
0
            _TIFFfreeExt(tif, td->td_stripbytecount_p);
7735
0
            td->td_stripbytecount_p = NULL;
7736
0
            td->td_stripoffsetbyteallocsize = 0;
7737
0
        }
7738
0
    }
7739
0
    if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
7740
0
        return 0;
7741
7742
0
    if (~((*parray)[strile]) == 0)
7743
0
    {
7744
0
        if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray))
7745
0
        {
7746
0
            (*parray)[strile] = 0;
7747
0
            return 0;
7748
0
        }
7749
0
    }
7750
7751
0
    return 1;
7752
0
}
7753
7754
static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
7755
                                                     TIFFDirEntry *dirent,
7756
                                                     uint64_t **parray,
7757
                                                     int *pbErr)
7758
1.33M
{
7759
1.33M
    TIFFDirectory *td = &tif->tif_dir;
7760
1.33M
    if (pbErr)
7761
0
        *pbErr = 0;
7762
1.33M
    if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
7763
1.33M
        !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
7764
0
    {
7765
0
        if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) ||
7766
            /* If the values may fit in the toff_long/toff_long8 member */
7767
            /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
7768
0
            dirent->tdir_count <= 4)
7769
0
        {
7770
0
            if (!_TIFFFillStriles(tif))
7771
0
            {
7772
0
                if (pbErr)
7773
0
                    *pbErr = 1;
7774
                /* Do not return, as we want this function to always */
7775
                /* return the same value if called several times with */
7776
                /* the same arguments */
7777
0
            }
7778
0
        }
7779
0
        else
7780
0
        {
7781
0
            if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
7782
0
            {
7783
0
                if (pbErr)
7784
0
                    *pbErr = 1;
7785
0
                return 0;
7786
0
            }
7787
0
        }
7788
0
    }
7789
1.33M
    if (*parray == NULL || strile >= td->td_nstrips)
7790
0
    {
7791
0
        if (pbErr)
7792
0
            *pbErr = 1;
7793
0
        return 0;
7794
0
    }
7795
1.33M
    return (*parray)[strile];
7796
1.33M
}
7797
7798
/* Return the value of the TileOffsets/StripOffsets array for the specified
7799
 * tile/strile */
7800
uint64_t TIFFGetStrileOffset(TIFF *tif, uint32_t strile)
7801
860k
{
7802
860k
    return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
7803
860k
}
7804
7805
/* Return the value of the TileOffsets/StripOffsets array for the specified
7806
 * tile/strile */
7807
uint64_t TIFFGetStrileOffsetWithErr(TIFF *tif, uint32_t strile, int *pbErr)
7808
860k
{
7809
860k
    TIFFDirectory *td = &tif->tif_dir;
7810
860k
    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
7811
860k
                                                &(td->td_stripoffset_entry),
7812
860k
                                                &(td->td_stripoffset_p), pbErr);
7813
860k
}
7814
7815
/* Return the value of the TileByteCounts/StripByteCounts array for the
7816
 * specified tile/strile */
7817
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
7818
470k
{
7819
470k
    return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
7820
470k
}
7821
7822
/* Return the value of the TileByteCounts/StripByteCounts array for the
7823
 * specified tile/strile */
7824
uint64_t TIFFGetStrileByteCountWithErr(TIFF *tif, uint32_t strile, int *pbErr)
7825
470k
{
7826
470k
    TIFFDirectory *td = &tif->tif_dir;
7827
470k
    return _TIFFGetStrileOffsetOrByteCountValue(
7828
470k
        tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
7829
470k
        pbErr);
7830
470k
}
7831
7832
5
int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
7833
7834
static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
7835
298
{
7836
298
    register TIFFDirectory *td = &tif->tif_dir;
7837
298
    int return_value = 1;
7838
7839
    /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
7840
298
    if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
7841
298
        (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
7842
298
        return 1;
7843
7844
0
    if (tif->tif_flags & TIFF_LAZYSTRILELOAD)
7845
0
    {
7846
        /* In case of lazy loading, reload completely the arrays */
7847
0
        _TIFFfreeExt(tif, td->td_stripoffset_p);
7848
0
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
7849
0
        td->td_stripoffset_p = NULL;
7850
0
        td->td_stripbytecount_p = NULL;
7851
0
        td->td_stripoffsetbyteallocsize = 0;
7852
0
        tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;
7853
0
    }
7854
7855
    /* If stripoffset array is already loaded, exit with success */
7856
0
    if (td->td_stripoffset_p != NULL)
7857
0
        return 1;
7858
7859
    /* If tdir_count was canceled, then we already got there, but in error */
7860
0
    if (td->td_stripoffset_entry.tdir_count == 0)
7861
0
        return 0;
7862
7863
0
    if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
7864
0
                             &td->td_stripoffset_p))
7865
0
    {
7866
0
        return_value = 0;
7867
0
    }
7868
7869
0
    if (loadStripByteCount &&
7870
0
        !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
7871
0
                             td->td_nstrips, &td->td_stripbytecount_p))
7872
0
    {
7873
0
        return_value = 0;
7874
0
    }
7875
7876
0
    _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
7877
0
    _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
7878
7879
#ifdef STRIPBYTECOUNTSORTED_UNUSED
7880
    if (tif->tif_dir.td_nstrips > 1 && return_value == 1)
7881
    {
7882
        uint32_t strip;
7883
7884
        tif->tif_dir.td_stripbytecountsorted = 1;
7885
        for (strip = 1; strip < tif->tif_dir.td_nstrips; strip++)
7886
        {
7887
            if (tif->tif_dir.td_stripoffset_p[strip - 1] >
7888
                tif->tif_dir.td_stripoffset_p[strip])
7889
            {
7890
                tif->tif_dir.td_stripbytecountsorted = 0;
7891
                break;
7892
            }
7893
        }
7894
    }
7895
#endif
7896
7897
0
    return return_value;
7898
0
}