Coverage Report

Created: 2024-06-18 06:05

/src/libtiff/libtiff/tif_open.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library.
27
 */
28
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
0
{
42
0
    (void)fd;
43
0
    (void)pbase;
44
0
    (void)psize;
45
0
    return (0);
46
0
}
47
48
static void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
49
0
{
50
0
    (void)fd;
51
0
    (void)base;
52
0
    (void)size;
53
0
}
54
55
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode,
56
                 const char *module)
57
0
{
58
0
    int m = -1;
59
60
0
    switch (mode[0])
61
0
    {
62
0
        case 'r':
63
0
            m = O_RDONLY;
64
0
            if (mode[1] == '+')
65
0
                m = O_RDWR;
66
0
            break;
67
0
        case 'w':
68
0
        case 'a':
69
0
            m = O_RDWR | O_CREAT;
70
0
            if (mode[0] == 'w')
71
0
                m |= O_TRUNC;
72
0
            break;
73
0
        default:
74
0
            _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
75
0
            break;
76
0
    }
77
0
    return (m);
78
0
}
79
80
TIFFOpenOptions *TIFFOpenOptionsAlloc()
81
0
{
82
0
    TIFFOpenOptions *opts =
83
0
        (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions));
84
0
    return opts;
85
0
}
86
87
0
void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); }
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
0
{
97
0
    opts->max_single_mem_alloc = max_single_mem_alloc;
98
0
}
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,
107
                                            tmsize_t max_cumulated_mem_alloc)
108
0
{
109
0
    opts->max_cumulated_mem_alloc = max_cumulated_mem_alloc;
110
0
}
111
112
void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts,
113
                                        TIFFErrorHandlerExtR handler,
114
                                        void *errorhandler_user_data)
115
0
{
116
0
    opts->errorhandler = handler;
117
0
    opts->errorhandler_user_data = errorhandler_user_data;
118
0
}
119
120
void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts,
121
                                          TIFFErrorHandlerExtR handler,
122
                                          void *warnhandler_user_data)
123
0
{
124
0
    opts->warnhandler = handler;
125
0
    opts->warnhandler_user_data = warnhandler_user_data;
126
0
}
127
128
static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif,
129
                                                 const char *pszFunction,
130
                                                 tmsize_t s)
131
0
{
132
0
    TIFFErrorExtR(tif, pszFunction,
133
0
                  "Memory allocation of %" PRIu64
134
0
                  " bytes is beyond the %" PRIu64
135
0
                  " byte limit defined in open options",
136
0
                  (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc);
137
0
}
138
139
static void _TIFFEmitErrorAboveMaxCumulatedMemAlloc(TIFF *tif,
140
                                                    const char *pszFunction,
141
                                                    tmsize_t s)
142
0
{
143
0
    TIFFErrorExtR(tif, pszFunction,
144
0
                  "Cumulated memory allocation of %" PRIu64 " + %" PRIu64
145
0
                  " bytes is beyond the %" PRIu64
146
0
                  " cumulated byte limit defined in open options",
147
0
                  (uint64_t)tif->tif_cur_cumulated_mem_alloc, (uint64_t)s,
148
0
                  (uint64_t)tif->tif_max_cumulated_mem_alloc);
149
0
}
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
0
#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)
165
0
{
166
0
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
167
0
        s > tif->tif_max_single_mem_alloc)
168
0
    {
169
0
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
170
0
        return NULL;
171
0
    }
172
0
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
173
0
    {
174
0
        if (s > tif->tif_max_cumulated_mem_alloc -
175
0
                    tif->tif_cur_cumulated_mem_alloc ||
176
0
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
177
0
        {
178
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s);
179
0
            return NULL;
180
0
        }
181
0
        void *ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
182
0
        if (!ptr)
183
0
            return NULL;
184
0
        tif->tif_cur_cumulated_mem_alloc += s;
185
0
        memcpy(ptr, &s, sizeof(s));
186
0
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
187
0
    }
188
0
    return _TIFFmalloc(s);
189
0
}
190
191
/** calloc() version that takes into account memory-specific open options */
192
void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
193
0
{
194
0
    if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
195
0
        return NULL;
196
0
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
197
0
    {
198
0
        if (nmemb * siz > tif->tif_max_single_mem_alloc)
199
0
        {
200
0
            _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
201
0
                                                 nmemb * siz);
202
0
            return NULL;
203
0
        }
204
0
    }
