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

OSGeo / gdal / 8872387746

29 Apr 2024 02:16AM UTC coverage: 69.076% (+0.003%) from 69.073%
8872387746

Pull #9801

github

web-flow
Bump actions/upload-artifact from 4.3.2 to 4.3.3

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 4.3.2 to 4.3.3.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/1746f4ab6...65462800f)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #9801: Bump actions/upload-artifact from 4.3.2 to 4.3.3

534153 of 773282 relevant lines covered (69.08%)

205719.18 hits per line

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

63.96
/frmts/gtiff/libtiff/tif_open.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

29
#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
30
#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
31
#endif
32

33
#include "tiffiop.h"
34
#include <assert.h>
35
#include <limits.h>
36

37
/*
38
 * Dummy functions to fill the omitted client procedures.
39
 */
40
static int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize)
×
41
{
42
    (void)fd;
43
    (void)pbase;
44
    (void)psize;
45
    return (0);
×
46
}
47

48
static void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
×
49
{
50
    (void)fd;
51
    (void)base;
52
    (void)size;
53
}
×
54

55
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode,
56,419✔
56
                 const char *module)
57
{
58
    int m = -1;
56,419✔
59

60
    switch (mode[0])
56,419✔
61
    {
62
        case 'r':
21,424✔
63
            m = O_RDONLY;
21,424✔
64
            if (mode[1] == '+')
21,424✔
65
                m = O_RDWR;
2,545✔
66
            break;
21,424✔
67
        case 'w':
34,979✔
68
        case 'a':
69
            m = O_RDWR | O_CREAT;
34,979✔
70
            if (mode[0] == 'w')
34,979✔
71
                m |= O_TRUNC;
34,979✔
72
            break;
34,979✔
73
        default:
16✔
74
            _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
16✔
75
            break;
×
76
    }
77
    return (m);
56,403✔
78
}
79

80
TIFFOpenOptions *TIFFOpenOptionsAlloc()
56,429✔
81
{
82
    TIFFOpenOptions *opts =
83
        (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions));
56,429✔
84
    return opts;
56,391✔
85
}
86

87
void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); }
56,375✔
88

89
/** Define a limit in bytes for a single memory allocation done by libtiff.
90
 *  If max_single_mem_alloc is set to 0, which is the default, no other limit
91
 *  that the underlying _TIFFmalloc() or
92
 *  TIFFOpenOptionsSetMaxCumulatedMemAlloc() will be applied.
93
 */
94
void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts,
×
95
                                         tmsize_t max_single_mem_alloc)
96
{
97
    opts->max_single_mem_alloc = max_single_mem_alloc;
×
98
}
×
99

100
/** Define a limit in bytes for the cumulated memory allocations done by libtiff
101
 *  on a given TIFF handle.
102
 *  If max_cumulated_mem_alloc is set to 0, which is the default, no other limit
103
 *  that the underlying _TIFFmalloc() or
104
 *  TIFFOpenOptionsSetMaxSingleMemAlloc() will be applied.
105
 */
106
void TIFFOpenOptionsSetMaxCumulatedMemAlloc(TIFFOpenOptions *opts,
56,421✔
107
                                            tmsize_t max_cumulated_mem_alloc)
108
{
109
    opts->max_cumulated_mem_alloc = max_cumulated_mem_alloc;
56,421✔
110
}
56,421✔
111

112
void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts,
56,418✔
113
                                        TIFFErrorHandlerExtR handler,
114
                                        void *errorhandler_user_data)
115
{
116
    opts->errorhandler = handler;
56,418✔
117
    opts->errorhandler_user_data = errorhandler_user_data;
56,418✔
118
}
56,418✔
119

120
void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts,
56,393✔
121
                                          TIFFErrorHandlerExtR handler,
122
                                          void *warnhandler_user_data)
123
{
124
    opts->warnhandler = handler;
56,393✔
125
    opts->warnhandler_user_data = warnhandler_user_data;
56,393✔
126
}
56,393✔
127

128
static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif,
×
129
                                                 const char *pszFunction,
130
                                                 tmsize_t s)
