Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/tiff/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
18.8k
{
58
18.8k
    int m = -1;
59
60
18.8k
    switch (mode[0])
61
18.8k
    {
62
0
        case 'r':
63
0
            m = O_RDONLY;
64
0
            if (mode[1] == '+')
65
0
                m = O_RDWR;
66
0
            break;
67
18.8k
        case 'w':
68
18.8k
        case 'a':
69
18.8k
            m = O_RDWR | O_CREAT;
70
18.8k
            if (mode[0] == 'w')
71
18.8k
                m |= O_TRUNC;
72
18.8k
            break;
73
0
        default:
74
0
            _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
75
0
            break;
76
18.8k
    }
77
18.8k
    return (m);
78
18.8k
}
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
371k
{
166
371k
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
167
371k
        s > tif->tif_max_single_mem_alloc)
168
0
    {
169
0
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
170
0
        return NULL;
171
0
    }
172
371k
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
173
0
    {
174
0
        void *ptr;
175
0
        if (s > tif->tif_max_cumulated_mem_alloc -
176
0
                    tif->tif_cur_cumulated_mem_alloc ||
177
0
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
178
0
        {
179
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s);
180
0
            return NULL;
181
0
        }
182
0
        ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
183
0
        if (!ptr)
184
0
            return NULL;
185
0
        tif->tif_cur_cumulated_mem_alloc += s;
186
0
        memcpy(ptr, &s, sizeof(s));
187
0
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
188
0
    }
189
371k
    return _TIFFmalloc(s);
190
371k
}
191
192
/** calloc() version that takes into account memory-specific open options */
193
void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
194
0
{
195
0
    if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
196
0
        return NULL;
197
0
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
198
0
    {
199
0
        if (nmemb * siz > tif->tif_max_single_mem_alloc)
200
0
        {
201
0
            _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
202
0
                                                 nmemb * siz);
203
0
            return NULL;
204
0
        }
205
0
    }
206
0
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
207
0
    {
208
0
        const tmsize_t s = nmemb * siz;
209
0
        void *ptr;
210
0
        if (s > tif->tif_max_cumulated_mem_alloc -
211
0
                    tif->tif_cur_cumulated_mem_alloc ||
212
0
            s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
213
0
        {
214
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s);
215
0
            return NULL;
216
0
        }
217
0
        ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1);
218
0
        if (!ptr)
219
0
            return NULL;
220
0
        tif->tif_cur_cumulated_mem_alloc += s;
221
0
        memcpy(ptr, &s, sizeof(s));
222
0
        return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
223
0
    }
224
0
    return _TIFFcalloc(nmemb, siz);
225
0
}
226
227
/** realloc() version that takes into account memory-specific open options */
228
void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
229
161k
{
230
161k
    if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
231
161k
        s > tif->tif_max_single_mem_alloc)
232
0
    {
233
0
        _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
234
0
        return NULL;
235
0
    }
236
161k
    if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
237
0
    {
238
0
        void *oldPtr = p;
239
0
        void *newPtr;
240
0
        tmsize_t oldSize = 0;
241
0
        if (p)
242
0
        {
243
0
            oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
244
0
            memcpy(&oldSize, oldPtr, sizeof(oldSize));
245
0
            assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
246
0
        }
247
0
        if (s > oldSize &&
248
0
            (s > tif->tif_max_cumulated_mem_alloc -
249
0
                     (tif->tif_cur_cumulated_mem_alloc - oldSize) ||
250
0
             s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE))
251
0
        {
252
0
            _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt",
253
0
                                                    s - oldSize);
254
0
            return NULL;
255
0
        }
256
0
        newPtr =
257
0
            _TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
258
0
        if (newPtr == NULL)
259
0
            return NULL;
260
0
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
261
0
        tif->tif_cur_cumulated_mem_alloc += s;
262
0
        memcpy(newPtr, &s, sizeof(s));
263
0
        return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
264
0
    }
265
161k
    return _TIFFrealloc(p, s);
