• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OSGeo / gdal / 15992523769

01 Jul 2025 07:16AM UTC coverage: 71.09% (-0.004%) from 71.094%
15992523769

Pull #12682

github

web-flow
Merge 1383a7df3 into f6ed9706f
Pull Request #12682: Bump msys2/setup-msys2 from 2.27.0 to 2.28.0

574092 of 807557 relevant lines covered (71.09%)

250020.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

34.03
/frmts/gtiff/libtiff/tif_dirread.c
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
#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)
27,398✔
283
{
284
    UInt64Aligned_t result;
285

286
    result.c[0] = value[0];
27,398✔
287
    result.c[1] = value[1];
27,398✔
288
    result.c[2] = value[2];
27,398✔
289
    result.c[3] = value[3];
27,398✔
290
    result.c[4] = value[4];
27,398✔
291
    result.c[5] = value[5];
27,398✔
292
    result.c[6] = value[6];
27,398✔
293
    result.c[7] = value[7];
27,398✔
294

295
    return result.l;
27,398✔
296
}
297

298
static enum TIFFReadDirEntryErr
299
TIFFReadDirEntryByte(TIFF *tif, TIFFDirEntry *direntry, uint8_t *value)
×
300
{
301
    enum TIFFReadDirEntryErr err;
302
    if (direntry->tdir_count != 1)
×
303
        return (TIFFReadDirEntryErrCount);
×
304
    switch (direntry->tdir_type)
×
305
    {
306
        case TIFF_BYTE:
×
307
        case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
308
                                field_readcount==1 */
309
            TIFFReadDirEntryCheckedByte(tif, direntry, value);
×
310
            return (TIFFReadDirEntryErrOk);
×
311
        case TIFF_SBYTE:
×
312
        {
313
            int8_t m;
314
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
315
            err = TIFFReadDirEntryCheckRangeByteSbyte(m);
×
316
            if (err != TIFFReadDirEntryErrOk)
×
317
                return (err);
×
318
            *value = (uint8_t)m;
×
319
            return (TIFFReadDirEntryErrOk);
×
320
        }
321
        case TIFF_SHORT:
×
322
        {
323
            uint16_t m;
324
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
325
            err = TIFFReadDirEntryCheckRangeByteShort(m);
×
326
            if (err != TIFFReadDirEntryErrOk)
×
327
                return (err);
×
328
            *value = (uint8_t)m;
×
329
            return (TIFFReadDirEntryErrOk);
×
330
        }
331
        case TIFF_SSHORT:
×
332
        {
333
            int16_t m;
334
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
335
            err = TIFFReadDirEntryCheckRangeByteSshort(m);
×
336
            if (err != TIFFReadDirEntryErrOk)
×
337
                return (err);
×
338
            *value = (uint8_t)m;
×
339
            return (TIFFReadDirEntryErrOk);
×
340
        }
341
        case TIFF_LONG:
×
342
        {
343
            uint32_t m;
344
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
345
            err = TIFFReadDirEntryCheckRangeByteLong(m);
×
346
            if (err != TIFFReadDirEntryErrOk)
×
347
                return (err);
×
348
            *value = (uint8_t)m;
×
349
            return (TIFFReadDirEntryErrOk);
×
350
        }
351
        case TIFF_SLONG:
×
352
        {
353
            int32_t m;
354
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
355
            err = TIFFReadDirEntryCheckRangeByteSlong(m);
×
356
            if (err != TIFFReadDirEntryErrOk)
×
357
                return (err);
×
358
            *value = (uint8_t)m;
×
359
            return (TIFFReadDirEntryErrOk);
×
360
        }
361
        case TIFF_LONG8:
×
362
        {
363
            uint64_t m;
364
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
365
            if (err != TIFFReadDirEntryErrOk)
×
366
                return (err);
×
367
            err = TIFFReadDirEntryCheckRangeByteLong8(m);
×
368
            if (err != TIFFReadDirEntryErrOk)
×
369
                return (err);
×
370
            *value = (uint8_t)m;
×
371
            return (TIFFReadDirEntryErrOk);
×
372
        }
373
        case TIFF_SLONG8:
×
374
        {
375
            int64_t m;
376
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
377
            if (err != TIFFReadDirEntryErrOk)
×
378
                return (err);
×
379
            err = TIFFReadDirEntryCheckRangeByteSlong8(m);
×
380
            if (err != TIFFReadDirEntryErrOk)
×
381
                return (err);
×
382
            *value = (uint8_t)m;
×
383
            return (TIFFReadDirEntryErrOk);
×
384
        }
385
        default:
×
386
            return (TIFFReadDirEntryErrType);
×
387
    }
388
}
389

390
static enum TIFFReadDirEntryErr
391
TIFFReadDirEntrySbyte(TIFF *tif, TIFFDirEntry *direntry, int8_t *value)
×
392
{
393
    enum TIFFReadDirEntryErr err;
394
    if (direntry->tdir_count != 1)
×
395
        return (TIFFReadDirEntryErrCount);
×
396
    switch (direntry->tdir_type)
×
397
    {
398
        case TIFF_BYTE:
×
399
        case TIFF_UNDEFINED: /* Support to read TIFF_UNDEFINED with
400
                                field_readcount==1 */
401
        {
402
            uint8_t m;
403
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
404
            err = TIFFReadDirEntryCheckRangeSbyteByte(m);
×
405
            if (err != TIFFReadDirEntryErrOk)
×
406
                return (err);
×
407
            *value = (int8_t)m;
×
408
            return (TIFFReadDirEntryErrOk);
×
409
        }
410
        case TIFF_SBYTE:
×
411
        {
412
            TIFFReadDirEntryCheckedSbyte(tif, direntry, value);
×
413
            return (TIFFReadDirEntryErrOk);
×
414
        }
415
        case TIFF_SHORT:
×
416
        {
417
            uint16_t m;
418
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
419
            err = TIFFReadDirEntryCheckRangeSbyteShort(m);
×
420
            if (err != TIFFReadDirEntryErrOk)
×
421
                return (err);
×
422
            *value = (int8_t)m;
×
423
            return (TIFFReadDirEntryErrOk);
×
424
        }
425
        case TIFF_SSHORT:
×
426
        {
427
            int16_t m;
428
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
429
            err = TIFFReadDirEntryCheckRangeSbyteSshort(m);
×
430
            if (err != TIFFReadDirEntryErrOk)
×
431
                return (err);
×
432
            *value = (int8_t)m;
×
433
            return (TIFFReadDirEntryErrOk);
×
434
        }
435
        case TIFF_LONG:
×
436
        {
437
            uint32_t m;
438
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
439
            err = TIFFReadDirEntryCheckRangeSbyteLong(m);
×
440
            if (err != TIFFReadDirEntryErrOk)
×
441
                return (err);
×
442
            *value = (int8_t)m;
×
443
            return (TIFFReadDirEntryErrOk);
×
444
        }
445
        case TIFF_SLONG:
×
446
        {
447
            int32_t m;
448
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
449
            err = TIFFReadDirEntryCheckRangeSbyteSlong(m);
×
450
            if (err != TIFFReadDirEntryErrOk)
×
451
                return (err);
×
452
            *value = (int8_t)m;
×
453
            return (TIFFReadDirEntryErrOk);
×
454
        }
455
        case TIFF_LONG8:
×
456
        {
457
            uint64_t m;
458
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
459
            if (err != TIFFReadDirEntryErrOk)
×
460
                return (err);
×
461
            err = TIFFReadDirEntryCheckRangeSbyteLong8(m);
×
462
            if (err != TIFFReadDirEntryErrOk)
×
463
                return (err);
×
464
            *value = (int8_t)m;
×
465
            return (TIFFReadDirEntryErrOk);
×
466
        }
467
        case TIFF_SLONG8:
×
468
        {
469
            int64_t m;
470
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
471
            if (err != TIFFReadDirEntryErrOk)
×
472
                return (err);
×
473
            err = TIFFReadDirEntryCheckRangeSbyteSlong8(m);
×
474
            if (err != TIFFReadDirEntryErrOk)
×
475
                return (err);
×
476
            *value = (int8_t)m;
×
477
            return (TIFFReadDirEntryErrOk);
×
478
        }
479
        default:
×
480
            return (TIFFReadDirEntryErrType);
×
481
    }
482
} /*-- TIFFReadDirEntrySbyte() --*/
483

484
static enum TIFFReadDirEntryErr
485
TIFFReadDirEntryShort(TIFF *tif, TIFFDirEntry *direntry, uint16_t *value)
317,003✔
486
{
487
    enum TIFFReadDirEntryErr err;
488
    if (direntry->tdir_count != 1)
317,003✔
489
        return (TIFFReadDirEntryErrCount);
31,414✔
490
    switch (direntry->tdir_type)
285,589✔
491
    {
492
        case TIFF_BYTE:
×
493
        {
494
            uint8_t m;
495
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
496
            *value = (uint16_t)m;
×
497
            return (TIFFReadDirEntryErrOk);
×
498
        }
499
        case TIFF_SBYTE:
×
500
        {
501
            int8_t m;
502
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
503
            err = TIFFReadDirEntryCheckRangeShortSbyte(m);
×
504
            if (err != TIFFReadDirEntryErrOk)
×
505
                return (err);
×
506
            *value = (uint16_t)m;
×
507
            return (TIFFReadDirEntryErrOk);
×
508
        }
509
        case TIFF_SHORT:
285,518✔
510
            TIFFReadDirEntryCheckedShort(tif, direntry, value);
285,518✔
511
            return (TIFFReadDirEntryErrOk);
285,507✔
512
        case TIFF_SSHORT:
×
513
        {
514
            int16_t m;
515
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
516
            err = TIFFReadDirEntryCheckRangeShortSshort(m);
×
517
            if (err != TIFFReadDirEntryErrOk)
×
518
                return (err);
×
519
            *value = (uint16_t)m;
×
520
            return (TIFFReadDirEntryErrOk);
×
521
        }
522
        case TIFF_LONG:
1✔
523
        {
524
            uint32_t m;
525
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
1✔
526
            err = TIFFReadDirEntryCheckRangeShortLong(m);
1✔
527
            if (err != TIFFReadDirEntryErrOk)
1✔
528
                return (err);
×
529
            *value = (uint16_t)m;
1✔
530
            return (TIFFReadDirEntryErrOk);
1✔
531
        }
532
        case TIFF_SLONG:
×
533
        {
534
            int32_t m;
535
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
536
            err = TIFFReadDirEntryCheckRangeShortSlong(m);
×
537
            if (err != TIFFReadDirEntryErrOk)
×
538
                return (err);
×
539
            *value = (uint16_t)m;
×
540
            return (TIFFReadDirEntryErrOk);
×
541
        }
542
        case TIFF_LONG8:
×
543
        {
544
            uint64_t m;
545
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
546
            if (err != TIFFReadDirEntryErrOk)
×
547
                return (err);
×
548
            err = TIFFReadDirEntryCheckRangeShortLong8(m);
×
549
            if (err != TIFFReadDirEntryErrOk)
×
550
                return (err);
×
551
            *value = (uint16_t)m;
×
552
            return (TIFFReadDirEntryErrOk);
×
553
        }
554
        case TIFF_SLONG8:
×
555
        {
556
            int64_t m;
557
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
558
            if (err != TIFFReadDirEntryErrOk)
×
559
                return (err);
×
560
            err = TIFFReadDirEntryCheckRangeShortSlong8(m);
×
561
            if (err != TIFFReadDirEntryErrOk)
×
562
                return (err);
×
563
            *value = (uint16_t)m;
×
564
            return (TIFFReadDirEntryErrOk);
×
565
        }
566
        default:
70✔
567
            return (TIFFReadDirEntryErrType);
70✔
568
    }
569
} /*-- TIFFReadDirEntryShort() --*/
570

571
static enum TIFFReadDirEntryErr
572
TIFFReadDirEntrySshort(TIFF *tif, TIFFDirEntry *direntry, int16_t *value)
×
573
{
574
    enum TIFFReadDirEntryErr err;
575
    if (direntry->tdir_count != 1)
×
576
        return (TIFFReadDirEntryErrCount);
×
577
    switch (direntry->tdir_type)
×
578
    {
579
        case TIFF_BYTE:
×
580
        {
581
            uint8_t m;
582
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
583
            *value = (int16_t)m;
×
584
            return (TIFFReadDirEntryErrOk);
×
585
        }
586
        case TIFF_SBYTE:
×
587
        {
588
            int8_t m;
589
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
590
            *value = (int16_t)m;
×
591
            return (TIFFReadDirEntryErrOk);
×
592
        }
593
        case TIFF_SHORT:
×
594
        {
595
            uint16_t m;
596
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
597
            err = TIFFReadDirEntryCheckRangeSshortShort(m);
×
598
            if (err != TIFFReadDirEntryErrOk)
×
599
                return (err);
×
600
            *value = (uint16_t)m;
×
601
            return (TIFFReadDirEntryErrOk);
×
602
        }
603
        case TIFF_SSHORT:
×
604
            TIFFReadDirEntryCheckedSshort(tif, direntry, value);
×
605
            return (TIFFReadDirEntryErrOk);
×
606
        case TIFF_LONG:
×
607
        {
608
            uint32_t m;
609
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
610
            err = TIFFReadDirEntryCheckRangeSshortLong(m);
×
611
            if (err != TIFFReadDirEntryErrOk)
×
612
                return (err);
×
613
            *value = (int16_t)m;
×
614
            return (TIFFReadDirEntryErrOk);
×
615
        }
616
        case TIFF_SLONG:
×
617
        {
618
            int32_t m;
619
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
620
            err = TIFFReadDirEntryCheckRangeSshortSlong(m);
×
621
            if (err != TIFFReadDirEntryErrOk)
×
622
                return (err);
×
623
            *value = (int16_t)m;
×
624
            return (TIFFReadDirEntryErrOk);
×
625
        }
626
        case TIFF_LONG8:
×
627
        {
628
            uint64_t m;
629
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
630
            if (err != TIFFReadDirEntryErrOk)
×
631
                return (err);
×
632
            err = TIFFReadDirEntryCheckRangeSshortLong8(m);
×
633
            if (err != TIFFReadDirEntryErrOk)
×
634
                return (err);
×
635
            *value = (int16_t)m;
×
636
            return (TIFFReadDirEntryErrOk);
×
637
        }
638
        case TIFF_SLONG8:
×
639
        {
640
            int64_t m;
641
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
642
            if (err != TIFFReadDirEntryErrOk)
×
643
                return (err);
×
644
            err = TIFFReadDirEntryCheckRangeSshortSlong8(m);
×
645
            if (err != TIFFReadDirEntryErrOk)
×
646
                return (err);
×
647
            *value = (int16_t)m;
×
648
            return (TIFFReadDirEntryErrOk);
×
649
        }
650
        default:
×
651
            return (TIFFReadDirEntryErrType);
×
652
    }
653
} /*-- TIFFReadDirEntrySshort() --*/
654

655
static enum TIFFReadDirEntryErr
656
TIFFReadDirEntryLong(TIFF *tif, TIFFDirEntry *direntry, uint32_t *value)
170,785✔
657
{
658
    enum TIFFReadDirEntryErr err;
659
    if (direntry->tdir_count != 1)
170,785✔
660
        return (TIFFReadDirEntryErrCount);
×
661
    switch (direntry->tdir_type)
170,785✔
662
    {
663
        case TIFF_BYTE:
×
664
        {
665
            uint8_t m;
666
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
667
            *value = (uint32_t)m;
×
668
            return (TIFFReadDirEntryErrOk);
×
669
        }
670
        case TIFF_SBYTE:
×
671
        {
672
            int8_t m;
673
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
674
            err = TIFFReadDirEntryCheckRangeLongSbyte(m);
×
675
            if (err != TIFFReadDirEntryErrOk)
×
676
                return (err);
×
677
            *value = (uint32_t)m;
×
678
            return (TIFFReadDirEntryErrOk);
×
679
        }
680
        case TIFF_SHORT:
164,787✔
681
        {
682
            uint16_t m;
683
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
164,787✔
684
            *value = (uint32_t)m;
164,795✔
685
            return (TIFFReadDirEntryErrOk);
164,795✔
686
        }
687
        case TIFF_SSHORT:
×
688
        {
689
            int16_t m;
690
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
691
            err = TIFFReadDirEntryCheckRangeLongSshort(m);
×
692
            if (err != TIFFReadDirEntryErrOk)
×
693
                return (err);
×
694
            *value = (uint32_t)m;
×
695
            return (TIFFReadDirEntryErrOk);
×
696
        }
697
        case TIFF_LONG:
5,957✔
698
            TIFFReadDirEntryCheckedLong(tif, direntry, value);
5,957✔
699
            return (TIFFReadDirEntryErrOk);
5,957✔
700
        case TIFF_SLONG:
×
701
        {
702
            int32_t m;
703
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
704
            err = TIFFReadDirEntryCheckRangeLongSlong(m);
×
705
            if (err != TIFFReadDirEntryErrOk)
×
706
                return (err);
×
707
            *value = (uint32_t)m;
×
708
            return (TIFFReadDirEntryErrOk);
×
709
        }
710
        case TIFF_LONG8:
×
711
        {
712
            uint64_t m;
713
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
714
            if (err != TIFFReadDirEntryErrOk)
×
715
                return (err);
×
716
            err = TIFFReadDirEntryCheckRangeLongLong8(m);
×
717
            if (err != TIFFReadDirEntryErrOk)
×
718
                return (err);
×
719
            *value = (uint32_t)m;
×
720
            return (TIFFReadDirEntryErrOk);
×
721
        }
722
        case TIFF_SLONG8:
×
723
        {
724
            int64_t m;
725
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
726
            if (err != TIFFReadDirEntryErrOk)
×
727
                return (err);
×
728
            err = TIFFReadDirEntryCheckRangeLongSlong8(m);
×
729
            if (err != TIFFReadDirEntryErrOk)
×
730
                return (err);
×
731
            *value = (uint32_t)m;
×
732
            return (TIFFReadDirEntryErrOk);
×
733
        }
734
        default:
41✔
735
            return (TIFFReadDirEntryErrType);
41✔
736
    }
737
} /*-- TIFFReadDirEntryLong() --*/
738

739
static enum TIFFReadDirEntryErr
740
TIFFReadDirEntrySlong(TIFF *tif, TIFFDirEntry *direntry, int32_t *value)
×
741
{
742
    enum TIFFReadDirEntryErr err;
743
    if (direntry->tdir_count != 1)
×
744
        return (TIFFReadDirEntryErrCount);
×
745
    switch (direntry->tdir_type)
×
746
    {
747
        case TIFF_BYTE:
×
748
        {
749
            uint8_t m;
750
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
751
            *value = (int32_t)m;
×
752
            return (TIFFReadDirEntryErrOk);
×
753
        }
754
        case TIFF_SBYTE:
×
755
        {
756
            int8_t m;
757
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
758
            *value = (int32_t)m;
×
759
            return (TIFFReadDirEntryErrOk);
×
760
        }
761
        case TIFF_SHORT:
×
762
        {
763
            uint16_t m;
764
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
765
            *value = (int32_t)m;
×
766
            return (TIFFReadDirEntryErrOk);
×
767
        }
768
        case TIFF_SSHORT:
×
769
        {
770
            int16_t m;
771
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
772
            *value = (int32_t)m;
×
773
            return (TIFFReadDirEntryErrOk);
×
774
        }
775
        case TIFF_LONG:
×
776
        {
777
            uint32_t m;
778
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
779
            err = TIFFReadDirEntryCheckRangeSlongLong(m);
×
780
            if (err != TIFFReadDirEntryErrOk)
×
781
                return (err);
×
782
            *value = (int32_t)m;
×
783
            return (TIFFReadDirEntryErrOk);
×
784
        }
785
        case TIFF_SLONG:
×
786
            TIFFReadDirEntryCheckedSlong(tif, direntry, value);
×
787
            return (TIFFReadDirEntryErrOk);
×
788
        case TIFF_LONG8:
×
789
        {
790
            uint64_t m;
791
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
792
            if (err != TIFFReadDirEntryErrOk)
×
793
                return (err);
×
794
            err = TIFFReadDirEntryCheckRangeSlongLong8(m);
×
795
            if (err != TIFFReadDirEntryErrOk)
×
796
                return (err);
×
797
            *value = (int32_t)m;
×
798
            return (TIFFReadDirEntryErrOk);
×
799
        }
800
        case TIFF_SLONG8:
×
801
        {
802
            int64_t m;
803
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
804
            if (err != TIFFReadDirEntryErrOk)
×
805
                return (err);
×
806
            err = TIFFReadDirEntryCheckRangeSlongSlong8(m);
×
807
            if (err != TIFFReadDirEntryErrOk)
×
808
                return (err);
×
809
            *value = (int32_t)m;
×
810
            return (TIFFReadDirEntryErrOk);
×
811
        }
812
        default:
×
813
            return (TIFFReadDirEntryErrType);
×
814
    }
815
} /*-- TIFFReadDirEntrySlong() --*/
816

817
static enum TIFFReadDirEntryErr
818
TIFFReadDirEntryLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
×
819
{
820
    enum TIFFReadDirEntryErr err;
821
    if (direntry->tdir_count != 1)
×
822
        return (TIFFReadDirEntryErrCount);
×
823
    switch (direntry->tdir_type)
×
824
    {
825
        case TIFF_BYTE:
×
826
        {
827
            uint8_t m;
828
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
829
            *value = (uint64_t)m;
×
830
            return (TIFFReadDirEntryErrOk);
×
831
        }
832
        case TIFF_SBYTE:
×
833
        {
834
            int8_t m;
835
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
836
            err = TIFFReadDirEntryCheckRangeLong8Sbyte(m);
×
837
            if (err != TIFFReadDirEntryErrOk)
×
838
                return (err);
×
839
            *value = (uint64_t)m;
×
840
            return (TIFFReadDirEntryErrOk);
×
841
        }
842
        case TIFF_SHORT:
×
843
        {
844
            uint16_t m;
845
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
846
            *value = (uint64_t)m;
×
847
            return (TIFFReadDirEntryErrOk);
×
848
        }
849
        case TIFF_SSHORT:
×
850
        {
851
            int16_t m;
852
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
853
            err = TIFFReadDirEntryCheckRangeLong8Sshort(m);
×
854
            if (err != TIFFReadDirEntryErrOk)
×
855
                return (err);
×
856
            *value = (uint64_t)m;
×
857
            return (TIFFReadDirEntryErrOk);
×
858
        }
859
        case TIFF_LONG:
×
860
        {
861
            uint32_t m;
862
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
863
            *value = (uint64_t)m;
×
864
            return (TIFFReadDirEntryErrOk);
×
865
        }
866
        case TIFF_SLONG:
×
867
        {
868
            int32_t m;
869
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
870
            err = TIFFReadDirEntryCheckRangeLong8Slong(m);
×
871
            if (err != TIFFReadDirEntryErrOk)
×
872
                return (err);
×
873
            *value = (uint64_t)m;
×
874
            return (TIFFReadDirEntryErrOk);
×
875
        }
876
        case TIFF_LONG8:
×
877
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
×
878
            return (err);
×
879
        case TIFF_SLONG8:
×
880
        {
881
            int64_t m;
882
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
883
            if (err != TIFFReadDirEntryErrOk)
×
884
                return (err);
×
885
            err = TIFFReadDirEntryCheckRangeLong8Slong8(m);
×
886
            if (err != TIFFReadDirEntryErrOk)
×
887
                return (err);
×
888
            *value = (uint64_t)m;
×
889
            return (TIFFReadDirEntryErrOk);
×
890
        }
891
        default:
×
892
            return (TIFFReadDirEntryErrType);
×
893
    }
894
} /*-- TIFFReadDirEntryLong8() --*/
895

896
static enum TIFFReadDirEntryErr
897
TIFFReadDirEntrySlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
×
898
{
899
    enum TIFFReadDirEntryErr err;
900
    if (direntry->tdir_count != 1)
×
901
        return (TIFFReadDirEntryErrCount);
×
902
    switch (direntry->tdir_type)
×
903
    {
904
        case TIFF_BYTE:
×
905
        {
906
            uint8_t m;
907
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
908
            *value = (int64_t)m;
×
909
            return (TIFFReadDirEntryErrOk);
×
910
        }
911
        case TIFF_SBYTE:
×
912
        {
913
            int8_t m;
914
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
915
            *value = (int64_t)m;
×
916
            return (TIFFReadDirEntryErrOk);
×
917
        }
918
        case TIFF_SHORT:
×
919
        {
920
            uint16_t m;
921
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
922
            *value = (int64_t)m;
×
923
            return (TIFFReadDirEntryErrOk);
×
924
        }
925
        case TIFF_SSHORT:
×
926
        {
927
            int16_t m;
928
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
929
            *value = (int64_t)m;
×
930
            return (TIFFReadDirEntryErrOk);
×
931
        }
932
        case TIFF_LONG:
×
933
        {
934
            uint32_t m;
935
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
936
            *value = (int64_t)m;
×
937
            return (TIFFReadDirEntryErrOk);
×
938
        }
939
        case TIFF_SLONG:
×
940
        {
941
            int32_t m;
942
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
943
            *value = (int64_t)m;
×
944
            return (TIFFReadDirEntryErrOk);
×
945
        }
946
        case TIFF_LONG8:
×
947
        {
948
            uint64_t m;
949
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
950
            if (err != TIFFReadDirEntryErrOk)
×
951
                return (err);
×
952
            err = TIFFReadDirEntryCheckRangeSlong8Long8(m);
×
953
            if (err != TIFFReadDirEntryErrOk)
×
954
                return (err);
×
955
            *value = (int64_t)m;
×
956
            return (TIFFReadDirEntryErrOk);
×
957
        }
958
        case TIFF_SLONG8:
×
959
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, value);
×
960
            return (err);
×
961
        default:
×
962
            return (TIFFReadDirEntryErrType);
×
963
    }
964
} /*-- TIFFReadDirEntrySlong8() --*/
965

966
static enum TIFFReadDirEntryErr
967
TIFFReadDirEntryFloat(TIFF *tif, TIFFDirEntry *direntry, float *value)
248✔
968
{
969
    enum TIFFReadDirEntryErr err;
970
    if (direntry->tdir_count != 1)
248✔
971
        return (TIFFReadDirEntryErrCount);
×
972
    switch (direntry->tdir_type)
248✔
973
    {
974
        case TIFF_BYTE:
×
975
        {
976
            uint8_t m;
977
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
978
            *value = (float)m;
×
979
            return (TIFFReadDirEntryErrOk);
×
980
        }
981
        case TIFF_SBYTE:
×
982
        {
983
            int8_t m;
984
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
985
            *value = (float)m;
×
986
            return (TIFFReadDirEntryErrOk);
×
987
        }
988
        case TIFF_SHORT:
×
989
        {
990
            uint16_t m;
991
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
992
            *value = (float)m;
×
993
            return (TIFFReadDirEntryErrOk);
×
994
        }
995
        case TIFF_SSHORT:
×
996
        {
997
            int16_t m;
998
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
999
            *value = (float)m;
×
1000
            return (TIFFReadDirEntryErrOk);
×
1001
        }
1002
        case TIFF_LONG:
×
1003
        {
1004
            uint32_t m;
1005
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
1006
            *value = (float)m;
×
1007
            return (TIFFReadDirEntryErrOk);
×
1008
        }
1009
        case TIFF_SLONG:
×
1010
        {
1011
            int32_t m;
1012
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
1013
            *value = (float)m;
×
1014
            return (TIFFReadDirEntryErrOk);
×
1015
        }
1016
        case TIFF_LONG8:
×
1017
        {
1018
            uint64_t m;
1019
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
1020
            if (err != TIFFReadDirEntryErrOk)
×
1021
                return (err);
×
1022
            *value = (float)m;
×
1023
            return (TIFFReadDirEntryErrOk);
×
1024
        }
1025
        case TIFF_SLONG8:
×
1026
        {
1027
            int64_t m;
1028
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
1029
            if (err != TIFFReadDirEntryErrOk)
×
1030
                return (err);
×
1031
            *value = (float)m;
×
1032
            return (TIFFReadDirEntryErrOk);
×
1033
        }
1034
        case TIFF_RATIONAL:
248✔
1035
        {
1036
            double m;
1037
            err = TIFFReadDirEntryCheckedRational(tif, direntry, &m);
248✔
1038
            if (err != TIFFReadDirEntryErrOk)
248✔
1039
                return (err);
×
1040
            *value = (float)m;
248✔
1041
            return (TIFFReadDirEntryErrOk);
248✔
1042
        }
1043
        case TIFF_SRATIONAL:
×
1044
        {
1045
            double m;
1046
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, &m);
×
1047
            if (err != TIFFReadDirEntryErrOk)
×
1048
                return (err);
×
1049
            *value = (float)m;
×
1050
            return (TIFFReadDirEntryErrOk);
×
1051
        }
1052
        case TIFF_FLOAT:
×
1053
            TIFFReadDirEntryCheckedFloat(tif, direntry, value);
×
1054
            return (TIFFReadDirEntryErrOk);
×
1055
        case TIFF_DOUBLE:
×
1056
        {
1057
            double m;
1058
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, &m);
×
1059
            if (err != TIFFReadDirEntryErrOk)
×
1060
                return (err);
×
1061
            if ((m > FLT_MAX) || (m < -FLT_MAX))
×
1062
                return (TIFFReadDirEntryErrRange);
×
1063
            *value = (float)m;
×
1064
            return (TIFFReadDirEntryErrOk);
×
1065
        }
1066
        default:
×
1067
            return (TIFFReadDirEntryErrType);
×
1068
    }
1069
}
1070

1071
static enum TIFFReadDirEntryErr
1072
TIFFReadDirEntryDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
×
1073
{
1074
    enum TIFFReadDirEntryErr err;
1075
    if (direntry->tdir_count != 1)
×
1076
        return (TIFFReadDirEntryErrCount);
×
1077
    switch (direntry->tdir_type)
×
1078
    {
1079
        case TIFF_BYTE:
×
1080
        {
1081
            uint8_t m;
1082
            TIFFReadDirEntryCheckedByte(tif, direntry, &m);
×
1083
            *value = (double)m;
×
1084
            return (TIFFReadDirEntryErrOk);
×
1085
        }
1086
        case TIFF_SBYTE:
×
1087
        {
1088
            int8_t m;
1089
            TIFFReadDirEntryCheckedSbyte(tif, direntry, &m);
×
1090
            *value = (double)m;
×
1091
            return (TIFFReadDirEntryErrOk);
×
1092
        }
1093
        case TIFF_SHORT:
×
1094
        {
1095
            uint16_t m;
1096
            TIFFReadDirEntryCheckedShort(tif, direntry, &m);
×
1097
            *value = (double)m;
×
1098
            return (TIFFReadDirEntryErrOk);
×
1099
        }
1100
        case TIFF_SSHORT:
×
1101
        {
1102
            int16_t m;
1103
            TIFFReadDirEntryCheckedSshort(tif, direntry, &m);
×
1104
            *value = (double)m;
×
1105
            return (TIFFReadDirEntryErrOk);
×
1106
        }
1107
        case TIFF_LONG:
×
1108
        {
1109
            uint32_t m;
1110
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
×
1111
            *value = (double)m;
×
1112
            return (TIFFReadDirEntryErrOk);
×
1113
        }
1114
        case TIFF_SLONG:
×
1115
        {
1116
            int32_t m;
1117
            TIFFReadDirEntryCheckedSlong(tif, direntry, &m);
×
1118
            *value = (double)m;
×
1119
            return (TIFFReadDirEntryErrOk);
×
1120
        }
1121
        case TIFF_LONG8:
×
1122
        {
1123
            uint64_t m;
1124
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, &m);
×
1125
            if (err != TIFFReadDirEntryErrOk)
×
1126
                return (err);
×
1127
            *value = (double)m;
×
1128
            return (TIFFReadDirEntryErrOk);
×
1129
        }
1130
        case TIFF_SLONG8:
×
1131
        {
1132
            int64_t m;
1133
            err = TIFFReadDirEntryCheckedSlong8(tif, direntry, &m);
×
1134
            if (err != TIFFReadDirEntryErrOk)
×
1135
                return (err);
×
1136
            *value = (double)m;
×
1137
            return (TIFFReadDirEntryErrOk);
×
1138
        }
1139
        case TIFF_RATIONAL:
×
1140
            err = TIFFReadDirEntryCheckedRational(tif, direntry, value);
×
1141
            return (err);
×
1142
        case TIFF_SRATIONAL:
×
1143
            err = TIFFReadDirEntryCheckedSrational(tif, direntry, value);
×
1144
            return (err);
×
1145
        case TIFF_FLOAT:
×
1146
        {
1147
            float m;
1148
            TIFFReadDirEntryCheckedFloat(tif, direntry, &m);
×
1149
            *value = (double)m;
×
1150
            return (TIFFReadDirEntryErrOk);
×
1151
        }
1152
        case TIFF_DOUBLE:
×
1153
            err = TIFFReadDirEntryCheckedDouble(tif, direntry, value);
×
1154
            return (err);
×
1155
        default:
×
1156
            return (TIFFReadDirEntryErrType);
×
1157
    }
1158
}
1159

1160
static enum TIFFReadDirEntryErr
1161
TIFFReadDirEntryIfd8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
8✔
1162
{
1163
    enum TIFFReadDirEntryErr err;
1164
    if (direntry->tdir_count != 1)
8✔
1165
        return (TIFFReadDirEntryErrCount);
×
1166
    switch (direntry->tdir_type)
8✔
1167
    {
1168
        case TIFF_LONG:
8✔
1169
        case TIFF_IFD:
1170
        {
1171
            uint32_t m;
1172
            TIFFReadDirEntryCheckedLong(tif, direntry, &m);
8✔
1173
            *value = (uint64_t)m;
8✔
1174
            return (TIFFReadDirEntryErrOk);
8✔
1175
        }
1176
        case TIFF_LONG8:
×
1177
        case TIFF_IFD8:
1178
            err = TIFFReadDirEntryCheckedLong8(tif, direntry, value);
×
1179
            return (err);
×
1180
        default:
×
1181
            return (TIFFReadDirEntryErrType);
×
1182
    }
1183
}
1184

1185
#define INITIAL_THRESHOLD (1024 * 1024)
1186
#define THRESHOLD_MULTIPLIER 10
1187
#define MAX_THRESHOLD                                                          \
1188
    (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER *      \
1189
     INITIAL_THRESHOLD)
1190

1191
static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(TIFF *tif,
172,803✔
1192
                                                               uint64_t offset,
1193
                                                               tmsize_t size,
1194
                                                               void **pdest)
1195
{
1196
#if SIZEOF_SIZE_T == 8
1197
    tmsize_t threshold = INITIAL_THRESHOLD;
172,803✔
1198
#endif
1199
    tmsize_t already_read = 0;
172,803✔
1200

1201
    assert(!isMapped(tif));
172,803✔
1202

1203
    if (!SeekOK(tif, offset))
172,803✔
1204
        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
    while (already_read < size)
345,585✔
1213
    {
1214
        void *new_dest;
1215
        tmsize_t bytes_read;
1216
        tmsize_t to_read = size - already_read;
172,793✔
1217
#if SIZEOF_SIZE_T == 8
1218
        if (to_read >= threshold && threshold < MAX_THRESHOLD)
172,793✔
1219
        {
1220
            to_read = threshold;
18✔
1221
            threshold *= THRESHOLD_MULTIPLIER;
18✔
1222
        }
1223
#endif
1224

1225
        new_dest =
1226
            (uint8_t *)_TIFFreallocExt(tif, *pdest, already_read + to_read);
172,793✔
1227
        if (new_dest == NULL)
172,841✔
1228
        {
1229
            TIFFErrorExtR(tif, tif->tif_name,
×
1230
                          "Failed to allocate memory for %s "
1231
                          "(%" TIFF_SSIZE_FORMAT
1232
                          " elements of %" TIFF_SSIZE_FORMAT " bytes each)",
1233
                          "TIFFReadDirEntryArray", (tmsize_t)1,
1234
                          already_read + to_read);
1235
            return TIFFReadDirEntryErrAlloc;
×
1236
        }
1237
        *pdest = new_dest;
172,841✔
1238

1239
        bytes_read = TIFFReadFile(tif, (char *)*pdest + already_read, to_read);
172,841✔
1240
        already_read += bytes_read;
172,818✔
1241
        if (bytes_read != to_read)
172,818✔
1242
        {
1243
            return TIFFReadDirEntryErrIo;
12✔
1244
        }
1245
    }
1246
    return TIFFReadDirEntryErrOk;
172,792✔
1247
}
1248

1249
/* Caution: if raising that value, make sure int32 / uint32 overflows can't
1250
 * occur elsewhere */
1251
#define MAX_SIZE_TAG_DATA 2147483647U
1252

1253
static enum TIFFReadDirEntryErr
1254
TIFFReadDirEntryArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
253,935✔
1255
                               uint32_t *count, uint32_t desttypesize,
1256
                               void **value, uint64_t maxcount)
