Coverage Report

Created: 2025-06-22 07:10

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