266
161k
}
267
268
/** free() version that takes into account memory-specific open options */
269
void _TIFFfreeExt(TIFF *tif, void *p)
270
479k
{
271
479k
    if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
272
0
    {
273
0
        void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
274
0
        tmsize_t oldSize;
275
0
        memcpy(&oldSize, oldPtr, sizeof(oldSize));
276
0
        assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
277
0
        tif->tif_cur_cumulated_mem_alloc -= oldSize;
278
0
        p = oldPtr;
279
0
    }
280
479k
    _TIFFfree(p);
281
479k
}
282
283
TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata,
284
                     TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc,
285
                     TIFFSeekProc seekproc, TIFFCloseProc closeproc,
286
                     TIFFSizeProc sizeproc, TIFFMapFileProc mapproc,
287
                     TIFFUnmapFileProc unmapproc)
288
18.8k
{
289
18.8k
    return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
290
18.8k
                             seekproc, closeproc, sizeproc, mapproc, unmapproc,
291
18.8k
                             NULL);
292
18.8k
}
293
294
TIFF *TIFFClientOpenExt(const char *name, const char *mode,
295
                        thandle_t clientdata, TIFFReadWriteProc readproc,
296
                        TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
297
                        TIFFCloseProc closeproc, TIFFSizeProc sizeproc,
298
                        TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc,
299
                        TIFFOpenOptions *opts)