205
0
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
206
0
    {
207
0
        const tmsize_t s = nmemb * siz;
208
0
        if (s > tif->tif_max_cumulated_mem_alloc -
209
0
                    tif->tif_cur_cumulated_mem_alloc ||
210
0
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
211
0
        {
212
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s);
213
0
            return NULL;
214
0
        }
215
0
        void *ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1);
216
0
        if (!ptr)
217
0
            return NULL;
218
0
        tif->tif_cur_cumulated_mem_alloc += s;
219
0
        memcpy(ptr, &s, sizeof(s));
220
0
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
221
0
    }
222
0
    return _TIFFcalloc(nmemb, siz);
223
0
}
224
225
/** realloc() version that takes into account memory-specific open options */
226
void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
227
0
{
228
0
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
229
0
        s > tif->tif_max_single_mem_alloc)
230
0
    {
231
0
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
232
0
        return NULL;
233
0
    }
234
0
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
235
0
    {
236
0
        void *oldPtr = p;
237
0
        tmsize_t oldSize = 0;
238
0
        if (p)
239
0
        {
240
0
            oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
241
0
            memcpy(&oldSize, oldPtr, sizeof(oldSize));
242
0
            assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
243
0
        }
244
0
        if (s > oldSize &&
245
0
            (s > tif->tif_max_cumulated_mem_alloc -
246
0
                     (tif->tif_cur_cumulated_mem_alloc - oldSize) ||
247
0
             s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE))
248
0
        {
249
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt",
250
0
                                                    s - oldSize);
251
0
            return NULL;
252
0
        }
253
0
        void *newPtr =
254
0
            _TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
255
0
        if (newPtr == NULL)
256
0
            return NULL;
257
0
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
258
0
        tif->tif_cur_cumulated_mem_alloc += s;
259
0
        memcpy(newPtr, &s, sizeof(s));
260
0
        return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
261
0
    }
262
0
    return _TIFFrealloc(p, s);
263
0
}
264
265
/** free() version that takes into account memory-specific open options */
266
void _TIFFfreeExt(TIFF *tif, void *p)
267
0
{
268
0
    if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
269
0
    {
270
0
        void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
271
0
        tmsize_t oldSize;
272
0
        memcpy(&oldSize, oldPtr, sizeof(oldSize));
273
0
        assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
274
0
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
275
0
        p = oldPtr;
276
0
    }
277
0
    _TIFFfree(p);
278
0
}
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
0
{
286
0
    return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
287
0
                             seekproc, closeproc, sizeproc, mapproc, unmapproc,
288
0
                             NULL);