1257
{
1258
    int typesize;
1259
    uint32_t datasize;
1260
    void *data;
1261
    uint64_t target_count64;
1262
    int original_datasize_clamped;
1263
    typesize = TIFFDataWidth(direntry->tdir_type);
253,935✔
1264

1265
    target_count64 =
253,879✔
1266
        (direntry->tdir_count > maxcount) ? maxcount : direntry->tdir_count;
253,879✔
1267

1268
    if ((target_count64 == 0) || (typesize == 0))
253,879✔
1269
    {
1270
        *value = 0;
×
1271
        return (TIFFReadDirEntryErrOk);
×
1272
    }
1273
    (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
    original_datasize_clamped =
253,896✔
1279
        ((direntry->tdir_count > 10) ? 10 : (int)direntry->tdir_count) *
253,896✔
1280
        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
    if ((uint64_t)(MAX_SIZE_TAG_DATA / typesize) < target_count64)
253,896✔
1288
        return (TIFFReadDirEntryErrSizesan);
×
1289
    if ((uint64_t)(MAX_SIZE_TAG_DATA / desttypesize) < target_count64)
253,896✔
1290
        return (TIFFReadDirEntryErrSizesan);
×
1291

1292
    *count = (uint32_t)target_count64;
253,896✔
1293
    datasize = (*count) * typesize;
253,896✔
1294
    assert((tmsize_t)datasize > 0);
253,896✔
1295

1296
    if (datasize > 100 * 1024 * 1024)
253,896✔
1297
    {
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
        const uint64_t filesize = TIFFGetFileSize(tif);
×
1302
        if (datasize > filesize)
×
1303
        {
1304
            TIFFWarningExtR(tif, "ReadDirEntryArray",
×
1305
                            "Requested memory size for tag %d (0x%x) %" PRIu32
1306
                            " is greater than filesize %" PRIu64
1307
                            ". Memory not allocated, tag not read",
1308
                            direntry->tdir_tag, direntry->tdir_tag, datasize,
×
1309
                            filesize);
1310
            return (TIFFReadDirEntryErrAlloc);
×
1311
        }
1312
    }
1313

1314
    if (isMapped(tif) && datasize > (uint64_t)tif->tif_size)
253,896✔
1315
        return TIFFReadDirEntryErrIo;
×
1316

1317
    if (!isMapped(tif) && (((tif->tif_flags & TIFF_BIGTIFF) && datasize > 8) ||
253,896✔
1318
                           (!(tif->tif_flags & TIFF_BIGTIFF) && datasize > 4)))
252,838✔
1319
    {
1320
        data = NULL;
172,753✔
1321
    }
1322
    else
1323
    {
1324
        data = _TIFFCheckMalloc(tif, *count, typesize, "ReadDirEntryArray");
81,143✔
1325
        if (data == 0)
81,169✔
1326
            return (TIFFReadDirEntryErrAlloc);
×
1327
    }
1328
    if (!(tif->tif_flags & TIFF_BIGTIFF))
253,922✔
1329
    {
1330
        /* Only the condition on original_datasize_clamped. The second
1331
         * one is implied, but Coverity Scan cannot see it. */
1332
        if (original_datasize_clamped <= 4 && datasize <= 4)
251,405✔
1333
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
79,703✔
1334
        else
1335
        {
1336
            enum TIFFReadDirEntryErr err;
1337
            uint32_t offset = direntry->tdir_offset.toff_long;
171,702✔
1338
            if (tif->tif_flags & TIFF_SWAB)
171,702✔
1339
                TIFFSwabLong(&offset);
1,941✔
1340
            if (isMapped(tif))
171,729✔
1341
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
32✔
1342
                                           (tmsize_t)datasize, data);
1343
            else
1344
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
171,697✔
1345
                                                     (tmsize_t)datasize, &data);
1346
            if (err != TIFFReadDirEntryErrOk)
171,760✔
1347
            {
1348
                _TIFFfreeExt(tif, data);
12✔
1349
                return (err);
12✔
1350
            }
1351
        }
1352
    }
1353
    else
1354
    {
1355
        /* See above comment for the Classic TIFF case */
1356
        if (original_datasize_clamped <= 8 && datasize <= 8)
2,517✔
1357
            _TIFFmemcpy(data, &direntry->tdir_offset, datasize);
1,417✔
1358
        else
1359
        {
1360
            enum TIFFReadDirEntryErr err;
1361
            uint64_t offset = direntry->tdir_offset.toff_long8;
1,100✔
1362
            if (tif->tif_flags & TIFF_SWAB)
1,100✔
1363
                TIFFSwabLong8(&offset);
14✔
1364
            if (isMapped(tif))
1,063✔
1365
                err = TIFFReadDirEntryData(tif, (uint64_t)offset,
×
1366
                                           (tmsize_t)datasize, data);
1367
            else
1368
                err = TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset,
1,063✔
1369
                                                     (tmsize_t)datasize, &data);
1370
            if (err != TIFFReadDirEntryErrOk)
1,063✔
1371
            {
1372
                _TIFFfreeExt(tif, data);
×
1373
                return (err);
×
1374
            }
1375
        }
1376
    }
1377
    *value = data;
253,951✔
1378
    return (TIFFReadDirEntryErrOk);
253,951✔
1379
}
1380

1381
static enum TIFFReadDirEntryErr
1382
TIFFReadDirEntryArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t *count,
171,994✔
1383
                      uint32_t desttypesize, void **value)
1384
{
1385
    return TIFFReadDirEntryArrayWithLimit(tif, direntry, count, desttypesize,
171,994✔
1386
                                          value, ~((uint64_t)0));
1387
}
1388

1389
static enum TIFFReadDirEntryErr
1390
TIFFReadDirEntryByteArray(TIFF *tif, TIFFDirEntry *direntry, uint8_t **value)
38,041✔
1391
{
1392
    enum TIFFReadDirEntryErr err;
1393
    uint32_t count;
1394
    void *origdata;
1395
    uint8_t *data;
1396
    switch (direntry->tdir_type)
38,041✔
1397
    {
1398
        case TIFF_ASCII:
38,025✔
1399
        case TIFF_UNDEFINED:
1400
        case TIFF_BYTE:
1401
        case TIFF_SBYTE:
1402
        case TIFF_SHORT:
1403
        case TIFF_SSHORT:
1404
        case TIFF_LONG:
1405
        case TIFF_SLONG:
1406
        case TIFF_LONG8:
1407
        case TIFF_SLONG8:
1408
            break;
38,025✔
1409
        default:
16✔
1410
            return (TIFFReadDirEntryErrType);
16✔
1411
    }
1412
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
38,025✔
1413
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
38,029✔
1414
    {
1415
        *value = 0;
20✔
1416
        return (err);
20✔
1417
    }
1418
    switch (direntry->tdir_type)
38,009✔
1419
    {
1420
        case TIFF_ASCII:
38,010✔
1421
        case TIFF_UNDEFINED:
1422
        case TIFF_BYTE:
1423
            *value = (uint8_t *)origdata;
38,010✔
1424
            return (TIFFReadDirEntryErrOk);
38,010✔
1425
        case TIFF_SBYTE:
×
1426
        {
1427
            int8_t *m;
1428
            uint32_t n;
1429
            m = (int8_t *)origdata;
×
1430
            for (n = 0; n < count; n++)
×
1431
            {
1432
                err = TIFFReadDirEntryCheckRangeByteSbyte(*m);
×
1433
                if (err != TIFFReadDirEntryErrOk)
×
1434
                {
1435
                    _TIFFfreeExt(tif, origdata);
×
1436
                    return (err);
×
1437
                }
1438
                m++;
×
1439
            }
1440
            *value = (uint8_t *)origdata;
×
1441
            return (TIFFReadDirEntryErrOk);
×
1442
        }
1443
    }
1444
    data = (uint8_t *)_TIFFmallocExt(tif, count);
×
1445
    if (data == 0)
×
1446
    {
1447
        _TIFFfreeExt(tif, origdata);
×
1448
        return (TIFFReadDirEntryErrAlloc);
×
1449
    }
1450
    switch (direntry->tdir_type)
×
1451
    {
1452
        case TIFF_SHORT:
×
1453
        {
1454
            uint16_t *ma;
1455
            uint8_t *mb;
1456
            uint32_t n;
1457
            ma = (uint16_t *)origdata;
×
1458
            mb = data;
×
1459
            for (n = 0; n < count; n++)
×
1460
            {
1461
                if (tif->tif_flags & TIFF_SWAB)
×
1462
                    TIFFSwabShort(ma);
×
1463
                err = TIFFReadDirEntryCheckRangeByteShort(*ma);
×
1464
                if (err != TIFFReadDirEntryErrOk)
×
1465
                    break;
×
1466
                *mb++ = (uint8_t)(*ma++);
×
1467
            }
1468
        }
1469
        break;
×
1470
        case TIFF_SSHORT:
×
1471
        {
1472
            int16_t *ma;
1473
            uint8_t *mb;
1474
            uint32_t n;
1475
            ma = (int16_t *)origdata;
×
1476
            mb = data;
×
1477
            for (n = 0; n < count; n++)
×
1478
            {
1479
                if (tif->tif_flags & TIFF_SWAB)
×
1480
                    TIFFSwabShort((uint16_t *)ma);
×
1481
                err = TIFFReadDirEntryCheckRangeByteSshort(*ma);
×
1482
                if (err != TIFFReadDirEntryErrOk)
×
1483
                    break;
×
1484
                *mb++ = (uint8_t)(*ma++);
×
1485
            }
1486
        }
1487
        break;
×
1488
        case TIFF_LONG:
×
1489
        {
1490
            uint32_t *ma;
1491
            uint8_t *mb;
1492
            uint32_t n;
1493
            ma = (uint32_t *)origdata;
×
1494
            mb = data;
×
1495
            for (n = 0; n < count; n++)
×
1496
            {
1497
                if (tif->tif_flags & TIFF_SWAB)
×
1498
                    TIFFSwabLong(ma);
×
1499
                err = TIFFReadDirEntryCheckRangeByteLong(*ma);
×
1500
                if (err != TIFFReadDirEntryErrOk)
×
1501
                    break;
×
1502
                *mb++ = (uint8_t)(*ma++);
×
1503
            }
1504
        }
1505
        break;
×
1506
        case TIFF_SLONG:
×
1507
        {
1508
            int32_t *ma;
1509
            uint8_t *mb;
1510
            uint32_t n;
1511
            ma = (int32_t *)origdata;
×
1512
            mb = data;
×
1513
            for (n = 0; n < count; n++)
×
1514
            {
1515
                if (tif->tif_flags & TIFF_SWAB)
×
1516
                    TIFFSwabLong((uint32_t *)ma);
×
1517
                err = TIFFReadDirEntryCheckRangeByteSlong(*ma);
×
1518
                if (err != TIFFReadDirEntryErrOk)
×
1519
                    break;
×
1520
                *mb++ = (uint8_t)(*ma++);
×
1521
            }
1522
        }
1523
        break;
×
1524
        case TIFF_LONG8:
×
1525
        {
1526
            uint64_t *ma;
1527
            uint8_t *mb;
1528
            uint32_t n;
1529
            ma = (uint64_t *)origdata;
×
1530
            mb = data;
×
1531
            for (n = 0; n < count; n++)
×
1532
            {
1533
                if (tif->tif_flags & TIFF_SWAB)
×
1534
                    TIFFSwabLong8(ma);
×
1535
                err = TIFFReadDirEntryCheckRangeByteLong8(*ma);
×
1536
                if (err != TIFFReadDirEntryErrOk)
×
1537
                    break;
×
1538
                *mb++ = (uint8_t)(*ma++);
×
1539
            }
1540
        }
1541
        break;
×
1542
        case TIFF_SLONG8:
×
1543
        {
1544
            int64_t *ma;
1545
            uint8_t *mb;
1546
            uint32_t n;
1547
            ma = (int64_t *)origdata;
×
1548
            mb = data;
×
1549
            for (n = 0; n < count; n++)
×
1550
            {
1551
                if (tif->tif_flags & TIFF_SWAB)
×
1552
                    TIFFSwabLong8((uint64_t *)ma);
×
1553
                err = TIFFReadDirEntryCheckRangeByteSlong8(*ma);
×
1554
                if (err != TIFFReadDirEntryErrOk)
×
1555
                    break;
×
1556
                *mb++ = (uint8_t)(*ma++);
×
1557
            }
1558
        }
1559
        break;
×
1560
    }
1561
    _TIFFfreeExt(tif, origdata);
×
1562
    if (err != TIFFReadDirEntryErrOk)
×
1563
    {
1564
        _TIFFfreeExt(tif, data);
×
1565
        return (err);
×
1566
    }
1567
    *value = data;
×
1568
    return (TIFFReadDirEntryErrOk);
×
1569
}
1570

1571
static enum TIFFReadDirEntryErr
1572
TIFFReadDirEntrySbyteArray(TIFF *tif, TIFFDirEntry *direntry, int8_t **value)
×
1573
{
1574
    enum TIFFReadDirEntryErr err;
1575
    uint32_t count;
1576
    void *origdata;
1577
    int8_t *data;
1578
    switch (direntry->tdir_type)
×
1579
    {
1580
        case TIFF_UNDEFINED:
×
1581
        case TIFF_BYTE:
1582
        case TIFF_SBYTE:
1583
        case TIFF_SHORT:
1584
        case TIFF_SSHORT:
1585
        case TIFF_LONG:
1586
        case TIFF_SLONG:
1587
        case TIFF_LONG8:
1588
        case TIFF_SLONG8:
1589
            break;
×
1590
        default:
×
1591
            return (TIFFReadDirEntryErrType);
×
1592
    }
1593
    err = TIFFReadDirEntryArray(tif, direntry, &count, 1, &origdata);
×
1594
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
×
1595
    {
1596
        *value = 0;
×
1597
        return (err);
×
1598
    }
1599
    switch (direntry->tdir_type)
×
1600
    {
1601
        case TIFF_UNDEFINED:
×
1602
        case TIFF_BYTE:
1603
        {
1604
            uint8_t *m;
1605
            uint32_t n;
1606
            m = (uint8_t *)origdata;
×
1607
            for (n = 0; n < count; n++)
×
1608
            {
1609
                err = TIFFReadDirEntryCheckRangeSbyteByte(*m);
×
1610
                if (err != TIFFReadDirEntryErrOk)
×
1611
                {
1612
                    _TIFFfreeExt(tif, origdata);
×
1613
                    return (err);
×
1614
                }
1615
                m++;
×
1616
            }
1617
            *value = (int8_t *)origdata;
×
1618
            return (TIFFReadDirEntryErrOk);
×
1619
        }
1620
        case TIFF_SBYTE:
×
1621
            *value = (int8_t *)origdata;
×
1622
            return (TIFFReadDirEntryErrOk);
×
1623
    }
1624
    data = (int8_t *)_TIFFmallocExt(tif, count);
×
1625
    if (data == 0)
×
1626
    {
1627
        _TIFFfreeExt(tif, origdata);
×
1628
        return (TIFFReadDirEntryErrAlloc);
×
1629
    }
1630
    switch (direntry->tdir_type)
×
1631
    {
1632
        case TIFF_SHORT:
×
1633
        {
1634
            uint16_t *ma;
1635
            int8_t *mb;
1636
            uint32_t n;
1637
            ma = (uint16_t *)origdata;
×
1638
            mb = data;
×
1639
            for (n = 0; n < count; n++)
×
1640
            {
1641
                if (tif->tif_flags & TIFF_SWAB)
×
1642
                    TIFFSwabShort(ma);
×
1643
                err = TIFFReadDirEntryCheckRangeSbyteShort(*ma);
×
1644
                if (err != TIFFReadDirEntryErrOk)
×
1645
                    break;
×
1646
                *mb++ = (int8_t)(*ma++);
×
1647
            }
1648
        }
1649
        break;
×
1650
        case TIFF_SSHORT:
×
1651
        {
1652
            int16_t *ma;
1653
            int8_t *mb;
1654
            uint32_t n;
1655
            ma = (int16_t *)origdata;
×
1656
            mb = data;
×
1657
            for (n = 0; n < count; n++)
×
1658
            {
1659
                if (tif->tif_flags & TIFF_SWAB)
×
1660
                    TIFFSwabShort((uint16_t *)ma);
×
1661
                err = TIFFReadDirEntryCheckRangeSbyteSshort(*ma);
×
1662
                if (err != TIFFReadDirEntryErrOk)
×
1663
                    break;
×
1664
                *mb++ = (int8_t)(*ma++);
×
1665
            }
1666
        }
1667
        break;
×
1668
        case TIFF_LONG:
×
1669
        {
1670
            uint32_t *ma;
1671
            int8_t *mb;
1672
            uint32_t n;
1673
            ma = (uint32_t *)origdata;
×
1674
            mb = data;
×
1675
            for (n = 0; n < count; n++)
×
1676
            {
1677
                if (tif->tif_flags & TIFF_SWAB)
×
1678
                    TIFFSwabLong(ma);
×
1679
                err = TIFFReadDirEntryCheckRangeSbyteLong(*ma);
×
1680
                if (err != TIFFReadDirEntryErrOk)
×
1681
                    break;
×
1682
                *mb++ = (int8_t)(*ma++);
×
1683
            }
1684
        }
1685
        break;
×
1686
        case TIFF_SLONG:
×
1687
        {
1688
            int32_t *ma;
1689
            int8_t *mb;
1690
            uint32_t n;
1691
            ma = (int32_t *)origdata;
×
1692
            mb = data;
×
1693
            for (n = 0; n < count; n++)
×
1694
            {
1695
                if (tif->tif_flags & TIFF_SWAB)
×
1696
                    TIFFSwabLong((uint32_t *)ma);
×
1697
                err = TIFFReadDirEntryCheckRangeSbyteSlong(*ma);
×
1698
                if (err != TIFFReadDirEntryErrOk)
×
1699
                    break;
×
1700
                *mb++ = (int8_t)(*ma++);
×
1701
            }
1702
        }
1703
        break;
×
1704
        case TIFF_LONG8:
×
1705
        {
1706
            uint64_t *ma;
1707
            int8_t *mb;
1708
            uint32_t n;
1709
            ma = (uint64_t *)origdata;
×
1710
            mb = data;
×
1711
            for (n = 0; n < count; n++)
×
1712
            {
1713
                if (tif->tif_flags & TIFF_SWAB)
×
1714
                    TIFFSwabLong8(ma);
×
1715
                err = TIFFReadDirEntryCheckRangeSbyteLong8(*ma);
×
1716
                if (err != TIFFReadDirEntryErrOk)
×
1717
                    break;
×
1718
                *mb++ = (int8_t)(*ma++);
×
1719
            }
1720
        }
1721
        break;
×
1722
        case TIFF_SLONG8:
×
1723
        {
1724
            int64_t *ma;
1725
            int8_t *mb;
1726
            uint32_t n;
1727
            ma = (int64_t *)origdata;
×
1728
            mb = data;
×
1729
            for (n = 0; n < count; n++)
×
1730
            {
1731
                if (tif->tif_flags & TIFF_SWAB)
×
1732
                    TIFFSwabLong8((uint64_t *)ma);
×
1733
                err = TIFFReadDirEntryCheckRangeSbyteSlong8(*ma);
×
1734
                if (err != TIFFReadDirEntryErrOk)
×
1735
                    break;
×
1736
                *mb++ = (int8_t)(*ma++);
×
1737
            }
1738
        }
1739
        break;
×
1740
    }
1741
    _TIFFfreeExt(tif, origdata);
×
1742
    if (err != TIFFReadDirEntryErrOk)
×
1743
    {
1744
        _TIFFfreeExt(tif, data);
×
1745
        return (err);
×
1746
    }
1747
    *value = data;
×
1748
    return (TIFFReadDirEntryErrOk);
×
1749
}
1750

1751
static enum TIFFReadDirEntryErr
1752
TIFFReadDirEntryShortArray(TIFF *tif, TIFFDirEntry *direntry, uint16_t **value)
65,304✔
1753
{
1754
    enum TIFFReadDirEntryErr err;
1755
    uint32_t count;
1756
    void *origdata;
1757
    uint16_t *data;
1758
    switch (direntry->tdir_type)
65,304✔
1759
    {
1760
        case TIFF_BYTE:
65,306✔
1761
        case TIFF_SBYTE:
1762
        case TIFF_SHORT:
1763
        case TIFF_SSHORT:
1764
        case TIFF_LONG:
1765
        case TIFF_SLONG:
1766
        case TIFF_LONG8:
1767
        case TIFF_SLONG8:
1768
            break;
65,306✔
1769
        default:
×
1770
            return (TIFFReadDirEntryErrType);
×
1771
    }
1772
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
65,306✔
1773
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
65,301✔
1774
    {
1775
        *value = 0;
23✔
1776
        return (err);
23✔
1777
    }
1778
    switch (direntry->tdir_type)
65,278✔
1779
    {
1780
        case TIFF_SHORT:
65,267✔
1781
            *value = (uint16_t *)origdata;
65,267✔
1782
            if (tif->tif_flags & TIFF_SWAB)
65,267✔
1783
                TIFFSwabArrayOfShort(*value, count);
1,014✔
1784
            return (TIFFReadDirEntryErrOk);
65,268✔
1785
        case TIFF_SSHORT:
×
1786
        {
1787
            int16_t *m;
1788
            uint32_t n;
1789
            m = (int16_t *)origdata;
×
1790
            for (n = 0; n < count; n++)
×
1791
            {
1792
                if (tif->tif_flags & TIFF_SWAB)
×
1793
                    TIFFSwabShort((uint16_t *)m);
×
1794
                err = TIFFReadDirEntryCheckRangeShortSshort(*m);
×
1795
                if (err != TIFFReadDirEntryErrOk)
×
1796
                {
1797
                    _TIFFfreeExt(tif, origdata);
×
1798
                    return (err);
×
1799
                }
1800
                m++;
×
1801
            }
1802
            *value = (uint16_t *)origdata;
×
1803
            return (TIFFReadDirEntryErrOk);
×
1804
        }
1805
    }
1806
    data = (uint16_t *)_TIFFmallocExt(tif, count * 2);
11✔
1807
    if (data == 0)
×
1808
    {
1809
        _TIFFfreeExt(tif, origdata);
×
1810
        return (TIFFReadDirEntryErrAlloc);
×
1811
    }
1812
    switch (direntry->tdir_type)
×
1813
    {
1814
        case TIFF_BYTE:
×
1815
        {
1816
            uint8_t *ma;
1817
            uint16_t *mb;
1818
            uint32_t n;
1819
            ma = (uint8_t *)origdata;
×
1820
            mb = data;
×
1821
            for (n = 0; n < count; n++)
×
1822
                *mb++ = (uint16_t)(*ma++);
×
1823
        }
1824
        break;
×
1825
        case TIFF_SBYTE:
×
1826
        {
1827
            int8_t *ma;
1828
            uint16_t *mb;
1829
            uint32_t n;
1830
            ma = (int8_t *)origdata;
×
1831
            mb = data;
×
1832
            for (n = 0; n < count; n++)
×
1833
            {
1834
                err = TIFFReadDirEntryCheckRangeShortSbyte(*ma);
×
1835
                if (err != TIFFReadDirEntryErrOk)
×
1836
                    break;
×
1837
                *mb++ = (uint16_t)(*ma++);
×
1838
            }
1839
        }
1840
        break;
×
1841
        case TIFF_LONG:
×
1842
        {
1843
            uint32_t *ma;
1844
            uint16_t *mb;
1845
            uint32_t n;
1846
            ma = (uint32_t *)origdata;
×
1847
            mb = data;
×
1848
            for (n = 0; n < count; n++)
×
1849
            {
1850
                if (tif->tif_flags & TIFF_SWAB)
×
1851
                    TIFFSwabLong(ma);
×
1852
                err = TIFFReadDirEntryCheckRangeShortLong(*ma);
×
1853
                if (err != TIFFReadDirEntryErrOk)
×
1854
                    break;
×
1855
                *mb++ = (uint16_t)(*ma++);
×
1856
            }
1857
        }
1858
        break;
×
1859
        case TIFF_SLONG:
×
1860
        {
1861
            int32_t *ma;
1862
            uint16_t *mb;
1863
            uint32_t n;
1864
            ma = (int32_t *)origdata;
×
1865
            mb = data;
×
1866
            for (n = 0; n < count; n++)
×
1867
            {
1868
                if (tif->tif_flags & TIFF_SWAB)
×
1869
                    TIFFSwabLong((uint32_t *)ma);
×
1870
                err = TIFFReadDirEntryCheckRangeShortSlong(*ma);
×
1871
                if (err != TIFFReadDirEntryErrOk)
×
1872
                    break;
×
1873
                *mb++ = (uint16_t)(*ma++);
×
1874
            }
1875
        }
1876
        break;
×
1877
        case TIFF_LONG8:
×
1878
        {
1879
            uint64_t *ma;
1880
            uint16_t *mb;
1881
            uint32_t n;
1882
            ma = (uint64_t *)origdata;
×
1883
            mb = data;
×
1884
            for (n = 0; n < count; n++)
×
1885
            {
1886
                if (tif->tif_flags & TIFF_SWAB)
×
1887
                    TIFFSwabLong8(ma);
×
1888
                err = TIFFReadDirEntryCheckRangeShortLong8(*ma);
×
1889
                if (err != TIFFReadDirEntryErrOk)
×
1890
                    break;
×
1891
                *mb++ = (uint16_t)(*ma++);
×
1892
            }
1893
        }
1894
        break;
×
1895
        case TIFF_SLONG8:
×
1896
        {
1897
            int64_t *ma;
1898
            uint16_t *mb;
1899
            uint32_t n;
1900
            ma = (int64_t *)origdata;
×
1901
            mb = data;
×
1902
            for (n = 0; n < count; n++)
×
1903
            {
1904
                if (tif->tif_flags & TIFF_SWAB)
×
1905
                    TIFFSwabLong8((uint64_t *)ma);
×
1906
                err = TIFFReadDirEntryCheckRangeShortSlong8(*ma);
×
1907
                if (err != TIFFReadDirEntryErrOk)
×
1908
                    break;
×
1909
                *mb++ = (uint16_t)(*ma++);
×
1910
            }
1911
        }
1912
        break;
×
1913
    }
1914
    _TIFFfreeExt(tif, origdata);
×
1915
    if (err != TIFFReadDirEntryErrOk)
×
1916
    {
1917
        _TIFFfreeExt(tif, data);
×
1918
        return (err);
×
1919
    }
1920
    *value = data;
×
1921
    return (TIFFReadDirEntryErrOk);
×
1922
}
1923

1924
static enum TIFFReadDirEntryErr
1925
TIFFReadDirEntrySshortArray(TIFF *tif, TIFFDirEntry *direntry, int16_t **value)
×
1926
{
1927
    enum TIFFReadDirEntryErr err;
1928
    uint32_t count;
1929
    void *origdata;
1930
    int16_t *data;
1931
    switch (direntry->tdir_type)
×
1932
    {
1933
        case TIFF_BYTE:
×
1934
        case TIFF_SBYTE:
1935
        case TIFF_SHORT:
1936
        case TIFF_SSHORT:
1937
        case TIFF_LONG:
1938
        case TIFF_SLONG:
1939
        case TIFF_LONG8:
1940
        case TIFF_SLONG8:
1941
            break;
×
1942
        default:
×
1943
            return (TIFFReadDirEntryErrType);
×
1944
    }
1945
    err = TIFFReadDirEntryArray(tif, direntry, &count, 2, &origdata);
×
1946
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
×
1947
    {
1948
        *value = 0;
×
1949
        return (err);
×
1950
    }
1951
    switch (direntry->tdir_type)
×
1952
    {
1953
        case TIFF_SHORT:
×
1954
        {
1955
            uint16_t *m;
1956
            uint32_t n;
1957
            m = (uint16_t *)origdata;
×
1958
            for (n = 0; n < count; n++)
×
1959
            {
1960
                if (tif->tif_flags & TIFF_SWAB)
×
1961
                    TIFFSwabShort(m);
×
1962
                err = TIFFReadDirEntryCheckRangeSshortShort(*m);
×
1963
                if (err != TIFFReadDirEntryErrOk)
×
1964
                {
1965
                    _TIFFfreeExt(tif, origdata);
×
1966
                    return (err);
×
1967
                }
1968
                m++;
×
1969
            }
1970
            *value = (int16_t *)origdata;
×
1971
            return (TIFFReadDirEntryErrOk);
×
1972
        }
1973
        case TIFF_SSHORT:
×
1974
            *value = (int16_t *)origdata;
×
1975
            if (tif->tif_flags & TIFF_SWAB)
×
1976
                TIFFSwabArrayOfShort((uint16_t *)(*value), count);
×
1977
            return (TIFFReadDirEntryErrOk);
×
1978
    }
1979
    data = (int16_t *)_TIFFmallocExt(tif, count * 2);
×
1980
    if (data == 0)
×
1981
    {
1982
        _TIFFfreeExt(tif, origdata);
×
1983
        return (TIFFReadDirEntryErrAlloc);
×
1984
    }
1985
    switch (direntry->tdir_type)
×
1986
    {
1987
        case TIFF_BYTE:
×
1988
        {
1989
            uint8_t *ma;
1990
            int16_t *mb;
1991
            uint32_t n;
1992
            ma = (uint8_t *)origdata;
×
1993
            mb = data;
×
1994
            for (n = 0; n < count; n++)
×
1995
                *mb++ = (int16_t)(*ma++);
×
1996
        }
1997
        break;
×
1998
        case TIFF_SBYTE:
×
1999
        {
2000
            int8_t *ma;
2001
            int16_t *mb;
2002
            uint32_t n;
2003
            ma = (int8_t *)origdata;
×
2004
            mb = data;
×
2005
            for (n = 0; n < count; n++)
×
2006
                *mb++ = (int16_t)(*ma++);
×
2007
        }
2008
        break;
×
2009
        case TIFF_LONG:
×
2010
        {
2011
            uint32_t *ma;
2012
            int16_t *mb;
2013
            uint32_t n;
2014
            ma = (uint32_t *)origdata;
×
2015
            mb = data;
×
2016
            for (n = 0; n < count; n++)
×
2017
            {
2018
                if (tif->tif_flags & TIFF_SWAB)
×
2019
                    TIFFSwabLong(ma);
×
2020
                err = TIFFReadDirEntryCheckRangeSshortLong(*ma);
×
2021
                if (err != TIFFReadDirEntryErrOk)
×
2022
                    break;
×
2023
                *mb++ = (int16_t)(*ma++);
×
2024
            }
2025
        }
2026
        break;
×
2027
        case TIFF_SLONG:
×
2028
        {
2029
            int32_t *ma;
2030
            int16_t *mb;
2031
            uint32_t n;
2032
            ma = (int32_t *)origdata;
×
2033
            mb = data;
×
2034
            for (n = 0; n < count; n++)
×
2035
            {
2036
                if (tif->tif_flags & TIFF_SWAB)
×
2037
                    TIFFSwabLong((uint32_t *)ma);
×
2038
                err = TIFFReadDirEntryCheckRangeSshortSlong(*ma);
×
2039
                if (err != TIFFReadDirEntryErrOk)
×
2040
                    break;
×
2041
                *mb++ = (int16_t)(*ma++);
×
2042
            }
2043
        }
2044
        break;
×
2045
        case TIFF_LONG8:
×
2046
        {
2047
            uint64_t *ma;
2048
            int16_t *mb;
2049
            uint32_t n;
2050
            ma = (uint64_t *)origdata;
×
2051
            mb = data;
×
2052
            for (n = 0; n < count; n++)
×
2053
            {
2054
                if (tif->tif_flags & TIFF_SWAB)
×
2055
                    TIFFSwabLong8(ma);
×
2056
                err = TIFFReadDirEntryCheckRangeSshortLong8(*ma);
×
2057
                if (err != TIFFReadDirEntryErrOk)
×
2058
                    break;
×
2059
                *mb++ = (int16_t)(*ma++);
×
2060
            }
2061
        }
2062
        break;
×
2063
        case TIFF_SLONG8:
×
2064
        {
2065
            int64_t *ma;
2066
            int16_t *mb;
2067
            uint32_t n;
2068
            ma = (int64_t *)origdata;
×
2069
            mb = data;
×
2070
            for (n = 0; n < count; n++)
×
2071
            {
2072
                if (tif->tif_flags & TIFF_SWAB)
×
2073
                    TIFFSwabLong8((uint64_t *)ma);
×
2074
                err = TIFFReadDirEntryCheckRangeSshortSlong8(*ma);
×
2075
                if (err != TIFFReadDirEntryErrOk)
×
2076
                    break;
×
2077
                *mb++ = (int16_t)(*ma++);
×
2078
            }
2079
        }
2080
        break;
×
2081
    }
2082
    _TIFFfreeExt(tif, origdata);
×
2083
    if (err != TIFFReadDirEntryErrOk)
×
2084
    {
2085
        _TIFFfreeExt(tif, data);
×
2086
        return (err);
×
2087
    }
2088
    *value = data;
×
2089
    return (TIFFReadDirEntryErrOk);
×
2090
}
2091

2092
static enum TIFFReadDirEntryErr
2093
TIFFReadDirEntryLongArray(TIFF *tif, TIFFDirEntry *direntry, uint32_t **value)
789✔
2094
{
2095
    enum TIFFReadDirEntryErr err;
2096
    uint32_t count;
2097
    void *origdata;
2098
    uint32_t *data;
2099
    switch (direntry->tdir_type)
789✔
2100
    {
2101
        case TIFF_BYTE:
789✔
2102
        case TIFF_SBYTE:
2103
        case TIFF_SHORT:
2104
        case TIFF_SSHORT:
2105
        case TIFF_LONG:
2106
        case TIFF_SLONG:
2107
        case TIFF_LONG8:
2108
        case TIFF_SLONG8:
2109
            break;
789✔
2110
        default:
×
2111
            return (TIFFReadDirEntryErrType);
×
2112
    }
2113
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
789✔
2114
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
789✔
2115
    {
2116
        *value = 0;
×
2117
        return (err);
×
2118
    }
2119
    switch (direntry->tdir_type)
789✔
2120
    {
2121
        case TIFF_LONG:
789✔
2122
            *value = (uint32_t *)origdata;
789✔
2123
            if (tif->tif_flags & TIFF_SWAB)
789✔
2124
                TIFFSwabArrayOfLong(*value, count);
×
2125
            return (TIFFReadDirEntryErrOk);
789✔
2126
        case TIFF_SLONG:
×
2127
        {
2128
            int32_t *m;
2129
            uint32_t n;
2130
            m = (int32_t *)origdata;
×
2131
            for (n = 0; n < count; n++)
×
2132
            {
2133
                if (tif->tif_flags & TIFF_SWAB)
×
2134
                    TIFFSwabLong((uint32_t *)m);
×
2135
                err = TIFFReadDirEntryCheckRangeLongSlong(*m);
×
2136
                if (err != TIFFReadDirEntryErrOk)
×
2137
                {
2138
                    _TIFFfreeExt(tif, origdata);
×
2139
                    return (err);
×
2140
                }
2141
                m++;
×
2142
            }
2143
            *value = (uint32_t *)origdata;
×
2144
            return (TIFFReadDirEntryErrOk);
×
2145
        }
2146
    }
2147
    data = (uint32_t *)_TIFFmallocExt(tif, count * 4);
×
2148
    if (data == 0)
×
2149
    {
2150
        _TIFFfreeExt(tif, origdata);
×
2151
        return (TIFFReadDirEntryErrAlloc);
×
2152
    }
2153
    switch (direntry->tdir_type)
×
2154
    {
2155
        case TIFF_BYTE:
×
2156
        {
2157
            uint8_t *ma;
2158
            uint32_t *mb;
2159
            uint32_t n;
2160
            ma = (uint8_t *)origdata;
×
2161
            mb = data;
×
2162
            for (n = 0; n < count; n++)
×
2163
                *mb++ = (uint32_t)(*ma++);
×
2164
        }
2165
        break;
×
2166
        case TIFF_SBYTE:
×
2167
        {
2168
            int8_t *ma;
2169
            uint32_t *mb;
2170
            uint32_t n;
2171
            ma = (int8_t *)origdata;
×
2172
            mb = data;
×
2173
            for (n = 0; n < count; n++)
×
2174
            {
2175
                err = TIFFReadDirEntryCheckRangeLongSbyte(*ma);
×
2176
                if (err != TIFFReadDirEntryErrOk)
×
2177
                    break;
×
2178
                *mb++ = (uint32_t)(*ma++);
×
2179
            }
2180
        }
2181
        break;
×
2182
        case TIFF_SHORT:
×
2183
        {
2184
            uint16_t *ma;
2185
            uint32_t *mb;
2186
            uint32_t n;
2187
            ma = (uint16_t *)origdata;
×
2188
            mb = data;
×
2189
            for (n = 0; n < count; n++)
×
2190
            {
2191
                if (tif->tif_flags & TIFF_SWAB)
×
2192
                    TIFFSwabShort(ma);
×
2193
                *mb++ = (uint32_t)(*ma++);
×
2194
            }
2195
        }
2196
        break;
×
2197
        case TIFF_SSHORT:
×
2198
        {
2199
            int16_t *ma;
2200
            uint32_t *mb;
2201
            uint32_t n;
2202
            ma = (int16_t *)origdata;
×
2203
            mb = data;
×
2204
            for (n = 0; n < count; n++)
×
2205
            {
2206
                if (tif->tif_flags & TIFF_SWAB)
×
2207
                    TIFFSwabShort((uint16_t *)ma);
×
2208
                err = TIFFReadDirEntryCheckRangeLongSshort(*ma);
×
2209
                if (err != TIFFReadDirEntryErrOk)
×
2210
                    break;
×
2211
                *mb++ = (uint32_t)(*ma++);
×
2212
            }
2213
        }
2214
        break;
×
2215
        case TIFF_LONG8:
×
2216
        {
2217
            uint64_t *ma;
2218
            uint32_t *mb;
2219
            uint32_t n;
2220
            ma = (uint64_t *)origdata;
×
2221
            mb = data;
×
2222
            for (n = 0; n < count; n++)
×
2223
            {
2224
                if (tif->tif_flags & TIFF_SWAB)
×
2225
                    TIFFSwabLong8(ma);
×
2226
                err = TIFFReadDirEntryCheckRangeLongLong8(*ma);
×
2227
                if (err != TIFFReadDirEntryErrOk)
×
2228
                    break;
×
2229
                *mb++ = (uint32_t)(*ma++);
×
2230
            }
2231
        }
2232
        break;
×
2233
        case TIFF_SLONG8:
×
2234
        {
2235
            int64_t *ma;
2236
            uint32_t *mb;
2237
            uint32_t n;
2238
            ma = (int64_t *)origdata;
×
2239
            mb = data;
×
2240
            for (n = 0; n < count; n++)
×
2241
            {
2242
                if (tif->tif_flags & TIFF_SWAB)
×
2243
                    TIFFSwabLong8((uint64_t *)ma);
×
2244
                err = TIFFReadDirEntryCheckRangeLongSlong8(*ma);
×
2245
                if (err != TIFFReadDirEntryErrOk)
×
2246
                    break;
×
2247
                *mb++ = (uint32_t)(*ma++);
×
2248
            }
2249
        }
2250
        break;
×
2251
    }
2252
    _TIFFfreeExt(tif, origdata);
×
2253
    if (err != TIFFReadDirEntryErrOk)
×
2254
    {
2255
        _TIFFfreeExt(tif, data);
×
2256
        return (err);
×
2257
    }
2258
    *value = data;
×
2259
    return (TIFFReadDirEntryErrOk);
×
2260
}
2261