131
{
132
    TIFFErrorExtR(tif, pszFunction,
×
133
                  "Memory allocation of %" PRIu64
134
                  " bytes is beyond the %" PRIu64
135
                  " byte limit defined in open options",
136
                  (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc);
×
137
}
×
138

139
static void _TIFFEmitErrorAboveMaxCumulatedMemAlloc(TIFF *tif,
×
140
                                                    const char *pszFunction,
141
                                                    tmsize_t s)
142
{
143
    TIFFErrorExtR(tif, pszFunction,
×
144
                  "Cumulated memory allocation of %" PRIu64 " + %" PRIu64
145
                  " bytes is beyond the %" PRIu64
146
                  " cumulated byte limit defined in open options",
147
                  (uint64_t)tif->tif_cur_cumulated_mem_alloc, (uint64_t)s,
×
148
                  (uint64_t)tif->tif_max_cumulated_mem_alloc);
×
149
}
×
150

151
/* When allocating memory, we write at the beginning of the buffer it size.
152
 * This allows us to keep track of the total memory allocated when we
153
 * malloc/calloc/realloc and free. In theory we need just SIZEOF_SIZE_T bytes
154
 * for that, but on x86_64, allocations of more than 16 bytes are aligned on
155
 * 16 bytes. Hence using 2 * SIZEOF_SIZE_T.
156
 * It is critical that _TIFFmallocExt/_TIFFcallocExt/_TIFFreallocExt are
157
 * paired with _TIFFfreeExt.
158
 * CMakeLists.txt defines TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS, which in
159
 * turn disables the definition of the non Ext version in tiffio.h
160
 */
161
#define LEADING_AREA_TO_STORE_ALLOC_SIZE (2 * SIZEOF_SIZE_T)
162

163
/** malloc() version that takes into account memory-specific open options */
164
void *_TIFFmallocExt(TIFF *tif, tmsize_t s)
573,425✔
165
{
166
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
573,425✔
167
        s > tif->tif_max_single_mem_alloc)
×
168
    {
169
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
×
170
        return NULL;
×
171
    }
172
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
573,425✔
173
    {
174
        if (s > tif->tif_max_cumulated_mem_alloc -
516,328✔
175
                    tif->tif_cur_cumulated_mem_alloc ||
516,328✔
176
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
516,300✔
177
        {
178
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s);
7✔
179
            return NULL;
×
180
        }
181
        void *ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
516,321✔
182
        if (!ptr)
516,433✔
183
            return NULL;
×
184
        tif->tif_cur_cumulated_mem_alloc += s;
516,433✔
185
        memcpy(ptr, &s, sizeof(s));
516,433✔
186
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
516,433✔
187
    }
188
    return _TIFFmalloc(s);
57,097✔
189
}
190

191
/** calloc() version that takes into account memory-specific open options */
192
void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
9,133✔
193
{
194
    if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
9,133✔
195
        return NULL;
1✔
196
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
9,132✔
197
    {
198
        if (nmemb * siz > tif->tif_max_single_mem_alloc)
×
199
        {
200
            _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
×
201
                                                 nmemb * siz);
202
            return NULL;
×
203
        }
204
    }
205
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
9,132✔
206
    {
207
        const tmsize_t s = nmemb * siz;
9,132✔
208
        if (s > tif->tif_max_cumulated_mem_alloc -
9,132✔
209
                    tif->tif_cur_cumulated_mem_alloc ||
9,132✔
210
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
9,132✔
211
        {
212
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s);
×
213
            return NULL;
×
214
        }
215
        void *ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1);
9,132✔
216
        if (!ptr)
9,132✔
217
            return NULL;
×
218
        tif->tif_cur_cumulated_mem_alloc += s;
9,132✔
219
        memcpy(ptr, &s, sizeof(s));
9,132✔
220
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
9,132✔
221
    }
222
    return _TIFFcalloc(nmemb, siz);
×
223
}
224

225
/** realloc() version that takes into account memory-specific open options */
226
void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
1,389,140✔
227
{
228
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
1,389,140✔
229
        s > tif->tif_max_single_mem_alloc)
×
230
    {
231
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
×
232
        return NULL;
×
233
    }