289
0
}
290
291
TIFF *TIFFClientOpenExt(const char *name, const char *mode,
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
0
{
298
0
    static const char module[] = "TIFFClientOpenExt";
299
0
    TIFF *tif;
300
0
    int m;
301
0
    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
0
    assert(sizeof(uint8_t) == 1);
308
0
    assert(sizeof(int8_t) == 1);
309
0
    assert(sizeof(uint16_t) == 2);
310
0
    assert(sizeof(int16_t) == 2);
311
0
    assert(sizeof(uint32_t) == 4);
312
0
    assert(sizeof(int32_t) == 4);
313
0
    assert(sizeof(uint64_t) == 8);
314
0
    assert(sizeof(int64_t) == 8);
315
0
    {
316
0
        union
317
0
        {
318
0
            uint8_t a8[2];
319
0
            uint16_t a16;
320
0
        } n;
321
0
        n.a8[0] = 1;
322
0
        n.a8[1] = 0;
323
0
        (void)n;
324
#ifdef WORDS_BIGENDIAN
325
        assert(n.a16 == 256);
326
#else
327
0
        assert(n.a16 == 1);
328
0
#endif
329
0
    }
330
331
0
    m = _TIFFgetMode(opts, clientdata, mode, module);
332
0
    if (m == -1)
333
0
        goto bad2;
334
0
    tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
335
0
    if (opts && opts->max_single_mem_alloc > 0 &&
336
0
        size_to_alloc > opts->max_single_mem_alloc)
337
0
    {
338
0
        _TIFFErrorEarly(opts, clientdata, module,
339
0
                        "%s: Memory allocation of %" PRIu64
340
0
                        " bytes is beyond the %" PRIu64
341
0
                        " byte limit defined in open options",
342
0
                        name, (uint64_t)size_to_alloc,
343
0
                        (uint64_t)opts->max_single_mem_alloc);
344
0
        goto bad2;
345
0
    }
346
0
    if (opts && opts->max_cumulated_mem_alloc > 0 &&
347
0
        size_to_alloc > opts->max_cumulated_mem_alloc)
348
0
    {
349
0
        _TIFFErrorEarly(opts, clientdata, module,
350
0
                        "%s: Memory allocation of %" PRIu64
351
0
                        " bytes is beyond the %" PRIu64
352
0
                        " cumulated byte limit defined in open options",
353
0
                        name, (uint64_t)size_to_alloc,
354
0
                        (uint64_t)opts->max_cumulated_mem_alloc);
355
0
        goto bad2;
356
0
    }
357
0
    tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
358
0
    if (tif == NULL)
359
0
    {
360
0
        _TIFFErrorEarly(opts, clientdata, module,
361
0
                        "%s: Out of memory (TIFF structure)", name);
362
0
        goto bad2;
363
0
    }
364
0
    _TIFFmemset(tif, 0, sizeof(*tif));
365
0
    tif->tif_name = (char *)tif + sizeof(TIFF);
366
0
    strcpy(tif->tif_name, name);
367
0
    tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
368
0
    tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */
369
0
    tif->tif_curoff = 0;
370
0
    tif->tif_curstrip = (uint32_t)-1; /* invalid strip */
371
0
    tif->tif_row = (uint32_t)-1;      /* read/write pre-increment */
372
0
    tif->tif_clientdata = clientdata;
373
0
    tif->tif_readproc = readproc;
374
0
    tif->tif_writeproc = writeproc;
375
0
    tif->tif_seekproc = seekproc;
376
0
    tif->tif_closeproc = closeproc;
377
0
    tif->tif_sizeproc = sizeproc;
378
0
    tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
379
0
    tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
380
0
    if (opts)
381
0
    {
382
0
        tif->tif_errorhandler = opts->errorhandler;
383
0
        tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
384
0
        tif->tif_warnhandler = opts->warnhandler;
385
0
        tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
386
0
        tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
387
0
        tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc;
388
0
    }
389
390
0
    if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
391
0
    {
392
0
        TIFFErrorExtR(tif, module,
393
0
                      "One of the client procedures is NULL pointer.");
394
0
        _TIFFfreeExt(NULL, tif);
395
0
        goto bad2;
396
0
    }
397
398
0
    _TIFFSetDefaultCompressionState(tif); /* setup default state */
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
0
    tif->tif_flags = FILLORDER_MSB2LSB;
405
0
    if (m == O_RDONLY)
406
0
        tif->tif_flags |= TIFF_MAPPED;
407
408
0
#ifdef STRIPCHOP_DEFAULT
409
0
    if (m == O_RDONLY || m == O_RDWR)
410
0
        tif->tif_flags |= STRIPCHOP_DEFAULT;
411
0
#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
0
    for (cp = mode; *cp; cp++)
469
0
        switch (*cp)
470
0
        {
471
0
            case 'b':
472
0
#ifndef WORDS_BIGENDIAN
473
0
                if (m & O_CREAT)
474
0
                    tif->tif_flags |= TIFF_SWAB;
475
0
#endif
476
0
                break;
477
0
            case 'l':
478
#ifdef WORDS_BIGENDIAN
479
                if ((m & O_CREAT))
480
                    tif->tif_flags |= TIFF_SWAB;
481
#endif
482
0
                break;
483
0
            case 'B':
484
0
                tif->tif_flags =
485
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
486
0
                break;
487
0
            case 'L':
488
0
                tif->tif_flags =
489
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
490
0
                break;
491
0
            case 'H':
492
0
                TIFFWarningExtR(tif, name,
493
0
                                "H(ost) mode is deprecated. Since "
494
0
                                "libtiff 4.5.1, it is an alias of 'B' / "
495
0
                                "FILLORDER_MSB2LSB.");
496
0
                tif->tif_flags =
497
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
498
0
                break;
499
0
            case 'M':
500
0
                if (m == O_RDONLY)
501
0
                    tif->tif_flags |= TIFF_MAPPED;
502
0
                break;
503
0
            case 'm':
504
0
                if (m == O_RDONLY)
505
0
                    tif->tif_flags &= ~TIFF_MAPPED;
506
0
                break;
507
0
            case 'C':
508
0
                if (m == O_RDONLY)
509
0
                    tif->tif_flags |= TIFF_STRIPCHOP;
510
0
                break;
511
0
            case 'c':
512
0
                if (m == O_RDONLY)
513
0
                    tif->tif_flags &= ~TIFF_STRIPCHOP;
514
0
                break;
515
0
            case 'h':
516
0
                tif->tif_flags |= TIFF_HEADERONLY;
517
0
                break;
518
0
            case '8':
519
0
                if (m & O_CREAT)
520
0
                    tif->tif_flags |= TIFF_BIGTIFF;
521
0
                break;
522
0
            case 'D':
523
0
                tif->tif_flags |= TIFF_DEFERSTRILELOAD;
524
0
                break;
525
0
            case 'O':
526
0
                if (m == O_RDONLY)
527
0
                    tif->tif_flags |=
528
0
                        (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);
529
0
                break;
530
0
        }
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
0
    if ((m & O_TRUNC) ||
543
0
        !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
544
0
    {
545
0
        if (tif->tif_mode == O_RDONLY)
546
0
        {
547
0
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
548
0
            goto bad;
549
0
        }
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
0
        tif->tif_header.common.tiff_magic =
558
0
            (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
559
0
#endif
560
0
        TIFFHeaderUnion tif_header_swapped;
561
0
        if (!(tif->tif_flags & TIFF_BIGTIFF))
562
0
        {
563
0
            tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
564
0
            tif->tif_header.classic.tiff_diroff = 0;
565
0
            tif->tif_header_size = sizeof(TIFFHeaderClassic);
566
            /* Swapped copy for writing */
567
0
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
568
0
                        sizeof(TIFFHeaderUnion));
569
0
            if (tif->tif_flags & TIFF_SWAB)
570
0
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
571
0
        }
572
0
        else
573
0
        {
574
0
            tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
575
0
            tif->tif_header.big.tiff_offsetsize = 8;
576
0
            tif->tif_header.big.tiff_unused = 0;
577
0
            tif->tif_header.big.tiff_diroff = 0;
578
0
            tif->tif_header_size = sizeof(TIFFHeaderBig);
579
            /* Swapped copy for writing */
580
0
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
581
0
                        sizeof(TIFFHeaderUnion));
582
0
            if (tif->tif_flags & TIFF_SWAB)
583
0
            {
584
0
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
585
0
                TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize);
586
0
            }
587
0
        }
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
0
        TIFFSeekFile(tif, 0, SEEK_SET);
596
0
        if (!WriteOK(tif, &tif_header_swapped,
597
0
                     (tmsize_t)(tif->tif_header_size)))
598
0
        {
599
0
            TIFFErrorExtR(tif, name, "Error writing TIFF header");
600
0
            goto bad;
601
0
        }
602
        /*
603
         * Setup default directory.
604
         */
605
0
        if (!TIFFDefaultDirectory(tif))
606
0
            goto bad;
607
0
        tif->tif_diroff = 0;
608
0
        tif->tif_lastdiroff = 0;
609
0
        tif->tif_setdirectory_force_absolute = FALSE;
610
0
        return (tif);
611
0
    }
612
613
    /*
614
     * Setup the byte order handling according to the opened file for reading.
615
     */
616
0
    if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
617
0
        tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
618
0
#if MDI_SUPPORT
619
0
        &&
620
#if HOST_BIGENDIAN
621
        tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
622
#else
623
0
        tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
624
0
#endif
625
0
    )