2262
static enum TIFFReadDirEntryErr
2263
TIFFReadDirEntrySlongArray(TIFF *tif, TIFFDirEntry *direntry, int32_t **value)
×
2264
{
2265
    enum TIFFReadDirEntryErr err;
2266
    uint32_t count;
2267
    void *origdata;
2268
    int32_t *data;
2269
    switch (direntry->tdir_type)
×
2270
    {
2271
        case TIFF_BYTE:
×
2272
        case TIFF_SBYTE:
2273
        case TIFF_SHORT:
2274
        case TIFF_SSHORT:
2275
        case TIFF_LONG:
2276
        case TIFF_SLONG:
2277
        case TIFF_LONG8:
2278
        case TIFF_SLONG8:
2279
            break;
×
2280
        default:
×
2281
            return (TIFFReadDirEntryErrType);
×
2282
    }
2283
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
×
2284
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
×
2285
    {
2286
        *value = 0;
×
2287
        return (err);
×
2288
    }
2289
    switch (direntry->tdir_type)
×
2290
    {
2291
        case TIFF_LONG:
×
2292
        {
2293
            uint32_t *m;
2294
            uint32_t n;
2295
            m = (uint32_t *)origdata;
×
2296
            for (n = 0; n < count; n++)
×
2297
            {
2298
                if (tif->tif_flags & TIFF_SWAB)
×
2299
                    TIFFSwabLong((uint32_t *)m);
×
2300
                err = TIFFReadDirEntryCheckRangeSlongLong(*m);
×
2301
                if (err != TIFFReadDirEntryErrOk)
×
2302
                {
2303
                    _TIFFfreeExt(tif, origdata);
×
2304
                    return (err);
×
2305
                }
2306
                m++;
×
2307
            }
2308
            *value = (int32_t *)origdata;
×
2309
            return (TIFFReadDirEntryErrOk);
×
2310
        }
2311
        case TIFF_SLONG:
×
2312
            *value = (int32_t *)origdata;
×
2313
            if (tif->tif_flags & TIFF_SWAB)
×
2314
                TIFFSwabArrayOfLong((uint32_t *)(*value), count);
×
2315
            return (TIFFReadDirEntryErrOk);
×
2316
    }
2317
    data = (int32_t *)_TIFFmallocExt(tif, count * 4);
×
2318
    if (data == 0)
×
2319
    {
2320
        _TIFFfreeExt(tif, origdata);
×
2321
        return (TIFFReadDirEntryErrAlloc);
×
2322
    }
2323
    switch (direntry->tdir_type)
×
2324
    {
2325
        case TIFF_BYTE:
×
2326
        {
2327
            uint8_t *ma;
2328
            int32_t *mb;
2329
            uint32_t n;
2330
            ma = (uint8_t *)origdata;
×
2331
            mb = data;
×
2332
            for (n = 0; n < count; n++)
×
2333
                *mb++ = (int32_t)(*ma++);
×
2334
        }
2335
        break;
×
2336
        case TIFF_SBYTE:
×
2337
        {
2338
            int8_t *ma;
2339
            int32_t *mb;
2340
            uint32_t n;
2341
            ma = (int8_t *)origdata;
×
2342
            mb = data;
×
2343
            for (n = 0; n < count; n++)
×
2344
                *mb++ = (int32_t)(*ma++);
×
2345
        }
2346
        break;
×
2347
        case TIFF_SHORT:
×
2348
        {
2349
            uint16_t *ma;
2350
            int32_t *mb;
2351
            uint32_t n;
2352
            ma = (uint16_t *)origdata;
×
2353
            mb = data;
×
2354
            for (n = 0; n < count; n++)
×
2355
            {
2356
                if (tif->tif_flags & TIFF_SWAB)
×
2357
                    TIFFSwabShort(ma);
×
2358
                *mb++ = (int32_t)(*ma++);
×
2359
            }
2360
        }
2361
        break;
×
2362
        case TIFF_SSHORT:
×
2363
        {
2364
            int16_t *ma;
2365
            int32_t *mb;
2366
            uint32_t n;
2367
            ma = (int16_t *)origdata;
×
2368
            mb = data;
×
2369
            for (n = 0; n < count; n++)
×
2370
            {
2371
                if (tif->tif_flags & TIFF_SWAB)
×
2372
                    TIFFSwabShort((uint16_t *)ma);
×
2373
                *mb++ = (int32_t)(*ma++);
×
2374
            }
2375
        }
2376
        break;
×
2377
        case TIFF_LONG8:
×
2378
        {
2379
            uint64_t *ma;
2380
            int32_t *mb;
2381
            uint32_t n;
2382
            ma = (uint64_t *)origdata;
×
2383
            mb = data;
×
2384
            for (n = 0; n < count; n++)
×
2385
            {
2386
                if (tif->tif_flags & TIFF_SWAB)
×
2387
                    TIFFSwabLong8(ma);
×
2388
                err = TIFFReadDirEntryCheckRangeSlongLong8(*ma);
×
2389
                if (err != TIFFReadDirEntryErrOk)
×
2390
                    break;
×
2391
                *mb++ = (int32_t)(*ma++);
×
2392
            }
2393
        }
2394
        break;
×
2395
        case TIFF_SLONG8:
×
2396
        {
2397
            int64_t *ma;
2398
            int32_t *mb;
2399
            uint32_t n;
2400
            ma = (int64_t *)origdata;
×
2401
            mb = data;
×
2402
            for (n = 0; n < count; n++)
×
2403
            {
2404
                if (tif->tif_flags & TIFF_SWAB)
×
2405
                    TIFFSwabLong8((uint64_t *)ma);
×
2406
                err = TIFFReadDirEntryCheckRangeSlongSlong8(*ma);
×
2407
                if (err != TIFFReadDirEntryErrOk)
×
2408
                    break;
×
2409
                *mb++ = (int32_t)(*ma++);
×
2410
            }
2411
        }
2412
        break;
×
2413
    }
2414
    _TIFFfreeExt(tif, origdata);
×
2415
    if (err != TIFFReadDirEntryErrOk)
×
2416
    {
2417
        _TIFFfreeExt(tif, data);
×
2418
        return (err);
×
2419
    }
2420
    *value = data;
×
2421
    return (TIFFReadDirEntryErrOk);
×
2422
}
2423

2424
static enum TIFFReadDirEntryErr
2425
TIFFReadDirEntryLong8ArrayWithLimit(TIFF *tif, TIFFDirEntry *direntry,
81,997✔
2426
                                    uint64_t **value, uint64_t maxcount)
2427
{
2428
    enum TIFFReadDirEntryErr err;
2429
    uint32_t count;
2430
    void *origdata;
2431
    uint64_t *data;
2432
    switch (direntry->tdir_type)
81,997✔
2433
    {
2434
        case TIFF_BYTE:
81,972✔
2435
        case TIFF_SBYTE:
2436
        case TIFF_SHORT:
2437
        case TIFF_SSHORT:
2438
        case TIFF_LONG:
2439
        case TIFF_SLONG:
2440
        case TIFF_LONG8:
2441
        case TIFF_SLONG8:
2442
            break;
81,972✔
2443
        default:
25✔
2444
            return (TIFFReadDirEntryErrType);
25✔
2445
    }
2446
    err = TIFFReadDirEntryArrayWithLimit(tif, direntry, &count, 8, &origdata,
81,972✔
2447
                                         maxcount);
2448
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
81,976✔
2449
    {
2450
        *value = 0;
15✔
2451
        return (err);
15✔
2452
    }
2453
    switch (direntry->tdir_type)
81,961✔
2454
    {
2455
        case TIFF_LONG8:
588✔
2456
            *value = (uint64_t *)origdata;
588✔
2457
            if (tif->tif_flags & TIFF_SWAB)
588✔
2458
                TIFFSwabArrayOfLong8(*value, count);
15✔
2459
            return (TIFFReadDirEntryErrOk);
588✔
2460
        case TIFF_SLONG8:
1✔
2461
        {
2462
            int64_t *m;
2463
            uint32_t n;
2464
            m = (int64_t *)origdata;
1✔
2465
            for (n = 0; n < count; n++)
2✔
2466
            {
2467
                if (tif->tif_flags & TIFF_SWAB)
1✔
2468
                    TIFFSwabLong8((uint64_t *)m);
×
2469
                err = TIFFReadDirEntryCheckRangeLong8Slong8(*m);
1✔
2470
                if (err != TIFFReadDirEntryErrOk)
1✔
2471
                {
2472
                    _TIFFfreeExt(tif, origdata);
×
2473
                    return (err);
×
2474
                }
2475
                m++;
1✔
2476
            }
2477
            *value = (uint64_t *)origdata;
1✔
2478
            return (TIFFReadDirEntryErrOk);
1✔
2479
        }
2480
    }
2481
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
81,372✔
2482
    if (data == 0)
81,416✔
2483
    {
2484
        _TIFFfreeExt(tif, origdata);
×
2485
        return (TIFFReadDirEntryErrAlloc);
×
2486
    }
2487
    switch (direntry->tdir_type)
81,416✔
2488
    {
2489
        case TIFF_BYTE:
1✔
2490
        {
2491
            uint8_t *ma;
2492
            uint64_t *mb;
2493
            uint32_t n;
2494
            ma = (uint8_t *)origdata;
1✔
2495
            mb = data;
1✔
2496
            for (n = 0; n < count; n++)
2✔
2497
                *mb++ = (uint64_t)(*ma++);
1✔
2498
        }
2499
        break;
1✔
2500
        case TIFF_SBYTE:
×
2501
        {
2502
            int8_t *ma;
2503
            uint64_t *mb;
2504
            uint32_t n;
2505
            ma = (int8_t *)origdata;
×
2506
            mb = data;
×
2507
            for (n = 0; n < count; n++)
×
2508
            {
2509
                err = TIFFReadDirEntryCheckRangeLong8Sbyte(*ma);
×
2510
                if (err != TIFFReadDirEntryErrOk)
×
2511
                    break;
×
2512
                *mb++ = (uint64_t)(*ma++);
×
2513
            }
2514
        }
2515
        break;
×
2516
        case TIFF_SHORT:
3,988✔
2517
        {
2518
            uint16_t *ma;
2519
            uint64_t *mb;
2520
            uint32_t n;
2521
            ma = (uint16_t *)origdata;
3,988✔
2522
            mb = data;
3,988✔
2523
            for (n = 0; n < count; n++)
3,412,760✔
2524
            {
2525
                if (tif->tif_flags & TIFF_SWAB)
3,408,770✔
2526
                    TIFFSwabShort(ma);
13,767✔
2527
                *mb++ = (uint64_t)(*ma++);
3,408,770✔
2528
            }
2529
        }
2530
        break;
3,988✔
2531
        case TIFF_SSHORT:
×
2532
        {
2533
            int16_t *ma;
2534
            uint64_t *mb;
2535
            uint32_t n;
2536
            ma = (int16_t *)origdata;
×
2537
            mb = data;
×
2538
            for (n = 0; n < count; n++)
×
2539
            {
2540
                if (tif->tif_flags & TIFF_SWAB)
×
2541
                    TIFFSwabShort((uint16_t *)ma);
×
2542
                err = TIFFReadDirEntryCheckRangeLong8Sshort(*ma);
×
2543
                if (err != TIFFReadDirEntryErrOk)
×
2544
                    break;
×
2545
                *mb++ = (uint64_t)(*ma++);
×
2546
            }
2547
        }
2548
        break;
×
2549
        case TIFF_LONG:
77,417✔
2550
        {
2551
            uint32_t *ma;
2552
            uint64_t *mb;
2553
            uint32_t n;
2554
            ma = (uint32_t *)origdata;
77,417✔
2555
            mb = data;
77,417✔
2556
            for (n = 0; n < count; n++)
7,793,780✔
2557
            {
2558
                if (tif->tif_flags & TIFF_SWAB)
7,716,370✔
2559
                    TIFFSwabLong(ma);
415,494✔
2560
                *mb++ = (uint64_t)(*ma++);
7,716,360✔
2561
            }
2562
        }
2563
        break;
77,404✔
2564
        case TIFF_SLONG:
×
2565
        {
2566
            int32_t *ma;
2567
            uint64_t *mb;
2568
            uint32_t n;
2569
            ma = (int32_t *)origdata;
×
2570
            mb = data;
×
2571
            for (n = 0; n < count; n++)
×
2572
            {
2573
                if (tif->tif_flags & TIFF_SWAB)
×
2574
                    TIFFSwabLong((uint32_t *)ma);
×
2575
                err = TIFFReadDirEntryCheckRangeLong8Slong(*ma);
×
2576
                if (err != TIFFReadDirEntryErrOk)
×
2577
                    break;
×
2578
                *mb++ = (uint64_t)(*ma++);
×
2579
            }
2580
        }
2581
        break;
×
2582
    }
2583
    _TIFFfreeExt(tif, origdata);
81,403✔
2584
    if (err != TIFFReadDirEntryErrOk)
81,409✔
2585
    {
2586
        _TIFFfreeExt(tif, data);
21✔
2587
        return (err);
×
2588
    }
2589
    *value = data;
81,388✔
2590
    return (TIFFReadDirEntryErrOk);
81,388✔
2591
}
2592

2593
static enum TIFFReadDirEntryErr
2594
TIFFReadDirEntryLong8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
6✔
2595
{
2596
    return TIFFReadDirEntryLong8ArrayWithLimit(tif, direntry, value,
6✔
2597
                                               ~((uint64_t)0));
2598
}
2599

2600
static enum TIFFReadDirEntryErr
2601
TIFFReadDirEntrySlong8Array(TIFF *tif, TIFFDirEntry *direntry, int64_t **value)
×
2602
{
2603
    enum TIFFReadDirEntryErr err;
2604
    uint32_t count;
2605
    void *origdata;
2606
    int64_t *data;
2607
    switch (direntry->tdir_type)
×
2608
    {
2609
        case TIFF_BYTE:
×
2610
        case TIFF_SBYTE:
2611
        case TIFF_SHORT:
2612
        case TIFF_SSHORT:
2613
        case TIFF_LONG:
2614
        case TIFF_SLONG:
2615
        case TIFF_LONG8:
2616
        case TIFF_SLONG8:
2617
            break;
×
2618
        default:
×
2619
            return (TIFFReadDirEntryErrType);
×
2620
    }
2621
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
×
2622
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
×
2623
    {
2624
        *value = 0;
×
2625
        return (err);
×
2626
    }
2627
    switch (direntry->tdir_type)
×
2628
    {
2629
        case TIFF_LONG8:
×
2630
        {
2631
            uint64_t *m;
2632
            uint32_t n;
2633
            m = (uint64_t *)origdata;
×
2634
            for (n = 0; n < count; n++)
×
2635
            {
2636
                if (tif->tif_flags & TIFF_SWAB)
×
2637
                    TIFFSwabLong8(m);
×
2638
                err = TIFFReadDirEntryCheckRangeSlong8Long8(*m);
×
2639
                if (err != TIFFReadDirEntryErrOk)
×
2640
                {
2641
                    _TIFFfreeExt(tif, origdata);
×
2642
                    return (err);
×
2643
                }
2644
                m++;
×
2645
            }
2646
            *value = (int64_t *)origdata;
×
2647
            return (TIFFReadDirEntryErrOk);
×
2648
        }
2649
        case TIFF_SLONG8:
×
2650
            *value = (int64_t *)origdata;
×
2651
            if (tif->tif_flags & TIFF_SWAB)
×
2652
                TIFFSwabArrayOfLong8((uint64_t *)(*value), count);
×
2653
            return (TIFFReadDirEntryErrOk);
×
2654
    }
2655
    data = (int64_t *)_TIFFmallocExt(tif, count * 8);
×
2656
    if (data == 0)
×
2657
    {
2658
        _TIFFfreeExt(tif, origdata);
×
2659
        return (TIFFReadDirEntryErrAlloc);
×
2660
    }
2661
    switch (direntry->tdir_type)
×
2662
    {
2663
        case TIFF_BYTE:
×
2664
        {
2665
            uint8_t *ma;
2666
            int64_t *mb;
2667
            uint32_t n;
2668
            ma = (uint8_t *)origdata;
×
2669
            mb = data;
×
2670
            for (n = 0; n < count; n++)
×
2671
                *mb++ = (int64_t)(*ma++);
×
2672
        }
2673
        break;
×
2674
        case TIFF_SBYTE:
×
2675
        {
2676
            int8_t *ma;
2677
            int64_t *mb;
2678
            uint32_t n;
2679
            ma = (int8_t *)origdata;
×
2680
            mb = data;
×
2681
            for (n = 0; n < count; n++)
×
2682
                *mb++ = (int64_t)(*ma++);
×
2683
        }
2684
        break;
×
2685
        case TIFF_SHORT:
×
2686
        {
2687
            uint16_t *ma;
2688
            int64_t *mb;
2689
            uint32_t n;
2690
            ma = (uint16_t *)origdata;
×
2691
            mb = data;
×
2692
            for (n = 0; n < count; n++)
×
2693
            {
2694
                if (tif->tif_flags & TIFF_SWAB)
×
2695
                    TIFFSwabShort(ma);
×
2696
                *mb++ = (int64_t)(*ma++);
×
2697
            }
2698
        }
2699
        break;
×
2700
        case TIFF_SSHORT:
×
2701
        {
2702
            int16_t *ma;
2703
            int64_t *mb;
2704
            uint32_t n;
2705
            ma = (int16_t *)origdata;
×
2706
            mb = data;
×
2707
            for (n = 0; n < count; n++)
×
2708
            {
2709
                if (tif->tif_flags & TIFF_SWAB)
×
2710
                    TIFFSwabShort((uint16_t *)ma);
×
2711
                *mb++ = (int64_t)(*ma++);
×
2712
            }
2713
        }
2714
        break;
×
2715
        case TIFF_LONG:
×
2716
        {
2717
            uint32_t *ma;
2718
            int64_t *mb;
2719
            uint32_t n;
2720
            ma = (uint32_t *)origdata;
×
2721
            mb = data;
×
2722
            for (n = 0; n < count; n++)
×
2723
            {
2724
                if (tif->tif_flags & TIFF_SWAB)
×
2725
                    TIFFSwabLong(ma);
×
2726
                *mb++ = (int64_t)(*ma++);
×
2727
            }
2728
        }
2729
        break;
×
2730
        case TIFF_SLONG:
×
2731
        {
2732
            int32_t *ma;
2733
            int64_t *mb;
2734
            uint32_t n;
2735
            ma = (int32_t *)origdata;
×
2736
            mb = data;
×
2737
            for (n = 0; n < count; n++)
×
2738
            {
2739
                if (tif->tif_flags & TIFF_SWAB)
×
2740
                    TIFFSwabLong((uint32_t *)ma);
×
2741
                *mb++ = (int64_t)(*ma++);
×
2742
            }
2743
        }
2744
        break;
×
2745
    }
2746
    _TIFFfreeExt(tif, origdata);
×
2747
    *value = data;
×
2748
    return (TIFFReadDirEntryErrOk);
×
2749
}
2750

2751
static enum TIFFReadDirEntryErr
2752
TIFFReadDirEntryFloatArray(TIFF *tif, TIFFDirEntry *direntry, float **value)
1,084✔
2753
{
2754
    enum TIFFReadDirEntryErr err;
2755
    uint32_t count;
2756
    void *origdata;
2757
    float *data;
2758
    switch (direntry->tdir_type)
1,084✔
2759
    {
2760
        case TIFF_BYTE:
1,084✔
2761
        case TIFF_SBYTE:
2762
        case TIFF_SHORT:
2763
        case TIFF_SSHORT:
2764
        case TIFF_LONG:
2765
        case TIFF_SLONG:
2766
        case TIFF_LONG8:
2767
        case TIFF_SLONG8:
2768
        case TIFF_RATIONAL:
2769
        case TIFF_SRATIONAL:
2770
        case TIFF_FLOAT:
2771
        case TIFF_DOUBLE:
2772
            break;
1,084✔
2773
        default:
×
2774
            return (TIFFReadDirEntryErrType);
×
2775
    }
2776
    err = TIFFReadDirEntryArray(tif, direntry, &count, 4, &origdata);
1,084✔
2777
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
1,084✔
2778
    {
2779
        *value = 0;
×
2780
        return (err);
×
2781
    }
2782
    switch (direntry->tdir_type)
1,084✔
2783
    {
2784
        case TIFF_FLOAT:
×
2785
            if (tif->tif_flags & TIFF_SWAB)
×
2786
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
×
2787
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
2788
            *value = (float *)origdata;
×
2789
            return (TIFFReadDirEntryErrOk);
×
2790
    }
2791
    data = (float *)_TIFFmallocExt(tif, count * sizeof(float));
1,084✔
2792
    if (data == 0)
1,084✔
2793
    {
2794
        _TIFFfreeExt(tif, origdata);
×
2795
        return (TIFFReadDirEntryErrAlloc);
×
2796
    }
2797
    switch (direntry->tdir_type)
1,084✔
2798
    {
2799
        case TIFF_BYTE:
×
2800
        {
2801
            uint8_t *ma;
2802
            float *mb;
2803
            uint32_t n;
2804
            ma = (uint8_t *)origdata;
×
2805
            mb = data;
×
2806
            for (n = 0; n < count; n++)
×
2807
                *mb++ = (float)(*ma++);
×
2808
        }
2809
        break;
×
2810
        case TIFF_SBYTE:
×
2811
        {
2812
            int8_t *ma;
2813
            float *mb;
2814
            uint32_t n;
2815
            ma = (int8_t *)origdata;
×
2816
            mb = data;
×
2817
            for (n = 0; n < count; n++)
×
2818
                *mb++ = (float)(*ma++);
×
2819
        }
2820
        break;
×
2821
        case TIFF_SHORT:
×
2822
        {
2823
            uint16_t *ma;
2824
            float *mb;
2825
            uint32_t n;
2826
            ma = (uint16_t *)origdata;
×
2827
            mb = data;
×
2828
            for (n = 0; n < count; n++)
×
2829
            {
2830
                if (tif->tif_flags & TIFF_SWAB)
×
2831
                    TIFFSwabShort(ma);
×
2832
                *mb++ = (float)(*ma++);
×
2833
            }
2834
        }
2835
        break;
×
2836
        case TIFF_SSHORT:
×
2837
        {
2838
            int16_t *ma;
2839
            float *mb;
2840
            uint32_t n;
2841
            ma = (int16_t *)origdata;
×
2842
            mb = data;
×
2843
            for (n = 0; n < count; n++)
×
2844
            {
2845
                if (tif->tif_flags & TIFF_SWAB)
×
2846
                    TIFFSwabShort((uint16_t *)ma);
×
2847
                *mb++ = (float)(*ma++);
×
2848
            }
2849
        }
2850
        break;
×
2851
        case TIFF_LONG:
2✔
2852
        {
2853
            uint32_t *ma;
2854
            float *mb;
2855
            uint32_t n;
2856
            ma = (uint32_t *)origdata;
2✔
2857
            mb = data;
2✔
2858
            for (n = 0; n < count; n++)
14✔
2859
            {
2860
                if (tif->tif_flags & TIFF_SWAB)
12✔
2861
                    TIFFSwabLong(ma);
12✔
2862
                *mb++ = (float)(*ma++);
12✔
2863
            }
2864
        }
2865
        break;
2✔
2866
        case TIFF_SLONG:
×
2867
        {
2868
            int32_t *ma;
2869
            float *mb;
2870
            uint32_t n;
2871
            ma = (int32_t *)origdata;
×
2872
            mb = data;
×
2873
            for (n = 0; n < count; n++)
×
2874
            {
2875
                if (tif->tif_flags & TIFF_SWAB)
×
2876
                    TIFFSwabLong((uint32_t *)ma);
×
2877
                *mb++ = (float)(*ma++);
×
2878
            }
2879
        }
2880
        break;
×
2881
        case TIFF_LONG8:
×
2882
        {
2883
            uint64_t *ma;
2884
            float *mb;
2885
            uint32_t n;
2886
            ma = (uint64_t *)origdata;
×
2887
            mb = data;
×
2888
            for (n = 0; n < count; n++)
×
2889
            {
2890
                if (tif->tif_flags & TIFF_SWAB)
×
2891
                    TIFFSwabLong8(ma);
×
2892
                *mb++ = (float)(*ma++);
×
2893
            }
2894
        }
2895
        break;
×
2896
        case TIFF_SLONG8:
×
2897
        {
2898
            int64_t *ma;
2899
            float *mb;
2900
            uint32_t n;
2901
            ma = (int64_t *)origdata;
×
2902
            mb = data;
×
2903
            for (n = 0; n < count; n++)
×
2904
            {
2905
                if (tif->tif_flags & TIFF_SWAB)
×
2906
                    TIFFSwabLong8((uint64_t *)ma);
×
2907
                *mb++ = (float)(*ma++);
×
2908
            }
2909
        }
2910
        break;
×
2911
        case TIFF_RATIONAL:
1,082✔
2912
        {
2913
            uint32_t *ma;
2914
            uint32_t maa;
2915
            uint32_t mab;
2916
            float *mb;
2917
            uint32_t n;
2918
            ma = (uint32_t *)origdata;
1,082✔
2919
            mb = data;
1,082✔
2920
            for (n = 0; n < count; n++)
7,455✔
2921
            {
2922
                if (tif->tif_flags & TIFF_SWAB)
6,373✔
2923
                    TIFFSwabLong(ma);
96✔
2924
                maa = *ma++;
6,373✔
2925
                if (tif->tif_flags & TIFF_SWAB)
6,373✔
2926
                    TIFFSwabLong(ma);
96✔
2927
                mab = *ma++;
6,373✔
2928
                if (mab == 0)
6,373✔
2929
                    *mb++ = 0.0;
×
2930
                else
2931
                    *mb++ = (float)maa / (float)mab;
6,373✔
2932
            }
2933
        }
2934
        break;
1,082✔
2935
        case TIFF_SRATIONAL:
×
2936
        {
2937
            uint32_t *ma;
2938
            int32_t maa;
2939
            uint32_t mab;
2940
            float *mb;
2941
            uint32_t n;
2942
            ma = (uint32_t *)origdata;
×
2943
            mb = data;
×
2944
            for (n = 0; n < count; n++)
×
2945
            {
2946
                if (tif->tif_flags & TIFF_SWAB)
×
2947
                    TIFFSwabLong(ma);
×
2948
                maa = *(int32_t *)ma;
×
2949
                ma++;
×
2950
                if (tif->tif_flags & TIFF_SWAB)
×
2951
                    TIFFSwabLong(ma);
×
2952
                mab = *ma++;
×
2953
                if (mab == 0)
×
2954
                    *mb++ = 0.0;
×
2955
                else
2956
                    *mb++ = (float)maa / (float)mab;
×
2957
            }
2958
        }
2959
        break;
×
2960
        case TIFF_DOUBLE:
×
2961
        {
2962
            double *ma;
2963
            float *mb;
2964
            uint32_t n;
2965
            if (tif->tif_flags & TIFF_SWAB)
×
2966
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
×
2967
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
2968
            ma = (double *)origdata;
×
2969
            mb = data;
×
2970
            for (n = 0; n < count; n++)
×
2971
            {
2972
                double val = *ma++;
×
2973
                if (val > FLT_MAX)
×
2974
                    val = FLT_MAX;
×
2975
                else if (val < -FLT_MAX)
×
2976
                    val = -FLT_MAX;
×
2977
                *mb++ = (float)val;
×
2978
            }
2979
        }
2980
        break;
×
2981
    }
2982
    _TIFFfreeExt(tif, origdata);
1,084✔
2983
    *value = data;
1,084✔
2984
    return (TIFFReadDirEntryErrOk);
1,084✔
2985
}
2986

2987
static enum TIFFReadDirEntryErr
2988
TIFFReadDirEntryDoubleArray(TIFF *tif, TIFFDirEntry *direntry, double **value)
66,719✔
2989
{
2990
    enum TIFFReadDirEntryErr err;
2991
    uint32_t count;
2992
    void *origdata;
2993
    double *data;
2994
    switch (direntry->tdir_type)
66,719✔
2995
    {
2996
        case TIFF_BYTE:
66,698✔
2997
        case TIFF_SBYTE:
2998
        case TIFF_SHORT:
2999
        case TIFF_SSHORT:
3000
        case TIFF_LONG:
3001
        case TIFF_SLONG:
3002
        case TIFF_LONG8:
3003
        case TIFF_SLONG8:
3004
        case TIFF_RATIONAL:
3005
        case TIFF_SRATIONAL:
3006
        case TIFF_FLOAT:
3007
        case TIFF_DOUBLE:
3008
            break;
66,698✔
3009
        default:
21✔
3010
            return (TIFFReadDirEntryErrType);
21✔
3011
    }
3012
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
66,698✔
3013
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
66,689✔
3014
    {
3015
        *value = 0;
30✔
3016
        return (err);
30✔
3017
    }
3018
    switch (direntry->tdir_type)
66,659✔
3019
    {
3020
        case TIFF_DOUBLE:
66,656✔
3021
            if (tif->tif_flags & TIFF_SWAB)
66,656✔
3022
                TIFFSwabArrayOfLong8((uint64_t *)origdata, count);
291✔
3023
            TIFFCvtIEEEDoubleToNative(tif, count, (double *)origdata);
3024
            *value = (double *)origdata;
66,667✔
3025
            return (TIFFReadDirEntryErrOk);
66,667✔
3026
    }
3027
    data = (double *)_TIFFmallocExt(tif, count * sizeof(double));
3✔
3028
    if (data == 0)
×
3029
    {
3030
        _TIFFfreeExt(tif, origdata);
×
3031
        return (TIFFReadDirEntryErrAlloc);
×
3032
    }
3033
    switch (direntry->tdir_type)
×
3034
    {
3035
        case TIFF_BYTE:
×
3036
        {
3037
            uint8_t *ma;
3038
            double *mb;
3039
            uint32_t n;
3040
            ma = (uint8_t *)origdata;
×
3041
            mb = data;
×
3042
            for (n = 0; n < count; n++)
×
3043
                *mb++ = (double)(*ma++);
×
3044
        }
3045
        break;
×
3046
        case TIFF_SBYTE:
×
3047
        {
3048
            int8_t *ma;
3049
            double *mb;
3050
            uint32_t n;
3051
            ma = (int8_t *)origdata;
×
3052
            mb = data;
×
3053
            for (n = 0; n < count; n++)
×
3054
                *mb++ = (double)(*ma++);
×
3055
        }
3056
        break;
×
3057
        case TIFF_SHORT:
×
3058
        {
3059
            uint16_t *ma;
3060
            double *mb;
3061
            uint32_t n;
3062
            ma = (uint16_t *)origdata;
×
3063
            mb = data;
×
3064
            for (n = 0; n < count; n++)
×
3065
            {
3066
                if (tif->tif_flags & TIFF_SWAB)
×
3067
                    TIFFSwabShort(ma);
×
3068
                *mb++ = (double)(*ma++);
×
3069
            }
3070
        }
3071
        break;
×
3072
        case TIFF_SSHORT:
×
3073
        {
3074
            int16_t *ma;
3075
            double *mb;
3076
            uint32_t n;
3077
            ma = (int16_t *)origdata;
×
3078
            mb = data;
×
3079
            for (n = 0; n < count; n++)
×
3080
            {
3081
                if (tif->tif_flags & TIFF_SWAB)
×
3082
                    TIFFSwabShort((uint16_t *)ma);
×
3083
                *mb++ = (double)(*ma++);
×
3084
            }
3085
        }
3086
        break;
×
3087
        case TIFF_LONG:
×
3088
        {
3089
            uint32_t *ma;
3090
            double *mb;
3091
            uint32_t n;
3092
            ma = (uint32_t *)origdata;
×
3093
            mb = data;
×
3094
            for (n = 0; n < count; n++)
×
3095
            {
3096
                if (tif->tif_flags & TIFF_SWAB)
×
3097
                    TIFFSwabLong(ma);
×
3098
                *mb++ = (double)(*ma++);
×
3099
            }
3100
        }
3101
        break;
×
3102
        case TIFF_SLONG:
×
3103
        {
3104
            int32_t *ma;
3105
            double *mb;
3106
            uint32_t n;
3107
            ma = (int32_t *)origdata;
×
3108
            mb = data;
×
3109
            for (n = 0; n < count; n++)
×
3110
            {
3111
                if (tif->tif_flags & TIFF_SWAB)
×
3112
                    TIFFSwabLong((uint32_t *)ma);
×
3113
                *mb++ = (double)(*ma++);
×
3114
            }
3115
        }
3116
        break;
×
3117
        case TIFF_LONG8:
×
3118
        {
3119
            uint64_t *ma;
3120
            double *mb;
3121
            uint32_t n;
3122
            ma = (uint64_t *)origdata;
×
3123
            mb = data;
×
3124
            for (n = 0; n < count; n++)
×
3125
            {
3126
                if (tif->tif_flags & TIFF_SWAB)
×
3127
                    TIFFSwabLong8(ma);
×
3128
                *mb++ = (double)(*ma++);
×
3129
            }
3130
        }
3131
        break;
×
3132
        case TIFF_SLONG8:
×
3133
        {
3134
            int64_t *ma;
3135
            double *mb;
3136
            uint32_t n;
3137
            ma = (int64_t *)origdata;
×
3138
            mb = data;
×
3139
            for (n = 0; n < count; n++)
×
3140
            {
3141
                if (tif->tif_flags & TIFF_SWAB)
×
3142
                    TIFFSwabLong8((uint64_t *)ma);
×
3143
                *mb++ = (double)(*ma++);
×
3144
            }
3145
        }
3146
        break;
×
3147
        case TIFF_RATIONAL:
×
3148
        {
3149
            uint32_t *ma;
3150
            uint32_t maa;
3151
            uint32_t mab;
3152
            double *mb;
3153
            uint32_t n;
3154
            ma = (uint32_t *)origdata;
×
3155
            mb = data;
×
3156
            for (n = 0; n < count; n++)
×
3157
            {
3158
                if (tif->tif_flags & TIFF_SWAB)
×
3159
                    TIFFSwabLong(ma);
×
3160
                maa = *ma++;
×
3161
                if (tif->tif_flags & TIFF_SWAB)
×
3162
                    TIFFSwabLong(ma);
×
3163
                mab = *ma++;
×
3164
                if (mab == 0)
×
3165
                    *mb++ = 0.0;
×
3166
                else
3167
                    *mb++ = (double)maa / (double)mab;
×
3168
            }
3169
        }
3170
        break;
×
3171
        case TIFF_SRATIONAL:
×
3172
        {
3173
            uint32_t *ma;
3174
            int32_t maa;
3175
            uint32_t mab;
3176
            double *mb;
3177
            uint32_t n;
3178
            ma = (uint32_t *)origdata;
×
3179
            mb = data;
×
3180
            for (n = 0; n < count; n++)
×
3181
            {
3182
                if (tif->tif_flags & TIFF_SWAB)
×
3183
                    TIFFSwabLong(ma);
×
3184
                maa = *(int32_t *)ma;
×
3185
                ma++;
×
3186
                if (tif->tif_flags & TIFF_SWAB)
×
3187
                    TIFFSwabLong(ma);
×
3188
                mab = *ma++;
×
3189
                if (mab == 0)
×
3190
                    *mb++ = 0.0;
×
3191
                else
3192
                    *mb++ = (double)maa / (double)mab;
×
3193
            }
3194
        }
3195
        break;
×
3196
        case TIFF_FLOAT:
×
3197
        {
3198
            float *ma;
3199
            double *mb;
3200
            uint32_t n;
3201
            if (tif->tif_flags & TIFF_SWAB)
×
3202
                TIFFSwabArrayOfLong((uint32_t *)origdata, count);
×
3203
            TIFFCvtIEEEFloatToNative(tif, count, (float *)origdata);
3204
            ma = (float *)origdata;
×
3205
            mb = data;
×
3206
            for (n = 0; n < count; n++)
×
3207
                *mb++ = (double)(*ma++);
×
3208
        }
3209
        break;
×
3210
    }
3211
    _TIFFfreeExt(tif, origdata);
×
3212
    *value = data;
×
3213
    return (TIFFReadDirEntryErrOk);
×
3214
}
3215

3216
static enum TIFFReadDirEntryErr
3217
TIFFReadDirEntryIfd8Array(TIFF *tif, TIFFDirEntry *direntry, uint64_t **value)
78✔
3218
{
3219
    enum TIFFReadDirEntryErr err;
3220
    uint32_t count;
3221
    void *origdata;
3222
    uint64_t *data;
3223
    switch (direntry->tdir_type)
78✔
3224
    {
3225
        case TIFF_LONG:
78✔
3226
        case TIFF_LONG8:
3227
        case TIFF_IFD:
3228
        case TIFF_IFD8:
3229
            break;
78✔
3230
        default:
×
3231
            return (TIFFReadDirEntryErrType);
×
3232
    }
3233
    err = TIFFReadDirEntryArray(tif, direntry, &count, 8, &origdata);
78✔
3234
    if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
78✔
3235
    {
3236
        *value = 0;
×
3237
        return (err);
×
3238
    }
3239
    switch (direntry->tdir_type)
78✔
3240
    {
3241
        case TIFF_LONG8:
×
3242
        case TIFF_IFD8:
3243
            *value = (uint64_t *)origdata;
×
3244
            if (tif->tif_flags & TIFF_SWAB)
×
3245
                TIFFSwabArrayOfLong8(*value, count);
×
3246
            return (TIFFReadDirEntryErrOk);
×
3247
    }
3248
    data = (uint64_t *)_TIFFmallocExt(tif, count * 8);
78✔
3249
    if (data == 0)
78✔
3250
    {
3251
        _TIFFfreeExt(tif, origdata);
×
3252
        return (TIFFReadDirEntryErrAlloc);
×
3253
    }
3254
    switch (direntry->tdir_type)
78✔
3255
    {
3256
        case TIFF_LONG:
78✔
3257
        case TIFF_IFD:
3258
        {
3259
            uint32_t *ma;
3260
            uint64_t *mb;
3261
            uint32_t n;
3262
            ma = (uint32_t *)origdata;
78✔
3263
            mb = data;
78✔
3264
            for (n = 0; n < count; n++)
234✔
3265
            {
3266
                if (tif->tif_flags & TIFF_SWAB)
156✔
3267
                    TIFFSwabLong(ma);
×
3268
                *mb++ = (uint64_t)(*ma++);
156✔
3269
            }
3270
        }
3271
        break;
78✔
3272
    }
3273
    _TIFFfreeExt(tif, origdata);
78✔
3274
    *value = data;
78✔
3275
    return (TIFFReadDirEntryErrOk);
78✔
3276
}
3277

3278
static enum TIFFReadDirEntryErr
3279
TIFFReadDirEntryPersampleShort(TIFF *tif, TIFFDirEntry *direntry,
31,419✔
3280
                               uint16_t *value)
3281
{
3282
    enum TIFFReadDirEntryErr err;
3283
    uint16_t *m;
3284
    uint16_t *na;
3285
    uint16_t nb;
3286
    if (direntry->tdir_count < (uint64_t)tif->tif_dir.td_samplesperpixel)
31,419✔
3287
        return (TIFFReadDirEntryErrCount);
×
3288
    err = TIFFReadDirEntryShortArray(tif, direntry, &m);
31,419✔
3289
    if (err != TIFFReadDirEntryErrOk || m == NULL)
31,417✔
3290
        return (err);
9✔
3291
    na = m;
31,408✔
3292
    nb = tif->tif_dir.td_samplesperpixel;
31,408✔
3293
    *value = *na++;
31,408✔
3294
    nb--;
31,408✔
3295
    while (nb > 0)
2,589,290✔
3296
    {
3297
        if (*na++ != *value)
2,557,880✔
3298
        {
3299
            err = TIFFReadDirEntryErrPsdif;
×
3300
            break;
×
3301
        }
3302
        nb--;
2,557,880✔
3303
    }
3304
    _TIFFfreeExt(tif, m);
31,408✔
3305
    return (err);
31,416✔
3306
}
3307