300
18.8k
{
301
18.8k
    static const char module[] = "TIFFClientOpenExt";
302
18.8k
    TIFF *tif;
303
18.8k
    int m;
304
18.8k
    const char *cp;
305
18.8k
    tmsize_t size_to_alloc;
306
307
    /* The following are configuration checks. They should be redundant, but
308
     * should not compile to any actual code in an optimised release build
309
     * anyway. If any of them fail, (makefile-based or other) configuration is
310
     * not correct */
311
18.8k
    assert(sizeof(uint8_t) == 1);
312
18.8k
    assert(sizeof(int8_t) == 1);
313
18.8k
    assert(sizeof(uint16_t) == 2);
314
18.8k
    assert(sizeof(int16_t) == 2);
315
18.8k
    assert(sizeof(uint32_t) == 4);
316
18.8k
    assert(sizeof(int32_t) == 4);
317
18.8k
    assert(sizeof(uint64_t) == 8);
318
18.8k
    assert(sizeof(int64_t) == 8);
319
18.8k
    {
320
18.8k
        union
321
18.8k
        {
322
18.8k
            uint8_t a8[2];
323
18.8k
            uint16_t a16;
324
18.8k
        } n;
325
18.8k
        n.a8[0] = 1;
326
18.8k
        n.a8[1] = 0;
327
18.8k
        (void)n;
328
#ifdef WORDS_BIGENDIAN
329
        assert(n.a16 == 256);
330
#else
331
18.8k
        assert(n.a16 == 1);
332
18.8k
#endif
333
18.8k
    }
334
335
18.8k
    m = _TIFFgetMode(opts, clientdata, mode, module);
336
18.8k
    if (m == -1)
337
0
        goto bad2;
338
18.8k
    size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
339
18.8k
    if (opts && opts->max_single_mem_alloc > 0 &&
340
18.8k
        size_to_alloc > opts->max_single_mem_alloc)
341
0
    {
342
0
        _TIFFErrorEarly(opts, clientdata, module,
343
0
                        "%s: Memory allocation of %" PRIu64
344
0
                        " bytes is beyond the %" PRIu64
345
0
                        " byte limit defined in open options",
346
0
                        name, (uint64_t)size_to_alloc,
347
0
                        (uint64_t)opts->max_single_mem_alloc);
348
0
        goto bad2;
349
0
    }
350
18.8k
    if (opts && opts->max_cumulated_mem_alloc > 0 &&
351
18.8k
        size_to_alloc > opts->max_cumulated_mem_alloc)
352
0
    {
353
0
        _TIFFErrorEarly(opts, clientdata, module,
354
0
                        "%s: Memory allocation of %" PRIu64
355
0
                        " bytes is beyond the %" PRIu64
356
0
                        " cumulated byte limit defined in open options",
357
0
                        name, (uint64_t)size_to_alloc,
358
0
                        (uint64_t)opts->max_cumulated_mem_alloc);
359
0
        goto bad2;
360
0
    }
361
18.8k
    tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
362
18.8k
    if (tif == NULL)
363
0
    {
364
0
        _TIFFErrorEarly(opts, clientdata, module,
365
0
                        "%s: Out of memory (TIFF structure)", name);
366
0
        goto bad2;
367
0
    }
368
18.8k
    _TIFFmemset(tif, 0, sizeof(*tif));
369
18.8k
    tif->tif_name = (char *)tif + sizeof(TIFF);
370
18.8k
    strcpy(tif->tif_name, name);
371
18.8k
    tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
372
18.8k
    tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */
373
18.8k
    tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER;
374
18.8k
    tif->tif_curoff = 0;
375
18.8k
    tif->tif_curstrip = (uint32_t)-1; /* invalid strip */
376
18.8k
    tif->tif_row = (uint32_t)-1;      /* read/write pre-increment */
377
18.8k
    tif->tif_clientdata = clientdata;
378
18.8k
    tif->tif_readproc = readproc;
379
18.8k
    tif->tif_writeproc = writeproc;
380
18.8k
    tif->tif_seekproc = seekproc;
381
18.8k
    tif->tif_closeproc = closeproc;
382
18.8k
    tif->tif_sizeproc = sizeproc;
383
18.8k
    tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
384
18.8k
    tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
385
18.8k
    if (opts)
386
0
    {
387
0
        tif->tif_errorhandler = opts->errorhandler;
388
0
        tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
389
0
        tif->tif_warnhandler = opts->warnhandler;
390
0
        tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
391
0
        tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
392
0
        tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc;
393
0
    }
394
395
18.8k
    if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
396
0
    {
397
0
        TIFFErrorExtR(tif, module,
398
0
                      "One of the client procedures is NULL pointer.");
399
0
        _TIFFfreeExt(NULL, tif);
400
0
        goto bad2;
401
0
    }
402
403
18.8k
    _TIFFSetDefaultCompressionState(tif); /* setup default state */
404
    /*
405
     * Default is to return data MSB2LSB and enable the
406
     * use of memory-mapped files and strip chopping when
407
     * a file is opened read-only.
408
     */
409
18.8k
    tif->tif_flags = FILLORDER_MSB2LSB;
410
18.8k
    if (m == O_RDONLY)
411
0
        tif->tif_flags |= TIFF_MAPPED;
412
413
18.8k
#ifdef STRIPCHOP_DEFAULT
414
18.8k
    if (m == O_RDONLY || m == O_RDWR)
415
0
        tif->tif_flags |= STRIPCHOP_DEFAULT;
416
18.8k
#endif
417
418
    /*
419
     * Process library-specific flags in the open mode string.
420
     * The following flags may be used to control intrinsic library
421
     * behavior that may or may not be desirable (usually for
422
     * compatibility with some application that claims to support
423
     * TIFF but only supports some brain dead idea of what the
424
     * vendor thinks TIFF is):
425
     *
426
     * 'l' use little-endian byte order for creating a file
427
     * 'b' use big-endian byte order for creating a file
428
     * 'L' read/write information using LSB2MSB bit order
429
     * 'B' read/write information using MSB2LSB bit order
430
     * 'H' read/write information using host bit order
431
     * 'M' enable use of memory-mapped files when supported
432
     * 'm' disable use of memory-mapped files
433
     * 'C' enable strip chopping support when reading
434
     * 'c' disable strip chopping support
435
     * 'h' read TIFF header only, do not load the first IFD
436
     * '4' ClassicTIFF for creating a file (default)
437
     * '8' BigTIFF for creating a file
438
     * 'D' enable use of deferred strip/tile offset/bytecount array loading.
439
     * 'O' on-demand loading of values instead of whole array loading (implies
440
     * D)
441
     *
442
     * The use of the 'l' and 'b' flags is strongly discouraged.
443
     * These flags are provided solely because numerous vendors,
444
     * typically on the PC, do not correctly support TIFF; they
445
     * only support the Intel little-endian byte order.  This
446
     * support is not configured by default because it supports
447
     * the violation of the TIFF spec that says that readers *MUST*
448
     * support both byte orders.  It is strongly recommended that
449
     * you not use this feature except to deal with busted apps
450
     * that write invalid TIFF.  And even in those cases you should
451
     * bang on the vendors to fix their software.
452
     *
453
     * The 'L', 'B', and 'H' flags are intended for applications
454
     * that can optimize operations on data by using a particular
455
     * bit order.  By default the library returns data in MSB2LSB
456
     * bit order for compatibility with older versions of this
457
     * library.  Returning data in the bit order of the native CPU
458
     * makes the most sense but also requires applications to check
459
     * the value of the FillOrder tag; something they probably do
460
     * not do right now.
461
     *
462
     * The 'M' and 'm' flags are provided because some virtual memory
463
     * systems exhibit poor behavior when large images are mapped.
464
     * These options permit clients to control the use of memory-mapped
465
     * files on a per-file basis.
466
     *
467
     * The 'C' and 'c' flags are provided because the library support
468
     * for chopping up large strips into multiple smaller strips is not
469
     * application-transparent and as such can cause problems.  The 'c'
470
     * option permits applications that only want to look at the tags,
471
     * for example, to get the unadulterated TIFF tag information.
472
     */
473
56.4k
    for (cp = mode; *cp; cp++)
474
37.6k
        switch (*cp)
475
37.6k
        {
476
0
            case 'b':
477
0
#ifndef WORDS_BIGENDIAN
478
0
                if (m & O_CREAT)
479
0
                    tif->tif_flags |= TIFF_SWAB;
480
0
#endif
481
0
                break;
482
18.8k
            case 'l':
483
#ifdef WORDS_BIGENDIAN
484
                if ((m & O_CREAT))
485
                    tif->tif_flags |= TIFF_SWAB;
486
#endif
487
18.8k
                break;
488
0
            case 'B':
489
0
                tif->tif_flags =
490
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
491
0
                break;
492
0
            case 'L':
493
0
                tif->tif_flags =
494
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
495
0
                break;
496
0
            case 'H':
497
0
                TIFFWarningExtR(tif, name,
498
0
                                "H(ost) mode is deprecated. Since "
499
0
                                "libtiff 4.5.1, it is an alias of 'B' / "
500
0
                                "FILLORDER_MSB2LSB.");
501
0
                tif->tif_flags =
502
0
                    (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
503
0
                break;
504
0
            case 'M':
505
0
                if (m == O_RDONLY)
506
0
                    tif->tif_flags |= TIFF_MAPPED;
507
0
                break;
508
0
            case 'm':
509
0
                if (m == O_RDONLY)
510
0
                    tif->tif_flags &= ~TIFF_MAPPED;
511
0
                break;
512
0
            case 'C':
513
0
                if (m == O_RDONLY)
514
0
                    tif->tif_flags |= TIFF_STRIPCHOP;
515
0
                break;
516
0
            case 'c':
517
0
                if (m == O_RDONLY)
518
0
                    tif->tif_flags &= ~TIFF_STRIPCHOP;
519
0
                break;
520
0
            case 'h':
521
0
                tif->tif_flags |= TIFF_HEADERONLY;
522
0
                break;
523
0
            case '8':
524
0
                if (m & O_CREAT)
525
0
                    tif->tif_flags |= TIFF_BIGTIFF;
526
0
                break;
527
0
            case 'D':
528
0
                tif->tif_flags |= TIFF_DEFERSTRILELOAD;
529
0
                break;
530
0
            case 'O':
531
0
                if (m == O_RDONLY)
532
0
                    tif->tif_flags |=
533
0
                        (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);
534
0
                break;
535
37.6k
        }
536
537
#ifdef DEFER_STRILE_LOAD
538
    /* Compatibility with old DEFER_STRILE_LOAD compilation flag */
539
    /* Probably unneeded, since to the best of my knowledge (E. Rouault) */
540
    /* GDAL was the only user of this, and will now use the new 'D' flag */
541
    tif->tif_flags |= TIFF_DEFERSTRILELOAD;
542
#endif
543
544
    /*
545
     * Read in TIFF header.
546
     */
547
18.8k
    if ((m & O_TRUNC) ||
548
18.8k
        !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
549
18.8k
    {
550
18.8k
        TIFFHeaderUnion tif_header_swapped;
551
18.8k
        if (tif->tif_mode == O_RDONLY)
552
0
        {
553
0
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
554
0
            goto bad;
555
0
        }
556
        /*
557
         * Setup header and write.
558
         */
559
#ifdef WORDS_BIGENDIAN
560
        tif->tif_header.common.tiff_magic =
561
            (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
562
#else
563
18.8k
        tif->tif_header.common.tiff_magic =
564
18.8k
            (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
565
18.8k
#endif
566
18.8k
        if (!(tif->tif_flags & TIFF_BIGTIFF))
567
18.8k
        {
568
18.8k
            tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
569
18.8k
            tif->tif_header.classic.tiff_diroff = 0;
570
18.8k
            tif->tif_header_size = sizeof(TIFFHeaderClassic);
571
            /* Swapped copy for writing */
572
18.8k
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
573
18.8k
                        sizeof(TIFFHeaderUnion));
574
18.8k
            if (tif->tif_flags & TIFF_SWAB)
575
0
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
576
18.8k
        }
577
0
        else
578
0
        {
579
0
            tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
580
0
            tif->tif_header.big.tiff_offsetsize = 8;
581
0
            tif->tif_header.big.tiff_unused = 0;
582
0
            tif->tif_header.big.tiff_diroff = 0;
583
0
            tif->tif_header_size = sizeof(TIFFHeaderBig);
584
            /* Swapped copy for writing */
585
0
            _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
586
0
                        sizeof(TIFFHeaderUnion));
587
0
            if (tif->tif_flags & TIFF_SWAB)
588
0
            {
589
0
                TIFFSwabShort(&tif_header_swapped.common.tiff_version);
590
0
                TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize);
591
0
            }
592
0
        }
593
        /*
594
         * The doc for "fopen" for some STD_C_LIBs says that if you
595
         * open a file for modify ("+"), then you must fseek (or
596
         * fflush?) between any freads and fwrites.  This is not
597
         * necessary on most systems, but has been shown to be needed
598
         * on Solaris.
599
         */
600
18.8k
        TIFFSeekFile(tif, 0, SEEK_SET);
601
18.8k
        if (!WriteOK(tif, &tif_header_swapped,
602
18.8k
                     (tmsize_t)(tif->tif_header_size)))
603
0
        {
604
0
            TIFFErrorExtR(tif, name, "Error writing TIFF header");
605
0
            goto bad;
606
0
        }
607
        /*
608
         * Setup default directory.
609
         */
610
18.8k
        if (!TIFFDefaultDirectory(tif))
611
0
            goto bad;
612
18.8k
        tif->tif_diroff = 0;
613
18.8k
        tif->tif_lastdiroff = 0;
614
18.8k
        tif->tif_setdirectory_force_absolute = FALSE;
615
        /* tif_curdircount = 0 means 'empty file opened for writing, but no IFD
616
         * written yet' */
617
18.8k
        tif->tif_curdircount = 0;
618
18.8k
        return (tif);
619
18.8k
    }
620
621
    /*
622
     * Setup the byte order handling according to the opened file for reading.
623
     */
624
0
    if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
625
0
        tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
626
0
#if MDI_SUPPORT
627
0
        &&
628
#if HOST_BIGENDIAN
629
        tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
630
#else
631
0
        tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
632
0
#endif
633
0
    )
634
0
    {
635
0
        TIFFErrorExtR(tif, name,
636
0
                      "Not a TIFF or MDI file, bad magic number %" PRIu16
637
0
                      " (0x%" PRIx16 ")",
638
#else
639
    )
640
    {
641
        TIFFErrorExtR(tif, name,
642
                      "Not a TIFF file, bad magic number %" PRIu16
643
                      " (0x%" PRIx16 ")",
644
#endif
645
0
                      tif->tif_header.common.tiff_magic,
646
0
                      tif->tif_header.common.tiff_magic);
647
0
        goto bad;
648
0
    }
649
0
    if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
650
0
    {
651
0
#ifndef WORDS_BIGENDIAN
652
0
        tif->tif_flags |= TIFF_SWAB;
653
0
#endif
654
0
    }
655
0
    else
656
0
    {
657
#ifdef WORDS_BIGENDIAN
658
        tif->tif_flags |= TIFF_SWAB;
659
#endif
660
0
    }
661
0
    if (tif->tif_flags & TIFF_SWAB)
662
0
        TIFFSwabShort(&tif->tif_header.common.tiff_version);
663
0
    if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
664
0
        (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
665
0
    {
666
0
        TIFFErrorExtR(tif, name,
667
0
                      "Not a TIFF file, bad version number %" PRIu16
668
0
                      " (0x%" PRIx16 ")",
669
0
                      tif->tif_header.common.tiff_version,
670
0
                      tif->tif_header.common.tiff_version);
671
0
        goto bad;
672
0
    }
673
0
    if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
674
0
    {
675
0
        if (tif->tif_flags & TIFF_SWAB)
676
0
            TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
677
0
        tif->tif_header_size = sizeof(TIFFHeaderClassic);
678
0
    }
679
0
    else
680
0
    {
681
0
        if (!ReadOK(tif,
682
0
                    ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
683
0
                    (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
684
0
        {
685
0
            TIFFErrorExtR(tif, name, "Cannot read TIFF header");
686
0
            goto bad;
687
0
        }
688
0
        if (tif->tif_flags & TIFF_SWAB)
689
0
        {
690
0
            TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
691
0
            TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
692
0
        }
693
0
        if (tif->tif_header.big.tiff_offsetsize != 8)
694
0
        {
695
0
            TIFFErrorExtR(tif, name,
696
0
                          "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
697
0
                          " (0x%" PRIx16 ")",
698
0
                          tif->tif_header.big.tiff_offsetsize,
699
0
                          tif->tif_header.big.tiff_offsetsize);
700
0
            goto bad;
701
0
        }
702
0
        if (tif->tif_header.big.tiff_unused != 0)
703
0
        {
704
0
            TIFFErrorExtR(tif, name,
705
0
                          "Not a TIFF file, bad BigTIFF unused %" PRIu16
706
0
                          " (0x%" PRIx16 ")",
707
0
                          tif->tif_header.big.tiff_unused,
708
0
                          tif->tif_header.big.tiff_unused);
709
0
            goto bad;
710
0
        }
711
0
        tif->tif_header_size = sizeof(TIFFHeaderBig);
712
0
        tif->tif_flags |= TIFF_BIGTIFF;
713
0
    }
714
0
    tif->tif_flags |= TIFF_MYBUFFER;
715
0
    tif->tif_rawcp = tif->tif_rawdata = 0;
716
0
    tif->tif_rawdatasize = 0;
717
0
    tif->tif_rawdataoff = 0;
718
0
    tif->tif_rawdataloaded = 0;
719
720
0
    switch (mode[0])
721
0
    {
722
0
        case 'r':
723
0
            if (!(tif->tif_flags & TIFF_BIGTIFF))
724
0
                tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
725
0
            else
726
0
                tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
727
            /*
728
             * Try to use a memory-mapped file if the client
729
             * has not explicitly suppressed usage with the
730
             * 'm' flag in the open mode (see above).
731
             */
732
0
            if (tif->tif_flags & TIFF_MAPPED)
733
0
            {
734
0
                toff_t n;
735
0
                if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
736
0
                {
737
0
                    tif->tif_size = (tmsize_t)n;
738
0
                    assert((toff_t)tif->tif_size == n);
739
0
                }
740
0
                else
741
0
                    tif->tif_flags &= ~TIFF_MAPPED;
742
0
            }
743
            /*
744
             * Sometimes we do not want to read the first directory (for
745
             * example, it may be broken) and want to proceed to other
746
             * directories. I this case we use the TIFF_HEADERONLY flag to open
747
             * file and return immediately after reading TIFF header.
748
             * However, the pointer to TIFFSetField() and TIFFGetField()
749
             * (i.e. tif->tif_tagmethods.vsetfield and
750
             * tif->tif_tagmethods.vgetfield) need to be initialized, which is
751
             * done in TIFFDefaultDirectory().
752
             */
753
0
            if (tif->tif_flags & TIFF_HEADERONLY)
754
0
            {
755
0
                if (!TIFFDefaultDirectory(tif))
756
0
                    goto bad;
757
0
                return (tif);
758
0
            }
759
760
            /*
761
             * Setup initial directory.
762
             */
763
0
            if (TIFFReadDirectory(tif))
764
0
            {
765
0
                return (tif);
766
0
            }
767
0
            break;
768
0
        case 'a':
769
            /*
770
             * New directories are automatically append
771
             * to the end of the directory chain when they
772
             * are written out (see TIFFWriteDirectory).
773
             */
774
0
            if (!TIFFDefaultDirectory(tif))
775
0
                goto bad;
776
0
            return (tif);
777
0
    }
778
0
bad:
779
0
    tif->tif_mode = O_RDONLY; /* XXX avoid flush */
780
0
    TIFFCleanup(tif);
781
0
bad2:
782
0
    return ((TIFF *)0);
783
0
}
784
785
/*
786
 * Query functions to access private data.
787
 */
788
789
/*
790
 * Return open file's name.
791
 */
792
16.7k
const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
793
794
/*
795
 * Set the file name.
796
 */
797
const char *TIFFSetFileName(TIFF *tif, const char *name)
798
0
{
799
0
    const char *old_name = tif->tif_name;
800
0
    tif->tif_name = (char *)name;
801
0
    return (old_name);
802
0
}
803
804
/*
805
 * Return open file's I/O descriptor.
806
 */
807
0
int TIFFFileno(TIFF *tif) { return (tif->tif_fd); }
808
809
/*
810
 * Set open file's I/O descriptor, and return previous value.
811
 */
812
int TIFFSetFileno(TIFF *tif, int fd)
813
0
{
814
0
    int old_fd = tif->tif_fd;
815
0
    tif->tif_fd = fd;
816
0
    return old_fd;
817
0
}
818
819
/*
820
 * Return open file's clientdata.
821
 */
822
18.8k
thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
823
824
/*
825
 * Set open file's clientdata, and return previous value.
826
 */
827
thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
828
0
{
829
0
    thandle_t m = tif->tif_clientdata;
830
0
    tif->tif_clientdata = newvalue;
831
0
    return m;
832
0
}
833
834
/*
835
 * Return read/write mode.
836
 */
837
0
int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
838
839
/*
840
 * Return read/write mode.
841
 */
842
int TIFFSetMode(TIFF *tif, int mode)
843
0
{
844
0
    int old_mode = tif->tif_mode;
845
0
    tif->tif_mode = mode;
846
0
    return (old_mode);
847
0
}
848
849
/*
850
 * Return nonzero if file is organized in
851
 * tiles; zero if organized as strips.
852
 */
853
0
int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
854
855
/*
856
 * Return current row being read/written.
857
 */
858
0
uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); }
859
860
/*
861
 * Return index of the current directory.
862
 */
863
0
tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
864
865
/*
866
 * Return current strip.
867
 */
868
0
uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); }
869
870
/*
871
 * Return current tile.
872
 */
873
0
uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); }
874
875
/*
876
 * Return nonzero if the file has byte-swapped data.
877
 */