234
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
1,389,140✔
235
    {
236
        void *oldPtr = p;
1,334,490✔
237
        tmsize_t oldSize = 0;
1,334,490✔
238
        if (p)
1,334,490✔
239
        {
240
            oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
445,851✔
241
            memcpy(&oldSize, oldPtr, sizeof(oldSize));
445,851✔
242
            assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
445,851✔
243
        }
244
        if (s > oldSize &&
1,334,490✔
245
            (s > tif->tif_max_cumulated_mem_alloc -
1,333,180✔
246
                     (tif->tif_cur_cumulated_mem_alloc - oldSize) ||
1,333,180✔
247
             s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE))
1,333,240✔
248
        {
249
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt",
×
250
                                                    s - oldSize);
251
            return NULL;
×
252
        }
253
        void *newPtr =
254
            _TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
1,334,550✔
255
        if (newPtr == NULL)
1,334,700✔
256
            return NULL;
×
257
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
1,334,700✔
258
        tif->tif_cur_cumulated_mem_alloc += s;
1,334,700✔
259
        memcpy(newPtr, &s, sizeof(s));
1,334,700✔
260
        return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
1,334,700✔
261
    }
262
    return _TIFFrealloc(p, s);
54,656✔
263
}
264

265
/** free() version that takes into account memory-specific open options */
266
void _TIFFfreeExt(TIFF *tif, void *p)
1,502,120✔
267
{
268
    if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
1,502,120✔
269
    {
270
        void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
1,414,470✔
271
        tmsize_t oldSize;
272
        memcpy(&oldSize, oldPtr, sizeof(oldSize));
1,414,470✔
273
        assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
1,414,470✔
274
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
1,414,470✔
275
        p = oldPtr;
1,414,470✔
276
    }
277
    _TIFFfree(p);
1,502,120✔
278
}
1,502,220✔
279

280
TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata,
×
281
                     TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc,
282
                     TIFFSeekProc seekproc, TIFFCloseProc closeproc,
283
                     TIFFSizeProc sizeproc, TIFFMapFileProc mapproc,
284
                     TIFFUnmapFileProc unmapproc)
285
{
286
    return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
×
287
                             seekproc, closeproc, sizeproc, mapproc, unmapproc,
288
                             NULL);
289
}
290

291
TIFF *TIFFClientOpenExt(const char *name, const char *mode,
56,433✔
292
                        thandle_t clientdata, TIFFReadWriteProc readproc,
293
                        TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
294
                        TIFFCloseProc closeproc, TIFFSizeProc sizeproc,
295
                        TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc,
296
                        TIFFOpenOptions *opts)