3308
static void TIFFReadDirEntryCheckedByte(TIFF *tif, TIFFDirEntry *direntry,
×
3309
                                        uint8_t *value)
3310
{
3311
    (void)tif;
3312
    *value = *(uint8_t *)(&direntry->tdir_offset);
×
3313
}
×
3314

3315
static void TIFFReadDirEntryCheckedSbyte(TIFF *tif, TIFFDirEntry *direntry,
×
3316
                                         int8_t *value)
3317
{
3318
    (void)tif;
3319
    *value = *(int8_t *)(&direntry->tdir_offset);
×
3320
}
×
3321

3322
static void TIFFReadDirEntryCheckedShort(TIFF *tif, TIFFDirEntry *direntry,
450,329✔
3323
                                         uint16_t *value)
3324
{
3325
    *value = direntry->tdir_offset.toff_short;
450,329✔
3326
    /* *value=*(uint16_t*)(&direntry->tdir_offset); */
3327
    if (tif->tif_flags & TIFF_SWAB)
450,329✔
3328
        TIFFSwabShort(value);
4,781✔
3329
}
450,329✔
3330

3331
static void TIFFReadDirEntryCheckedSshort(TIFF *tif, TIFFDirEntry *direntry,
×
3332
                                          int16_t *value)
3333
{
3334
    *value = *(int16_t *)(&direntry->tdir_offset);
×
3335
    if (tif->tif_flags & TIFF_SWAB)
×
3336
        TIFFSwabShort((uint16_t *)value);
×
3337
}
×
3338

3339
static void TIFFReadDirEntryCheckedLong(TIFF *tif, TIFFDirEntry *direntry,
5,966✔
3340
                                        uint32_t *value)
3341
{
3342
    *value = *(uint32_t *)(&direntry->tdir_offset);
5,966✔
3343
    if (tif->tif_flags & TIFF_SWAB)
5,966✔
3344
        TIFFSwabLong(value);
114✔
3345
}
5,966✔
3346

3347
static void TIFFReadDirEntryCheckedSlong(TIFF *tif, TIFFDirEntry *direntry,
×
3348
                                         int32_t *value)
3349
{
3350
    *value = *(int32_t *)(&direntry->tdir_offset);
×
3351
    if (tif->tif_flags & TIFF_SWAB)
×
3352
        TIFFSwabLong((uint32_t *)value);
×
3353
}
×
3354

3355
static enum TIFFReadDirEntryErr
3356
TIFFReadDirEntryCheckedLong8(TIFF *tif, TIFFDirEntry *direntry, uint64_t *value)
×
3357
{
3358
    if (!(tif->tif_flags & TIFF_BIGTIFF))
×
3359
    {
3360
        enum TIFFReadDirEntryErr err;
3361
        uint32_t offset = direntry->tdir_offset.toff_long;
×
3362
        if (tif->tif_flags & TIFF_SWAB)
×
3363
            TIFFSwabLong(&offset);
×
3364
        err = TIFFReadDirEntryData(tif, offset, 8, value);
×
3365
        if (err != TIFFReadDirEntryErrOk)
×
3366
            return (err);
×
3367
    }
3368
    else
3369
        *value = direntry->tdir_offset.toff_long8;
×
3370
    if (tif->tif_flags & TIFF_SWAB)
×
3371
        TIFFSwabLong8(value);
×
3372
    return (TIFFReadDirEntryErrOk);
×
3373
}
3374

3375
static enum TIFFReadDirEntryErr
3376
TIFFReadDirEntryCheckedSlong8(TIFF *tif, TIFFDirEntry *direntry, int64_t *value)
×
3377
{
3378
    if (!(tif->tif_flags & TIFF_BIGTIFF))
×
3379
    {
3380
        enum TIFFReadDirEntryErr err;
3381
        uint32_t offset = direntry->tdir_offset.toff_long;
×
3382
        if (tif->tif_flags & TIFF_SWAB)
×
3383
            TIFFSwabLong(&offset);
×
3384
        err = TIFFReadDirEntryData(tif, offset, 8, value);
×
3385
        if (err != TIFFReadDirEntryErrOk)
×
3386
            return (err);
×
3387
    }
3388
    else
3389
        *value = *(int64_t *)(&direntry->tdir_offset);
×
3390
    if (tif->tif_flags & TIFF_SWAB)
×
3391
        TIFFSwabLong8((uint64_t *)value);
×
3392
    return (TIFFReadDirEntryErrOk);
×
3393
}
3394

3395
static enum TIFFReadDirEntryErr
3396
TIFFReadDirEntryCheckedRational(TIFF *tif, TIFFDirEntry *direntry,
248✔
3397
                                double *value)
3398
{
3399
    UInt64Aligned_t m;
3400

3401
    assert(sizeof(double) == 8);
3402
    assert(sizeof(uint64_t) == 8);
3403
    assert(sizeof(uint32_t) == 4);
3404
    if (!(tif->tif_flags & TIFF_BIGTIFF))
248✔
3405
    {
3406
        enum TIFFReadDirEntryErr err;
3407
        uint32_t offset = direntry->tdir_offset.toff_long;
248✔
3408
        if (tif->tif_flags & TIFF_SWAB)
248✔
3409
            TIFFSwabLong(&offset);
8✔
3410
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
248✔
3411
        if (err != TIFFReadDirEntryErrOk)
248✔
3412
            return (err);
×
3413
    }
3414
    else
3415
        m.l = direntry->tdir_offset.toff_long8;
×
3416
    if (tif->tif_flags & TIFF_SWAB)
248✔
3417
        TIFFSwabArrayOfLong(m.i, 2);
8✔
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
    if (m.i[0] == 0 || m.i[1] == 0)
248✔
3422
        *value = 0.0;
36✔
3423
    else
3424
        *value = (double)m.i[0] / (double)m.i[1];
212✔
3425
    return (TIFFReadDirEntryErrOk);
248✔
3426
}
3427

3428
static enum TIFFReadDirEntryErr
3429
TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
×
3430
                                 double *value)
3431
{
3432
    UInt64Aligned_t m;
3433
    assert(sizeof(double) == 8);
3434
    assert(sizeof(uint64_t) == 8);
3435
    assert(sizeof(int32_t) == 4);
3436
    assert(sizeof(uint32_t) == 4);
3437
    if (!(tif->tif_flags & TIFF_BIGTIFF))
×
3438
    {
3439
        enum TIFFReadDirEntryErr err;
3440
        uint32_t offset = direntry->tdir_offset.toff_long;
×
3441
        if (tif->tif_flags & TIFF_SWAB)
×
3442
            TIFFSwabLong(&offset);
×
3443
        err = TIFFReadDirEntryData(tif, offset, 8, m.i);
×
3444
        if (err != TIFFReadDirEntryErrOk)
×
3445
            return (err);
×
3446
    }
3447
    else
3448
        m.l = direntry->tdir_offset.toff_long8;
×
3449
    if (tif->tif_flags & TIFF_SWAB)
×
3450
        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
    if ((int32_t)m.i[0] == 0 || m.i[1] == 0)
×
3455
        *value = 0.0;
×
3456
    else
3457
        *value = (double)((int32_t)m.i[0]) / (double)m.i[1];
×
3458
    return (TIFFReadDirEntryErrOk);
×
3459
}
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
{
3507
    union
3508
    {
3509
        float f;
3510
        uint32_t i;
3511
    } float_union;
3512
    assert(sizeof(float) == 4);
3513
    assert(sizeof(uint32_t) == 4);
3514
    assert(sizeof(float_union) == 4);
3515
    float_union.i = *(uint32_t *)(&direntry->tdir_offset);
×
3516
    *value = float_union.f;
×
3517
    if (tif->tif_flags & TIFF_SWAB)
×
3518
        TIFFSwabLong((uint32_t *)value);
×
3519
}
×
3520

3521
static enum TIFFReadDirEntryErr
3522
TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value)
×
3523
{
3524
    assert(sizeof(double) == 8);
3525
    assert(sizeof(uint64_t) == 8);
3526
    assert(sizeof(UInt64Aligned_t) == 8);
3527
    if (!(tif->tif_flags & TIFF_BIGTIFF))
×
3528
    {
3529
        enum TIFFReadDirEntryErr err;
3530
        uint32_t offset = direntry->tdir_offset.toff_long;
×
3531
        if (tif->tif_flags & TIFF_SWAB)
×
3532
            TIFFSwabLong(&offset);
×
3533
        err = TIFFReadDirEntryData(tif, offset, 8, value);
×
3534
        if (err != TIFFReadDirEntryErrOk)
×
3535
            return (err);
×
3536
    }
3537
    else
3538
    {
3539
        UInt64Aligned_t uint64_union;
3540
        uint64_union.l = direntry->tdir_offset.toff_long8;
×
3541
        *value = uint64_union.d;
×
3542
    }
3543
    if (tif->tif_flags & TIFF_SWAB)
×
3544
        TIFFSwabLong8((uint64_t *)value);
×
3545
    return (TIFFReadDirEntryErrOk);
×
3546
}
3547

3548
static enum TIFFReadDirEntryErr
3549
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value)
×
3550
{
3551
    if (value < 0)
×
3552
        return (TIFFReadDirEntryErrRange);
×
3553
    else
3554
        return (TIFFReadDirEntryErrOk);
×
3555
}
3556

3557
static enum TIFFReadDirEntryErr
3558
TIFFReadDirEntryCheckRangeByteShort(uint16_t value)
×
3559
{
3560
    if (value > 0xFF)
×
3561
        return (TIFFReadDirEntryErrRange);
×
3562
    else
3563
        return (TIFFReadDirEntryErrOk);
×
3564
}
3565

3566
static enum TIFFReadDirEntryErr
3567
TIFFReadDirEntryCheckRangeByteSshort(int16_t value)
×
3568
{
3569
    if ((value < 0) || (value > 0xFF))
×
3570
        return (TIFFReadDirEntryErrRange);
×
3571
    else
3572
        return (TIFFReadDirEntryErrOk);
×
3573
}
3574

3575
static enum TIFFReadDirEntryErr
3576
TIFFReadDirEntryCheckRangeByteLong(uint32_t value)
×
3577
{
3578
    if (value > 0xFF)
×
3579
        return (TIFFReadDirEntryErrRange);
×
3580
    else
3581
        return (TIFFReadDirEntryErrOk);
×
3582
}
3583

3584
static enum TIFFReadDirEntryErr
3585
TIFFReadDirEntryCheckRangeByteSlong(int32_t value)
×
3586
{
3587
    if ((value < 0) || (value > 0xFF))
×
3588
        return (TIFFReadDirEntryErrRange);
×
3589
    else
3590
        return (TIFFReadDirEntryErrOk);
×
3591
}
3592

3593
static enum TIFFReadDirEntryErr
3594
TIFFReadDirEntryCheckRangeByteLong8(uint64_t value)
×
3595
{
3596
    if (value > 0xFF)
×
3597
        return (TIFFReadDirEntryErrRange);
×
3598
    else
3599
        return (TIFFReadDirEntryErrOk);
×
3600
}
3601

3602
static enum TIFFReadDirEntryErr
3603
TIFFReadDirEntryCheckRangeByteSlong8(int64_t value)
×
3604
{
3605
    if ((value < 0) || (value > 0xFF))
×
3606
        return (TIFFReadDirEntryErrRange);
×
3607
    else
3608
        return (TIFFReadDirEntryErrOk);
×
3609
}
3610

3611
static enum TIFFReadDirEntryErr
3612
TIFFReadDirEntryCheckRangeSbyteByte(uint8_t value)
×
3613
{
3614
    if (value > 0x7F)
×
3615
        return (TIFFReadDirEntryErrRange);
×
3616
    else
3617
        return (TIFFReadDirEntryErrOk);
×
3618
}
3619

3620
static enum TIFFReadDirEntryErr
3621
TIFFReadDirEntryCheckRangeSbyteShort(uint16_t value)
×
3622
{
3623
    if (value > 0x7F)
×
3624
        return (TIFFReadDirEntryErrRange);
×
3625
    else
3626
        return (TIFFReadDirEntryErrOk);
×
3627
}
3628

3629
static enum TIFFReadDirEntryErr
3630
TIFFReadDirEntryCheckRangeSbyteSshort(int16_t value)
×
3631
{
3632
    if ((value < -0x80) || (value > 0x7F))
×
3633
        return (TIFFReadDirEntryErrRange);
×
3634
    else
3635
        return (TIFFReadDirEntryErrOk);
×
3636
}
3637

3638
static enum TIFFReadDirEntryErr
3639
TIFFReadDirEntryCheckRangeSbyteLong(uint32_t value)
×
3640
{
3641
    if (value > 0x7F)
×
3642
        return (TIFFReadDirEntryErrRange);
×
3643
    else
3644
        return (TIFFReadDirEntryErrOk);
×
3645
}
3646

3647
static enum TIFFReadDirEntryErr
3648
TIFFReadDirEntryCheckRangeSbyteSlong(int32_t value)
×
3649
{
3650
    if ((value < -0x80) || (value > 0x7F))
×
3651
        return (TIFFReadDirEntryErrRange);
×
3652
    else
3653
        return (TIFFReadDirEntryErrOk);
×
3654
}
3655

3656
static enum TIFFReadDirEntryErr
3657
TIFFReadDirEntryCheckRangeSbyteLong8(uint64_t value)
×
3658
{
3659
    if (value > 0x7F)
×
3660
        return (TIFFReadDirEntryErrRange);
×
3661
    else
3662
        return (TIFFReadDirEntryErrOk);
×
3663
}
3664

3665
static enum TIFFReadDirEntryErr
3666
TIFFReadDirEntryCheckRangeSbyteSlong8(int64_t value)
×
3667
{
3668
    if ((value < -0x80) || (value > 0x7F))
×
3669
        return (TIFFReadDirEntryErrRange);
×
3670
    else
3671
        return (TIFFReadDirEntryErrOk);
×
3672
}
3673

3674
static enum TIFFReadDirEntryErr
3675
TIFFReadDirEntryCheckRangeShortSbyte(int8_t value)
×
3676
{
3677
    if (value < 0)
×
3678
        return (TIFFReadDirEntryErrRange);
×
3679
    else
3680
        return (TIFFReadDirEntryErrOk);
×
3681
}
3682

3683
static enum TIFFReadDirEntryErr
3684
TIFFReadDirEntryCheckRangeShortSshort(int16_t value)
×
3685
{
3686
    if (value < 0)
×
3687
        return (TIFFReadDirEntryErrRange);
×
3688
    else
3689
        return (TIFFReadDirEntryErrOk);
×
3690
}
3691

3692
static enum TIFFReadDirEntryErr
3693
TIFFReadDirEntryCheckRangeShortLong(uint32_t value)
1✔
3694
{
3695
    if (value > 0xFFFF)
1✔
3696
        return (TIFFReadDirEntryErrRange);
×
3697
    else
3698
        return (TIFFReadDirEntryErrOk);
1✔
3699
}
3700

3701
static enum TIFFReadDirEntryErr
3702
TIFFReadDirEntryCheckRangeShortSlong(int32_t value)
×
3703
{
3704
    if ((value < 0) || (value > 0xFFFF))
×
3705
        return (TIFFReadDirEntryErrRange);
×
3706
    else
3707
        return (TIFFReadDirEntryErrOk);
×
3708
}
3709

3710
static enum TIFFReadDirEntryErr
3711
TIFFReadDirEntryCheckRangeShortLong8(uint64_t value)
×
3712
{
3713
    if (value > 0xFFFF)
×
3714
        return (TIFFReadDirEntryErrRange);
×
3715
    else
3716
        return (TIFFReadDirEntryErrOk);
×
3717
}
3718

3719
static enum TIFFReadDirEntryErr
3720
TIFFReadDirEntryCheckRangeShortSlong8(int64_t value)
×
3721
{
3722
    if ((value < 0) || (value > 0xFFFF))
×
3723
        return (TIFFReadDirEntryErrRange);
×
3724
    else
3725
        return (TIFFReadDirEntryErrOk);
×
3726
}
3727

3728
static enum TIFFReadDirEntryErr
3729
TIFFReadDirEntryCheckRangeSshortShort(uint16_t value)
×
3730
{
3731
    if (value > 0x7FFF)
×
3732
        return (TIFFReadDirEntryErrRange);
×
3733
    else
3734
        return (TIFFReadDirEntryErrOk);
×
3735
}
3736

3737
static enum TIFFReadDirEntryErr
3738
TIFFReadDirEntryCheckRangeSshortLong(uint32_t value)
×
3739
{
3740
    if (value > 0x7FFF)
×
3741
        return (TIFFReadDirEntryErrRange);
×
3742
    else
3743
        return (TIFFReadDirEntryErrOk);
×
3744
}
3745

3746
static enum TIFFReadDirEntryErr
3747
TIFFReadDirEntryCheckRangeSshortSlong(int32_t value)
×
3748
{
3749
    if ((value < -0x8000) || (value > 0x7FFF))
×
3750
        return (TIFFReadDirEntryErrRange);
×
3751
    else
3752
        return (TIFFReadDirEntryErrOk);
×
3753
}
3754

3755
static enum TIFFReadDirEntryErr
3756
TIFFReadDirEntryCheckRangeSshortLong8(uint64_t value)
×
3757
{
3758
    if (value > 0x7FFF)
×
3759
        return (TIFFReadDirEntryErrRange);
×
3760
    else
3761
        return (TIFFReadDirEntryErrOk);
×
3762
}
3763

3764
static enum TIFFReadDirEntryErr
3765
TIFFReadDirEntryCheckRangeSshortSlong8(int64_t value)
×
3766
{
3767
    if ((value < -0x8000) || (value > 0x7FFF))
×
3768
        return (TIFFReadDirEntryErrRange);
×
3769
    else
3770
        return (TIFFReadDirEntryErrOk);
×
3771
}
3772

3773
static enum TIFFReadDirEntryErr
3774
TIFFReadDirEntryCheckRangeLongSbyte(int8_t value)
×
3775
{
3776
    if (value < 0)
×
3777
        return (TIFFReadDirEntryErrRange);
×
3778
    else
3779
        return (TIFFReadDirEntryErrOk);
×
3780
}
3781

3782
static enum TIFFReadDirEntryErr
3783
TIFFReadDirEntryCheckRangeLongSshort(int16_t value)
×
3784
{
3785
    if (value < 0)
×
3786
        return (TIFFReadDirEntryErrRange);
×
3787
    else
3788
        return (TIFFReadDirEntryErrOk);
×
3789
}
3790

3791
static enum TIFFReadDirEntryErr
3792
TIFFReadDirEntryCheckRangeLongSlong(int32_t value)
×
3793
{
3794
    if (value < 0)
×
3795
        return (TIFFReadDirEntryErrRange);
×
3796
    else
3797
        return (TIFFReadDirEntryErrOk);
×
3798
}
3799

3800
static enum TIFFReadDirEntryErr
3801
TIFFReadDirEntryCheckRangeLongLong8(uint64_t value)
×
3802
{
3803
    if (value > UINT32_MAX)
×
3804
        return (TIFFReadDirEntryErrRange);
×
3805
    else
3806
        return (TIFFReadDirEntryErrOk);
×
3807
}
3808

3809
static enum TIFFReadDirEntryErr
3810
TIFFReadDirEntryCheckRangeLongSlong8(int64_t value)
×
3811
{
3812
    if ((value < 0) || (value > (int64_t)UINT32_MAX))
×
3813
        return (TIFFReadDirEntryErrRange);
×
3814
    else
3815
        return (TIFFReadDirEntryErrOk);
×
3816
}
3817

3818
static enum TIFFReadDirEntryErr
3819
TIFFReadDirEntryCheckRangeSlongLong(uint32_t value)
×
3820
{
3821
    if (value > 0x7FFFFFFFUL)
×
3822
        return (TIFFReadDirEntryErrRange);
×
3823
    else
3824
        return (TIFFReadDirEntryErrOk);
×
3825
}
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
{
3831
    if (value > 0x7FFFFFFF)
×
3832
        return (TIFFReadDirEntryErrRange);
×
3833
    else
3834
        return (TIFFReadDirEntryErrOk);
×
3835
}
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
{
3841
    if ((value < 0 - ((int64_t)0x7FFFFFFF + 1)) || (value > 0x7FFFFFFF))
×
3842
        return (TIFFReadDirEntryErrRange);
×
3843
    else
3844
        return (TIFFReadDirEntryErrOk);
×
3845
}
3846

3847
static enum TIFFReadDirEntryErr
3848
TIFFReadDirEntryCheckRangeLong8Sbyte(int8_t value)
×
3849
{
3850
    if (value < 0)
×
3851
        return (TIFFReadDirEntryErrRange);
×
3852
    else
3853
        return (TIFFReadDirEntryErrOk);
×
3854
}
3855

3856
static enum TIFFReadDirEntryErr
3857
TIFFReadDirEntryCheckRangeLong8Sshort(int16_t value)
×
3858
{
3859
    if (value < 0)
×
3860
        return (TIFFReadDirEntryErrRange);
×
3861
    else
3862
        return (TIFFReadDirEntryErrOk);
×
3863
}
3864

3865
static enum TIFFReadDirEntryErr
3866
TIFFReadDirEntryCheckRangeLong8Slong(int32_t value)
×
3867
{
3868
    if (value < 0)
×
3869
        return (TIFFReadDirEntryErrRange);
×
3870
    else
3871
        return (TIFFReadDirEntryErrOk);
×
3872
}
3873

3874
static enum TIFFReadDirEntryErr
3875
TIFFReadDirEntryCheckRangeLong8Slong8(int64_t value)
1✔
3876
{
3877
    if (value < 0)
1✔
3878
        return (TIFFReadDirEntryErrRange);
×
3879
    else
3880
        return (TIFFReadDirEntryErrOk);
1✔
3881
}
3882

3883
static enum TIFFReadDirEntryErr
3884
TIFFReadDirEntryCheckRangeSlong8Long8(uint64_t value)
×
3885
{
3886
    if (value > INT64_MAX)
×
3887
        return (TIFFReadDirEntryErrRange);
×
3888
    else
3889
        return (TIFFReadDirEntryErrOk);
×
3890
}
3891

3892
static enum TIFFReadDirEntryErr TIFFReadDirEntryData(TIFF *tif, uint64_t offset,
280✔
3893
                                                     tmsize_t size, void *dest)
3894
{
3895
    assert(size > 0);
280✔
3896
    if (!isMapped(tif))
280✔
3897
    {
3898
        if (!SeekOK(tif, offset))
248✔
3899
            return (TIFFReadDirEntryErrIo);
×
3900
        if (!ReadOK(tif, dest, size))
248✔
3901
            return (TIFFReadDirEntryErrIo);
×
3902
    }
3903
    else
3904
    {
3905
        size_t ma, mb;
3906
        ma = (size_t)offset;
32✔
3907
        if ((uint64_t)ma != offset || ma > (~(size_t)0) - (size_t)size)
32✔
3908
        {
3909
            return TIFFReadDirEntryErrIo;
×
3910
        }
3911
        mb = ma + size;
32✔
3912
        if (mb > (uint64_t)tif->tif_size)
32✔
3913
            return (TIFFReadDirEntryErrIo);
×
3914
        _TIFFmemcpy(dest, tif->tif_base + ma, size);
32✔
3915
    }
3916
    return (TIFFReadDirEntryErrOk);
280✔
3917
}
3918

3919
static void TIFFReadDirEntryOutputErr(TIFF *tif, enum TIFFReadDirEntryErr err,
14✔
3920
                                      const char *module, const char *tagname,
3921
                                      int recover)
3922
{
3923
    if (!recover)
14✔
3924
    {
3925
        switch (err)
2✔
3926
        {
3927
            case TIFFReadDirEntryErrCount:
×
3928
                TIFFErrorExtR(tif, module, "Incorrect count for \"%s\"",
×
3929
                              tagname);
3930
                break;
×
3931
            case TIFFReadDirEntryErrType:
×
3932
                TIFFErrorExtR(tif, module, "Incompatible type for \"%s\"",
×
3933
                              tagname);
3934
                break;
×
3935
            case TIFFReadDirEntryErrIo:
2✔
3936
                TIFFErrorExtR(tif, module, "IO error during reading of \"%s\"",
2✔
3937
                              tagname);
3938
                break;
2✔
3939
            case TIFFReadDirEntryErrRange:
×
3940
                TIFFErrorExtR(tif, module, "Incorrect value for \"%s\"",
×
3941
                              tagname);
3942
                break;
×
3943
            case TIFFReadDirEntryErrPsdif:
×
3944
                TIFFErrorExtR(
×
3945
                    tif, module,
3946
                    "Cannot handle different values per sample for \"%s\"",
3947
                    tagname);
3948
                break;
×
3949
            case TIFFReadDirEntryErrSizesan:
×
3950
                TIFFErrorExtR(tif, module,
×
3951
                              "Sanity check on size of \"%s\" value failed",
3952
                              tagname);
3953
                break;
×
3954
            case TIFFReadDirEntryErrAlloc:
×
3955
                TIFFErrorExtR(tif, module, "Out of memory reading of \"%s\"",
×
3956
                              tagname);
3957
                break;
×
3958
            default:
×
3959
                assert(0); /* we should never get here */
×
3960
                break;
3961
        }
3962
    }
3963
    else
3964
    {
3965
        switch (err)
12✔
3966
        {
3967
            case TIFFReadDirEntryErrCount:
2✔
3968
                TIFFWarningExtR(tif, module,
2✔
3969
                                "Incorrect count for \"%s\"; tag ignored",
3970
                                tagname);
3971
                break;
2✔
3972
            case TIFFReadDirEntryErrType:
×
3973
                TIFFWarningExtR(tif, module,
×
3974
                                "Incompatible type for \"%s\"; tag ignored",
3975
                                tagname);
3976
                break;
×
3977
            case TIFFReadDirEntryErrIo:
10✔
3978
                TIFFWarningExtR(
10✔
3979
                    tif, module,
3980
                    "IO error during reading of \"%s\"; tag ignored", tagname);
3981
                break;
10✔
3982
            case TIFFReadDirEntryErrRange:
×
3983
                TIFFWarningExtR(tif, module,
×
3984
                                "Incorrect value for \"%s\"; tag ignored",
3985
                                tagname);
3986
                break;
×
3987
            case TIFFReadDirEntryErrPsdif:
×
3988
                TIFFWarningExtR(tif, module,
×
3989
                                "Cannot handle different values per sample for "
3990
                                "\"%s\"; tag ignored",
3991
                                tagname);
3992
                break;
×
3993
            case TIFFReadDirEntryErrSizesan:
×
3994
                TIFFWarningExtR(
×
3995
                    tif, module,
3996
                    "Sanity check on size of \"%s\" value failed; tag ignored",
3997
                    tagname);
3998
                break;
×
3999
            case TIFFReadDirEntryErrAlloc:
×
4000
                TIFFWarningExtR(tif, module,
×
4001
                                "Out of memory reading of \"%s\"; tag ignored",
4002
                                tagname);
4003
                break;
×
4004
            default:
×
4005
                assert(0); /* we should never get here */
×
4006
                break;
4007
        }
4008
    }
4009
}
14✔
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)
52,013✔
4017
{
4018
    switch (photometric)
52,013✔
4019
    {
4020
        case PHOTOMETRIC_PALETTE:
37,416✔
4021
        case PHOTOMETRIC_MINISWHITE:
4022
        case PHOTOMETRIC_MINISBLACK:
4023
            return 1;
37,416✔
4024
        case PHOTOMETRIC_YCBCR:
13,871✔
4025
        case PHOTOMETRIC_RGB:
4026
        case PHOTOMETRIC_CIELAB:
4027
        case PHOTOMETRIC_LOGLUV:
4028
        case PHOTOMETRIC_ITULAB:
4029
        case PHOTOMETRIC_ICCLAB:
4030
            return 3;
13,871✔
4031
        case PHOTOMETRIC_SEPARATED:
761✔
4032
        case PHOTOMETRIC_MASK:
4033
            return 4;
761✔
4034
        case PHOTOMETRIC_LOGL:
×
4035
        case PHOTOMETRIC_CFA:
4036
        default:
4037
            return 0;
×
4038
    }
4039
}
4040

4041
static int ByteCountLooksBad(TIFF *tif)
32,562✔
4042
{
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
    uint64_t bytecount = TIFFGetStrileByteCount(tif, 0);
32,562✔
4057
    uint64_t offset = TIFFGetStrileOffset(tif, 0);
32,584✔
4058
    uint64_t filesize;
4059

4060
    if (offset == 0)
32,567✔
4061
        return 0;
10,644✔
4062
    if (bytecount == 0)
21,923✔
4063
        return 1;
1✔
4064
    if (tif->tif_dir.td_compression != COMPRESSION_NONE)
21,922✔
4065
        return 0;
753✔
4066
    filesize = TIFFGetFileSize(tif);
21,169✔
4067
    if (offset <= filesize && bytecount > filesize - offset)
21,168✔
4068
        return 1;
2✔
4069
    if (tif->tif_mode == O_RDONLY)
21,166✔
4070
    {
4071
        uint64_t scanlinesize = TIFFScanlineSize64(tif);
20,111✔
4072
        if (tif->tif_dir.td_imagelength > 0 &&
20,114✔
4073
            scanlinesize > UINT64_MAX / tif->tif_dir.td_imagelength)
20,098✔
4074
        {
4075
            return 1;
×
4076
        }
4077
        if (bytecount < scanlinesize * tif->tif_dir.td_imagelength)
20,114✔
4078
            return 1;
2✔
4079
    }
4080
    return 0;
21,167✔
4081
}
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)
346,807✔
4088
{
4089
    const uint64_t data_width = TIFFDataWidth(dp->tdir_type);
346,807✔
4090
    if (data_width != 0 && dp->tdir_count > UINT64_MAX / data_width)
346,728✔
4091
    {
4092
        TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
1✔
4093
                      "Too large IFD data size");
4094
        return false;
1✔
4095
    }
4096
    const uint64_t datalength = dp->tdir_count * data_width;
346,727✔
4097
    if (datalength > ((tif->tif_flags & TIFF_BIGTIFF) ? 0x8U : 0x4U))
346,727✔
4098
    {
4099
        if (tif->tif_dir.td_dirdatasize_read > UINT64_MAX - datalength)
187,989✔
4100
        {
4101
            TIFFErrorExtR(tif, "EvaluateIFDdatasizeReading",
×
4102
                          "Too large IFD data size");
4103
            return false;
×
4104
        }
4105
        tif->tif_dir.td_dirdatasize_read += datalength;
187,989✔
4106
        if (!(tif->tif_flags & TIFF_BIGTIFF))
187,989✔
4107
        {
4108
            /* The offset of TIFFDirEntry are not swapped when read in. That has
4109
             * to be done when used. */
4110
            uint32_t offset = dp->tdir_offset.toff_long;
186,342✔
4111
            if (tif->tif_flags & TIFF_SWAB)
186,342✔
4112
                TIFFSwabLong(&offset);
2,100✔
4113
            tif->tif_dir
4114
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
186,343✔
4115
                .offset = (uint64_t)offset;
186,343✔
4116
        }
4117
        else
4118
        {
4119
            tif->tif_dir
4120
                .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
1,647✔
4121
                .offset = dp->tdir_offset.toff_long8;
1,647✔
4122
            if (tif->tif_flags & TIFF_SWAB)
1,647✔
4123
                TIFFSwabLong8(
30✔
4124
                    &tif->tif_dir
4125
                         .td_dirdatasize_offsets[tif->tif_dir
30✔
4126
                                                     .td_dirdatasize_Noffsets]
30✔
4127
                         .offset);
4128
        }
4129
        tif->tif_dir
4130
            .td_dirdatasize_offsets[tif->tif_dir.td_dirdatasize_Noffsets]
187,988✔
4131
            .length = datalength;
187,988✔
4132
        tif->tif_dir.td_dirdatasize_Noffsets++;
187,988✔
4133
    }
4134
    return true;
346,726✔
4135
}
4136

4137
/*
4138
 * Compare function for qsort() sorting TIFFEntryOffsetAndLength array entries.
4139
 */
4140
static int cmpTIFFEntryOffsetAndLength(const void *a, const void *b)
69,552✔
4141
{
4142
    const TIFFEntryOffsetAndLength *ta = (const TIFFEntryOffsetAndLength *)a;
69,552✔
4143
    const TIFFEntryOffsetAndLength *tb = (const TIFFEntryOffsetAndLength *)b;
69,552✔
4144
    /* Compare offsets */
4145
    if (ta->offset > tb->offset)
69,552✔
4146
        return 1;
8,925✔
4147
    else if (ta->offset < tb->offset)
60,627✔
4148
        return -1;
60,627✔
4149
    else
4150
        return 0;
×
4151
}
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)
52,019✔
4161
{
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
    if (tif->tif_mode == O_RDONLY)
52,019✔
4166
        return;
33,957✔
4167

4168
    /* Sort TIFFEntryOffsetAndLength array in ascending order. */
4169
    qsort(tif->tif_dir.td_dirdatasize_offsets,
18,062✔
4170
          tif->tif_dir.td_dirdatasize_Noffsets,
18,062✔
4171
          sizeof(TIFFEntryOffsetAndLength), cmpTIFFEntryOffsetAndLength);
4172

4173
    /* Get offset of end of IFD entry space. */
4174
    uint64_t IFDendoffset;
4175
    if (!(tif->tif_flags & TIFF_BIGTIFF))
18,075✔
4176
        IFDendoffset = tif->tif_diroff + 2 + dircount * 12 + 4;
17,567✔
4177
    else
4178
        IFDendoffset = tif->tif_diroff + 8 + dircount * 20 + 8;
508✔
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
    uint64_t size = 0;
18,075✔
4184
    uint64_t offset;
4185
    uint32_t i;
4186
    for (i = 0; i < tif->tif_dir.td_dirdatasize_Noffsets; i++)
77,433✔
4187
    {
4188
        offset = tif->tif_dir.td_dirdatasize_offsets[i].offset;
59,394✔
4189
        if (offset == IFDendoffset)
59,394✔
4190
        {
4191
            size += tif->tif_dir.td_dirdatasize_offsets[i].length;
57,665✔
4192
            IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
57,665✔
4193
        }
4194
        else if (offset == IFDendoffset + 1)
1,729✔
4195
        {
4196
            /* Add gap byte after previous IFD data set. */
4197
            size += tif->tif_dir.td_dirdatasize_offsets[i].length + 1;
1,693✔
4198
            IFDendoffset += tif->tif_dir.td_dirdatasize_offsets[i].length;
1,693✔
4199
        }
4200
        else
4201
        {
4202
            /* Further data is no more continuously after IFD */
4203
            break;
36✔
4204
        }
4205
    }
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
    if (tif->tif_nextdiroff != 0)
18,075✔
4209
    {
4210
        if (tif->tif_nextdiroff == IFDendoffset + 1)
2,770✔
4211
            size++;
627✔
4212
    }
4213
    else
4214
    {
4215
        /* Check for IFD data ends at EOF. Then IFD can always be safely
4216
         * overwritten. */
4217
        offset = TIFFSeekFile(tif, 0, SEEK_END);
15,305✔
4218
        if (offset == IFDendoffset)
15,305✔
4219
        {
4220
            tif->tif_dir.td_dirdatasize_read = UINT64_MAX;
13,987✔
4221
            return;
13,987✔
4222
        }
4223
    }
4224

4225
    /* Finally, add the size of the IFD tag entries themselves. */
4226
    if (!(tif->tif_flags & TIFF_BIGTIFF))
4,088✔
4227
        tif->tif_dir.td_dirdatasize_read = 2 + dircount * 12 + 4 + size;
3,871✔
4228
    else
4229
        tif->tif_dir.td_dirdatasize_read = 8 + dircount * 20 + 8 + size;
217✔
4230
} /*-- 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)
59,855✔
4237
{
4238
    static const char module[] = "TIFFReadDirectory";
4239
    TIFFDirEntry *dir;
4240
    uint16_t dircount;
4241
    TIFFDirEntry *dp;
4242
    uint16_t di;
4243
    const TIFFField *fip;
4244
    uint32_t fii = FAILED_FII;
59,855✔
4245
    toff_t nextdiroff;
4246
    int bitspersample_read = FALSE;
59,855✔
4247
    int color_channels;
4248

4249
    if (tif->tif_nextdiroff == 0)
59,855✔
4250
    {
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
        tif->tif_diroff = tif->tif_nextdiroff;
7,777✔
4255
        return 0;
7,777✔
4256
    }
4257

4258
    nextdiroff = tif->tif_nextdiroff;
52,078✔
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
    if (!_TIFFCheckDirNumberAndOffset(tif,
52,078✔
4263
                                      tif->tif_curdir ==
52,078✔
4264
                                              TIFF_NON_EXISTENT_DIR_NUMBER
4265
                                          ? 0
4266
                                          : tif->tif_curdir + 1,
3,734✔
4267
                                      nextdiroff))
4268
    {
4269
        return 0; /* bad offset (IFD looping or more than TIFF_MAX_DIR_COUNT
×
4270
                     IFDs) */
4271
    }
4272
    dircount = TIFFFetchDirectory(tif, nextdiroff, &dir, &tif->tif_nextdiroff);
52,058✔
4273
    if (!dircount)
52,048✔
4274
    {
4275
        TIFFErrorExtR(tif, module,
17✔
4276
                      "Failed to read directory at offset %" PRIu64,
4277
                      nextdiroff);
4278
        return 0;
15✔
4279
    }
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
    if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
52,031✔
4284
        tif->tif_curdir = 0;
48,297✔
4285
    else
4286
        tif->tif_curdir++;
3,734✔
4287

4288
    TIFFReadDirectoryCheckOrder(tif, dir, dircount);
52,031✔
4289

4290
    /*
4291
     * Mark duplicates of any tag to be ignored (bugzilla 1994)
4292
     * to avoid certain pathological problems.
4293
     */
4294
    {
4295
        TIFFDirEntry *ma;
4296
        uint16_t mb;
4297
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
784,634✔
4298
        {
4299
            TIFFDirEntry *na;
4300
            uint16_t nb;
4301
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
5,620,380✔
4302
            {
4303
                if (ma->tdir_tag == na->tdir_tag)
4,887,800✔
4304
                {
4305
                    na->tdir_ignore = TRUE;
3✔
4306
                }
4307
            }
4308
        }
4309
    }