626
0
    {
627
0
        TIFFErrorExtR(tif, name,
628
0
                      "Not a TIFF or MDI file, bad magic number %" PRIu16
629
0
                      " (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
0
                      tif->tif_header.common.tiff_magic,
638
0
                      tif->tif_header.common.tiff_magic);
639
0
        goto bad;
640
0
    }
641
0
    if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
642
0
    {
643
0
#ifndef WORDS_BIGENDIAN
644
0
        tif->tif_flags |= TIFF_SWAB;
645
0
#endif
646
0
    }
647
0
    else
648
0
    {
649
#ifdef WORDS_BIGENDIAN
650
        tif->tif_flags |= TIFF_SWAB;
651
#endif
652
0
    }
653
0
    if (tif->tif_flags & TIFF_SWAB)
654
0
        TIFFSwabShort(&tif->tif_header.common.tiff_version);
655
0
    if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
656
0
        (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
657
0
    {
658
0
        TIFFErrorExtR(tif, name,
659
0
                      "Not a TIFF file, bad version number %" PRIu16
660
0
                      " (0x%" PRIx16 ")",
661
0
                      tif->tif_header.common.tiff_version,
662
0
                      tif->tif_header.common.tiff_version);
663
0
        goto bad;
664
0
    }
665
0
    if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
666
0
    {
667
0
        if (tif->tif_flags & TIFF_SWAB)
668
0
            TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
669
0
        tif->tif_header_size = sizeof(TIFFHeaderClassic);
670
0
    }
671
0
    else
672
0
    {
673
0
        if (!ReadOK(tif,
674
0
                    ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
675
0
                    (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
676
0
        {
677
0
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
678
0
            goto bad;
679
0
        }
680
0
        if (tif->tif_flags & TIFF_SWAB)
681
0
        {
682
0
            TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
683
0
            TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
684
0
        }
685
0
        if (tif->tif_header.big.tiff_offsetsize != 8)
686
0
        {
687
0
            TIFFErrorExtR(tif, name,
688
0
                          "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
689
0
                          " (0x%" PRIx16 ")",
690
0
                          tif->tif_header.big.tiff_offsetsize,
691
0
                          tif->tif_header.big.tiff_offsetsize);
692
0
            goto bad;
693
0
        }
694
0
        if (tif->tif_header.big.tiff_unused != 0)
695
0
        {
696
0
            TIFFErrorExtR(tif, name,
697
0
                          "Not a TIFF file, bad BigTIFF unused %" PRIu16
698
0
                          " (0x%" PRIx16 ")",
699
0
                          tif->tif_header.big.tiff_unused,
700
0
                          tif->tif_header.big.tiff_unused);
701
0
            goto bad;
702
0
        }
703
0
        tif->tif_header_size = sizeof(TIFFHeaderBig);
704
0
        tif->tif_flags |= TIFF_BIGTIFF;
705
0
    }
706
0
    tif->tif_flags |= TIFF_MYBUFFER;
707
0
    tif->tif_rawcp = tif->tif_rawdata = 0;
708
0
    tif->tif_rawdatasize = 0;
709
0
    tif->tif_rawdataoff = 0;
710
0
    tif->tif_rawdataloaded = 0;
711
712
0
    switch (mode[0])
713
0
    {
714
0
        case 'r':
715
0
            if (!(tif->tif_flags & TIFF_BIGTIFF))
716
0
                tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
717
0
            else
718
0
                tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
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
0
            if (tif->tif_flags & TIFF_MAPPED)
725
0
            {
726
0
                toff_t n;
727
0
                if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
728
0
                {
729
0
                    tif->tif_size = (tmsize_t)n;
730
0
                    assert((toff_t)tif->tif_size == n);
731
0
                }
732
0
                else
733
0
                    tif->tif_flags &= ~TIFF_MAPPED;
734
0
            }
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
0
            if (tif->tif_flags & TIFF_HEADERONLY)
742
0
                return (tif);
743
744
            /*
745
             * Setup initial directory.
746
             */
747
0
            if (TIFFReadDirectory(tif))
748
0
            {
749
0
                return (tif);
750
0
            }
751
0
            break;
752
0
        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
0
            if (!TIFFDefaultDirectory(tif))
759
0
                goto bad;
760
0
            return (tif);
761
0
    }
762
0
bad:
763
0
    tif->tif_mode = O_RDONLY; /* XXX avoid flush */
764
0
    TIFFCleanup(tif);
765
0
bad2:
766
0
    return ((TIFF *)0);
767
0
}
768
769
/*
770
 * Query functions to access private data.
771
 */
772
773
/*
774
 * Return open file's name.
775
 */
776
0
const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
777
778
/*
779
 * Set the file name.
780
 */
781
const char *TIFFSetFileName(TIFF *tif, const char *name)
782
0
{
783
0
    const char *old_name = tif->tif_name;
784
0
    tif->tif_name = (char *)name;
785
0
    return (old_name);
786
0
}
787
788
/*
789
 * Return open file's I/O descriptor.
790
 */
791
0
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
0
{
798
0
    int old_fd = tif->tif_fd;
799
0
    tif->tif_fd = fd;
800
0
    return old_fd;
801
0
}
802
803
/*
804
 * Return open file's clientdata.
805
 */
806
0
thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
807
808
/*
809
 * Set open file's clientdata, and return previous value.
810
 */
811
thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
812
0
{
813
0
    thandle_t m = tif->tif_clientdata;
814
0
    tif->tif_clientdata = newvalue;
815
0
    return m;
816
0
}
817
818
/*
819
 * Return read/write mode.
820
 */
821
0
int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
822
823
/*
824
 * Return read/write mode.
825
 */
826
int TIFFSetMode(TIFF *tif, int mode)
827
0
{
828
0
    int old_mode = tif->tif_mode;
829
0
    tif->tif_mode = mode;
830
0
    return (old_mode);
831
0
}
832
833
/*
834
 * Return nonzero if file is organized in
835
 * tiles; zero if organized as strips.
836
 */
837
0
int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
838
839
/*
840
 * Return current row being read/written.
841
 */
842
0
uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); }
843
844
/*
845
 * Return index of the current directory.
846
 */
847
0
tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
848
849
/*
850
 * Return current strip.
851
 */
852
0
uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); }
853
854
/*
855
 * Return current tile.
856
 */
857
0
uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); }
858
859
/*
860
 * Return nonzero if the file has byte-swapped data.
861
 */
862
0
int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
863
864
/*
865
 * Return nonzero if the data is returned up-sampled.
866
 */
867
0
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
0
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)
878
0
{
879
0
    return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
880
0
}
881
882
/*
883
 * Return nonzero if given file is BigTIFF style.
884
 */
885
0
int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); }
886
887
/*
888
 * Return pointer to file read method.
889
 */
890
0
TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
891
892
/*
893
 * Return pointer to file write method.
894
 */
895
0
TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
896
897
/*
898
 * Return pointer to file seek method.
899
 */
900
0
TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
901
902
/*
903
 * Return pointer to file close method.
904
 */
905
0
TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
906
907
/*
908
 * Return pointer to file size requesting method.
909
 */
910
0
TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
911
912
/*
913
 * Return pointer to memory mapping method.
914
 */
915
0
TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
916
917
/*
918
 * Return pointer to memory unmapping method.
919
 */
920
TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
921
0
{
922
0
    return (tif->tif_unmapproc);
923
0
}