297
{
298
    static const char module[] = "TIFFClientOpenExt";
299
    TIFF *tif;
300
    int m;
301
    const char *cp;
302

303
    /* The following are configuration checks. They should be redundant, but
304
     * should not compile to any actual code in an optimised release build
305
     * anyway. If any of them fail, (makefile-based or other) configuration is
306
     * not correct */
307
    assert(sizeof(uint8_t) == 1);
308
    assert(sizeof(int8_t) == 1);
309
    assert(sizeof(uint16_t) == 2);
310
    assert(sizeof(int16_t) == 2);
311
    assert(sizeof(uint32_t) == 4);
312
    assert(sizeof(int32_t) == 4);
313
    assert(sizeof(uint64_t) == 8);
314
    assert(sizeof(int64_t) == 8);
315
    {
316
        union
317
        {
318
            uint8_t a8[2];
319
            uint16_t a16;
320
        } n;
321
        n.a8[0] = 1;
56,433✔
322
        n.a8[1] = 0;
56,433✔
323
        (void)n;
324
#ifdef WORDS_BIGENDIAN
325
        assert(n.a16 == 256);
326
#else
327
        assert(n.a16 == 1);
56,433✔
328
#endif
329
    }
330

331
    m = _TIFFgetMode(opts, clientdata, mode, module);
56,433✔
332
    if (m == -1)
56,403✔
333
        goto bad2;
×
334
    tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
56,403✔
335
    if (opts && opts->max_single_mem_alloc > 0 &&
56,403✔
336
        size_to_alloc > opts->max_single_mem_alloc)
×
337
    {
338
        _TIFFErrorEarly(opts, clientdata, module,
×
339
                        "%s: Memory allocation of %" PRIu64
340
                        " bytes is beyond the %" PRIu64
341
                        " byte limit defined in open options",
342
                        name, (uint64_t)size_to_alloc,
343
                        (uint64_t)opts->max_single_mem_alloc);
×
344
        goto bad2;
×
345
    }
346
    if (opts && opts->max_cumulated_mem_alloc > 0 &&
56,403✔
347
        size_to_alloc > opts->max_cumulated_mem_alloc)
56,387✔
348
    {
349
        _TIFFErrorEarly(opts, clientdata, module,
×
350
                        "%s: Memory allocation of %" PRIu64
351
                        " bytes is beyond the %" PRIu64
352
                        " cumulated byte limit defined in open options",
353
                        name, (uint64_t)size_to_alloc,
354
                        (uint64_t)opts->max_cumulated_mem_alloc);
×
355
        goto bad2;
×
356
    }
357
    tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
56,403✔
358
    if (tif == NULL)
56,428✔
359
    {
360
        _TIFFErrorEarly(opts, clientdata, module,
×
361
                        "%s: Out of memory (TIFF structure)", name);
362
        goto bad2;
×
363
    }
364
    _TIFFmemset(tif, 0, sizeof(*tif));
56,428✔
365
    tif->tif_name = (char *)tif + sizeof(TIFF);
56,410✔
366
    strcpy(tif->tif_name, name);
56,410✔
367
    tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
56,410✔
368
    tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */
56,410✔
369
    tif->tif_curoff = 0;
56,410✔
370
    tif->tif_curstrip = (uint32_t)-1; /* invalid strip */
56,410✔
371
    tif->tif_row = (uint32_t)-1;      /* read/write pre-increment */
56,410✔
372
    tif->tif_clientdata = clientdata;
56,410✔
373
    tif->tif_readproc = readproc;
56,410✔
374
    tif->tif_writeproc = writeproc;
56,410✔
375
    tif->tif_seekproc = seekproc;
56,410✔
376
    tif->tif_closeproc = closeproc;
56,410✔
377
    tif->tif_sizeproc = sizeproc;
56,410✔
378
    tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
56,410✔
379
    tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
56,410✔
380
    if (opts)
56,410✔
381
    {
382
        tif->tif_errorhandler = opts->errorhandler;
56,384✔
383
        tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
56,384✔
384
        tif->tif_warnhandler = opts->warnhandler;
56,384✔
385
        tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
56,384✔
386
        tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
56,384✔
387
        tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc;
56,384✔
388
    }
389

390
    if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
56,410✔
391
    {
392
        TIFFErrorExtR(tif, module,
26✔
393
                      "One of the client procedures is NULL pointer.");
394
        _TIFFfreeExt(NULL, tif);
×
395
        goto bad2;
×
396
    }
397

398
    _TIFFSetDefaultCompressionState(tif); /* setup default state */
56,384✔
399
    /*
400
     * Default is to return data MSB2LSB and enable the
401
     * use of memory-mapped files and strip chopping when
402
     * a file is opened read-only.
403
     */
404
    tif->tif_flags = FILLORDER_MSB2LSB;
56,405✔
405
    if (m == O_RDONLY)
56,405✔
406
        tif->tif_flags |= TIFF_MAPPED;
18,876✔
407

408
#ifdef STRIPCHOP_DEFAULT
409
    if (m == O_RDONLY || m == O_RDWR)
56,405✔
410
        tif->tif_flags |= STRIPCHOP_DEFAULT;
21,433✔
411
#endif
412

413
    /*
414
     * Process library-specific flags in the open mode string.
415
     * The following flags may be used to control intrinsic library
416
     * behavior that may or may not be desirable (usually for
417
     * compatibility with some application that claims to support
418
     * TIFF but only supports some brain dead idea of what the
419
     * vendor thinks TIFF is):
420
     *
421
     * 'l' use little-endian byte order for creating a file
422
     * 'b' use big-endian byte order for creating a file
423
     * 'L' read/write information using LSB2MSB bit order
424
     * 'B' read/write information using MSB2LSB bit order
425
     * 'H' read/write information using host bit order
426
     * 'M' enable use of memory-mapped files when supported
427
     * 'm' disable use of memory-mapped files
428
     * 'C' enable strip chopping support when reading
429
     * 'c' disable strip chopping support
430
     * 'h' read TIFF header only, do not load the first IFD
431
     * '4' ClassicTIFF for creating a file (default)
432
     * '8' BigTIFF for creating a file
433
     * 'D' enable use of deferred strip/tile offset/bytecount array loading.
434
     * 'O' on-demand loading of values instead of whole array loading (implies
435
     * D)
436
     *
437
     * The use of the 'l' and 'b' flags is strongly discouraged.
438
     * These flags are provided solely because numerous vendors,
439
     * typically on the PC, do not correctly support TIFF; they
440
     * only support the Intel little-endian byte order.  This
441
     * support is not configured by default because it supports
442
     * the violation of the TIFF spec that says that readers *MUST*
443
     * support both byte orders.  It is strongly recommended that
444
     * you not use this feature except to deal with busted apps
445
     * that write invalid TIFF.  And even in those cases you should
446
     * bang on the vendors to fix their software.
447
     *
448
     * The 'L', 'B', and 'H' flags are intended for applications
449
     * that can optimize operations on data by using a particular
450
     * bit order.  By default the library returns data in MSB2LSB
451
     * bit order for compatibility with older versions of this
452
     * library.  Returning data in the bit order of the native CPU
453
     * makes the most sense but also requires applications to check
454
     * the value of the FillOrder tag; something they probably do
455
     * not do right now.
456
     *
457
     * The 'M' and 'm' flags are provided because some virtual memory
458
     * systems exhibit poor behavior when large images are mapped.
459
     * These options permit clients to control the use of memory-mapped
460
     * files on a per-file basis.
461
     *
462
     * The 'C' and 'c' flags are provided because the library support
463
     * for chopping up large strips into multiple smaller strips is not
464
     * application-transparent and as such can cause problems.  The 'c'
465
     * option permits applications that only want to look at the tags,
466
     * for example, to get the unadulterated TIFF tag information.
467
     */
468
    for (cp = mode; *cp; cp++)
227,725✔
469
        switch (*cp)
171,320✔
470
        {
471
            case 'b':
82✔
472
#ifndef WORDS_BIGENDIAN
473
                if (m & O_CREAT)
82✔
474
                    tif->tif_flags |= TIFF_SWAB;
82✔
475
#endif
476
                break;
82✔
477
            case 'l':
28,522✔
478
#ifdef WORDS_BIGENDIAN
479
                if ((m & O_CREAT))
480
                    tif->tif_flags |= TIFF_SWAB;
481
#endif
482
                break;
28,522✔
483
            case 'B':
×
484
                tif->tif_flags =
×
485
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
×
486
                break;
×
487
            case 'L':
×
488
                tif->tif_flags =
×
489
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
×
490
                break;
×
491
            case 'H':
×
492
                TIFFWarningExtR(tif, name,
×
493
                                "H(ost) mode is deprecated. Since "
494
                                "libtiff 4.5.1, it is an alias of 'B' / "
495
                                "FILLORDER_MSB2LSB.");
496
                tif->tif_flags =
×
497
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
×
498
                break;
×
499
            case 'M':
×
500
                if (m == O_RDONLY)
×
501
                    tif->tif_flags |= TIFF_MAPPED;
×
502
                break;
×
503
            case 'm':
×
504
                if (m == O_RDONLY)
×
505
                    tif->tif_flags &= ~TIFF_MAPPED;
×
506
                break;
×
507
            case 'C':
15,770✔
508
                if (m == O_RDONLY)
15,770✔
509
                    tif->tif_flags |= TIFF_STRIPCHOP;
15,072✔
510
                break;
15,770✔
511
            case 'c':
579✔
512
                if (m == O_RDONLY)
579✔
513
                    tif->tif_flags &= ~TIFF_STRIPCHOP;
579✔
514
                break;
579✔
515
            case 'h':
×
516
                tif->tif_flags |= TIFF_HEADERONLY;
×
517
                break;
×
518
            case '8':
74✔
519
                if (m & O_CREAT)
74✔
520
                    tif->tif_flags |= TIFF_BIGTIFF;
74✔
521
                break;
74✔
522
            case 'D':
16,838✔
523
                tif->tif_flags |= TIFF_DEFERSTRILELOAD;
16,838✔
524
                break;
16,838✔
525
            case 'O':
15,790✔
526
                if (m == O_RDONLY)
15,790✔
527
                    tif->tif_flags |=
15,780✔
528
                        (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);
529
                break;
15,790✔
530
        }
531

532
#ifdef DEFER_STRILE_LOAD
533
    /* Compatibility with old DEFER_STRILE_LOAD compilation flag */
534
    /* Probably unneeded, since to the best of my knowledge (E. Rouault) */
535
    /* GDAL was the only user of this, and will now use the new 'D' flag */
536
    tif->tif_flags |= TIFF_DEFERSTRILELOAD;
537
#endif
538

539
    /*
540
     * Read in TIFF header.
541
     */
542
    if ((m & O_TRUNC) ||
249,153✔
543
        !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
21,404✔
544
    {
545
        if (tif->tif_mode == O_RDONLY)
34,979✔
546
        {
547
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
×
548
            goto bad;
2✔
549
        }
550
        /*
551
         * Setup header and write.
552
         */
553
#ifdef WORDS_BIGENDIAN
554
        tif->tif_header.common.tiff_magic =
555
            (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
556
#else
557
        tif->tif_header.common.tiff_magic =
34,979✔
558
            (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
34,979✔
559
#endif
560
        TIFFHeaderUnion tif_header_swapped;
561
        if (!(tif->tif_flags & TIFF_BIGTIFF))
34,979✔
562
        {
563
            tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
34,905✔
564
            tif->tif_header.classic.tiff_diroff = 0;
34,905✔
565
            tif->tif_header_size = sizeof(TIFFHeaderClassic);
34,905✔
566
            /* Swapped copy for writing */
567
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
34,905✔
568
                        sizeof(TIFFHeaderUnion));
569
            if (tif->tif_flags & TIFF_SWAB)
34,867✔
570
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
78✔
571
        }
572
        else
573
        {
574
            tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
74✔
575
            tif->tif_header.big.tiff_offsetsize = 8;
74✔
576
            tif->tif_header.big.tiff_unused = 0;
74✔
577
            tif->tif_header.big.tiff_diroff = 0;
74✔
578
            tif->tif_header_size = sizeof(TIFFHeaderBig);
74✔
579
            /* Swapped copy for writing */
580
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
74✔
581
                        sizeof(TIFFHeaderUnion));
582
            if (tif->tif_flags & TIFF_SWAB)
74✔
583
            {
584
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
4✔
585
                TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize);
4✔
586
            }
587
        }
588
        /*
589
         * The doc for "fopen" for some STD_C_LIBs says that if you
590
         * open a file for modify ("+"), then you must fseek (or
591
         * fflush?) between any freads and fwrites.  This is not
592
         * necessary on most systems, but has been shown to be needed
593
         * on Solaris.
594
         */
595
        TIFFSeekFile(tif, 0, SEEK_SET);
34,941✔
596
        if (!WriteOK(tif, &tif_header_swapped,
34,979✔
597
                     (tmsize_t)(tif->tif_header_size)))
598
        {
599
            TIFFErrorExtR(tif, name, "Error writing TIFF header");
2✔
600
            goto bad;
2✔
601
        }
602
        /*
603
         * Setup default directory.
604
         */
605
        if (!TIFFDefaultDirectory(tif))
34,977✔
606
            goto bad;
×
607
        tif->tif_diroff = 0;
34,977✔
608
        tif->tif_lastdiroff = 0;
34,977✔
609
        tif->tif_setdirectory_force_absolute = FALSE;
34,977✔
610
        return (tif);
34,977✔
611
    }
612

613
    /*
614
     * Setup the byte order handling according to the opened file for reading.
615
     */
616
    if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
21,450✔
617
        tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
21,228✔
618
#if MDI_SUPPORT
619
        &&
620
#if HOST_BIGENDIAN
621
        tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
622
#else
623
        tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
624
#endif
625
    )