4310

4311
    tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
52,053✔
4312
    tif->tif_flags &= ~TIFF_BUF4WRITE;   /* reset before new dir */
52,053✔
4313
    tif->tif_flags &= ~TIFF_CHOPPEDUPARRAYS;
52,053✔
4314

4315
    /* free any old stuff and reinit */
4316
    TIFFFreeDirectory(tif);
52,053✔
4317
    TIFFDefaultDirectory(tif);
52,037✔
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
    tif->tif_dir.td_iswrittentofile = TRUE;
52,054✔
4323

4324
    /* Allocate arrays for offset values outside IFD entry for IFD data size
4325
     * checking. Note: Counter are reset within TIFFFreeDirectory(). */
4326
    tif->tif_dir.td_dirdatasize_offsets =
52,034✔
4327
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
52,054✔
4328
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
52,054✔
4329
    if (tif->tif_dir.td_dirdatasize_offsets == NULL)
52,034✔
4330
    {
4331
        TIFFErrorExtR(
×
4332
            tif, module,
4333
            "Failed to allocate memory for counting IFD data size at reading");
4334
        goto bad;
×
4335
    }
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
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
52,034✔
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
    dp =
4363
        TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_SAMPLESPERPIXEL);
52,047✔
4364
    if (dp)
52,043✔
4365
    {
4366
        if (!TIFFFetchNormalTag(tif, dp, 0))
52,025✔
4367
            goto bad;
×
4368
        dp->tdir_ignore = TRUE;
52,025✔
4369
    }
4370
    dp = TIFFReadDirectoryFindEntry(tif, dir, dircount, TIFFTAG_COMPRESSION);
52,043✔
4371
    if (dp)
52,043✔
4372
    {
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
        uint16_t value;
4380
        enum TIFFReadDirEntryErr err;
4381
        err = TIFFReadDirEntryShort(tif, dp, &value);
52,024✔
4382
        if (err == TIFFReadDirEntryErrCount)
52,025✔
4383
            err = TIFFReadDirEntryPersampleShort(tif, dp, &value);
×
4384
        if (err != TIFFReadDirEntryErrOk)
52,020✔
4385
        {
4386
            TIFFReadDirEntryOutputErr(tif, err, module, "Compression", 0);
×
4387
            goto bad;
×
4388
        }
4389
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, value))
52,020✔
4390
            goto bad;
×
4391
        dp->tdir_ignore = TRUE;
52,043✔
4392
    }
4393
    else
4394
    {
4395
        if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE))
19✔
4396
            goto bad;
×
4397
    }
4398
    /*
4399
     * First real pass over the directory.
4400
     */
4401
    for (di = 0, dp = dir; di < dircount; di++, dp++)
784,725✔
4402
    {
4403
        if (!dp->tdir_ignore)
732,532✔
4404
        {
4405
            TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
628,519✔
4406
            if (fii == FAILED_FII)
628,698✔
4407
            {
4408
                if (tif->tif_warn_about_unknown_tags)
5✔
4409
                {
4410
                    TIFFWarningExtR(tif, module,
×
4411
                                    "Unknown field with tag %" PRIu16
4412
                                    " (0x%" PRIx16 ") encountered",
4413
                                    dp->tdir_tag, dp->tdir_tag);
×
4414
                }
4415
                /* the following knowingly leaks the
4416
                   anonymous field structure */
4417
                const TIFFField *fld = _TIFFCreateAnonField(
5✔
4418
                    tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
5✔
4419
                if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
5✔
4420
                {
4421
                    TIFFWarningExtR(
×
4422
                        tif, module,
4423
                        "Registering anonymous field with tag %" PRIu16
4424
                        " (0x%" PRIx16 ") failed",
4425
                        dp->tdir_tag, dp->tdir_tag);
×
4426
                    dp->tdir_ignore = TRUE;
×
4427
                }
4428
                else
4429
                {
4430
                    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
5✔
4431
                    assert(fii != FAILED_FII);
5✔
4432
                }
4433
            }
4434
        }
4435
        if (!dp->tdir_ignore)
732,711✔
4436
        {
4437
            fip = tif->tif_fields[fii];
628,668✔
4438
            if (fip->field_bit == FIELD_IGNORE)
628,668✔
4439
                dp->tdir_ignore = TRUE;
×
4440
            else
4441
            {
4442
                switch (dp->tdir_tag)
628,668✔
4443
                {
4444
                    case TIFFTAG_STRIPOFFSETS:
104,102✔
4445
                    case TIFFTAG_STRIPBYTECOUNTS:
4446
                    case TIFFTAG_TILEOFFSETS:
4447
                    case TIFFTAG_TILEBYTECOUNTS:
4448
                        TIFFSetFieldBit(tif, fip->field_bit);
104,102✔
4449
                        break;
104,102✔
4450
                    case TIFFTAG_IMAGEWIDTH:
222,462✔
4451
                    case TIFFTAG_IMAGELENGTH:
4452
                    case TIFFTAG_IMAGEDEPTH:
4453
                    case TIFFTAG_TILELENGTH:
4454
                    case TIFFTAG_TILEWIDTH:
4455
                    case TIFFTAG_TILEDEPTH:
4456
                    case TIFFTAG_PLANARCONFIG:
4457
                    case TIFFTAG_ROWSPERSTRIP:
4458
                    case TIFFTAG_EXTRASAMPLES:
4459
                        if (!TIFFFetchNormalTag(tif, dp, 0))
222,462✔
4460
                            goto bad;
×
4461
                        dp->tdir_ignore = TRUE;
222,447✔
4462
                        break;
222,447✔
4463
                    default:
302,104✔
4464
                        if (!_TIFFCheckFieldIsValidForCodec(tif, dp->tdir_tag))
302,104✔
4465
                            dp->tdir_ignore = TRUE;
×
4466
                        break;
302,079✔
4467
                }
4468
            }
104,043✔
4469
        }
4470
    }
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
    if ((tif->tif_dir.td_compression == COMPRESSION_OJPEG) &&
52,193✔
4480
        (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE))
2✔
4481
    {
4482
        if (!_TIFFFillStriles(tif))
×
4483
            goto bad;
×
4484
        dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
×
4485
                                        TIFFTAG_STRIPOFFSETS);
4486
        if ((dp != 0) && (dp->tdir_count == 1))
×
4487
        {
4488
            dp = TIFFReadDirectoryFindEntry(tif, dir, dircount,
×
4489
                                            TIFFTAG_STRIPBYTECOUNTS);
4490
            if ((dp != 0) && (dp->tdir_count == 1))
×
4491
            {
4492
                tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;
×
4493
                TIFFWarningExtR(tif, module,
×
4494
                                "Planarconfig tag value assumed incorrect, "
4495
                                "assuming data is contig instead of chunky");
4496
            }
4497
        }
4498
    }
4499
    /*
4500
     * Allocate directory structure and setup defaults.
4501
     */
4502
    if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS))
52,047✔
4503
    {
4504
        MissingRequired(tif, "ImageLength");
×
4505
        goto bad;
×
4506
    }
4507

4508
    /*
4509
     * Second pass: extract other information.
4510
     */
4511
    for (di = 0, dp = dir; di < dircount; di++, dp++)
784,591✔
4512
    {
4513
        if (!dp->tdir_ignore)
732,492✔
4514
        {
4515
            switch (dp->tdir_tag)
406,068✔
4516
            {
4517
                case TIFFTAG_MINSAMPLEVALUE:
103,325✔
4518
                case TIFFTAG_MAXSAMPLEVALUE:
4519
                case TIFFTAG_BITSPERSAMPLE:
4520
                case TIFFTAG_DATATYPE:
4521
                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
                    {
4533
                        uint16_t value;
4534
                        enum TIFFReadDirEntryErr err;
4535
                        err = TIFFReadDirEntryShort(tif, dp, &value);
103,325✔
4536
                        if (!EvaluateIFDdatasizeReading(tif, dp))
103,301✔
4537
                            goto bad;
×
4538
                        if (err == TIFFReadDirEntryErrCount)
103,291✔
4539
                            err =
4540
                                TIFFReadDirEntryPersampleShort(tif, dp, &value);
31,410✔
4541
                        if (err != TIFFReadDirEntryErrOk)
103,294✔
4542
                        {
4543
                            fip = TIFFFieldWithTag(tif, dp->tdir_tag);
×
4544
                            TIFFReadDirEntryOutputErr(
×
4545
                                tif, err, module,
4546
                                fip ? fip->field_name : "unknown tagname", 0);
4547
                            goto bad;
×
4548
                        }
4549
                        if (!TIFFSetField(tif, dp->tdir_tag, value))
103,294✔
4550
                            goto bad;
×
4551
                        if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE)
103,307✔
4552
                            bitspersample_read = TRUE;
52,023✔
4553
                    }
4554
                    break;
103,307✔
4555
                case TIFFTAG_SMINSAMPLEVALUE:
×
4556
                case TIFFTAG_SMAXSAMPLEVALUE:
4557
                {
4558

4559
                    double *data = NULL;
×
4560
                    enum TIFFReadDirEntryErr err;
4561
                    uint32_t saved_flags;
4562
                    int m;
4563
                    if (dp->tdir_count !=
×
4564
                        (uint64_t)tif->tif_dir.td_samplesperpixel)
×
4565
                        err = TIFFReadDirEntryErrCount;
×
4566
                    else
4567
                        err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
×
4568
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
4569
                        goto bad;
×
4570
                    if (err != TIFFReadDirEntryErrOk)
×
4571
                    {
4572
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
×
4573
                        TIFFReadDirEntryOutputErr(
×
4574
                            tif, err, module,
4575
                            fip ? fip->field_name : "unknown tagname", 0);
4576
                        goto bad;
×
4577
                    }
4578
                    saved_flags = tif->tif_flags;
×
4579
                    tif->tif_flags |= TIFF_PERSAMPLE;
×
4580
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
4581
                    tif->tif_flags = saved_flags;
×
4582
                    _TIFFfreeExt(tif, data);
×
4583
                    if (!m)
×
4584
                        goto bad;
×
4585
                }
4586
                break;
×
4587
                case TIFFTAG_STRIPOFFSETS:
52,045✔
4588
                case TIFFTAG_TILEOFFSETS:
4589
                {
4590
                    switch (dp->tdir_type)
52,045✔
4591
                    {
4592
                        case TIFF_SHORT:
50,736✔
4593
                        case TIFF_LONG:
4594
                        case TIFF_LONG8:
4595
                            break;
50,736✔
4596
                        default:
1,309✔
4597
                            /* Warn except if directory typically created with
4598
                             * TIFFDeferStrileArrayWriting() */
4599
                            if (!(tif->tif_mode == O_RDWR &&
1,309✔
4600
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
1,289✔
4601
                                  dp->tdir_offset.toff_long8 == 0))
1,289✔
4602
                            {
4603
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
20✔
4604
                                TIFFWarningExtR(
2✔
4605
                                    tif, module, "Invalid data type for tag %s",
4606
                                    fip ? fip->field_name : "unknown tagname");
4607
                            }
4608
                            break;
1,291✔
4609
                    }
4610
                    _TIFFmemcpy(&(tif->tif_dir.td_stripoffset_entry), dp,
52,027✔
4611
                                sizeof(TIFFDirEntry));
4612
                    if (!EvaluateIFDdatasizeReading(tif, dp))
52,022✔
4613
                        goto bad;
1✔
4614
                }
4615
                break;
52,023✔
4616
                case TIFFTAG_STRIPBYTECOUNTS:
52,027✔
4617
                case TIFFTAG_TILEBYTECOUNTS:
4618
                {
4619
                    switch (dp->tdir_type)
52,027✔
4620
                    {
4621
                        case TIFF_SHORT:
50,735✔
4622
                        case TIFF_LONG:
4623
                        case TIFF_LONG8:
4624
                            break;
50,735✔
4625
                        default:
1,292✔
4626
                            /* Warn except if directory typically created with
4627
                             * TIFFDeferStrileArrayWriting() */
4628
                            if (!(tif->tif_mode == O_RDWR &&
1,292✔
4629
                                  dp->tdir_count == 0 && dp->tdir_type == 0 &&
1,289✔
4630
                                  dp->tdir_offset.toff_long8 == 0))
1,289✔
4631
                            {
4632
                                fip = TIFFFieldWithTag(tif, dp->tdir_tag);
3✔
4633
                                TIFFWarningExtR(
×
4634
                                    tif, module, "Invalid data type for tag %s",
4635
                                    fip ? fip->field_name : "unknown tagname");
4636
                            }
4637
                            break;
1,289✔
4638
                    }
4639
                    _TIFFmemcpy(&(tif->tif_dir.td_stripbytecount_entry), dp,
52,024✔
4640
                                sizeof(TIFFDirEntry));
4641
                    if (!EvaluateIFDdatasizeReading(tif, dp))
52,010✔
4642
                        goto bad;
×
4643
                }
4644
                break;
52,032✔
4645
                case TIFFTAG_COLORMAP:
360✔
4646
                case TIFFTAG_TRANSFERFUNCTION:
4647
                {
4648
                    enum TIFFReadDirEntryErr err;
4649
                    uint32_t countpersample;
4650
                    uint32_t countrequired;
4651
                    uint32_t incrementpersample;
4652
                    uint16_t *value = NULL;
360✔
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
                    if (!bitspersample_read)
360✔
4661
                    {
4662
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
×
4663
                        TIFFWarningExtR(
×
4664
                            tif, module,
4665
                            "Ignoring %s since BitsPerSample tag not found",
4666
                            fip ? fip->field_name : "unknown tagname");
4667
                        continue;
×
4668
                    }
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
                    if (tif->tif_dir.td_bitspersample > 24)
360✔
4673
                    {
4674
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
×
4675
                        TIFFWarningExtR(
×
4676
                            tif, module,
4677
                            "Ignoring %s because BitsPerSample=%" PRIu16 ">24",
4678
                            fip ? fip->field_name : "unknown tagname",
4679
                            tif->tif_dir.td_bitspersample);
×
4680
                        continue;
×
4681
                    }
4682
                    countpersample = (1U << tif->tif_dir.td_bitspersample);
360✔
4683
                    if ((dp->tdir_tag == TIFFTAG_TRANSFERFUNCTION) &&
360✔
4684
                        (dp->tdir_count == (uint64_t)countpersample))
15✔
4685
                    {
4686
                        countrequired = countpersample;
×
4687
                        incrementpersample = 0;
×
4688
                    }
4689
                    else
4690
                    {
4691
                        countrequired = 3 * countpersample;
360✔
4692
                        incrementpersample = countpersample;
360✔
4693
                    }
4694
                    if (dp->tdir_count != (uint64_t)countrequired)
360✔
4695
                        err = TIFFReadDirEntryErrCount;
×
4696
                    else
4697
                        err = TIFFReadDirEntryShortArray(tif, dp, &value);
360✔
4698
                    if (!EvaluateIFDdatasizeReading(tif, dp))
360✔
4699
                        goto bad;
×
4700
                    if (err != TIFFReadDirEntryErrOk)
360✔
4701
                    {
4702
                        fip = TIFFFieldWithTag(tif, dp->tdir_tag);
1✔
4703
                        TIFFReadDirEntryOutputErr(
1✔
4704
                            tif, err, module,
4705
                            fip ? fip->field_name : "unknown tagname", 1);
4706
                    }
4707
                    else
4708
                    {
4709
                        TIFFSetField(tif, dp->tdir_tag, value,
359✔
4710
                                     value + incrementpersample,
359✔
4711
                                     value + 2 * incrementpersample);
359✔
4712
                        _TIFFfreeExt(tif, value);
359✔
4713
                    }
4714
                }
4715
                break;
360✔
4716
                    /* BEGIN REV 4.0 COMPATIBILITY */
4717
                case TIFFTAG_OSUBFILETYPE:
×
4718
                {
4719
                    uint16_t valueo;
4720
                    uint32_t value;
4721
                    if (TIFFReadDirEntryShort(tif, dp, &valueo) ==
×
4722
                        TIFFReadDirEntryErrOk)
4723
                    {
4724
                        switch (valueo)
×
4725
                        {
4726
                            case OFILETYPE_REDUCEDIMAGE:
×
4727
                                value = FILETYPE_REDUCEDIMAGE;
×
4728
                                break;
×
4729
                            case OFILETYPE_PAGE:
×
4730
                                value = FILETYPE_PAGE;
×
4731
                                break;
×
4732
                            default:
×
4733
                                value = 0;
×
4734
                                break;
×
4735
                        }
4736
                        if (value != 0)
×
4737
                            TIFFSetField(tif, TIFFTAG_SUBFILETYPE, value);
×
4738
                    }
4739
                }
4740
                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
                default:
198,311✔
4788
                    (void)TIFFFetchNormalTag(tif, dp, TRUE);
198,311✔
4789
                    break;
198,398✔
4790
            } /* -- switch (dp->tdir_tag) -- */
4791
        }     /* -- if (!dp->tdir_ignore) */
326,424✔
4792
    }         /* -- for-loop -- */
4793

4794
    /* Evaluate final IFD data size. */
4795
    CalcFinalIFDdatasizeReading(tif, dircount);
52,099✔
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
    if (tif->tif_dir.td_compression == COMPRESSION_OJPEG)
52,019✔
4814
    {
4815
        if (!TIFFFieldSet(tif, FIELD_PHOTOMETRIC))
2✔
4816
        {
4817
            TIFFWarningExtR(
×
4818
                tif, module,
4819
                "Photometric tag is missing, assuming data is YCbCr");
4820
            if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR))
×
4821
                goto bad;
×
4822
        }
4823
        else if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
2✔
4824
        {
4825
            tif->tif_dir.td_photometric = PHOTOMETRIC_YCBCR;
×
4826
            TIFFWarningExtR(tif, module,
×
4827
                            "Photometric tag value assumed incorrect, "
4828
                            "assuming data is YCbCr instead of RGB");
4829
        }
4830
        if (!TIFFFieldSet(tif, FIELD_BITSPERSAMPLE))
2✔
4831
        {
4832
            TIFFWarningExtR(
×
4833
                tif, module,
4834
                "BitsPerSample tag is missing, assuming 8 bits per sample");
4835
            if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8))
×
4836
                goto bad;
×
4837
        }
4838
        if (!TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
2✔
4839
        {
4840
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_RGB)
×
4841
            {
4842
                TIFFWarningExtR(tif, module,
×
4843
                                "SamplesPerPixel tag is missing, "
4844
                                "assuming correct SamplesPerPixel value is 3");
4845
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
×
4846
                    goto bad;
×
4847
            }
4848
            if (tif->tif_dir.td_photometric == PHOTOMETRIC_YCBCR)
×
4849
            {
4850
                TIFFWarningExtR(tif, module,
×
4851
                                "SamplesPerPixel tag is missing, "
4852
                                "applying correct SamplesPerPixel value of 3");
4853
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3))
×
4854
                    goto bad;
×
4855
            }
4856
            else if ((tif->tif_dir.td_photometric == PHOTOMETRIC_MINISWHITE) ||
×
4857
                     (tif->tif_dir.td_photometric == PHOTOMETRIC_MINISBLACK))
×
4858
            {
4859
                /*
4860
                 * SamplesPerPixel tag is missing, but is not required
4861
                 * by spec.  Assume correct SamplesPerPixel value of 1.
4862
                 */
4863
                if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1))
×
4864
                    goto bad;
×
4865
            }
4866
        }
4867
    }
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
    if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
52,031✔
4875
    {
4876
        tif->tif_dir.td_nstrips = TIFFNumberOfStrips(tif);
43,152✔
4877
        tif->tif_dir.td_tilewidth = tif->tif_dir.td_imagewidth;
43,152✔
4878
        tif->tif_dir.td_tilelength = tif->tif_dir.td_rowsperstrip;
43,152✔
4879
        tif->tif_dir.td_tiledepth = tif->tif_dir.td_imagedepth;
43,152✔
4880
        tif->tif_flags &= ~TIFF_ISTILED;
43,152✔
4881
    }
4882
    else
4883
    {
4884
        tif->tif_dir.td_nstrips = TIFFNumberOfTiles(tif);
8,879✔
4885
        tif->tif_flags |= TIFF_ISTILED;
8,879✔
4886
    }
4887
    if (!tif->tif_dir.td_nstrips)
52,031✔
4888
    {
4889
        TIFFErrorExtR(tif, module, "Cannot handle zero number of %s",
×
4890
                      isTiled(tif) ? "tiles" : "strips");
×
4891
        goto bad;
×
4892
    }
4893
    tif->tif_dir.td_stripsperimage = tif->tif_dir.td_nstrips;
52,031✔
4894
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE)
52,031✔
4895
        tif->tif_dir.td_stripsperimage /= tif->tif_dir.td_samplesperpixel;
8,944✔
4896
    if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS))
52,031✔
4897
    {
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
        {
4915
            MissingRequired(tif, isTiled(tif) ? "TileOffsets" : "StripOffsets");
×
4916
            goto bad;
×
4917
        }
4918
    }
4919

4920
    if (tif->tif_mode == O_RDWR &&
52,031✔
4921
        tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
18,075✔
4922
        tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
18,075✔
4923
        tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
1,289✔
4924
        tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
1,289✔
4925
        tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
1,289✔
4926
        tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
1,289✔
4927
        tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
1,289✔
4928
        tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0)
1,289✔
4929
    {
4930
        /* Directory typically created with TIFFDeferStrileArrayWriting() */
4931
        TIFFSetupStrips(tif);
1,289✔
4932
    }
4933
    else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD))
50,742✔
4934
    {
4935
        if (tif->tif_dir.td_stripoffset_entry.tdir_tag != 0)
17,008✔
4936
        {
4937
            if (!TIFFFetchStripThing(tif, &(tif->tif_dir.td_stripoffset_entry),
17,010✔
4938
                                     tif->tif_dir.td_nstrips,
4939
                                     &tif->tif_dir.td_stripoffset_p))
4940
            {
4941
                goto bad;
×
4942
            }
4943
        }
4944
        if (tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0)
17,012✔
4945
        {
4946
            if (!TIFFFetchStripThing(
17,012✔
4947
                    tif, &(tif->tif_dir.td_stripbytecount_entry),
4948
                    tif->tif_dir.td_nstrips, &tif->tif_dir.td_stripbytecount_p))
4949
            {
4950
                goto bad;
×
4951
            }
4952
        }
4953
    }
4954

4955
    /*
4956
     * Make sure all non-color channels are extrasamples.
4957
     * If it's not the case, define them as such.
4958
     */
4959
    color_channels = _TIFFGetMaxColorChannels(tif->tif_dir.td_photometric);
52,040✔
4960
    if (color_channels &&
52,012✔
4961
        tif->tif_dir.td_samplesperpixel - tif->tif_dir.td_extrasamples >
52,012✔
4962
            color_channels)
4963
    {
4964
        uint16_t old_extrasamples;
4965
        uint16_t *new_sampleinfo;
4966

4967
        TIFFWarningExtR(
3✔
4968
            tif, module,
4969
            "Sum of Photometric type-related "
4970
            "color channels and ExtraSamples doesn't match SamplesPerPixel. "
4971
            "Defining non-color channels as ExtraSamples.");
4972

4973
        old_extrasamples = tif->tif_dir.td_extrasamples;
3✔
4974
        tif->tif_dir.td_extrasamples =
3✔
4975
            (uint16_t)(tif->tif_dir.td_samplesperpixel - color_channels);
3✔
4976

4977
        // sampleinfo should contain information relative to these new extra
4978
        // samples
4979
        new_sampleinfo = (uint16_t *)_TIFFcallocExt(
3✔
4980
            tif, tif->tif_dir.td_extrasamples, sizeof(uint16_t));
3✔
4981
        if (!new_sampleinfo)
3✔
4982
        {
4983
            TIFFErrorExtR(tif, module,
×
4984
                          "Failed to allocate memory for "
4985
                          "temporary new sampleinfo array "
4986
                          "(%" PRIu16 " 16 bit elements)",
4987
                          tif->tif_dir.td_extrasamples);
×
4988
            goto bad;
×
4989
        }
4990

4991
        if (old_extrasamples > 0)
3✔
4992
            memcpy(new_sampleinfo, tif->tif_dir.td_sampleinfo,
×
4993
                   old_extrasamples * sizeof(uint16_t));
4994
        _TIFFsetShortArrayExt(tif, &tif->tif_dir.td_sampleinfo, new_sampleinfo,
3✔
4995
                              tif->tif_dir.td_extrasamples);
3✔
4996
        _TIFFfreeExt(tif, new_sampleinfo);
3✔
4997
    }
4998

4999
    /*
5000
     * Verify Palette image has a Colormap.
5001
     */
5002
    if (tif->tif_dir.td_photometric == PHOTOMETRIC_PALETTE &&
52,011✔
5003
        !TIFFFieldSet(tif, FIELD_COLORMAP))
341✔
5004
    {
5005
        if (tif->tif_dir.td_bitspersample >= 8 &&
1✔
5006
            tif->tif_dir.td_samplesperpixel == 3)
1✔
5007
            tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
×
5008
        else if (tif->tif_dir.td_bitspersample >= 8)
1✔
5009
            tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
1✔
5010
        else
5011
        {
5012
            MissingRequired(tif, "Colormap");
×
5013
            goto bad;
×
5014
        }
5015
    }
5016
    /*
5017
     * OJPEG hack:
5018
     * We do no further messing with strip/tile offsets/bytecounts in OJPEG
5019
     * TIFFs
5020
     */
5021
    if (tif->tif_dir.td_compression != COMPRESSION_OJPEG)
52,011✔
5022
    {
5023
        /*
5024
         * Attempt to deal with a missing StripByteCounts tag.
5025
         */
5026
        if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS))
52,021✔
5027
        {
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
            if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
5✔
5034
                 tif->tif_dir.td_nstrips > 1) ||
5✔
5035
                (tif->tif_dir.td_planarconfig == PLANARCONFIG_SEPARATE &&
5✔
5036
                 tif->tif_dir.td_nstrips !=
×
5037
                     (uint32_t)tif->tif_dir.td_samplesperpixel))
×
5038
            {
5039
                MissingRequired(tif, "StripByteCounts");
×
5040
                goto bad;
×
5041
            }
5042
            TIFFWarningExtR(
5✔
5043
                tif, module,
5044
                "TIFF directory is missing required "
5045
                "\"StripByteCounts\" field, calculating from imagelength");
5046
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5✔
5047
                goto bad;
×
5048
        }
5049
        else if (tif->tif_dir.td_nstrips == 1 &&
52,016✔
5050
                 !(tif->tif_flags & TIFF_ISTILED) && ByteCountLooksBad(tif))
36,914✔
5051
        {
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
            TIFFWarningExtR(tif, module,
5✔
5059
                            "Bogus \"StripByteCounts\" field, ignoring and "
5060
                            "calculating from imagelength");
5061
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
5✔
5062
                goto bad;
×
5063
        }
5064
        else if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
52,027✔
5065
                 tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
18,298✔
5066
                 tif->tif_dir.td_nstrips > 2 &&
13,902✔
5067
                 tif->tif_dir.td_compression == COMPRESSION_NONE &&
3,426✔
5068
                 TIFFGetStrileByteCount(tif, 0) !=
1,137✔
5069
                     TIFFGetStrileByteCount(tif, 1) &&
1,137✔
5070
                 TIFFGetStrileByteCount(tif, 0) != 0 &&
×
5071
                 TIFFGetStrileByteCount(tif, 1) != 0)
×
5072
        {
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
            TIFFWarningExtR(tif, module,
×
5084
                            "Wrong \"StripByteCounts\" field, ignoring and "
5085
                            "calculating from imagelength");
5086
            if (EstimateStripByteCounts(tif, dir, dircount) < 0)
×
5087
                goto bad;
×
5088
        }
5089
    }
5090
    if (dir)
52,027✔
5091
    {
5092
        _TIFFfreeExt(tif, dir);
52,038✔
5093
        dir = NULL;
52,047✔
5094
    }
5095
    if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
52,036✔
5096
    {
5097
        if (tif->tif_dir.td_bitspersample >= 16)
52,038✔
5098
            tif->tif_dir.td_maxsamplevalue = 0xFFFF;
10,358✔
5099
        else
5100
            tif->tif_dir.td_maxsamplevalue =
41,680✔
5101
                (uint16_t)((1L << tif->tif_dir.td_bitspersample) - 1);
41,680✔
5102
    }
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
    (*tif->tif_fixuptags)(tif);
52,036✔
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
    if ((tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG) &&
52,041✔
5142
        (tif->tif_dir.td_nstrips == 1) &&
43,070✔
5143
        (tif->tif_dir.td_compression == COMPRESSION_NONE) &&
34,964✔
5144
        ((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP))
28,630✔
5145
    {
5146
        ChopUpSingleUncompressedStrip(tif);
21,249✔
5147
    }
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
    if (tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG &&
52,040✔
5153
        tif->tif_dir.td_compression == COMPRESSION_NONE &&
43,075✔
5154
        (tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) == TIFF_STRIPCHOP &&
57,280✔
5155
        TIFFStripSize64(tif) > 0x7FFFFFFFUL)
24,071✔
5156
    {
5157
        TryChopUpUncompressedBigTiff(tif);
5✔
5158
    }
5159

5160
    /*
5161
     * Clear the dirty directory flag.
5162
     */
5163
    tif->tif_flags &= ~TIFF_DIRTYDIRECT;
52,056✔
5164
    tif->tif_flags &= ~TIFF_DIRTYSTRIP;
52,056✔
5165

5166
    /*
5167
     * Reinitialize i/o since we are starting on a new directory.
5168
     */
5169
    tif->tif_row = (uint32_t)-1;
52,056✔
5170
    tif->tif_curstrip = (uint32_t)-1;
52,056✔
5171
    tif->tif_col = (uint32_t)-1;
52,056✔
5172
    tif->tif_curtile = (uint32_t)-1;
52,056✔
5173
    tif->tif_tilesize = (tmsize_t)-1;
52,056✔
5174

5175
    tif->tif_scanlinesize = TIFFScanlineSize(tif);
52,056✔
5176
    if (!tif->tif_scanlinesize)
52,030✔
5177
    {
5178
        TIFFErrorExtR(tif, module, "Cannot handle zero scanline size");
11✔
5179
        return (0);
×
5180
    }
5181

5182
    if (isTiled(tif))
52,019✔
5183
    {
5184
        tif->tif_tilesize = TIFFTileSize(tif);
8,879✔
5185
        if (!tif->tif_tilesize)
8,879✔
5186
        {
5187
            TIFFErrorExtR(tif, module, "Cannot handle zero tile size");
×
5188
            return (0);
×
5189
        }
5190
    }
5191
    else
5192
    {
5193
        if (!TIFFStripSize(tif))
43,140✔
5194
        {
5195
            TIFFErrorExtR(tif, module, "Cannot handle zero strip size");
13✔
5196
            return (0);
×
5197
        }
5198
    }
5199
    return (1);
52,031✔
5200
bad:
1✔
5201
    if (dir)
1✔
5202
        _TIFFfreeExt(tif, dir);
1✔
5203
    return (0);
1✔
5204
} /*-- TIFFReadDirectory() --*/
5205

5206
static void TIFFReadDirectoryCheckOrder(TIFF *tif, TIFFDirEntry *dir,
52,030✔
5207
                                        uint16_t dircount)
5208
{
5209
    static const char module[] = "TIFFReadDirectoryCheckOrder";
5210
    uint32_t m;
5211
    uint16_t n;
5212
    TIFFDirEntry *o;
5213
    m = 0;
52,030✔
5214
    for (n = 0, o = dir; n < dircount; n++, o++)
784,367✔
5215
    {
5216
        if (o->tdir_tag < m)
732,340✔
5217
        {
5218
            TIFFWarningExtR(tif, module,
3✔
5219
                            "Invalid TIFF directory; tags are not sorted in "
5220
                            "ascending order");
5221
            break;
3✔
5222
        }
5223
        m = o->tdir_tag + 1;
732,337✔
5224
    }
5225
}
52,030✔
5226

5227
static TIFFDirEntry *TIFFReadDirectoryFindEntry(TIFF *tif, TIFFDirEntry *dir,
104,081✔
5228
                                                uint16_t dircount,
5229
                                                uint16_t tagid)
5230
{
5231
    TIFFDirEntry *m;
5232
    uint16_t n;
5233
    (void)tif;
5234
    for (m = dir, n = 0; n < dircount; m++, n++)
575,263✔
5235
    {
5236
        if (m->tdir_tag == tagid)
575,238✔
5237
            return (m);
104,056✔
5238
    }
5239
    return (0);
25✔
5240
}
5241

5242
static void TIFFReadDirectoryFindFieldInfo(TIFF *tif, uint16_t tagid,
1,101,370✔
5243
                                           uint32_t *fii)
5244
{
5245
    int32_t ma, mb, mc;
5246
    ma = -1;
1,101,370✔
5247
    mc = (int32_t)tif->tif_nfields;
1,101,370✔
5248
    while (1)
5249
    {
5250
        if (ma + 1 == mc)
7,021,900✔
5251
        {
5252
            *fii = FAILED_FII;
5✔
5253
            return;
5✔
5254
        }
5255
        mb = (ma + mc) / 2;
7,021,900✔
5256
        if (tif->tif_fields[mb]->field_tag == (uint32_t)tagid)
7,021,900✔
5257
            break;
1,101,370✔
5258
        if (tif->tif_fields[mb]->field_tag < (uint32_t)tagid)
5,920,530✔
5259
            ma = mb;
2,235,100✔
5260
        else
5261
            mc = mb;
3,685,430✔
5262
    }
5263
    while (1)
5264
    {
5265
        if (mb == 0)
1,101,590✔
5266
            break;
11,541✔
5267
        if (tif->tif_fields[mb - 1]->field_tag != (uint32_t)tagid)
1,090,050✔
5268
            break;
1,089,830✔
5269
        mb--;
219✔
5270
    }
5271
    *fii = mb;
1,101,370✔
5272
}
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
{
5281
    static const char module[] = "TIFFReadCustomDirectory";
5282
    TIFFDirEntry *dir;
5283
    uint16_t dircount;
5284
    TIFFDirEntry *dp;
5285
    uint16_t di;
5286
    const TIFFField *fip;
5287
    uint32_t fii;
5288

5289
    dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
×
5290
    if (!dircount)
×
5291
    {
5292
        TIFFErrorExtR(tif, module,
×
5293
                      "Failed to read custom directory at offset %" PRIu64,
5294
                      diroff);
5295
        return 0;
×
5296
    }
5297
    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
    {
5304
        TIFFDirEntry *ma;
5305
        uint16_t mb;
5306
        for (ma = dir, mb = 0; mb < dircount; ma++, mb++)
×
5307
        {
5308
            TIFFDirEntry *na;
5309
            uint16_t nb;
5310
            for (na = ma + 1, nb = mb + 1; nb < dircount; na++, nb++)
×
5311
            {
5312
                if (ma->tdir_tag == na->tdir_tag)
×
5313
                {
5314
                    na->tdir_ignore = TRUE;
×
5315
                }
5316
            }
5317
        }
5318
    }
5319

5320
    /* Free any old stuff and reinit. */
5321
    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
    TIFFDefaultDirectory(tif);
×
5329
    _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
    tif->tif_dir.td_dirdatasize_offsets =
×
5334
        (TIFFEntryOffsetAndLength *)_TIFFmallocExt(
×
5335
            tif, dircount * sizeof(TIFFEntryOffsetAndLength));
×
5336
    if (tif->tif_dir.td_dirdatasize_offsets == NULL)
×
5337
    {
5338
        TIFFErrorExtR(
×
5339
            tif, module,
5340
            "Failed to allocate memory for counting IFD data size at reading");
5341
        if (dir)
×
5342
            _TIFFfreeExt(tif, dir);
×
5343
        return 0;
×
5344
    }
5345

5346
    for (di = 0, dp = dir; di < dircount; di++, dp++)
×
5347
    {
5348
        TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
×
5349
        if (fii == FAILED_FII)
×
5350
        {
5351
            if (tif->tif_warn_about_unknown_tags)
×
5352
            {
5353
                TIFFWarningExtR(tif, module,
×
5354
                                "Unknown field with tag %" PRIu16 " (0x%" PRIx16
5355
                                ") encountered",
5356
                                dp->tdir_tag, dp->tdir_tag);
×
5357
            }
5358
            const TIFFField *fld = _TIFFCreateAnonField(
×
5359
                tif, dp->tdir_tag, (TIFFDataType)dp->tdir_type);
×
5360
            if (fld == NULL || !_TIFFMergeFields(tif, fld, 1))
×
5361
            {
5362
                if (tif->tif_warn_about_unknown_tags)
×
5363
                {
5364
                    TIFFWarningExtR(
×
5365
                        tif, module,
5366
                        "Registering anonymous field with tag %" PRIu16
5367
                        " (0x%" PRIx16 ") failed",
5368
                        dp->tdir_tag, dp->tdir_tag);
×
5369
                }
5370
                dp->tdir_ignore = TRUE;
×
5371
            }
5372
            else
5373
            {
5374
                TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
×
5375
                assert(fii != FAILED_FII);
×
5376
            }
5377
        }
5378
        if (!dp->tdir_ignore)
×
5379
        {
5380
            fip = tif->tif_fields[fii];
×
5381
            if (fip->field_bit == FIELD_IGNORE)
×
5382
                dp->tdir_ignore = TRUE;
×
5383
            else
5384
            {
5385
                /* check data type */
5386
                while ((fip->field_type != TIFF_ANY) &&
×
5387
                       (fip->field_type != dp->tdir_type))
×
5388
                {
5389
                    fii++;
×
5390
                    if ((fii == tif->tif_nfields) ||
×
5391
                        (tif->tif_fields[fii]->field_tag !=
×
5392
                         (uint32_t)dp->tdir_tag))
×
5393
                    {
5394
                        fii = 0xFFFF;
×
5395
                        break;
×
5396
                    }
5397
                    fip = tif->tif_fields[fii];
×
5398
                }
5399
                if (fii == 0xFFFF)
×
5400
                {
5401
                    TIFFWarningExtR(tif, module,
×
5402
                                    "Wrong data type %" PRIu16
5403
                                    " for \"%s\"; tag ignored",
5404
                                    dp->tdir_type, fip->field_name);
×
5405
                    dp->tdir_ignore = TRUE;
×
5406
                }
5407
                else
5408
                {
5409
                    /* check count if known in advance */
5410
                    if ((fip->field_readcount != TIFF_VARIABLE) &&
×
5411
                        (fip->field_readcount != TIFF_VARIABLE2))
×
5412
                    {
5413
                        uint32_t expected;
5414
                        if (fip->field_readcount == TIFF_SPP)
×
5415
                            expected =
×
5416
                                (uint32_t)tif->tif_dir.td_samplesperpixel;
×
5417
                        else
5418
                            expected = (uint32_t)fip->field_readcount;
×
5419
                        if (!CheckDirCount(tif, dp, expected))
×
5420
                            dp->tdir_ignore = TRUE;
×
5421
                    }
5422
                }
5423
            }
5424
            if (!dp->tdir_ignore)
×
5425
            {
5426
                switch (dp->tdir_tag)
×
5427
                {
5428
                    case EXIFTAG_SUBJECTDISTANCE:
×
5429
                        if (!TIFFFieldIsAnonymous(fip))
×
5430
                        {
5431
                            /* should only be called on a Exif directory */
5432
                            /* when exifFields[] is active */
5433
                            (void)TIFFFetchSubjectDistance(tif, dp);
×
5434
                        }
5435
                        else
5436
                        {
5437
                            (void)TIFFFetchNormalTag(tif, dp, TRUE);
×
5438
                        }
5439
                        break;
×
5440
                    default:
×
5441
                        (void)TIFFFetchNormalTag(tif, dp, TRUE);
×
5442
                        break;
×
5443
                }
5444
            } /*-- if (!dp->tdir_ignore) */
×
5445
        }
5446
    }
5447
    /* Evaluate final IFD data size. */
5448
    CalcFinalIFDdatasizeReading(tif, dircount);
×
5449

5450
    /* To be able to return from SubIFD or custom-IFD to main-IFD */
5451
    tif->tif_setdirectory_force_absolute = TRUE;
×
5452
    if (dir)
×
5453
        _TIFFfreeExt(tif, dir);
×
5454
    return 1;
×
5455
}
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
{
5463
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetExifFields());
×
5464
}
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
{
5471
    return TIFFReadCustomDirectory(tif, diroff, _TIFFGetGpsFields());
×
5472
}
5473