878
0
int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
879
880
/*
881
 * Return nonzero if the data is returned up-sampled.
882
 */
883
0
int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); }
884
885
/*
886
 * Return nonzero if the data is returned in MSB-to-LSB bit order.
887
 */
888
0
int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); }
889
890
/*
891
 * Return nonzero if given file was written in big-endian order.
892
 */
893
int TIFFIsBigEndian(TIFF *tif)
894
0
{
895
0
    return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
896
0
}
897
898
/*
899
 * Return nonzero if given file is BigTIFF style.
900
 */
901
0
int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); }
902
903
/*
904
 * Return pointer to file read method.
905
 */
906
0
TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
907
908
/*
909
 * Return pointer to file write method.
910
 */
911
0
TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
912
913
/*
914
 * Return pointer to file seek method.
915
 */
916
0
TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
917
918
/*
919
 * Return pointer to file close method.
920
 */
921
0
TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
922
923
/*
924
 * Return pointer to file size requesting method.
925
 */
926
0
TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
927
928
/*
929
 * Return pointer to memory mapping method.
930
 */
931
0
TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
932
933
/*
934
 * Return pointer to memory unmapping method.
935
 */
936
TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
937
0
{
938
0
    return (tif->tif_unmapproc);
939
0
}
940
941
void
942
TIFFSetJpegMemFunction(TIFF *tif,
943
                       void *(*fn)(thandle_t))
944
0
{
945
0
    tif->get_jpeg_mem_ptr = fn;
946
0
}
947
948
949
/* vim: set ts=8 sts=8 sw=8 noet: */
950
/*
951
 * Local Variables:
952
 * mode: c
953
 * c-basic-offset: 8
954
 * fill-column: 78
955
 * End:
956
 */