626
    {
627
        TIFFErrorExtR(tif, name,
628
                      "Not a TIFF or MDI file, bad magic number %" PRIu16
629
                      " (0x%" PRIx16 ")",
630
#else
631
    )
632
    {
633
        TIFFErrorExtR(tif, name,
×
634
                      "Not a TIFF file, bad magic number %" PRIu16
635
                      " (0x%" PRIx16 ")",
636
#endif
637
                      tif->tif_header.common.tiff_magic,
×
638
                      tif->tif_header.common.tiff_magic);
×
639
        goto bad;
×
640
    }
641
    if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
21,450✔
642
    {
643
#ifndef WORDS_BIGENDIAN
644
        tif->tif_flags |= TIFF_SWAB;
217✔
645
#endif
646
    }
647
    else
648
    {
649
#ifdef WORDS_BIGENDIAN
650
        tif->tif_flags |= TIFF_SWAB;
651
#endif
652
    }
653
    if (tif->tif_flags & TIFF_SWAB)
21,450✔
654
        TIFFSwabShort(&tif->tif_header.common.tiff_version);
217✔
655
    if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
21,398✔
656
        (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
280✔
657
    {
658
        TIFFErrorExtR(tif, name,
×
659
                      "Not a TIFF file, bad version number %" PRIu16
660
                      " (0x%" PRIx16 ")",
661
                      tif->tif_header.common.tiff_version,
×
662
                      tif->tif_header.common.tiff_version);
×
663
        goto bad;
×
664
    }
665
    if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
21,398✔
666
    {
667
        if (tif->tif_flags & TIFF_SWAB)
21,118✔
668
            TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
206✔
669
        tif->tif_header_size = sizeof(TIFFHeaderClassic);
21,127✔
670
    }
671
    else
672
    {
673
        if (!ReadOK(tif,
280✔
674
                    ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
675
                    (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
676
        {
677
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
×
678
            goto bad;
×
679
        }
680
        if (tif->tif_flags & TIFF_SWAB)
280✔
681
        {
682
            TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
11✔
683
            TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
11✔
684
        }
685
        if (tif->tif_header.big.tiff_offsetsize != 8)
280✔
686
        {
687
            TIFFErrorExtR(tif, name,
×
688
                          "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
689
                          " (0x%" PRIx16 ")",
690
                          tif->tif_header.big.tiff_offsetsize,
×
691
                          tif->tif_header.big.tiff_offsetsize);
×
692
            goto bad;
×
693
        }
694
        if (tif->tif_header.big.tiff_unused != 0)
280✔
695
        {
696
            TIFFErrorExtR(tif, name,
×
697
                          "Not a TIFF file, bad BigTIFF unused %" PRIu16
698
                          " (0x%" PRIx16 ")",
699
                          tif->tif_header.big.tiff_unused,
×
700
                          tif->tif_header.big.tiff_unused);
×
701
            goto bad;
×
702
        }
703
        tif->tif_header_size = sizeof(TIFFHeaderBig);
280✔
704
        tif->tif_flags |= TIFF_BIGTIFF;
280✔
705
    }
706
    tif->tif_flags |= TIFF_MYBUFFER;
21,407✔
707
    tif->tif_rawcp = tif->tif_rawdata = 0;
21,407✔
708
    tif->tif_rawdatasize = 0;
21,407✔
709
    tif->tif_rawdataoff = 0;
21,407✔
710
    tif->tif_rawdataloaded = 0;
21,407✔
711

712
    switch (mode[0])
21,407✔
713
    {
714
        case 'r':
21,437✔
715
            if (!(tif->tif_flags & TIFF_BIGTIFF))
21,437✔
716
                tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
21,151✔
717
            else
718
                tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
286✔
719
            /*
720
             * Try to use a memory-mapped file if the client
721
             * has not explicitly suppressed usage with the
722
             * 'm' flag in the open mode (see above).
723
             */
724
            if (tif->tif_flags & TIFF_MAPPED)
21,437✔
725
            {
726
                toff_t n;
727
                if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
18,875✔
728
                {
729
                    tif->tif_size = (tmsize_t)n;
36✔
730
                    assert((toff_t)tif->tif_size == n);
36✔
731
                }
732
                else
733
                    tif->tif_flags &= ~TIFF_MAPPED;
18,846✔
734
            }
735
            /*
736
             * Sometimes we do not want to read the first directory (for
737
             * example, it may be broken) and want to proceed to other
738
             * directories. I this case we use the TIFF_HEADERONLY flag to open
739
             * file and return immediately after reading TIFF header.
740
             */
741
            if (tif->tif_flags & TIFF_HEADERONLY)
21,444✔
742
                return (tif);
×
743

744
            /*
745
             * Setup initial directory.
746
             */
747
            if (TIFFReadDirectory(tif))
21,444✔
748
            {
749
                return (tif);
21,394✔
750
            }
751
            break;
27✔
752
        case 'a':
×
753
            /*
754
             * New directories are automatically append
755
             * to the end of the directory chain when they
756
             * are written out (see TIFFWriteDirectory).
757
             */
758
            if (!TIFFDefaultDirectory(tif))
×
759
                goto bad;
×
760
            return (tif);
×
761
    }
762
bad:
×
763
    tif->tif_mode = O_RDONLY; /* XXX avoid flush */
×
764
    TIFFCleanup(tif);
×
765
bad2:
14✔
766
    return ((TIFF *)0);
14✔
767
}
768

769
/*
770
 * Query functions to access private data.
771
 */
772

773
/*
774
 * Return open file's name.
775
 */
776
const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
6✔
777

778
/*
779
 * Set the file name.
780
 */
781
const char *TIFFSetFileName(TIFF *tif, const char *name)
×
782
{
783
    const char *old_name = tif->tif_name;
×
784
    tif->tif_name = (char *)name;
×
785
    return (old_name);
×
786
}
787

788
/*
789
 * Return open file's I/O descriptor.
790
 */
791
int TIFFFileno(TIFF *tif) { return (tif->tif_fd); }
×
792

793
/*
794
 * Set open file's I/O descriptor, and return previous value.
795
 */
796
int TIFFSetFileno(TIFF *tif, int fd)
×
797
{
798
    int old_fd = tif->tif_fd;
×
799
    tif->tif_fd = fd;
×
800
    return old_fd;
×
801
}
802

803
/*
804
 * Return open file's clientdata.
805
 */
806
thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
91,427✔
807

808
/*
809
 * Set open file's clientdata, and return previous value.
810
 */
811
thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
×
812
{
813
    thandle_t m = tif->tif_clientdata;
×
814
    tif->tif_clientdata = newvalue;
×
815
    return m;
×
816
}
817

818
/*
819
 * Return read/write mode.
820
 */
821
int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
×
822

823
/*
824
 * Return read/write mode.
825
 */
826
int TIFFSetMode(TIFF *tif, int mode)
×
827
{
828
    int old_mode = tif->tif_mode;
×
829
    tif->tif_mode = mode;
×
830
    return (old_mode);
×
831
}
832

833
/*
834
 * Return nonzero if file is organized in
835
 * tiles; zero if organized as strips.
836
 */
837
int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
4,785,910✔
838

839
/*
840
 * Return current row being read/written.
841
 */
842
uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); }
×
843

844
/*
845
 * Return index of the current directory.
846
 */
847
tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
×
848

849
/*
850
 * Return current strip.
851
 */
852
uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); }
×
853

854
/*
855
 * Return current tile.
856
 */
857
uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); }
×
858

859
/*
860
 * Return nonzero if the file has byte-swapped data.
861
 */
862
int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
104,274✔
863

864
/*
865
 * Return nonzero if the data is returned up-sampled.
866
 */
867
int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); }
×
868

869
/*
870
 * Return nonzero if the data is returned in MSB-to-LSB bit order.
871
 */
872
int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); }
×
873

874
/*
875
 * Return nonzero if given file was written in big-endian order.
876
 */
877
int TIFFIsBigEndian(TIFF *tif)
26,219✔
878
{
879
    return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
26,219✔
880
}
881

882
/*
883
 * Return nonzero if given file is BigTIFF style.
884
 */
885
int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); }
×
886

887
/*
888
 * Return pointer to file read method.
889
 */
890
TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
×
891

892
/*
893
 * Return pointer to file write method.
894
 */
895
TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
×
896

897
/*
898
 * Return pointer to file seek method.
899
 */
900
TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
×
901

902
/*
903
 * Return pointer to file close method.
904
 */
905
TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
×
906

907
/*
908
 * Return pointer to file size requesting method.
909
 */
910
TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
10,173✔
911

912
/*
913
 * Return pointer to memory mapping method.
914
 */
915
TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
×
916

917
/*
918
 * Return pointer to memory unmapping method.
919
 */
920
TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
×
921
{
922
    return (tif->tif_unmapproc);
×
923
}
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