5474
static int EstimateStripByteCounts(TIFF *tif, TIFFDirEntry *dir,
10✔
5475
                                   uint16_t dircount)
5476
{
5477
    static const char module[] = "EstimateStripByteCounts";
5478

5479
    TIFFDirEntry *dp;
5480
    TIFFDirectory *td = &tif->tif_dir;
10✔
5481
    uint32_t strip;
5482

5483
    /* Do not try to load stripbytecount as we will compute it */
5484
    if (!_TIFFFillStrilesInternal(tif, 0))
10✔
5485
        return -1;
×
5486

5487
    const uint64_t allocsize = (uint64_t)td->td_nstrips * sizeof(uint64_t);
10✔
5488
    uint64_t filesize = 0;
10✔
5489
    if (allocsize > 100 * 1024 * 1024)
10✔
5490
    {
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
        filesize = TIFFGetFileSize(tif);
×
5494
        if (allocsize > filesize)
×
5495
        {
5496
            TIFFWarningExtR(
×
5497
                tif, module,
5498
                "Requested memory size for StripByteCounts of %" PRIu64
5499
                " is greater than filesize %" PRIu64 ". Memory not allocated",
5500
                allocsize, filesize);
5501
            return -1;
×
5502
        }
5503
    }
5504

5505
    if (td->td_stripbytecount_p)
10✔
5506
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
5✔
5507
    td->td_stripbytecount_p = (uint64_t *)_TIFFCheckMalloc(
20✔
5508
        tif, td->td_nstrips, sizeof(uint64_t), "for \"StripByteCounts\" array");
10✔
5509
    if (td->td_stripbytecount_p == NULL)
10✔
5510
        return -1;
×
5511

5512
    if (td->td_compression != COMPRESSION_NONE)
10✔
5513
    {
5514
        uint64_t space;
5515
        uint16_t n;
5516
        if (!(tif->tif_flags & TIFF_BIGTIFF))
2✔
5517
            space = sizeof(TIFFHeaderClassic) + 2 + dircount * 12 + 4;
2✔
5518
        else
5519
            space = sizeof(TIFFHeaderBig) + 8 + dircount * 20 + 8;
×
5520
        /* calculate amount of space used by indirect values */
5521
        for (dp = dir, n = dircount; n > 0; n--, dp++)
17✔
5522
        {
5523
            uint32_t typewidth;
5524
            uint64_t datasize;
5525
            typewidth = TIFFDataWidth((TIFFDataType)dp->tdir_type);
15✔
5526
            if (typewidth == 0)
15✔
5527
            {
5528
                TIFFErrorExtR(
×
5529
                    tif, module,
5530
                    "Cannot determine size of unknown tag type %" PRIu16,
5531
                    dp->tdir_type);
×
5532
                return -1;
×
5533
            }
5534
            if (dp->tdir_count > UINT64_MAX / typewidth)
15✔
5535
                return -1;
×
5536
            datasize = (uint64_t)typewidth * dp->tdir_count;
15✔
5537
            if (!(tif->tif_flags & TIFF_BIGTIFF))
15✔
5538
            {
5539
                if (datasize <= 4)
15✔
5540
                    datasize = 0;
11✔
5541
            }
5542
            else
5543
            {
5544
                if (datasize <= 8)
×
5545
                    datasize = 0;
×
5546
            }
5547
            if (space > UINT64_MAX - datasize)
15✔
5548
                return -1;
×
5549
            space += datasize;
15✔
5550
        }
5551
        if (filesize == 0)
2✔
5552
            filesize = TIFFGetFileSize(tif);
2✔
5553
        if (filesize < space)
2✔
5554
            /* we should perhaps return in error ? */
5555
            space = filesize;
1✔
5556
        else
5557
            space = filesize - space;
1✔
5558
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
2✔
5559
            space /= td->td_samplesperpixel;
×
5560
        for (strip = 0; strip < td->td_nstrips; strip++)
4✔
5561
            td->td_stripbytecount_p[strip] = space;
2✔
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
        strip--;
2✔
5570
        if (td->td_stripoffset_p[strip] >
2✔
5571
            UINT64_MAX - td->td_stripbytecount_p[strip])
2✔
5572
            return -1;
×
5573
        if (td->td_stripoffset_p[strip] + td->td_stripbytecount_p[strip] >
2✔
5574
            filesize)
5575
        {
5576
            if (td->td_stripoffset_p[strip] >= filesize)
1✔
5577
            {
5578
                /* Not sure what we should in that case... */
5579
                td->td_stripbytecount_p[strip] = 0;
×
5580
            }
5581
            else
5582
            {
5583
                td->td_stripbytecount_p[strip] =
1✔
5584
                    filesize - td->td_stripoffset_p[strip];
1✔
5585
            }
5586
        }
5587
    }
5588
    else if (isTiled(tif))
8✔
5589
    {
5590
        uint64_t bytespertile = TIFFTileSize64(tif);
×
5591

5592
        for (strip = 0; strip < td->td_nstrips; strip++)
×
5593
            td->td_stripbytecount_p[strip] = bytespertile;
×
5594
    }
5595
    else
5596
    {
5597
        uint64_t rowbytes = TIFFScanlineSize64(tif);
8✔
5598
        uint32_t rowsperstrip = td->td_imagelength / td->td_stripsperimage;
8✔
5599
        for (strip = 0; strip < td->td_nstrips; strip++)
16✔
5600
        {
5601
            if (rowbytes > 0 && rowsperstrip > UINT64_MAX / rowbytes)
8✔
5602
                return -1;
×
5603
            td->td_stripbytecount_p[strip] = rowbytes * rowsperstrip;
8✔
5604
        }
5605
    }
5606
    TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
10✔
5607
    if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
10✔
5608
        td->td_rowsperstrip = td->td_imagelength;
5✔
5609
    return 1;
10✔
5610
}
5611

5612
static void MissingRequired(TIFF *tif, const char *tagname)
×
5613
{
5614
    static const char module[] = "MissingRequired";
5615

5616
    TIFFErrorExtR(tif, module,
×
5617
                  "TIFF directory is missing required \"%s\" field", tagname);
5618
}
×
5619

5620
static unsigned long hashFuncOffsetToNumber(const void *elt)
285,238✔
5621
{
5622
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
285,238✔
5623
        (const TIFFOffsetAndDirNumber *)elt;
5624
    const uint32_t hash = (uint32_t)(offsetAndDirNumber->offset >> 32) ^
285,238✔
5625
                          ((uint32_t)offsetAndDirNumber->offset & 0xFFFFFFFFU);
285,238✔
5626
    return hash;
285,238✔
5627
}
5628

5629
static bool equalFuncOffsetToNumber(const void *elt1, const void *elt2)
52,557✔
5630
{
5631
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
52,557✔
5632
        (const TIFFOffsetAndDirNumber *)elt1;
5633
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
52,557✔
5634
        (const TIFFOffsetAndDirNumber *)elt2;
5635
    return offsetAndDirNumber1->offset == offsetAndDirNumber2->offset;
52,557✔
5636
}
5637

5638
static unsigned long hashFuncNumberToOffset(const void *elt)
244,695✔
5639
{
5640
    const TIFFOffsetAndDirNumber *offsetAndDirNumber =
244,695✔
5641
        (const TIFFOffsetAndDirNumber *)elt;
5642
    return offsetAndDirNumber->dirNumber;
244,695✔
5643
}
5644

5645
static bool equalFuncNumberToOffset(const void *elt1, const void *elt2)
13,383✔
5646
{
5647
    const TIFFOffsetAndDirNumber *offsetAndDirNumber1 =
13,383✔
5648
        (const TIFFOffsetAndDirNumber *)elt1;
5649
    const TIFFOffsetAndDirNumber *offsetAndDirNumber2 =
13,383✔
5650
        (const TIFFOffsetAndDirNumber *)elt2;
5651
    return offsetAndDirNumber1->dirNumber == offsetAndDirNumber2->dirNumber;
13,383✔
5652
}
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)
115,534✔
5665
{
5666
    if (diroff == 0) /* no more directories */
115,534✔
5667
        return 0;
×
5668

5669
    if (tif->tif_map_dir_offset_to_number == NULL)
115,534✔
5670
    {
5671
        tif->tif_map_dir_offset_to_number = TIFFHashSetNew(
69,332✔
5672
            hashFuncOffsetToNumber, equalFuncOffsetToNumber, free);
5673
        if (tif->tif_map_dir_offset_to_number == NULL)
69,306✔
5674
        {
5675
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5676
                          "Not enough memory");
5677
            return 1;
×
5678
        }
5679
    }
5680

5681
    if (tif->tif_map_dir_number_to_offset == NULL)
115,508✔
5682
    {
5683
        /* No free callback for this map, as it shares the same items as
5684
         * tif->tif_map_dir_offset_to_number. */
5685
        tif->tif_map_dir_number_to_offset = TIFFHashSetNew(
69,307✔
5686
            hashFuncNumberToOffset, equalFuncNumberToOffset, NULL);
5687
        if (tif->tif_map_dir_number_to_offset == NULL)
69,331✔
5688
        {
5689
            TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5690
                          "Not enough memory");
5691
            return 1;
×
5692
        }
5693
    }
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
    TIFFOffsetAndDirNumber entry;
5701
    entry.offset = diroff;
115,532✔
5702
    entry.dirNumber = dirn;
115,532✔
5703

5704
    TIFFOffsetAndDirNumber *foundEntry =
5705
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
115,532✔
5706
            tif->tif_map_dir_offset_to_number, &entry);
5707
    if (foundEntry)
115,502✔
5708
    {
5709
        if (foundEntry->dirNumber == dirn)
38,374✔
5710
        {
5711
            return 1;
38,374✔
5712
        }
5713
        else
5714
        {
5715
            TIFFWarningExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5716
                            "TIFF directory %d has IFD looping to directory %u "
5717
                            "at offset 0x%" PRIx64 " (%" PRIu64 ")",
5718
                            (int)dirn - 1, foundEntry->dirNumber, diroff,
×
5719
                            diroff);
5720
            return 0;
×
5721
        }
5722
    }
5723

5724
    /* Check if offset of an IFD has been changed and update offset of that IFD
5725
     * number. */
5726
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
77,128✔
5727
        tif->tif_map_dir_number_to_offset, &entry);
5728
    if (foundEntry)
77,139✔
5729
    {
5730
        if (foundEntry->offset != diroff)
78✔
5731
        {
5732
            TIFFOffsetAndDirNumber entryOld;
5733
            entryOld.offset = foundEntry->offset;
78✔
5734
            entryOld.dirNumber = dirn;
78✔
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
            TIFFOffsetAndDirNumber *foundEntryOld =
5739
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
78✔
5740
                    tif->tif_map_dir_number_to_offset, &entryOld);
5741
            if (foundEntryOld)
78✔
5742
            {
5743
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
78✔
5744
                                  foundEntryOld);
5745
            }
5746
            foundEntryOld = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
78✔
5747
                tif->tif_map_dir_offset_to_number, &entryOld);
5748
            if (foundEntryOld)
78✔
5749
            {
5750
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
78✔
5751
                                  foundEntryOld);
5752
            }
5753

5754
            TIFFOffsetAndDirNumber *entryPtr = (TIFFOffsetAndDirNumber *)malloc(
78✔
5755
                sizeof(TIFFOffsetAndDirNumber));
5756
            if (entryPtr == NULL)
78✔
5757
            {
5758
                return 0;
×
5759
            }
5760

5761
            /* Add IFD offset and dirn to IFD directory list */
5762
            *entryPtr = entry;
78✔
5763

5764
            if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
78✔
5765
            {
5766
                TIFFErrorExtR(
×
5767
                    tif, "_TIFFCheckDirNumberAndOffset",
5768
                    "Insertion in tif_map_dir_offset_to_number failed");
5769
                return 0;
×
5770
            }
5771
            if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
78✔
5772
            {
5773
                TIFFErrorExtR(
×
5774
                    tif, "_TIFFCheckDirNumberAndOffset",
5775
                    "Insertion in tif_map_dir_number_to_offset failed");
5776
                return 0;
×
5777
            }
5778
        }
5779
        return 1;
78✔
5780
    }
5781

5782
    /* Arbitrary (hopefully big enough) limit */
5783
    if (TIFFHashSetSize(tif->tif_map_dir_offset_to_number) >=
77,061✔
5784
        TIFF_MAX_DIR_COUNT)
5785
    {
5786
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5787
                      "Cannot handle more than %u TIFF directories",
5788
                      TIFF_MAX_DIR_COUNT);
5789
        return 0;
×
5790
    }
5791

5792
    TIFFOffsetAndDirNumber *entryPtr =
5793
        (TIFFOffsetAndDirNumber *)malloc(sizeof(TIFFOffsetAndDirNumber));
77,044✔
5794
    if (entryPtr == NULL)
77,044✔
5795
    {
5796
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5797
                      "malloc(sizeof(TIFFOffsetAndDirNumber)) failed");
5798
        return 0;
×
5799
    }
5800

5801
    /* Add IFD offset and dirn to IFD directory list */
5802
    *entryPtr = entry;
77,044✔
5803

5804
    if (!TIFFHashSetInsert(tif->tif_map_dir_offset_to_number, entryPtr))
77,044✔
5805
    {
5806
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
×
5807
                      "Insertion in tif_map_dir_offset_to_number failed");
5808
        return 0;
×
5809
    }
5810
    if (!TIFFHashSetInsert(tif->tif_map_dir_number_to_offset, entryPtr))
77,063✔
5811
    {
5812
        TIFFErrorExtR(tif, "_TIFFCheckDirNumberAndOffset",
4✔
5813
                      "Insertion in tif_map_dir_number_to_offset failed");
5814
        return 0;
×
5815
    }
5816

5817
    return 1;
77,057✔
5818
} /* --- _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)
10,733✔
5828
{
5829
    if (diroff == 0) /* no more directories */
10,733✔
5830
        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
    if (tif->tif_map_dir_offset_to_number == NULL)
10,733✔
5837
        return 0;
5✔
5838
    TIFFOffsetAndDirNumber entry;
5839
    entry.offset = diroff;
10,728✔
5840
    entry.dirNumber = 0; /* not used */
10,728✔
5841

5842
    TIFFOffsetAndDirNumber *foundEntry =
5843
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
10,728✔
5844
            tif->tif_map_dir_offset_to_number, &entry);
5845
    if (foundEntry)
10,728✔
5846
    {
5847
        *dirn = foundEntry->dirNumber;
8,890✔
5848
        return 1;
8,890✔
5849
    }
5850

5851
    /* This updates the directory list for all main-IFDs in the file. */
5852
    TIFFNumberOfDirectories(tif);
1,838✔
5853

5854
    foundEntry = (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
1,838✔
5855
        tif->tif_map_dir_offset_to_number, &entry);
5856
    if (foundEntry)
1,838✔
5857
    {
5858
        *dirn = foundEntry->dirNumber;
1,786✔
5859
        return 1;
1,786✔
5860
    }
5861

5862
    return 0;
52✔
5863
} /*--- _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)
10,443✔
5873
{
5874

5875
    if (tif->tif_map_dir_number_to_offset == NULL)
10,443✔
5876
        return 0;
×
5877
    TIFFOffsetAndDirNumber entry;
5878
    entry.offset = 0; /* not used */
10,443✔
5879
    entry.dirNumber = dirn;
10,443✔
5880

5881
    TIFFOffsetAndDirNumber *foundEntry =
5882
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
10,443✔
5883
            tif->tif_map_dir_number_to_offset, &entry);
5884
    if (foundEntry)
10,443✔
5885
    {
5886
        *diroff = foundEntry->offset;
10,443✔
5887
        return 1;
10,443✔
5888
    }
5889

5890
    return 0;
×
5891
} /*--- _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)
1,364✔
5900
{
5901
    if (tif->tif_map_dir_offset_to_number == NULL)
1,364✔
5902
        return 1;
×
5903

5904
    TIFFOffsetAndDirNumber entryOld;
5905
    entryOld.offset = diroff;
1,364✔
5906
    entryOld.dirNumber = 0;
1,364✔
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
    TIFFOffsetAndDirNumber *foundEntryOldOff =
5912
        (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
1,364✔
5913
            tif->tif_map_dir_offset_to_number, &entryOld);
5914
    if (foundEntryOldOff)
1,364✔
5915
    {
5916
        entryOld.dirNumber = foundEntryOldOff->dirNumber;
1,353✔
5917
        if (tif->tif_map_dir_number_to_offset != NULL)
1,353✔
5918
        {
5919
            TIFFOffsetAndDirNumber *foundEntryOldDir =
5920
                (TIFFOffsetAndDirNumber *)TIFFHashSetLookup(
1,353✔
5921
                    tif->tif_map_dir_number_to_offset, &entryOld);
5922
            if (foundEntryOldDir)
1,353✔
5923
            {
5924
                TIFFHashSetRemove(tif->tif_map_dir_number_to_offset,
1,353✔
5925
                                  foundEntryOldDir);
5926
                TIFFHashSetRemove(tif->tif_map_dir_offset_to_number,
1,353✔
5927
                                  foundEntryOldOff);
5928
                return 1;
1,353✔
5929
            }
5930
        }
5931
        else
5932
        {
5933
            TIFFErrorExtR(tif, "_TIFFRemoveEntryFromDirectoryListByOffset",
×
5934
                          "Unexpectedly tif_map_dir_number_to_offset is "
5935
                          "missing but tif_map_dir_offset_to_number exists.");
5936
            return 0;
×
5937
        }
5938
    }
5939
    return 1;
11✔
5940
} /*--- _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
{
5948
    if ((uint64_t)count > dir->tdir_count)
×
5949
    {
5950
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
×
5951
        TIFFWarningExtR(tif, tif->tif_name,
×
5952
                        "incorrect count for field \"%s\" (%" PRIu64
5953
                        ", expecting %" PRIu32 "); tag ignored",
5954
                        fip ? fip->field_name : "unknown tagname",
5955
                        dir->tdir_count, count);
5956
        return (0);
×
5957
    }
5958
    else if ((uint64_t)count < dir->tdir_count)
×
5959
    {
5960
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
×
5961
        TIFFWarningExtR(tif, tif->tif_name,
×
5962
                        "incorrect count for field \"%s\" (%" PRIu64
5963
                        ", expecting %" PRIu32 "); tag trimmed",
5964
                        fip ? fip->field_name : "unknown tagname",
5965
                        dir->tdir_count, count);
5966
        dir->tdir_count = count;
×
5967
        return (1);
×
5968
    }
5969
    return (1);
×
5970
}
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,
52,075✔
5978
                                   TIFFDirEntry **pdir, uint64_t *nextdiroff)
5979
{
5980
    static const char module[] = "TIFFFetchDirectory";
5981

5982
    void *origdir;
5983
    uint16_t dircount16;
5984
    uint32_t dirsize;
5985
    TIFFDirEntry *dir;
5986
    uint8_t *ma;
5987
    TIFFDirEntry *mb;
5988
    uint16_t n;
5989

5990
    assert(pdir);
52,075✔
5991

5992
    tif->tif_diroff = diroff;
52,075✔
5993
    if (nextdiroff)
52,075✔
5994
        *nextdiroff = 0;
52,050✔
5995
    if (!isMapped(tif))
52,075✔
5996
    {
5997
        if (!SeekOK(tif, tif->tif_diroff))
52,045✔
5998
        {
5999
            TIFFErrorExtR(tif, module,
18✔
6000
                          "%s: Seek error accessing TIFF directory",
6001
                          tif->tif_name);
6002
            return 0;
×
6003
        }
6004
        if (!(tif->tif_flags & TIFF_BIGTIFF))
52,038✔
6005
        {
6006
            if (!ReadOK(tif, &dircount16, sizeof(uint16_t)))
51,094✔
6007
            {
6008
                TIFFErrorExtR(tif, module,
11✔
6009
                              "%s: Can not read TIFF directory count",
6010
                              tif->tif_name);
6011
                return 0;
11✔
6012
            }
6013
            if (tif->tif_flags & TIFF_SWAB)
51,100✔
6014
                TIFFSwabShort(&dircount16);
561✔
6015
            if (dircount16 > 4096)
51,106✔
6016
            {
6017
                TIFFErrorExtR(tif, module,
×
6018
                              "Sanity check on directory count failed, this is "
6019
                              "probably not a valid IFD offset");
6020
                return 0;
×
6021
            }
6022
            dirsize = 12;
51,106✔
6023
        }
6024
        else
6025
        {
6026
            uint64_t dircount64;
6027
            if (!ReadOK(tif, &dircount64, sizeof(uint64_t)))
944✔
6028
            {
6029
                TIFFErrorExtR(tif, module,
×
6030
                              "%s: Can not read TIFF directory count",
6031
                              tif->tif_name);
6032
                return 0;
×
6033
            }
6034
            if (tif->tif_flags & TIFF_SWAB)
944✔
6035
                TIFFSwabLong8(&dircount64);
19✔
6036
            if (dircount64 > 4096)
944✔
6037
            {
6038
                TIFFErrorExtR(tif, module,
×
6039
                              "Sanity check on directory count failed, this is "
6040
                              "probably not a valid IFD offset");
6041
                return 0;
×
6042
            }
6043
            dircount16 = (uint16_t)dircount64;
944✔
6044
            dirsize = 20;
944✔
6045
        }
6046
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
52,050✔
6047
                                   "to read TIFF directory");
6048
        if (origdir == NULL)
52,057✔
6049
            return 0;
4✔
6050
        if (!ReadOK(tif, origdir, (tmsize_t)(dircount16 * dirsize)))
52,053✔
6051
        {
6052
            TIFFErrorExtR(tif, module, "%.100s: Can not read TIFF directory",
×
6053
                          tif->tif_name);
6054
            _TIFFfreeExt(tif, origdir);
×
6055
            return 0;
×
6056
        }
6057
        /*
6058
         * Read offset to next directory for sequential scans if
6059
         * needed.
6060
         */
6061
        if (nextdiroff)
52,039✔
6062
        {
6063
            if (!(tif->tif_flags & TIFF_BIGTIFF))
52,039✔
6064
            {
6065
                uint32_t nextdiroff32;
6066
                if (!ReadOK(tif, &nextdiroff32, sizeof(uint32_t)))
51,089✔
6067
                    nextdiroff32 = 0;
1✔
6068
                if (tif->tif_flags & TIFF_SWAB)
51,104✔
6069
                    TIFFSwabLong(&nextdiroff32);
561✔
6070
                *nextdiroff = nextdiroff32;
51,092✔
6071
            }
6072
            else
6073
            {
6074
                if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
950✔
6075
                    *nextdiroff = 0;
×
6076
                if (tif->tif_flags & TIFF_SWAB)
944✔
6077
                    TIFFSwabLong8(nextdiroff);
19✔
6078
            }
6079
        }
6080
    }
6081
    else
6082
    {
6083
        tmsize_t m;
6084
        tmsize_t off;
6085
        if (tif->tif_diroff > (uint64_t)INT64_MAX)
30✔
6086
        {
6087
            TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
×
6088
            return (0);
×
6089
        }
6090
        off = (tmsize_t)tif->tif_diroff;
30✔
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
        if (!(tif->tif_flags & TIFF_BIGTIFF))
30✔
6102
        {
6103
            m = off + sizeof(uint16_t);
8✔
6104
            if ((m < off) || (m < (tmsize_t)sizeof(uint16_t)) ||
8✔
6105
                (m > tif->tif_size))
8✔
6106
            {
6107
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
×
6108
                return 0;
×
6109
            }
6110
            else
6111
            {
6112
                _TIFFmemcpy(&dircount16, tif->tif_base + off, sizeof(uint16_t));
8✔
6113
            }
6114
            off += sizeof(uint16_t);
8✔
6115
            if (tif->tif_flags & TIFF_SWAB)
8✔
6116
                TIFFSwabShort(&dircount16);
×
6117
            if (dircount16 > 4096)
8✔
6118
            {
6119
                TIFFErrorExtR(tif, module,
×
6120
                              "Sanity check on directory count failed, this is "
6121
                              "probably not a valid IFD offset");
6122
                return 0;
×
6123
            }
6124
            dirsize = 12;
8✔
6125
        }
6126
        else
6127
        {
6128
            uint64_t dircount64;
6129
            m = off + sizeof(uint64_t);
22✔
6130
            if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
22✔
6131
                (m > tif->tif_size))
×
6132
            {
6133
                TIFFErrorExtR(tif, module, "Can not read TIFF directory count");
22✔
6134
                return 0;
×
6135
            }
6136
            else
6137
            {
6138
                _TIFFmemcpy(&dircount64, tif->tif_base + off, sizeof(uint64_t));
×
6139
            }
6140
            off += sizeof(uint64_t);
×
6141
            if (tif->tif_flags & TIFF_SWAB)
×
6142
                TIFFSwabLong8(&dircount64);
×
6143
            if (dircount64 > 4096)
×
6144
            {
6145
                TIFFErrorExtR(tif, module,
×
6146
                              "Sanity check on directory count failed, this is "
6147
                              "probably not a valid IFD offset");
6148
                return 0;
×
6149
            }
6150
            dircount16 = (uint16_t)dircount64;
×
6151
            dirsize = 20;
×
6152
        }
6153
        if (dircount16 == 0)
8✔
6154
        {
6155
            TIFFErrorExtR(tif, module,
×
6156
                          "Sanity check on directory count failed, zero tag "
6157
                          "directories not supported");
6158
            return 0;
×
6159
        }
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
        uint64_t filesize = TIFFGetFileSize(tif);
8✔
6163
        uint64_t allocsize = (uint64_t)dircount16 * dirsize;
8✔
6164
        if (allocsize > filesize)
8✔
6165
        {
6166
            TIFFWarningExtR(
×
6167
                tif, module,
6168
                "Requested memory size for TIFF directory of %" PRIu64
6169
                " is greater than filesize %" PRIu64
6170
                ". Memory not allocated, TIFF directory not read",
6171
                allocsize, filesize);
6172
            return 0;
×
6173
        }
6174
        origdir = _TIFFCheckMalloc(tif, dircount16, dirsize,
8✔
6175
                                   "to read TIFF directory");
6176
        if (origdir == NULL)
8✔
6177
            return 0;
×
6178
        m = off + dircount16 * dirsize;
8✔
6179
        if ((m < off) || (m < (tmsize_t)(dircount16 * dirsize)) ||
8✔
6180
            (m > tif->tif_size))
8✔
6181
        {
6182
            TIFFErrorExtR(tif, module, "Can not read TIFF directory");
×
6183
            _TIFFfreeExt(tif, origdir);
×
6184
            return 0;
×
6185
        }
6186
        else
6187
        {
6188
            _TIFFmemcpy(origdir, tif->tif_base + off, dircount16 * dirsize);
8✔
6189
        }
6190
        if (nextdiroff)
8✔
6191
        {
6192
            off += dircount16 * dirsize;
8✔
6193
            if (!(tif->tif_flags & TIFF_BIGTIFF))
8✔
6194
            {
6195
                uint32_t nextdiroff32;
6196
                m = off + sizeof(uint32_t);
8✔
6197
                if ((m < off) || (m < (tmsize_t)sizeof(uint32_t)) ||
8✔
6198
                    (m > tif->tif_size))
8✔
6199
                    nextdiroff32 = 0;
×
6200
                else
6201
                    _TIFFmemcpy(&nextdiroff32, tif->tif_base + off,
8✔
6202
                                sizeof(uint32_t));
6203
                if (tif->tif_flags & TIFF_SWAB)
8✔
6204
                    TIFFSwabLong(&nextdiroff32);
×
6205
                *nextdiroff = nextdiroff32;
8✔
6206
            }
6207
            else
6208
            {
6209
                m = off + sizeof(uint64_t);
×
6210
                if ((m < off) || (m < (tmsize_t)sizeof(uint64_t)) ||
×
6211
                    (m > tif->tif_size))
×
6212
                    *nextdiroff = 0;
×
6213
                else
6214
                    _TIFFmemcpy(nextdiroff, tif->tif_base + off,
×
6215
                                sizeof(uint64_t));
6216
                if (tif->tif_flags & TIFF_SWAB)
×
6217
                    TIFFSwabLong8(nextdiroff);
×
6218
            }
6219
        }
6220
    }
6221
    /* No check against filesize needed here because "dir" should have same size
6222
     * than "origdir" checked above. */
6223
    dir = (TIFFDirEntry *)_TIFFCheckMalloc(
52,044✔
6224
        tif, dircount16, sizeof(TIFFDirEntry), "to read TIFF directory");
6225
    if (dir == 0)
52,049✔
6226
    {
6227
        _TIFFfreeExt(tif, origdir);
10✔
6228
        return 0;
×
6229
    }
6230
    ma = (uint8_t *)origdir;
52,039✔
6231
    mb = dir;
52,039✔
6232
    for (n = 0; n < dircount16; n++)
784,468✔
6233
    {
6234
        mb->tdir_ignore = FALSE;
732,475✔
6235
        if (tif->tif_flags & TIFF_SWAB)
732,475✔
6236
            TIFFSwabShort((uint16_t *)ma);
7,746✔
6237
        mb->tdir_tag = *(uint16_t *)ma;
732,475✔
6238
        ma += sizeof(uint16_t);
732,475✔
6239
        if (tif->tif_flags & TIFF_SWAB)
732,475✔
6240
            TIFFSwabShort((uint16_t *)ma);
7,746✔
6241
        mb->tdir_type = *(uint16_t *)ma;
732,459✔
6242
        ma += sizeof(uint16_t);
732,459✔
6243
        if (!(tif->tif_flags & TIFF_BIGTIFF))
732,459✔
6244
        {
6245
            if (tif->tif_flags & TIFF_SWAB)
718,760✔
6246
                TIFFSwabLong((uint32_t *)ma);
7,537✔
6247
            mb->tdir_count = (uint64_t)(*(uint32_t *)ma);
718,728✔
6248
            ma += sizeof(uint32_t);
718,728✔
6249
            mb->tdir_offset.toff_long8 = 0;
718,728✔
6250
            *(uint32_t *)(&mb->tdir_offset) = *(uint32_t *)ma;
718,728✔
6251
            ma += sizeof(uint32_t);
718,728✔
6252
        }
6253
        else
6254
        {
6255
            if (tif->tif_flags & TIFF_SWAB)
13,699✔
6256
                TIFFSwabLong8((uint64_t *)ma);
209✔
6257
            mb->tdir_count = TIFFReadUInt64(ma);
13,699✔
6258
            ma += sizeof(uint64_t);
13,699✔
6259
            mb->tdir_offset.toff_long8 = TIFFReadUInt64(ma);
13,699✔
6260
            ma += sizeof(uint64_t);
13,701✔
6261
        }
6262
        mb++;
732,429✔
6263
    }
6264
    _TIFFfreeExt(tif, origdir);
51,993✔
6265
    *pdir = dir;
52,035✔
6266
    return dircount16;
52,035✔
6267
}
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)
472,896✔
6273
{
6274
    static const char module[] = "TIFFFetchNormalTag";
6275
    enum TIFFReadDirEntryErr err;
6276
    uint32_t fii;
6277
    const TIFFField *fip = NULL;
472,896✔
6278
    TIFFReadDirectoryFindFieldInfo(tif, dp->tdir_tag, &fii);
472,896✔
6279
    if (fii == FAILED_FII)
472,651✔
6280
    {
6281
        TIFFErrorExtR(tif, "TIFFFetchNormalTag",
×
6282
                      "No definition found for tag %" PRIu16, dp->tdir_tag);
×
6283
        return 0;
×
6284
    }
6285
    fip = tif->tif_fields[fii];
472,834✔
6286
    assert(fip != NULL); /* should not happen */
472,834✔
6287
    assert(fip->set_get_field_type !=
472,834✔
6288
           TIFF_SETGET_OTHER); /* if so, we shouldn't arrive here but deal with
6289
                                  this in specialized code */
6290
    assert(fip->set_get_field_type !=
472,834✔
6291
           TIFF_SETGET_INT); /* if so, we shouldn't arrive here as this is only
6292
                                the case for pseudo-tags */
6293
    err = TIFFReadDirEntryErrOk;
472,834✔
6294
    switch (fip->set_get_field_type)
472,834✔
6295
    {
6296
        case TIFF_SETGET_UNDEFINED:
5✔
6297
            TIFFErrorExtR(
5✔
6298
                tif, "TIFFFetchNormalTag",
6299
                "Defined set_get_field_type of custom tag %u (%s) is "
6300
                "TIFF_SETGET_UNDEFINED and thus tag is not read from file",
6301
                fip->field_tag, fip->field_name);
6302
            break;
5✔
6303
        case TIFF_SETGET_ASCII:
35,892✔
6304
        {
6305
            uint8_t *data;
6306
            assert(fip->field_passcount == 0);
35,892✔
6307
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
35,892✔
6308
            if (err == TIFFReadDirEntryErrOk)
35,870✔
6309
            {
6310
                size_t mb = 0;
35,871✔
6311
                int n;
6312
                if (data != NULL)
35,871✔
6313
                {
6314
                    if (dp->tdir_count > 0 && data[dp->tdir_count - 1] == 0)
35,875✔
6315
                    {
6316
                        /* optimization: if data is known to be 0 terminated, we
6317
                         * can use strlen() */
6318
                        mb = strlen((const char *)data);
35,869✔
6319
                    }
6320
                    else
6321
                    {
6322
                        /* general case. equivalent to non-portable */
6323
                        /* mb = strnlen((const char*)data,
6324
                         * (uint32_t)dp->tdir_count); */
6325
                        uint8_t *ma = data;
6✔
6326
                        while (mb < (uint32_t)dp->tdir_count)
33✔
6327
                        {
6328
                            if (*ma == 0)
27✔
6329
                                break;
×
6330
                            ma++;
27✔
6331
                            mb++;
27✔
6332
                        }
6333
                    }
6334
                }
6335
                if (!EvaluateIFDdatasizeReading(tif, dp))
35,871✔
6336
                {
6337
                    if (data != NULL)
21✔
6338
                        _TIFFfreeExt(tif, data);
×
6339
                    return (0);
×
6340
                }
6341
                if (mb + 1 < (uint32_t)dp->tdir_count)
35,872✔
6342
                    TIFFWarningExtR(
×
6343
                        tif, module,
6344
                        "ASCII value for tag \"%s\" contains null byte in "
6345
                        "value; value incorrectly truncated during reading due "
6346
                        "to implementation limitations",
6347
                        fip->field_name);
6348
                else if (mb + 1 > (uint32_t)dp->tdir_count)
35,872✔
6349
                {
6350
                    TIFFWarningExtR(tif, module,
1✔
6351
                                    "ASCII value for tag \"%s\" does not end "
6352
                                    "in null byte. Forcing it to be null",
6353
                                    fip->field_name);
6354
                    /* TIFFReadDirEntryArrayWithLimit() ensures this can't be
6355
                     * larger than MAX_SIZE_TAG_DATA */
6356
                    assert((uint32_t)dp->tdir_count + 1 == dp->tdir_count + 1);
1✔
6357
                    uint8_t *o =
6358
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
1✔
6359
                    if (o == NULL)
1✔
6360
                    {
6361
                        if (data != NULL)
×
6362
                            _TIFFfreeExt(tif, data);
×
6363
                        return (0);
×
6364
                    }
6365
                    if (dp->tdir_count > 0)
1✔
6366
                    {
6367
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
1✔
6368
                    }
6369
                    o[(uint32_t)dp->tdir_count] = 0;
1✔
6370
                    if (data != 0)
1✔
6371
                        _TIFFfreeExt(tif, data);
1✔
6372
                    data = o;
1✔
6373
                }
6374
                n = TIFFSetField(tif, dp->tdir_tag, data);
35,872✔
6375
                if (data != 0)
35,889✔
6376
                    _TIFFfreeExt(tif, data);
35,887✔
6377
                if (!n)
35,887✔
6378
                    return (0);
×
6379
            }
6380
        }
6381
        break;
35,886✔
6382
        case TIFF_SETGET_UINT8:
×
6383
        {
6384
            uint8_t data = 0;
×
6385
            assert(fip->field_readcount == 1);
×
6386
            assert(fip->field_passcount == 0);
×
6387
            err = TIFFReadDirEntryByte(tif, dp, &data);
×
6388
            if (err == TIFFReadDirEntryErrOk)
×
6389
            {
6390
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6391
                    return (0);
×
6392
            }
6393
        }
6394
        break;
×
6395
        case TIFF_SETGET_SINT8:
×
6396
        {
6397
            int8_t data = 0;
×
6398
            assert(fip->field_readcount == 1);
×
6399
            assert(fip->field_passcount == 0);
×
6400
            err = TIFFReadDirEntrySbyte(tif, dp, &data);
×
6401
            if (err == TIFFReadDirEntryErrOk)
×
6402
            {
6403
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6404
                    return (0);
×
6405
            }
6406
        }
6407
        break;
×
6408
        case TIFF_SETGET_UINT16:
161,600✔
6409
        {
6410
            uint16_t data;
6411
            assert(fip->field_readcount == 1);
161,600✔
6412
            assert(fip->field_passcount == 0);
161,600✔
6413
            err = TIFFReadDirEntryShort(tif, dp, &data);
161,600✔
6414
            if (err == TIFFReadDirEntryErrOk)
161,599✔
6415
            {
6416
                if (!TIFFSetField(tif, dp->tdir_tag, data))
161,608✔
6417
                    return (0);
×
6418
            }
6419
        }
6420
        break;
161,610✔
6421
        case TIFF_SETGET_SINT16:
×
6422
        {
6423
            int16_t data;
6424
            assert(fip->field_readcount == 1);
×
6425
            assert(fip->field_passcount == 0);
×
6426
            err = TIFFReadDirEntrySshort(tif, dp, &data);
×
6427
            if (err == TIFFReadDirEntryErrOk)
×
6428
            {
6429
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6430
                    return (0);
×
6431
            }
6432
        }
6433
        break;
×
6434
        case TIFF_SETGET_UINT32:
170,758✔
6435
        {
6436
            uint32_t data;
6437
            assert(fip->field_readcount == 1);
170,758✔
6438
            assert(fip->field_passcount == 0);
170,758✔
6439
            err = TIFFReadDirEntryLong(tif, dp, &data);
170,758✔
6440
            if (err == TIFFReadDirEntryErrOk)
170,728✔
6441
            {
6442
                if (!TIFFSetField(tif, dp->tdir_tag, data))
170,733✔
6443
                    return (0);
×
6444
            }
6445
        }
6446
        break;
170,744✔
6447
        case TIFF_SETGET_SINT32:
×
6448
        {
6449
            int32_t data;
6450
            assert(fip->field_readcount == 1);
×
6451
            assert(fip->field_passcount == 0);
×
6452
            err = TIFFReadDirEntrySlong(tif, dp, &data);
×
6453
            if (err == TIFFReadDirEntryErrOk)
×
6454
            {
6455
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6456
                    return (0);
×
6457
            }
6458
        }
6459
        break;
×
6460
        case TIFF_SETGET_UINT64:
×
6461
        {
6462
            uint64_t data;
6463
            assert(fip->field_readcount == 1);
×
6464
            assert(fip->field_passcount == 0);
×
6465
            err = TIFFReadDirEntryLong8(tif, dp, &data);
×
6466
            if (err == TIFFReadDirEntryErrOk)
×
6467
            {
6468
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
6469
                    return 0;
×
6470
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6471
                    return (0);
×
6472
            }
6473
        }
6474
        break;
×
6475
        case TIFF_SETGET_SINT64:
×
6476
        {
6477
            int64_t data;
6478
            assert(fip->field_readcount == 1);
×
6479
            assert(fip->field_passcount == 0);
×
6480
            err = TIFFReadDirEntrySlong8(tif, dp, &data);
×
6481
            if (err == TIFFReadDirEntryErrOk)
×
6482
            {
6483
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
6484
                    return 0;
×
6485
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6486
                    return (0);
×
6487
            }
6488
        }
6489
        break;
×
6490
        case TIFF_SETGET_FLOAT:
248✔
6491
        {
6492
            float data;
6493
            assert(fip->field_readcount == 1);
248✔
6494
            assert(fip->field_passcount == 0);
248✔
6495
            err = TIFFReadDirEntryFloat(tif, dp, &data);
248✔
6496
            if (err == TIFFReadDirEntryErrOk)
248✔
6497
            {
6498
                if (!EvaluateIFDdatasizeReading(tif, dp))
248✔
6499
                    return 0;
×
6500
                if (!TIFFSetField(tif, dp->tdir_tag, data))
248✔
6501
                    return (0);
×
6502
            }
6503
        }
6504
        break;
248✔
6505
        case TIFF_SETGET_DOUBLE:
×
6506
        {
6507
            double data;
6508
            assert(fip->field_readcount == 1);
×
6509
            assert(fip->field_passcount == 0);
×
6510
            err = TIFFReadDirEntryDouble(tif, dp, &data);
×
6511
            if (err == TIFFReadDirEntryErrOk)
×
6512
            {
6513
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
6514
                    return 0;
×
6515
                if (!TIFFSetField(tif, dp->tdir_tag, data))
×
6516
                    return (0);
×
6517
            }
6518
        }
6519
        break;
×
6520
        case TIFF_SETGET_IFD8:
8✔
6521
        {
6522
            uint64_t data;
6523
            assert(fip->field_readcount == 1);
8✔
6524
            assert(fip->field_passcount == 0);
8✔
6525
            err = TIFFReadDirEntryIfd8(tif, dp, &data);
8✔
6526
            if (err == TIFFReadDirEntryErrOk)
8✔
6527
            {
6528
                if (!EvaluateIFDdatasizeReading(tif, dp))
8✔
6529
                    return 0;
×
6530
                if (!TIFFSetField(tif, dp->tdir_tag, data))
8✔
6531
                    return (0);
×
6532
            }
6533
        }
6534
        break;
8✔
6535
        case TIFF_SETGET_UINT16_PAIR:
1,400✔
6536
        {
6537
            uint16_t *data;
6538
            assert(fip->field_readcount == 2);
1,400✔
6539
            assert(fip->field_passcount == 0);
1,400✔
6540
            if (dp->tdir_count != 2)
1,400✔
6541
            {
6542
                TIFFWarningExtR(tif, module,
×
6543
                                "incorrect count for field \"%s\", expected 2, "
6544
                                "got %" PRIu64,
6545
                                fip->field_name, dp->tdir_count);
6546
                return (0);
×
6547
            }
6548
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
1,400✔
6549
            if (err == TIFFReadDirEntryErrOk)
1,399✔
6550
            {
6551
                int m;
6552
                assert(data); /* avoid CLang static Analyzer false positive */
1,399✔
6553
                m = TIFFSetField(tif, dp->tdir_tag, data[0], data[1]);
1,399✔
6554
                _TIFFfreeExt(tif, data);
1,400✔
6555
                if (!m)
1,400✔
6556
                    return (0);
×
6557
            }
6558
        }
6559
        break;
1,400✔
6560
        case TIFF_SETGET_C0_UINT8:
×
6561
        {
6562
            uint8_t *data;
6563
            assert(fip->field_readcount >= 1);
×
6564
            assert(fip->field_passcount == 0);
×
6565
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6566
            {
6567
                TIFFWarningExtR(tif, module,
×
6568
                                "incorrect count for field \"%s\", expected "
6569
                                "%d, got %" PRIu64,
6570
                                fip->field_name, (int)fip->field_readcount,
×
6571
                                dp->tdir_count);
6572
                return (0);
×
6573
            }
6574
            else
6575
            {
6576
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
×
6577
                if (err == TIFFReadDirEntryErrOk)
×
6578
                {
6579
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6580
                    {
6581
                        if (data != 0)
×
6582
                            _TIFFfreeExt(tif, data);
×
6583
                        return 0;
×
6584
                    }
6585
                    int m;
6586
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6587
                    if (data != 0)
×
6588
                        _TIFFfreeExt(tif, data);
×
6589
                    if (!m)
×
6590
                        return (0);
×
6591
                }
6592
            }
6593
        }
6594
        break;
×
6595
        case TIFF_SETGET_C0_SINT8:
×
6596
        {
6597
            int8_t *data;
6598
            assert(fip->field_readcount >= 1);
×
6599
            assert(fip->field_passcount == 0);
×
6600
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6601
            {
6602
                TIFFWarningExtR(tif, module,
×
6603
                                "incorrect count for field \"%s\", expected "
6604
                                "%d, got %" PRIu64,
6605
                                fip->field_name, (int)fip->field_readcount,
×
6606
                                dp->tdir_count);
6607
                return (0);
×
6608
            }
6609
            else
6610
            {
6611
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
×
6612
                if (err == TIFFReadDirEntryErrOk)
×
6613
                {
6614
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6615
                    {
6616
                        if (data != 0)
×
6617
                            _TIFFfreeExt(tif, data);
×
6618
                        return 0;
×
6619
                    }
6620
                    int m;
6621
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6622
                    if (data != 0)
×
6623
                        _TIFFfreeExt(tif, data);
×
6624
                    if (!m)
×
6625
                        return (0);
×
6626
                }
6627
            }
6628
        }
6629
        break;
×
6630
        case TIFF_SETGET_C0_UINT16:
×
6631
        {
6632
            uint16_t *data;
6633
            assert(fip->field_readcount >= 1);
×
6634
            assert(fip->field_passcount == 0);
×
6635
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6636
            {
6637
                TIFFWarningExtR(tif, module,
×
6638
                                "incorrect count for field \"%s\", expected "
6639
                                "%d, got %" PRIu64,
6640
                                fip->field_name, (int)fip->field_readcount,
×
6641
                                dp->tdir_count);
6642
                return (0);
×
6643
            }
6644
            else
6645
            {
6646
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
×
6647
                if (err == TIFFReadDirEntryErrOk)
×
6648
                {
6649
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6650
                    {
6651
                        if (data != 0)
×
6652
                            _TIFFfreeExt(tif, data);
×
6653
                        return 0;
×
6654
                    }
6655
                    int m;
6656
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6657
                    if (data != 0)
×
6658
                        _TIFFfreeExt(tif, data);
×
6659
                    if (!m)
×
6660
                        return (0);
×
6661
                }
6662
            }
6663
        }
6664
        break;
×
6665
        case TIFF_SETGET_C0_SINT16:
×
6666
        {
6667
            int16_t *data;
6668
            assert(fip->field_readcount >= 1);
×
6669
            assert(fip->field_passcount == 0);
×
6670
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6671
            {
6672
                TIFFWarningExtR(tif, module,
×
6673
                                "incorrect count for field \"%s\", expected "
6674
                                "%d, got %" PRIu64,
6675
                                fip->field_name, (int)fip->field_readcount,
×
6676
                                dp->tdir_count);
6677
                return (0);
×
6678
            }
6679
            else
6680
            {
6681
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
×
6682
                if (err == TIFFReadDirEntryErrOk)
×
6683
                {
6684
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6685
                    {
6686
                        if (data != 0)
×
6687
                            _TIFFfreeExt(tif, data);
×
6688
                        return 0;
×
6689
                    }
6690
                    int m;
6691
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6692
                    if (data != 0)
×
6693
                        _TIFFfreeExt(tif, data);
×
6694
                    if (!m)
×
6695
                        return (0);
×
6696
                }
6697
            }
6698
        }
6699
        break;
×
6700
        case TIFF_SETGET_C0_UINT32:
×
6701
        {
6702
            uint32_t *data;
6703
            assert(fip->field_readcount >= 1);
×
6704
            assert(fip->field_passcount == 0);
×
6705
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6706
            {
6707
                TIFFWarningExtR(tif, module,
×
6708
                                "incorrect count for field \"%s\", expected "
6709
                                "%d, got %" PRIu64,
6710
                                fip->field_name, (int)fip->field_readcount,
×
6711
                                dp->tdir_count);
6712
                return (0);
×
6713
            }
6714
            else
6715
            {
6716
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
×
6717
                if (err == TIFFReadDirEntryErrOk)
×
6718
                {
6719
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6720
                    {
6721
                        if (data != 0)
×
6722
                            _TIFFfreeExt(tif, data);
×
6723
                        return 0;
×
6724
                    }
6725
                    int m;
6726
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6727
                    if (data != 0)
×
6728
                        _TIFFfreeExt(tif, data);
×
6729
                    if (!m)
×
6730
                        return (0);
×
6731
                }
6732
            }
6733
        }
6734
        break;
×
6735
        case TIFF_SETGET_C0_SINT32:
×
6736
        {
6737
            int32_t *data;
6738
            assert(fip->field_readcount >= 1);
×
6739
            assert(fip->field_passcount == 0);
×
6740
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6741
            {
6742
                TIFFWarningExtR(tif, module,
×
6743
                                "incorrect count for field \"%s\", expected "
6744
                                "%d, got %" PRIu64,
6745
                                fip->field_name, (int)fip->field_readcount,
×
6746
                                dp->tdir_count);
6747
                return (0);
×
6748
            }
6749
            else
6750
            {
6751
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
×
6752
                if (err == TIFFReadDirEntryErrOk)
×
6753
                {
6754
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6755
                    {
6756
                        if (data != 0)
×
6757
                            _TIFFfreeExt(tif, data);
×
6758
                        return 0;
×
6759
                    }
6760
                    int m;
6761
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6762
                    if (data != 0)
×
6763
                        _TIFFfreeExt(tif, data);
×
6764
                    if (!m)
×
6765
                        return (0);
×
6766
                }
6767
            }
6768
        }
6769
        break;
×
6770
        case TIFF_SETGET_C0_UINT64:
×
6771
        {
6772
            uint64_t *data;
6773
            assert(fip->field_readcount >= 1);
×
6774
            assert(fip->field_passcount == 0);
×
6775
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6776
            {
6777
                TIFFWarningExtR(tif, module,
×
6778
                                "incorrect count for field \"%s\", expected "
6779
                                "%d, got %" PRIu64,
6780
                                fip->field_name, (int)fip->field_readcount,
×
6781
                                dp->tdir_count);
6782
                return (0);
×
6783
            }
6784
            else
6785
            {
6786
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
×
6787
                if (err == TIFFReadDirEntryErrOk)
×
6788
                {
6789
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6790
                    {
6791
                        if (data != 0)
×
6792
                            _TIFFfreeExt(tif, data);
×
6793
                        return 0;
×
6794
                    }
6795
                    int m;
6796
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6797
                    if (data != 0)
×
6798
                        _TIFFfreeExt(tif, data);
×
6799
                    if (!m)
×
6800
                        return (0);
×
6801
                }
6802
            }
6803
        }
6804
        break;
×
6805
        case TIFF_SETGET_C0_SINT64:
×
6806
        {
6807
            int64_t *data;
6808
            assert(fip->field_readcount >= 1);
×
6809
            assert(fip->field_passcount == 0);
×
6810
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6811
            {
6812
                TIFFWarningExtR(tif, module,
×
6813
                                "incorrect count for field \"%s\", expected "
6814
                                "%d, got %" PRIu64,
6815
                                fip->field_name, (int)fip->field_readcount,
×
6816
                                dp->tdir_count);
6817
                return (0);
×
6818
            }
6819
            else
6820
            {
6821
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
×
6822
                if (err == TIFFReadDirEntryErrOk)
×
6823
                {
6824
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6825
                    {
6826
                        if (data != 0)
×
6827
                            _TIFFfreeExt(tif, data);
×
6828
                        return 0;
×
6829
                    }
6830
                    int m;
6831
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6832
                    if (data != 0)
×
6833
                        _TIFFfreeExt(tif, data);
×
6834
                    if (!m)
×
6835
                        return (0);
×
6836
                }
6837
            }
6838
        }
6839
        break;
×
6840
        case TIFF_SETGET_C0_FLOAT:
1,084✔
6841
        {
6842
            float *data;
6843
            assert(fip->field_readcount >= 1);
1,084✔
6844
            assert(fip->field_passcount == 0);
1,084✔
6845
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
1,084✔
6846
            {
6847
                TIFFWarningExtR(tif, module,
×
6848
                                "incorrect count for field \"%s\", expected "
6849
                                "%d, got %" PRIu64,
6850
                                fip->field_name, (int)fip->field_readcount,
×
6851
                                dp->tdir_count);
6852
                return (0);
×
6853
            }
6854
            else
6855
            {
6856
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
1,084✔
6857
                if (err == TIFFReadDirEntryErrOk)
1,084✔
6858
                {
6859
                    if (!EvaluateIFDdatasizeReading(tif, dp))
1,084✔
6860
                    {
6861
                        if (data != 0)
×
6862
                            _TIFFfreeExt(tif, data);
×
6863
                        return 0;
×
6864
                    }
6865
                    int m;
6866
                    m = TIFFSetField(tif, dp->tdir_tag, data);
1,084✔
6867
                    if (data != 0)
1,084✔
6868
                        _TIFFfreeExt(tif, data);
1,084✔
6869
                    if (!m)
1,084✔
6870
                        return (0);
×
6871
                }
6872
            }
6873
        }
6874
        break;
1,084✔
6875
        /*--: Rational2Double: Extend for Double Arrays and Rational-Arrays read
6876
         * into Double-Arrays. */
6877
        case TIFF_SETGET_C0_DOUBLE:
×
6878
        {
6879
            double *data;
6880
            assert(fip->field_readcount >= 1);
×
6881
            assert(fip->field_passcount == 0);
×
6882
            if (dp->tdir_count != (uint64_t)fip->field_readcount)
×
6883
            {
6884
                TIFFWarningExtR(tif, module,
×
6885
                                "incorrect count for field \"%s\", expected "
6886
                                "%d, got %" PRIu64,
6887
                                fip->field_name, (int)fip->field_readcount,
×
6888
                                dp->tdir_count);
6889
                return (0);
×
6890
            }
6891
            else
6892
            {
6893
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
×
6894
                if (err == TIFFReadDirEntryErrOk)
×
6895
                {
6896
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6897
                    {
6898
                        if (data != 0)
×
6899
                            _TIFFfreeExt(tif, data);
×
6900
                        return 0;
×
6901
                    }
6902
                    int m;
6903
                    m = TIFFSetField(tif, dp->tdir_tag, data);
×
6904
                    if (data != 0)
×
6905
                        _TIFFfreeExt(tif, data);
×
6906
                    if (!m)
×
6907
                        return (0);
×
6908
                }
6909
            }
6910
        }
6911
        break;
×
6912
        case TIFF_SETGET_C16_ASCII:
×
6913
        {
6914
            uint8_t *data;
6915
            assert(fip->field_readcount == TIFF_VARIABLE);
×
6916
            assert(fip->field_passcount == 1);
×
6917
            if (dp->tdir_count > 0xFFFF)
×
6918
                err = TIFFReadDirEntryErrCount;
×
6919
            else
6920
            {
6921
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
×
6922
                if (err == TIFFReadDirEntryErrOk)
×
6923
                {
6924
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6925
                    {
6926
                        if (data != 0)
×
6927
                            _TIFFfreeExt(tif, data);
×
6928
                        return 0;
×
6929
                    }
6930
                    int m;
6931
                    if (data != 0 && dp->tdir_count > 0 &&
×
6932
                        data[dp->tdir_count - 1] != '\0')
×
6933
                    {
6934
                        TIFFWarningExtR(tif, module,
×
6935
                                        "ASCII value for ASCII array tag "
6936
                                        "\"%s\" does not end in null "
6937
                                        "byte. Forcing it to be null",
6938
                                        fip->field_name);
6939
                        /* Enlarge buffer and add terminating null. */
6940
                        uint8_t *o =
6941
                            _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
×
6942
                        if (o == NULL)
×
6943
                        {
6944
                            if (data != NULL)
×
6945
                                _TIFFfreeExt(tif, data);
×
6946
                            return (0);
×
6947
                        }
6948
                        if (dp->tdir_count > 0)
×
6949
                        {
6950
                            _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
×
6951
                        }
6952
                        o[(uint32_t)dp->tdir_count] = 0;
×
6953
                        dp->tdir_count++; /* Increment for added null. */
×
6954
                        if (data != 0)
×
6955
                            _TIFFfreeExt(tif, data);
×
6956
                        data = o;
×
6957
                    }
6958
                    m = TIFFSetField(tif, dp->tdir_tag,
×
6959
                                     (uint16_t)(dp->tdir_count), data);
×
6960
                    if (data != 0)
×
6961
                        _TIFFfreeExt(tif, data);
×
6962
                    if (!m)
×
6963
                        return (0);
×
6964
                }
6965
            }
6966
        }
6967
        break;
×
6968
        case TIFF_SETGET_C16_UINT8:
×
6969
        {
6970
            uint8_t *data;
6971
            assert(fip->field_readcount == TIFF_VARIABLE);
×
6972
            assert(fip->field_passcount == 1);
×
6973
            if (dp->tdir_count > 0xFFFF)
×
6974
                err = TIFFReadDirEntryErrCount;
×
6975
            else
6976
            {
6977
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
×
6978
                if (err == TIFFReadDirEntryErrOk)
×
6979
                {
6980
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
6981
                    {
6982
                        if (data != 0)
×
6983
                            _TIFFfreeExt(tif, data);
×
6984
                        return 0;
×
6985
                    }
6986
                    int m;
6987
                    m = TIFFSetField(tif, dp->tdir_tag,
×
6988
                                     (uint16_t)(dp->tdir_count), data);
×
6989
                    if (data != 0)
×
6990
                        _TIFFfreeExt(tif, data);
×
6991
                    if (!m)
×
6992
                        return (0);
×
6993
                }
6994
            }
6995
        }
6996
        break;
×
6997
        case TIFF_SETGET_C16_SINT8:
×
6998
        {
6999
            int8_t *data;
7000
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7001
            assert(fip->field_passcount == 1);
×
7002
            if (dp->tdir_count > 0xFFFF)
×
7003
                err = TIFFReadDirEntryErrCount;
×
7004
            else
7005
            {
7006
                err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
×
7007
                if (err == TIFFReadDirEntryErrOk)
×
7008
                {
7009
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7010
                    {
7011
                        if (data != 0)
×
7012
                            _TIFFfreeExt(tif, data);
×
7013
                        return 0;
×
7014
                    }
7015
                    int m;
7016
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7017
                                     (uint16_t)(dp->tdir_count), data);
×
7018
                    if (data != 0)
×
7019
                        _TIFFfreeExt(tif, data);
×
7020
                    if (!m)
×
7021
                        return (0);
×
7022
                }
7023
            }
7024
        }
7025
        break;
×
7026
        case TIFF_SETGET_C16_UINT16:
32,127✔
7027
        {
7028
            uint16_t *data;
7029
            assert(fip->field_readcount == TIFF_VARIABLE);
32,127✔
7030
            assert(fip->field_passcount == 1);
32,127✔
7031
            if (dp->tdir_count > 0xFFFF)
32,127✔
7032
                err = TIFFReadDirEntryErrCount;
×
7033
            else
7034
            {
7035
                err = TIFFReadDirEntryShortArray(tif, dp, &data);
32,127✔
7036
                if (err == TIFFReadDirEntryErrOk)
32,096✔
7037
                {
7038
                    if (!EvaluateIFDdatasizeReading(tif, dp))
32,101✔
7039
                    {
7040
                        if (data != 0)
×
7041
                            _TIFFfreeExt(tif, data);
×
7042
                        return 0;
×
7043
                    }
7044
                    int m;
7045
                    m = TIFFSetField(tif, dp->tdir_tag,
32,118✔
7046
                                     (uint16_t)(dp->tdir_count), data);
32,118✔
7047
                    if (data != 0)
32,116✔
7048
                        _TIFFfreeExt(tif, data);
32,119✔
7049
                    if (!m)
32,120✔
7050
                        return (0);
×
7051
                }
7052
            }
7053
        }
7054
        break;
32,115✔
7055
        case TIFF_SETGET_C16_SINT16:
×
7056
        {
7057
            int16_t *data;
7058
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7059
            assert(fip->field_passcount == 1);
×
7060
            if (dp->tdir_count > 0xFFFF)
×
7061
                err = TIFFReadDirEntryErrCount;
×
7062
            else
7063
            {
7064
                err = TIFFReadDirEntrySshortArray(tif, dp, &data);
×
7065
                if (err == TIFFReadDirEntryErrOk)
×
7066
                {
7067
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7068
                    {
7069
                        if (data != 0)
×
7070
                            _TIFFfreeExt(tif, data);
×
7071
                        return 0;
×
7072
                    }
7073
                    int m;
7074
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7075
                                     (uint16_t)(dp->tdir_count), data);
×
7076
                    if (data != 0)
×
7077
                        _TIFFfreeExt(tif, data);
×
7078
                    if (!m)
×
7079
                        return (0);
×
7080
                }
7081
            }
7082
        }
7083
        break;
×
7084
        case TIFF_SETGET_C16_UINT32:
×
7085
        {
7086
            uint32_t *data;
7087
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7088
            assert(fip->field_passcount == 1);
×
7089
            if (dp->tdir_count > 0xFFFF)
×
7090
                err = TIFFReadDirEntryErrCount;
×
7091
            else
7092
            {
7093
                err = TIFFReadDirEntryLongArray(tif, dp, &data);
×
7094
                if (err == TIFFReadDirEntryErrOk)
×
7095
                {
7096
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7097
                    {
7098
                        if (data != 0)
×
7099
                            _TIFFfreeExt(tif, data);
×
7100
                        return 0;
×
7101
                    }
7102
                    int m;
7103
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7104
                                     (uint16_t)(dp->tdir_count), data);
×
7105
                    if (data != 0)
×
7106
                        _TIFFfreeExt(tif, data);
×
7107
                    if (!m)
×
7108
                        return (0);
×
7109
                }
7110
            }
7111
        }
7112
        break;
×
7113
        case TIFF_SETGET_C16_SINT32:
×
7114
        {
7115
            int32_t *data;
7116
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7117
            assert(fip->field_passcount == 1);
×
7118
            if (dp->tdir_count > 0xFFFF)
×
7119
                err = TIFFReadDirEntryErrCount;
×
7120
            else
7121
            {
7122
                err = TIFFReadDirEntrySlongArray(tif, dp, &data);
×
7123
                if (err == TIFFReadDirEntryErrOk)
×
7124
                {
7125
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7126
                    {
7127
                        if (data != 0)
×
7128
                            _TIFFfreeExt(tif, data);
×
7129
                        return 0;
×
7130
                    }
7131
                    int m;
7132
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7133
                                     (uint16_t)(dp->tdir_count), data);
×
7134
                    if (data != 0)
×
7135
                        _TIFFfreeExt(tif, data);
×
7136
                    if (!m)
×
7137
                        return (0);
×
7138
                }
7139
            }
7140
        }
7141
        break;
×
7142
        case TIFF_SETGET_C16_UINT64:
×
7143
        {
7144
            uint64_t *data;
7145
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7146
            assert(fip->field_passcount == 1);
×
7147
            if (dp->tdir_count > 0xFFFF)
×
7148
                err = TIFFReadDirEntryErrCount;
×
7149
            else
7150
            {
7151
                err = TIFFReadDirEntryLong8Array(tif, dp, &data);
×
7152
                if (err == TIFFReadDirEntryErrOk)
×
7153
                {
7154
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7155
                    {
7156
                        if (data != 0)
×
7157
                            _TIFFfreeExt(tif, data);
×
7158
                        return 0;
×
7159
                    }
7160
                    int m;
7161
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7162
                                     (uint16_t)(dp->tdir_count), data);
×
7163
                    if (data != 0)
×
7164
                        _TIFFfreeExt(tif, data);
×
7165
                    if (!m)
×
7166
                        return (0);
×
7167
                }
7168
            }
7169
        }
7170
        break;
×
7171
        case TIFF_SETGET_C16_SINT64:
×
7172
        {
7173
            int64_t *data;
7174
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7175
            assert(fip->field_passcount == 1);
×
7176
            if (dp->tdir_count > 0xFFFF)
×
7177
                err = TIFFReadDirEntryErrCount;
×
7178
            else
7179
            {
7180
                err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
×
7181
                if (err == TIFFReadDirEntryErrOk)
×
7182
                {
7183
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7184
                    {
7185
                        if (data != 0)
×
7186
                            _TIFFfreeExt(tif, data);
×
7187
                        return 0;
×
7188
                    }
7189
                    int m;
7190
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7191
                                     (uint16_t)(dp->tdir_count), data);
×
7192
                    if (data != 0)
×
7193
                        _TIFFfreeExt(tif, data);
×
7194
                    if (!m)
×
7195
                        return (0);
×
7196
                }
7197
            }
7198
        }
7199
        break;
×
7200
        case TIFF_SETGET_C16_FLOAT:
×
7201
        {
7202
            float *data;
7203
            assert(fip->field_readcount == TIFF_VARIABLE);
×
7204
            assert(fip->field_passcount == 1);
×
7205
            if (dp->tdir_count > 0xFFFF)
×
7206
                err = TIFFReadDirEntryErrCount;
×
7207
            else
7208
            {
7209
                err = TIFFReadDirEntryFloatArray(tif, dp, &data);
×
7210
                if (err == TIFFReadDirEntryErrOk)
×
7211
                {
7212
                    if (!EvaluateIFDdatasizeReading(tif, dp))
×
7213
                    {
7214
                        if (data != 0)
×
7215
                            _TIFFfreeExt(tif, data);
×
7216
                        return 0;
×
7217
                    }
7218
                    int m;
7219
                    m = TIFFSetField(tif, dp->tdir_tag,
×
7220
                                     (uint16_t)(dp->tdir_count), data);
×
7221
                    if (data != 0)
×
7222
                        _TIFFfreeExt(tif, data);
×
7223
                    if (!m)
×
7224
                        return (0);
×
7225
                }
7226
            }
7227
        }
7228
        break;
×
7229
        case TIFF_SETGET_C16_DOUBLE:
66,717✔
7230
        {
7231
            double *data;
7232
            assert(fip->field_readcount == TIFF_VARIABLE);
66,717✔
7233
            assert(fip->field_passcount == 1);
66,717✔
7234
            if (dp->tdir_count > 0xFFFF)
66,717✔
7235
                err = TIFFReadDirEntryErrCount;
×
7236
            else
7237
            {
7238
                err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
66,717✔
7239
                if (err == TIFFReadDirEntryErrOk)
66,686✔
7240
                {
7241
                    if (!EvaluateIFDdatasizeReading(tif, dp))
66,678✔
7242
                    {
7243
                        if (data != 0)
×
7244
                            _TIFFfreeExt(tif, data);
×
7245
                        return 0;
×
7246
                    }
7247
                    int m;
7248
                    m = TIFFSetField(tif, dp->tdir_tag,
66,675✔
7249
                                     (uint16_t)(dp->tdir_count), data);
66,675✔
7250
                    if (data != 0)
66,690✔
7251
                        _TIFFfreeExt(tif, data);
66,685✔
7252
                    if (!m)
66,693✔
7253
                        return (0);
×
7254
                }
7255
            }
7256
        }
7257
        break;
66,701✔
7258
        case TIFF_SETGET_C16_IFD8:
78✔
7259
        {
7260
            uint64_t *data;
7261
            assert(fip->field_readcount == TIFF_VARIABLE);
78✔
7262
            assert(fip->field_passcount == 1);
78✔
7263
            if (dp->tdir_count > 0xFFFF)
78✔
7264
                err = TIFFReadDirEntryErrCount;
×
7265
            else
7266
            {
7267
                err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
78✔
7268
                if (err == TIFFReadDirEntryErrOk)
78✔
7269
                {
7270
                    if (!EvaluateIFDdatasizeReading(tif, dp))
78✔
7271
                    {
7272
                        if (data != 0)
×
7273
                            _TIFFfreeExt(tif, data);
×
7274
                        return 0;
×
7275
                    }
7276
                    int m;
7277
                    m = TIFFSetField(tif, dp->tdir_tag,
78✔
7278
                                     (uint16_t)(dp->tdir_count), data);
78✔
7279
                    if (data != 0)
78✔
7280
                        _TIFFfreeExt(tif, data);
78✔
7281
                    if (!m)
78✔
7282
                        return (0);
×
7283
                }
7284
            }
7285
        }
7286
        break;
78✔
7287
        case TIFF_SETGET_C32_ASCII:
×
7288
        {
7289
            uint8_t *data;
7290
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7291
            assert(fip->field_passcount == 1);
×
7292
            err = TIFFReadDirEntryByteArray(tif, dp, &data);
×
7293
            if (err == TIFFReadDirEntryErrOk)
×
7294
            {
7295
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7296
                {
7297
                    if (data != 0)
×
7298
                        _TIFFfreeExt(tif, data);
×
7299
                    return 0;
×
7300
                }
7301
                int m;
7302
                if (data != 0 && dp->tdir_count > 0 &&
×
7303
                    data[dp->tdir_count - 1] != '\0')
×
7304
                {
7305
                    TIFFWarningExtR(
×
7306
                        tif, module,
7307
                        "ASCII value for ASCII array tag \"%s\" does not end "
7308
                        "in null byte. Forcing it to be null",
7309
                        fip->field_name);
7310
                    /* Enlarge buffer and add terminating null. */
7311
                    uint8_t *o =
7312
                        _TIFFmallocExt(tif, (uint32_t)dp->tdir_count + 1);
×
7313
                    if (o == NULL)
×
7314
                    {
7315
                        if (data != NULL)
×
7316
                            _TIFFfreeExt(tif, data);
×
7317
                        return (0);
×
7318
                    }
7319
                    if (dp->tdir_count > 0)
×
7320
                    {
7321
                        _TIFFmemcpy(o, data, (uint32_t)dp->tdir_count);
×
7322
                    }
7323
                    o[(uint32_t)dp->tdir_count] = 0;
×
7324
                    dp->tdir_count++; /* Increment for added null. */
×
7325
                    if (data != 0)
×
7326
                        _TIFFfreeExt(tif, data);
×
7327
                    data = o;
×
7328
                }
7329
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7330
                                 data);
7331
                if (data != 0)
×
7332
                    _TIFFfreeExt(tif, data);
×
7333
                if (!m)
×
7334
                    return (0);
×
7335
            }
7336
        }
7337
        break;
×
7338
        case TIFF_SETGET_C32_UINT8:
2,135✔
7339
        {
7340
            uint8_t *data;
7341
            uint32_t count = 0;
2,135✔
7342
            assert(fip->field_readcount == TIFF_VARIABLE2);
2,135✔
7343
            assert(fip->field_passcount == 1);
2,135✔
7344
            if (fip->field_tag == TIFFTAG_RICHTIFFIPTC &&
2,135✔
7345
                dp->tdir_type == TIFF_LONG)
×
7346
            {
×
7347
                /* Adobe's software (wrongly) writes RichTIFFIPTC tag with
7348
                 * data type LONG instead of UNDEFINED. Work around this
7349
                 * frequently found issue */
7350
                void *origdata;
7351
                err = TIFFReadDirEntryArray(tif, dp, &count, 4, &origdata);
×
7352
                if ((err != TIFFReadDirEntryErrOk) || (origdata == 0))
×
7353
                {
7354
                    data = NULL;
×
7355
                }
7356
                else
7357
                {
7358
                    if (tif->tif_flags & TIFF_SWAB)
×
7359
                        TIFFSwabArrayOfLong((uint32_t *)origdata, count);
×
7360
                    data = (uint8_t *)origdata;
×
7361
                    count = (uint32_t)(count * 4);
×
7362
                }
7363
            }
7364
            else
7365
            {
7366
                err = TIFFReadDirEntryByteArray(tif, dp, &data);
2,135✔
7367
                count = (uint32_t)(dp->tdir_count);
2,136✔
7368
            }
7369
            if (err == TIFFReadDirEntryErrOk)
2,136✔
7370
            {
7371
                if (!EvaluateIFDdatasizeReading(tif, dp))
2,136✔
7372
                {
7373
                    if (data != 0)
×
7374
                        _TIFFfreeExt(tif, data);
×
7375
                    return 0;
×
7376
                }
7377
                int m;
7378
                m = TIFFSetField(tif, dp->tdir_tag, count, data);
2,136✔
7379
                if (data != 0)
2,136✔
7380
                    _TIFFfreeExt(tif, data);
2,136✔
7381
                if (!m)
2,135✔
7382
                    return (0);
×
7383
            }
7384
        }
7385
        break;
2,135✔
7386
        case TIFF_SETGET_C32_SINT8:
×
7387
        {
7388
            int8_t *data = NULL;
×
7389
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7390
            assert(fip->field_passcount == 1);
×
7391
            err = TIFFReadDirEntrySbyteArray(tif, dp, &data);
×
7392
            if (err == TIFFReadDirEntryErrOk)
×
7393
            {
7394
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7395
                {
7396
                    if (data != 0)
×
7397
                        _TIFFfreeExt(tif, data);
×
7398
                    return 0;
×
7399
                }
7400
                int m;
7401
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7402
                                 data);
7403
                if (data != 0)
×
7404
                    _TIFFfreeExt(tif, data);
×
7405
                if (!m)
×
7406
                    return (0);
×
7407
            }
7408
        }
7409
        break;
×
7410
        case TIFF_SETGET_C32_UINT16:
×
7411
        {
7412
            uint16_t *data;
7413
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7414
            assert(fip->field_passcount == 1);
×
7415
            err = TIFFReadDirEntryShortArray(tif, dp, &data);
×
7416
            if (err == TIFFReadDirEntryErrOk)
×
7417
            {
7418
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7419
                {
7420
                    if (data != 0)
×
7421
                        _TIFFfreeExt(tif, data);
×
7422
                    return 0;
×
7423
                }
7424
                int m;
7425
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7426
                                 data);
7427
                if (data != 0)
×
7428
                    _TIFFfreeExt(tif, data);
×
7429
                if (!m)
×
7430
                    return (0);
×
7431
            }
7432
        }
7433
        break;
×
7434
        case TIFF_SETGET_C32_SINT16:
×
7435
        {
7436
            int16_t *data = NULL;
×
7437
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7438
            assert(fip->field_passcount == 1);
×
7439
            err = TIFFReadDirEntrySshortArray(tif, dp, &data);
×
7440
            if (err == TIFFReadDirEntryErrOk)
×
7441
            {
7442
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7443
                {
7444
                    if (data != 0)
×
7445
                        _TIFFfreeExt(tif, data);
×
7446
                    return 0;
×
7447
                }
7448
                int m;
7449
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7450
                                 data);
7451
                if (data != 0)
×
7452
                    _TIFFfreeExt(tif, data);
×
7453
                if (!m)
×
7454
                    return (0);
×
7455
            }
7456
        }
7457
        break;
×
7458
        case TIFF_SETGET_C32_UINT32:
789✔
7459
        {
7460
            uint32_t *data;
7461
            assert(fip->field_readcount == TIFF_VARIABLE2);
789✔
7462
            assert(fip->field_passcount == 1);
789✔
7463
            err = TIFFReadDirEntryLongArray(tif, dp, &data);
789✔
7464
            if (err == TIFFReadDirEntryErrOk)
789✔
7465
            {
7466
                if (!EvaluateIFDdatasizeReading(tif, dp))
789✔
7467
                {
7468
                    if (data != 0)
×
7469
                        _TIFFfreeExt(tif, data);
×
7470
                    return 0;
×
7471
                }
7472
                int m;
7473
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
789✔
7474
                                 data);
7475
                if (data != 0)
788✔
7476
                    _TIFFfreeExt(tif, data);
788✔
7477
                if (!m)
788✔
7478
                    return (0);
×
7479
            }
7480
        }
7481
        break;
788✔
7482
        case TIFF_SETGET_C32_SINT32:
×
7483
        {
7484
            int32_t *data = NULL;
×
7485
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7486
            assert(fip->field_passcount == 1);
×
7487
            err = TIFFReadDirEntrySlongArray(tif, dp, &data);
×
7488
            if (err == TIFFReadDirEntryErrOk)
×
7489
            {
7490
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7491
                {
7492
                    if (data != 0)
×
7493
                        _TIFFfreeExt(tif, data);
×
7494
                    return 0;
×
7495
                }
7496
                int m;
7497
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7498
                                 data);
7499
                if (data != 0)
×
7500
                    _TIFFfreeExt(tif, data);
×
7501
                if (!m)
×
7502
                    return (0);
×
7503
            }
7504
        }
7505
        break;
×
7506
        case TIFF_SETGET_C32_UINT64:
6✔
7507
        {
7508
            uint64_t *data;
7509
            assert(fip->field_readcount == TIFF_VARIABLE2);
6✔
7510
            assert(fip->field_passcount == 1);
6✔
7511
            err = TIFFReadDirEntryLong8Array(tif, dp, &data);
6✔
7512
            if (err == TIFFReadDirEntryErrOk)
6✔
7513
            {
7514
                if (!EvaluateIFDdatasizeReading(tif, dp))
6✔
7515
                {
7516
                    if (data != 0)
×
7517
                        _TIFFfreeExt(tif, data);
×
7518
                    return 0;
×
7519
                }
7520
                int m;
7521
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
6✔
7522
                                 data);
7523
                if (data != 0)
6✔
7524
                    _TIFFfreeExt(tif, data);
6✔
7525
                if (!m)
6✔
7526
                    return (0);
×
7527
            }
7528
        }
7529
        break;
6✔
7530
        case TIFF_SETGET_C32_SINT64:
×
7531
        {
7532
            int64_t *data = NULL;
×
7533
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7534
            assert(fip->field_passcount == 1);
×
7535
            err = TIFFReadDirEntrySlong8Array(tif, dp, &data);
×
7536
            if (err == TIFFReadDirEntryErrOk)
×
7537
            {
7538
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7539
                {
7540
                    if (data != 0)
×
7541
                        _TIFFfreeExt(tif, data);
×
7542
                    return 0;
×
7543
                }
7544
                int m;
7545
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7546
                                 data);
7547
                if (data != 0)
×
7548
                    _TIFFfreeExt(tif, data);
×
7549
                if (!m)
×
7550
                    return (0);
×
7551
            }
7552
        }
7553
        break;
×
7554
        case TIFF_SETGET_C32_FLOAT:
×
7555
        {
7556
            float *data;
7557
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7558
            assert(fip->field_passcount == 1);
×
7559
            err = TIFFReadDirEntryFloatArray(tif, dp, &data);
×
7560
            if (err == TIFFReadDirEntryErrOk)
×
7561
            {
7562
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7563
                {
7564
                    if (data != 0)
×
7565
                        _TIFFfreeExt(tif, data);
×
7566
                    return 0;
×
7567
                }
7568
                int m;
7569
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7570
                                 data);
7571
                if (data != 0)
×
7572
                    _TIFFfreeExt(tif, data);
×
7573
                if (!m)
×
7574
                    return (0);
×
7575
            }
7576
        }
7577
        break;
×
7578
        case TIFF_SETGET_C32_DOUBLE:
×
7579
        {
7580
            double *data;
7581
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7582
            assert(fip->field_passcount == 1);
×
7583
            err = TIFFReadDirEntryDoubleArray(tif, dp, &data);
×
7584
            if (err == TIFFReadDirEntryErrOk)
×
7585
            {
7586
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7587
                {
7588
                    if (data != 0)
×
7589
                        _TIFFfreeExt(tif, data);
×
7590
                    return 0;
×
7591
                }
7592
                int m;
7593
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7594
                                 data);
7595
                if (data != 0)
×
7596
                    _TIFFfreeExt(tif, data);
×
7597
                if (!m)
×
7598
                    return (0);
×
7599
            }
7600
        }
7601
        break;
×
7602
        case TIFF_SETGET_C32_IFD8:
×
7603
        {
7604
            uint64_t *data;
7605
            assert(fip->field_readcount == TIFF_VARIABLE2);
×
7606
            assert(fip->field_passcount == 1);
×
7607
            err = TIFFReadDirEntryIfd8Array(tif, dp, &data);
×
7608
            if (err == TIFFReadDirEntryErrOk)
×
7609
            {
7610
                if (!EvaluateIFDdatasizeReading(tif, dp))
×
7611
                {
7612
                    if (data != 0)
×
7613
                        _TIFFfreeExt(tif, data);
×
7614
                    return 0;
×
7615
                }
7616
                int m;
7617
                m = TIFFSetField(tif, dp->tdir_tag, (uint32_t)(dp->tdir_count),
×
7618
                                 data);
7619
                if (data != 0)
×
7620
                    _TIFFfreeExt(tif, data);
×
7621
                if (!m)
×
7622
                    return (0);
×
7623
            }
7624
        }
7625
        break;
×
7626
        default:
×
7627
            assert(0); /* we should never get here */
×
7628
            break;
7629
    }
7630
    if (err != TIFFReadDirEntryErrOk)
472,808✔
7631
    {
7632
        TIFFReadDirEntryOutputErr(tif, err, module, fip->field_name, recover);
9✔
7633
        return (0);
9✔
7634
    }
7635
    return (1);
472,799✔
7636
}
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,
81,996✔
7643
                               uint64_t **lpp)
7644
{
7645
    static const char module[] = "TIFFFetchStripThing";
7646
    enum TIFFReadDirEntryErr err;
7647
    uint64_t *data;
7648
    err = TIFFReadDirEntryLong8ArrayWithLimit(tif, dir, &data, nstrips);
81,996✔
7649
    if (err != TIFFReadDirEntryErrOk)
81,986✔
7650
    {
7651
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
2✔
7652
        TIFFReadDirEntryOutputErr(tif, err, module,
2✔
7653
                                  fip ? fip->field_name : "unknown tagname", 0);
7654
        return (0);
2✔
7655
    }
7656
    if (dir->tdir_count < (uint64_t)nstrips)
81,984✔
7657
    {
7658
        uint64_t *resizeddata;
7659
        const TIFFField *fip = TIFFFieldWithTag(tif, dir->tdir_tag);
2✔
7660
        const char *pszMax = getenv("LIBTIFF_STRILE_ARRAY_MAX_RESIZE_COUNT");
2✔
7661
        uint32_t max_nstrips = 1000000;
2✔
7662
        if (pszMax)
2✔
7663
            max_nstrips = (uint32_t)atoi(pszMax);
×
7664
        TIFFReadDirEntryOutputErr(tif, TIFFReadDirEntryErrCount, module,
2✔
7665
                                  fip ? fip->field_name : "unknown tagname",
7666
                                  (nstrips <= max_nstrips));
7667

7668
        if (nstrips > max_nstrips)
2✔
7669
        {
7670
            _TIFFfreeExt(tif, data);
×
7671
            return (0);
×
7672
        }
7673

7674
        const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t);
2✔
7675
        if (allocsize > 100 * 1024 * 1024)
2✔
7676
        {
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
            const uint64_t filesize = TIFFGetFileSize(tif);
×
7681
            if (allocsize > filesize)
×
7682
            {
7683
                TIFFWarningExtR(
×
7684
                    tif, module,
7685
                    "Requested memory size for StripArray of %" PRIu64
7686
                    " is greater than filesize %" PRIu64
7687
                    ". Memory not allocated",
7688
                    allocsize, filesize);
7689
                _TIFFfreeExt(tif, data);
×
7690
                return (0);
×
7691
            }
7692
        }
7693
        resizeddata = (uint64_t *)_TIFFCheckMalloc(
2✔
7694
            tif, nstrips, sizeof(uint64_t), "for strip array");
7695
        if (resizeddata == 0)
2✔
7696
        {
7697
            _TIFFfreeExt(tif, data);
×
7698
            return (0);
×
7699
        }
7700
        if (dir->tdir_count)
2✔
7701
            _TIFFmemcpy(resizeddata, data,
2✔
7702
                        (uint32_t)dir->tdir_count * sizeof(uint64_t));
2✔
7703
        _TIFFmemset(resizeddata + (uint32_t)dir->tdir_count, 0,
2✔
7704
                    (nstrips - (uint32_t)dir->tdir_count) * sizeof(uint64_t));
2✔
7705
        _TIFFfreeExt(tif, data);
2✔
7706
        data = resizeddata;
12✔
7707
    }
7708
    *lpp = data;
81,994✔
7709
    return (1);
81,994✔
7710
}
7711

7712
/*
7713
 * Fetch and set the SubjectDistance EXIF tag.
7714
 */
7715
static int TIFFFetchSubjectDistance(TIFF *tif, TIFFDirEntry *dir)
×
7716
{
7717
    static const char module[] = "TIFFFetchSubjectDistance";
7718
    enum TIFFReadDirEntryErr err;
7719
    UInt64Aligned_t m;
7720
    m.l = 0;
×
7721
    assert(sizeof(double) == 8);
7722
    assert(sizeof(uint64_t) == 8);
7723
    assert(sizeof(uint32_t) == 4);
7724
    if (dir->tdir_count != 1)
×
7725
        err = TIFFReadDirEntryErrCount;
×
7726
    else if (dir->tdir_type != TIFF_RATIONAL)
×
7727
        err = TIFFReadDirEntryErrType;
×
7728
    else
7729
    {
7730
        if (!(tif->tif_flags & TIFF_BIGTIFF))
×
7731
        {
7732
            uint32_t offset;
7733
            offset = *(uint32_t *)(&dir->tdir_offset);
×
7734
            if (tif->tif_flags & TIFF_SWAB)
×
7735
                TIFFSwabLong(&offset);
×
7736
            err = TIFFReadDirEntryData(tif, offset, 8, m.i);
×
7737
        }
7738
        else
7739
        {
7740
            m.l = dir->tdir_offset.toff_long8;
×
7741
            err = TIFFReadDirEntryErrOk;
×
7742
        }
7743
    }
7744
    if (err == TIFFReadDirEntryErrOk)
×
7745
    {
7746
        double n;
7747
        if (tif->tif_flags & TIFF_SWAB)
×
7748
            TIFFSwabArrayOfLong(m.i, 2);
×
7749
        if (m.i[0] == 0)
×
7750
            n = 0.0;
×
7751
        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
            n = -1.0;
×
7758
        else
7759
            n = (double)m.i[0] / (double)m.i[1];
×
7760
        return (TIFFSetField(tif, dir->tdir_tag, n));
×
7761
    }
7762
    else
7763
    {
7764
        TIFFReadDirEntryOutputErr(tif, err, module, "SubjectDistance", TRUE);
×
7765
        return (0);
×
7766
    }
7767
}
7768

7769
static void allocChoppedUpStripArrays(TIFF *tif, uint32_t nstrips,
163✔
7770
                                      uint64_t stripbytes,
7771
                                      uint32_t rowsperstrip)
7772
{
7773
    TIFFDirectory *td = &tif->tif_dir;
163✔
7774
    uint64_t bytecount;
7775
    uint64_t offset;
7776
    uint64_t last_offset;
7777
    uint64_t last_bytecount;
7778
    uint32_t i;
7779
    uint64_t *newcounts;
7780
    uint64_t *newoffsets;
7781

7782
    offset = TIFFGetStrileOffset(tif, 0);
163✔
7783
    last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
163✔
7784
    last_bytecount = TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
163✔
7785
    if (last_offset > UINT64_MAX - last_bytecount ||
163✔
7786
        last_offset + last_bytecount < offset)
163✔
7787
    {
7788
        return;
×
7789
    }
7790
    bytecount = last_offset + last_bytecount - offset;
163✔
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
    const uint64_t allocsize = (uint64_t)nstrips * sizeof(uint64_t) * 2;
163✔
7797
    if (allocsize > 100 * 1024 * 1024)
163✔
7798
    {
7799
        const uint64_t filesize = TIFFGetFileSize(tif);
×
7800
        if (allocsize > filesize)
×
7801
        {
7802
            TIFFWarningExtR(tif, "allocChoppedUpStripArrays",
×
7803
                            "Requested memory size for StripByteCount and "
7804
                            "StripOffsets %" PRIu64
7805
                            " is greater than filesize %" PRIu64
7806
                            ". Memory not allocated",
7807
                            allocsize, filesize);
7808
            return;
×
7809
        }
7810
    }
7811

7812
    newcounts =
7813
        (uint64_t *)_TIFFCheckMalloc(tif, nstrips, sizeof(uint64_t),
163✔
7814
                                     "for chopped \"StripByteCounts\" array");
7815
    newoffsets = (uint64_t *)_TIFFCheckMalloc(
163✔
7816
        tif, nstrips, sizeof(uint64_t), "for chopped \"StripOffsets\" array");
7817
    if (newcounts == NULL || newoffsets == NULL)
163✔
7818
    {
7819
        /*
7820
         * Unable to allocate new strip information, give up and use
7821
         * the original one strip information.
7822
         */
7823
        if (newcounts != NULL)
×
7824
            _TIFFfreeExt(tif, newcounts);
×
7825
        if (newoffsets != NULL)
×
7826
            _TIFFfreeExt(tif, newoffsets);
×
7827
        return;
×
7828
    }
7829

7830
    /*
7831
     * Fill the strip information arrays with new bytecounts and offsets
7832
     * that reflect the broken-up format.
7833
     */
7834
    for (i = 0; i < nstrips; i++)
107,056✔
7835
    {
7836
        if (stripbytes > bytecount)
106,893✔
7837
            stripbytes = bytecount;
53✔
7838
        newcounts[i] = stripbytes;
106,893✔
7839
        newoffsets[i] = stripbytes ? offset : 0;
106,893✔
7840
        offset += stripbytes;
106,893✔
7841
        bytecount -= stripbytes;
106,893✔
7842
    }
7843

7844
    /*
7845
     * Replace old single strip info with multi-strip info.
7846
     */
7847
    td->td_stripsperimage = td->td_nstrips = nstrips;
163✔
7848
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
163✔
7849

7850
    _TIFFfreeExt(tif, td->td_stripbytecount_p);
163✔
7851
    _TIFFfreeExt(tif, td->td_stripoffset_p);
163✔
7852
    td->td_stripbytecount_p = newcounts;
163✔
7853
    td->td_stripoffset_p = newoffsets;
163✔
7854
#ifdef STRIPBYTECOUNTSORTED_UNUSED
7855
    td->td_stripbytecountsorted = 1;
7856
#endif
7857
    tif->tif_flags |= TIFF_CHOPPEDUPARRAYS;
163✔
7858
}
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)
21,274✔
7867
{
7868
    register TIFFDirectory *td = &tif->tif_dir;
21,274✔
7869
    uint64_t bytecount;
7870
    uint64_t offset;
7871
    uint32_t rowblock;
7872
    uint64_t rowblockbytes;
7873
    uint64_t stripbytes;
7874
    uint32_t nstrips;
7875
    uint32_t rowsperstrip;
7876

7877
    bytecount = TIFFGetStrileByteCount(tif, 0);
21,274✔
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
    if (bytecount == 0 && tif->tif_mode != O_RDONLY)
21,257✔
7882
        return;
520✔
7883
    offset = TIFFGetStrileByteCount(tif, 0);
20,737✔
7884
    assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
20,742✔
7885
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
20,734✔
7886
        rowblock = td->td_ycbcrsubsampling[1];
1✔
7887
    else
7888
        rowblock = 1;
20,733✔
7889
    rowblockbytes = TIFFVTileSize64(tif, rowblock);
20,734✔
7890
    /*
7891
     * Make the rows hold at least one scanline, but fill specified amount
7892
     * of data if possible.
7893
     */
7894
    if (rowblockbytes > STRIP_SIZE_DEFAULT)
20,728✔
7895
    {
7896
        stripbytes = rowblockbytes;
23✔
7897
        rowsperstrip = rowblock;
23✔
7898
    }
7899
    else if (rowblockbytes > 0)
20,705✔
7900
    {
7901
        uint32_t rowblocksperstrip;
7902
        rowblocksperstrip = (uint32_t)(STRIP_SIZE_DEFAULT / rowblockbytes);
20,690✔
7903
        rowsperstrip = rowblocksperstrip * rowblock;
20,690✔
7904
        stripbytes = rowblocksperstrip * rowblockbytes;
20,690✔
7905
    }
7906
    else
7907
        return;
15✔
7908

7909
    /*
7910
     * never increase the number of rows per strip
7911
     */
7912
    if (rowsperstrip >= td->td_rowsperstrip || rowsperstrip == 0)
20,713✔
7913
        return;
20,551✔
7914
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
162✔
7915
    if (nstrips == 0)
162✔
7916
        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
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000 &&
163✔
7921
        (offset >= TIFFGetFileSize(tif) ||
1✔
7922
         stripbytes > (TIFFGetFileSize(tif) - offset) / (nstrips - 1)))
×
7923
    {
7924
        return;
1✔
7925
    }
7926

7927
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
161✔
7928
}
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)
5✔
7937
{
7938
    TIFFDirectory *td = &tif->tif_dir;
5✔
7939
    uint32_t rowblock;
7940
    uint64_t rowblockbytes;
7941
    uint32_t i;
7942
    uint64_t stripsize;
7943
    uint32_t rowblocksperstrip;
7944
    uint32_t rowsperstrip;
7945
    uint64_t stripbytes;
7946
    uint32_t nstrips;
7947

7948
    stripsize = TIFFStripSize64(tif);
5✔
7949

7950
    assert(tif->tif_dir.td_planarconfig == PLANARCONFIG_CONTIG);
5✔
7951
    assert(tif->tif_dir.td_compression == COMPRESSION_NONE);
5✔
7952
    assert((tif->tif_flags & (TIFF_STRIPCHOP | TIFF_ISTILED)) ==
5✔
7953
           TIFF_STRIPCHOP);
7954
    assert(stripsize > 0x7FFFFFFFUL);
5✔
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
    if (TIFFGetStrileByteCount(tif, 0) == 0 && tif->tif_mode != O_RDONLY)
5✔
7960
        return;
×
7961

7962
    if ((td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
5✔
7963
        rowblock = td->td_ycbcrsubsampling[1];
×
7964
    else
7965
        rowblock = 1;
5✔
7966
    rowblockbytes = TIFFVStripSize64(tif, rowblock);
5✔
7967
    if (rowblockbytes == 0 || rowblockbytes > 0x7FFFFFFFUL)
5✔
7968
    {
7969
        /* In case of file with gigantic width */
7970
        return;
3✔
7971
    }
7972

7973
    /* Check that the strips are contiguous and of the expected size */
7974
    for (i = 0; i < td->td_nstrips; i++)
5✔
7975
    {
7976
        if (i == td->td_nstrips - 1)
3✔
7977
        {
7978
            if (TIFFGetStrileByteCount(tif, i) <
4✔
7979
                TIFFVStripSize64(tif,
2✔
7980
                                 td->td_imagelength - i * td->td_rowsperstrip))
2✔
7981
            {
7982
                return;
×
7983
            }
7984
        }
7985
        else
7986
        {
7987
            if (TIFFGetStrileByteCount(tif, i) != stripsize)
1✔
7988
            {
7989
                return;
×
7990
            }
7991
            if (i > 0 && TIFFGetStrileOffset(tif, i) !=
1✔
7992
                             TIFFGetStrileOffset(tif, i - 1) +
×
7993
                                 TIFFGetStrileByteCount(tif, i - 1))
×
7994
            {
7995
                return;
×
7996
            }
7997
        }
7998
    }
7999

8000
    /* Aim for 512 MB strips (that will still be manageable by 32 bit builds */
8001
    rowblocksperstrip = (uint32_t)(512 * 1024 * 1024 / rowblockbytes);
2✔
8002
    if (rowblocksperstrip == 0)
2✔
8003
        rowblocksperstrip = 1;
×
8004
    rowsperstrip = rowblocksperstrip * rowblock;
2✔
8005
    stripbytes = rowblocksperstrip * rowblockbytes;
2✔
8006
    assert(stripbytes <= 0x7FFFFFFFUL);
2✔
8007

8008
    if (rowsperstrip == 0)
2✔
8009
        return;
×
8010
    nstrips = TIFFhowmany_32(td->td_imagelength, rowsperstrip);
2✔
8011
    if (nstrips == 0)
2✔
8012
        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
    if (tif->tif_mode == O_RDONLY && nstrips > 1000000)
2✔
8017
    {
8018
        uint64_t last_offset = TIFFGetStrileOffset(tif, td->td_nstrips - 1);
×
8019
        uint64_t filesize = TIFFGetFileSize(tif);
×
8020
        uint64_t last_bytecount =
8021
            TIFFGetStrileByteCount(tif, td->td_nstrips - 1);
×
8022
        if (last_offset > filesize || last_bytecount > filesize - last_offset)
×
8023
        {
8024
            return;
×
8025
        }
8026
    }
8027

8028
    allocChoppedUpStripArrays(tif, nstrips, stripbytes, rowsperstrip);
2✔
8029
}
8030

8031
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
8032
static uint64_t _TIFFUnsanitizedAddUInt64AndInt(uint64_t a, int b)
6,315,160✔
8033
{
8034
    return a + b;
6,315,160✔
8035
}
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,
6,397✔
8042
                                      int strile, uint64_t *panVals)
8043
{
8044
    static const char module[] = "_TIFFPartialReadStripArray";
8045
#define IO_CACHE_PAGE_SIZE 4096
8046

8047
    size_t sizeofval;
8048
    const int bSwab = (tif->tif_flags & TIFF_SWAB) != 0;
6,397✔
8049
    int sizeofvalint;
8050
    uint64_t nBaseOffset;
8051
    uint64_t nOffset;
8052
    uint64_t nOffsetStartPage;
8053
    uint64_t nOffsetEndPage;
8054
    tmsize_t nToRead;
8055
    tmsize_t nRead;
8056
    uint64_t nLastStripOffset;
8057
    int iStartBefore;
8058
    int i;
8059
    const uint32_t arraySize = tif->tif_dir.td_stripoffsetbyteallocsize;
6,397✔
8060
    unsigned char buffer[2 * IO_CACHE_PAGE_SIZE];
8061

8062
    assert(dirent->tdir_count > 4);
6,397✔
8063

8064
    if (dirent->tdir_type == TIFF_SHORT)
6,397✔
8065
    {
8066
        sizeofval = sizeof(uint16_t);
1,887✔
8067
    }
8068
    else if (dirent->tdir_type == TIFF_LONG)
4,510✔
8069
    {
8070
        sizeofval = sizeof(uint32_t);
4,476✔
8071
    }
8072
    else if (dirent->tdir_type == TIFF_LONG8)
34✔
8073
    {
8074
        sizeofval = sizeof(uint64_t);
34✔
8075
    }
8076
    else if (dirent->tdir_type == TIFF_SLONG8)
×
8077
    {
8078
        /* Non conformant but used by some images as in */
8079
        /* https://github.com/OSGeo/gdal/issues/2165 */
8080
        sizeofval = sizeof(int64_t);
×
8081
    }
8082
    else
8083
    {
8084
        TIFFErrorExtR(tif, module,
×
8085
                      "Invalid type for [Strip|Tile][Offset/ByteCount] tag");
8086
        panVals[strile] = 0;
×
8087
        return 0;
×
8088
    }
8089
    sizeofvalint = (int)(sizeofval);
6,397✔
8090

8091
    if (tif->tif_flags & TIFF_BIGTIFF)
6,397✔
8092
    {
8093
        uint64_t offset = dirent->tdir_offset.toff_long8;
62✔
8094
        if (bSwab)
62✔
8095
            TIFFSwabLong8(&offset);
×
8096
        nBaseOffset = offset;
62✔
8097
    }
8098
    else
8099
    {
8100
        uint32_t offset = dirent->tdir_offset.toff_long;
6,335✔
8101
        if (bSwab)
6,335✔
8102
            TIFFSwabLong(&offset);
88✔
8103
        nBaseOffset = offset;
6,336✔
8104
    }
8105
    /* To avoid later unsigned integer overflows */
8106
    if (nBaseOffset > (uint64_t)INT64_MAX)
6,398✔
8107
    {
8108
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
×
8109
                      strile);
8110
        panVals[strile] = 0;
×
8111
        return 0;
×
8112
    }
8113
    nOffset = nBaseOffset + sizeofval * strile;
6,398✔
8114
    nOffsetStartPage = (nOffset / IO_CACHE_PAGE_SIZE) * IO_CACHE_PAGE_SIZE;
6,398✔
8115
    nOffsetEndPage = nOffsetStartPage + IO_CACHE_PAGE_SIZE;
6,398✔
8116

8117
    if (nOffset + sizeofval > nOffsetEndPage)
6,398✔
8118
        nOffsetEndPage += IO_CACHE_PAGE_SIZE;
1,974✔
8119
#undef IO_CACHE_PAGE_SIZE
8120

8121
    nLastStripOffset = nBaseOffset + arraySize * sizeofval;
6,398✔
8122
    if (nLastStripOffset < nOffsetEndPage)
6,398✔
8123
        nOffsetEndPage = nLastStripOffset;
3,362✔
8124
    if (nOffsetStartPage >= nOffsetEndPage)
6,398✔
8125
    {
8126
        TIFFErrorExtR(tif, module, "Cannot read offset/size for strile %d",
×
8127
                      strile);
8128
        panVals[strile] = 0;
×
8129
        return 0;
×
8130
    }
8131
    if (!SeekOK(tif, nOffsetStartPage))
6,398✔
8132
    {
8133
        panVals[strile] = 0;
×
8134
        return 0;
×
8135
    }
8136

8137
    nToRead = (tmsize_t)(nOffsetEndPage - nOffsetStartPage);
6,398✔
8138
    nRead = TIFFReadFile(tif, buffer, nToRead);
6,398✔
8139
    if (nRead < nToRead)
6,397✔
8140
    {
8141
        TIFFErrorExtR(tif, module,
2✔
8142
                      "Cannot read offset/size for strile around ~%d", strile);
8143
        return 0;
2✔
8144
    }
8145
    iStartBefore = -(int)((nOffset - nOffsetStartPage) / sizeofval);
6,395✔
8146
    if (strile + iStartBefore < 0)
6,395✔
8147
        iStartBefore = -strile;
3,384✔
8148
    for (i = iStartBefore;
6,395✔
8149
         (uint32_t)(strile + i) < arraySize &&
12,633,700✔
8150
         _TIFFUnsanitizedAddUInt64AndInt(nOffset, (i + 1) * sizeofvalint) <=
6,315,330✔
8151
             nOffsetEndPage;
8152
         ++i)
6,312,120✔
8153
    {
8154
        if (dirent->tdir_type == TIFF_SHORT)
6,312,250✔
8155
        {
8156
            uint16_t val;
8157
            memcpy(&val,
2,122,160✔
8158
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
2,122,160✔
8159
                   sizeof(val));
8160
            if (bSwab)
2,122,160✔
8161
                TIFFSwabShort(&val);
5,220✔
8162
            panVals[strile + i] = val;
2,122,160✔
8163
        }
8164
        else if (dirent->tdir_type == TIFF_LONG)
4,190,090✔
8165
        {
8166
            uint32_t val;
8167
            memcpy(&val,
4,188,630✔
8168
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
4,188,630✔
8169
                   sizeof(val));
8170
            if (bSwab)
4,188,630✔
8171
                TIFFSwabLong(&val);
5,528✔
8172
            panVals[strile + i] = val;
4,188,500✔
8173
        }
8174
        else if (dirent->tdir_type == TIFF_LONG8)
1,458✔
8175
        {
8176
            uint64_t val;
8177
            memcpy(&val,
1,463✔
8178
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
1,463✔
8179
                   sizeof(val));
8180
            if (bSwab)
1,463✔
8181
                TIFFSwabLong8(&val);
×
8182
            panVals[strile + i] = val;
1,463✔
8183
        }
8184
        else /* if( dirent->tdir_type == TIFF_SLONG8 ) */
8185
        {
8186
            /* Non conformant data type */
8187
            int64_t val;
8188
            memcpy(&val,
×
8189
                   buffer + (nOffset - nOffsetStartPage) + i * sizeofvalint,
×
8190
                   sizeof(val));
8191
            if (bSwab)
×
8192
                TIFFSwabLong8((uint64_t *)&val);
×
8193
            panVals[strile + i] = (uint64_t)val;
×
8194
        }
8195
    }
8196
    return 1;
6,166✔
8197
}
8198

8199
static int _TIFFFetchStrileValue(TIFF *tif, uint32_t strile,
6,373,120✔
8200
                                 TIFFDirEntry *dirent, uint64_t **parray)
8201
{
8202
    static const char module[] = "_TIFFFetchStrileValue";
8203
    TIFFDirectory *td = &tif->tif_dir;
6,373,120✔
8204
    if (strile >= dirent->tdir_count)
6,373,120✔
8205
    {
8206
        return 0;
×
8207
    }
8208
    if (strile >= td->td_stripoffsetbyteallocsize)
6,373,120✔
8209
    {
8210
        uint32_t nStripArrayAllocBefore = td->td_stripoffsetbyteallocsize;
1,697✔
8211
        uint32_t nStripArrayAllocNew;
8212
        uint64_t nArraySize64;
8213
        size_t nArraySize;
8214
        uint64_t *offsetArray;
8215
        uint64_t *bytecountArray;
8216

8217
        if (strile > 1000000)
1,697✔
8218
        {
8219
            uint64_t filesize = TIFFGetFileSize(tif);
3✔
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
            if (strile > filesize / sizeof(uint32_t))
3✔
8224
            {
8225
                TIFFErrorExtR(tif, module, "File too short");
2✔
8226
                return 0;
2✔
8227
            }
8228
        }
8229

8230
        if (td->td_stripoffsetbyteallocsize == 0 &&
1,695✔
8231
            td->td_nstrips < 1024 * 1024)
1,694✔
8232
        {
8233
            nStripArrayAllocNew = td->td_nstrips;
1,693✔
8234
        }
8235
        else
8236
        {
8237
#define TIFF_MAX(a, b) (((a) > (b)) ? (a) : (b))
8238
#define TIFF_MIN(a, b) (((a) < (b)) ? (a) : (b))
8239
            nStripArrayAllocNew = TIFF_MAX(strile + 1, 1024U * 512U);
2✔
8240
            if (nStripArrayAllocNew < 0xFFFFFFFFU / 2)
2✔
8241
                nStripArrayAllocNew *= 2;
2✔
8242
            nStripArrayAllocNew = TIFF_MIN(nStripArrayAllocNew, td->td_nstrips);
2✔
8243
        }
8244
        assert(strile < nStripArrayAllocNew);
1,695✔
8245
        nArraySize64 = (uint64_t)sizeof(uint64_t) * nStripArrayAllocNew;
1,695✔
8246
        nArraySize = (size_t)(nArraySize64);
1,695✔
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
        offsetArray = (uint64_t *)(_TIFFreallocExt(tif, td->td_stripoffset_p,
1,695✔
8256
                                                   nArraySize));
8257
        bytecountArray = (uint64_t *)(_TIFFreallocExt(
1,696✔
8258
            tif, td->td_stripbytecount_p, nArraySize));
1,696✔
8259
        if (offsetArray)
1,696✔
8260
            td->td_stripoffset_p = offsetArray;
1,695✔
8261
        if (bytecountArray)
1,696✔
8262
            td->td_stripbytecount_p = bytecountArray;
1,695✔
8263
        if (offsetArray && bytecountArray)
1,696✔
8264
        {
8265
            td->td_stripoffsetbyteallocsize = nStripArrayAllocNew;
1,695✔
8266
            /* Initialize new entries to ~0 / -1 */
8267
            /* coverity[overrun-buffer-arg] */
8268
            memset(td->td_stripoffset_p + nStripArrayAllocBefore, 0xFF,
1,695✔
8269
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
1,695✔
8270
                       sizeof(uint64_t));
8271
            /* coverity[overrun-buffer-arg] */
8272
            memset(td->td_stripbytecount_p + nStripArrayAllocBefore, 0xFF,
1,695✔
8273
                   (td->td_stripoffsetbyteallocsize - nStripArrayAllocBefore) *
1,695✔
8274
                       sizeof(uint64_t));
8275
        }
8276
        else
8277
        {
8278
            TIFFErrorExtR(tif, module,
1✔
8279
                          "Cannot allocate strip offset and bytecount arrays");
8280
            _TIFFfreeExt(tif, td->td_stripoffset_p);
×
8281
            td->td_stripoffset_p = NULL;
×
8282
            _TIFFfreeExt(tif, td->td_stripbytecount_p);
×
8283
            td->td_stripbytecount_p = NULL;
×
8284
            td->td_stripoffsetbyteallocsize = 0;
×
8285
        }
8286
    }
8287
    if (*parray == NULL || strile >= td->td_stripoffsetbyteallocsize)
6,373,110✔
8288
        return 0;
×
8289

8290
    if (~((*parray)[strile]) == 0)
6,373,110✔
8291
    {
8292
        if (!_TIFFPartialReadStripArray(tif, dirent, strile, *parray))
6,395✔
8293
        {
8294
            (*parray)[strile] = 0;
2✔
8295
            return 0;
2✔
8296
        }
8297
    }
8298

8299
    return 1;
6,373,110✔
8300
}
8301

8302
static uint64_t _TIFFGetStrileOffsetOrByteCountValue(TIFF *tif, uint32_t strile,
6,870,020✔
8303
                                                     TIFFDirEntry *dirent,
8304
                                                     uint64_t **parray,
8305
                                                     int *pbErr)
8306
{
8307
    TIFFDirectory *td = &tif->tif_dir;
6,870,020✔
8308
    if (pbErr)
6,870,020✔
8309
        *pbErr = 0;
4,314,140✔
8310
    if ((tif->tif_flags & TIFF_DEFERSTRILELOAD) &&
6,870,020✔
8311
        !(tif->tif_flags & TIFF_CHOPPEDUPARRAYS))
6,702,460✔
8312
    {
8313
        if (!(tif->tif_flags & TIFF_LAZYSTRILELOAD) ||
6,695,590✔
8314
            /* If the values may fit in the toff_long/toff_long8 member */
8315
            /* then use _TIFFFillStriles to simplify _TIFFFetchStrileValue */
8316
            dirent->tdir_count <= 4)
6,392,000✔
8317
        {
8318
            if (!_TIFFFillStriles(tif))
322,462✔
8319
            {
8320
                if (pbErr)
2✔
8321
                    *pbErr = 1;
2✔
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
            }
8326
        }
8327
        else
8328
        {
8329
            if (!_TIFFFetchStrileValue(tif, strile, dirent, parray))
6,373,120✔
8330
            {
8331
                if (pbErr)
4✔
8332
                    *pbErr = 1;
4✔
8333
                return 0;
4✔
8334
            }
8335
        }
8336
    }
8337
    if (*parray == NULL || strile >= td->td_nstrips)
6,870,050✔
8338
    {
8339
        if (pbErr)
71✔
8340
            *pbErr = 1;
2✔
8341
        return 0;
71✔
8342
    }
8343
    return (*parray)[strile];
6,869,980✔
8344
}
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)
2,158,460✔
8349
{
8350
    return TIFFGetStrileOffsetWithErr(tif, strile, NULL);
2,158,460✔
8351
}
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)
4,293,880✔
8356
{
8357
    TIFFDirectory *td = &tif->tif_dir;
4,293,880✔
8358
    return _TIFFGetStrileOffsetOrByteCountValue(tif, strile,
4,293,880✔
8359
                                                &(td->td_stripoffset_entry),
8360
                                                &(td->td_stripoffset_p), pbErr);
8361
}
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)
397,458✔
8366
{
8367
    return TIFFGetStrileByteCountWithErr(tif, strile, NULL);
397,458✔
8368
}
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)
2,576,160✔
8373
{
8374
    TIFFDirectory *td = &tif->tif_dir;
2,576,160✔
8375
    return _TIFFGetStrileOffsetOrByteCountValue(
2,576,160✔
8376
        tif, strile, &(td->td_stripbytecount_entry), &(td->td_stripbytecount_p),
8377
        pbErr);
8378
}
8379

8380
int _TIFFFillStriles(TIFF *tif) { return _TIFFFillStrilesInternal(tif, 1); }
699,702✔
8381

8382
static int _TIFFFillStrilesInternal(TIFF *tif, int loadStripByteCount)
699,729✔
8383
{
8384
    register TIFFDirectory *td = &tif->tif_dir;
699,729✔
8385
    int return_value = 1;
699,729✔
8386

8387
    /* Do not do anything if TIFF_DEFERSTRILELOAD is not set */
8388
    if (!(tif->tif_flags & TIFF_DEFERSTRILELOAD) ||
699,729✔
8389
        (tif->tif_flags & TIFF_CHOPPEDUPARRAYS) != 0)
345,025✔
8390
        return 1;
354,726✔
8391

8392
    if (tif->tif_flags & TIFF_LAZYSTRILELOAD)
345,003✔
8393
    {
8394
        /* In case of lazy loading, reload completely the arrays */
8395
        _TIFFfreeExt(tif, td->td_stripoffset_p);
18,963✔
8396
        _TIFFfreeExt(tif, td->td_stripbytecount_p);
18,979✔
8397
        td->td_stripoffset_p = NULL;
18,972✔
8398
        td->td_stripbytecount_p = NULL;
18,972✔
8399
        td->td_stripoffsetbyteallocsize = 0;
18,972✔
8400
        tif->tif_flags &= ~TIFF_LAZYSTRILELOAD;
18,972✔
8401
    }
8402

8403
    /* If stripoffset array is already loaded, exit with success */
8404
    if (td->td_stripoffset_p != NULL)
345,012✔
8405
        return 1;
320,915✔
8406

8407
    /* If tdir_count was canceled, then we already got there, but in error */
8408
    if (td->td_stripoffset_entry.tdir_count == 0)
24,097✔
8409
        return 0;
129✔
8410

8411
    if (!TIFFFetchStripThing(tif, &(td->td_stripoffset_entry), td->td_nstrips,
23,968✔
8412
                             &td->td_stripoffset_p))
8413
    {
8414
        return_value = 0;
1✔
8415
    }
8416

8417
    if (loadStripByteCount &&
47,942✔
8418
        !TIFFFetchStripThing(tif, &(td->td_stripbytecount_entry),
23,955✔
8419
                             td->td_nstrips, &td->td_stripbytecount_p))
8420
    {
8421
        return_value = 0;
1✔
8422
    }
8423

8424
    _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
23,987✔
8425
    _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
23,981✔
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
    return return_value;
23,981✔
8446
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc