Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/tifvsi.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GeoTIFF Driver
4
 * Purpose:  Implement system hook functions for libtiff on top of CPL/VSI,
5
 *           including > 2GB support.  Based on tif_unix.c from libtiff
6
 *           distribution.
7
 * Author:   Frank Warmerdam, warmerdam@pobox.com
8
 *
9
 ******************************************************************************
10
 * Copyright (c) 2005, Frank Warmerdam, warmerdam@pobox.com
11
 * Copyright (c) 2010-2012, Even Rouault <even dot rouault at spatialys.com>
12
 *
13
 * SPDX-License-Identifier: MIT
14
 ****************************************************************************/
15
16
// TIFF Library UNIX-specific Routines.
17
18
#include "cpl_port.h"
19
#include "tifvsi.h"
20
21
#include <assert.h>
22
#include <string.h>
23
#include <cerrno>
24
25
#include "cpl_conv.h"
26
#include "cpl_vsi.h"
27
#include "cpl_string.h"
28
29
// We avoid including xtiffio.h since it drags in the libgeotiff version
30
// of the VSI functions.
31
32
#ifdef RENAME_INTERNAL_LIBGEOTIFF_SYMBOLS
33
#include "gdal_libgeotiff_symbol_rename.h"
34
#endif
35
36
#include "xtiffio.h"
37
38
#include <limits>
39
40
#if (TIFFLIB_VERSION > 20220520) || defined(INTERNAL_LIBTIFF)  // > 4.4.0
41
#define SUPPORTS_LIBTIFF_OPEN_OPTIONS
42
43
extern int GTiffWarningHandlerExt(TIFF *tif, void *user_data,
44
                                  const char *module, const char *fmt,
45
                                  va_list ap);
46
extern int GTiffErrorHandlerExt(TIFF *tif, void *user_data, const char *module,
47
                                const char *fmt, va_list ap);
48
49
#endif
50
51
constexpr int BUFFER_SIZE = 65536;
52
53
struct GDALTiffHandle;
54
55
struct GDALTiffHandleShared
56
{
57
    VSILFILE *fpL;
58
    bool bReadOnly;
59
    bool bLazyStrileLoading;
60
    char *pszName;
61
    GDALTiffHandle *psActiveHandle;  // only used on the parent
62
    int nUserCounter;
63
    bool bAtEndOfFile;
64
    vsi_l_offset nFileLength;
65
};
66
67
struct GDALTiffHandle
68
{
69
    bool bFree;
70
71
    GDALTiffHandle *psParent;  // nullptr for the parent itself
72
    GDALTiffHandleShared *psShared;
73
74
    GByte *abyWriteBuffer;
75
    int nWriteBufferSize;
76
77
    // For pseudo-mmap'ed /vsimem/ file
78
    vsi_l_offset nDataLength;
79
    void *pBase;
80
81
    // If we pre-cached data (typically from /vsicurl/ )
82
    int nCachedRanges;
83
    void **ppCachedData;
84
    vsi_l_offset *panCachedOffsets;
85
    size_t *panCachedSizes;
86
};
87
88
static bool GTHFlushBuffer(thandle_t th);
89
90
static void SetActiveGTH(GDALTiffHandle *psGTH)
91
0
{
92
0
    auto psShared = psGTH->psShared;
93
0
    if (psShared->psActiveHandle != psGTH)
94
0
    {
95
0
        if (psShared->psActiveHandle != nullptr)
96
0
        {
97
0
            GTHFlushBuffer(static_cast<thandle_t>(psShared->psActiveHandle));
98
0
        }
99
0
        psShared->psActiveHandle = psGTH;
100
0
    }
101
0
}
102
103
void *VSI_TIFFGetCachedRange(thandle_t th, vsi_l_offset nOffset, size_t nSize)
104
0
{
105
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
106
0
    for (int i = 0; i < psGTH->nCachedRanges; i++)
107
0
    {
108
0
        if (nOffset >= psGTH->panCachedOffsets[i] &&
109
0
            nOffset + nSize <=
110
0
                psGTH->panCachedOffsets[i] + psGTH->panCachedSizes[i])
111
0
        {
112
0
            return static_cast<GByte *>(psGTH->ppCachedData[i]) +
113
0
                   (nOffset - psGTH->panCachedOffsets[i]);
114
0
        }
115
0
        if (nOffset < psGTH->panCachedOffsets[i])
116
0
            break;
117
0
    }
118
0
    return nullptr;
119
0
}
120
121
static tsize_t _tiffReadProc(thandle_t th, tdata_t buf, tsize_t size)
122
0
{
123
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
124
    // SetActiveGTH(psGTH);
125
126
0
    if (psGTH->nCachedRanges)
127
0
    {
128
0
        const vsi_l_offset nCurOffset = VSIFTellL(psGTH->psShared->fpL);
129
0
        void *data =
130
0
            VSI_TIFFGetCachedRange(th, nCurOffset, static_cast<size_t>(size));
131
0
        if (data)
132
0
        {
133
0
            memcpy(buf, data, size);
134
0
            VSIFSeekL(psGTH->psShared->fpL, nCurOffset + size, SEEK_SET);
135
0
            return size;
136
0
        }
137
0
    }
138
139
#ifdef DEBUG_VERBOSE_EXTRA
140
    CPLDebug("GTiff", "Reading %d bytes at offset " CPL_FRMT_GUIB,
141
             static_cast<int>(size), VSIFTellL(psGTH->psShared->fpL));
142
#endif
143
0
    return VSIFReadL(buf, 1, size, psGTH->psShared->fpL);
144
0
}
145
146
static bool GTHFlushBuffer(thandle_t th)
147
0
{
148
0
    GDALTiffHandle *psGTH = static_cast<GDALTiffHandle *>(th);
149
0
    bool bRet = true;
150
0
    if (psGTH->abyWriteBuffer && psGTH->nWriteBufferSize)
151
0
    {
152
0
        const tsize_t nRet =
153
0
            VSIFWriteL(psGTH->abyWriteBuffer, 1, psGTH->nWriteBufferSize,
154
0
                       psGTH->psShared->fpL);
155
0
        bRet = nRet == psGTH->nWriteBufferSize;
156
0
        if (!bRet)
157
0
        {
158
0
            TIFFErrorExt(th, "_tiffWriteProc", "%s", VSIStrerror(errno));
159
0
        }
160
0
        psGTH->nWriteBufferSize = 0;
161
0
    }
162
0
    return bRet;
163
0
}
164
165
static tsize_t _tiffWriteProc(thandle_t th, tdata_t buf, tsize_t size)
166
0
{
167
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
168
0
    SetActiveGTH(psGTH);
169
170
    // If we have a write buffer and are at end of file, then accumulate
171
    // the bytes until the buffer is full.
172
0
    if (psGTH->psShared->bAtEndOfFile && psGTH->abyWriteBuffer)
173
0
    {
174
0
        const GByte *pabyData = reinterpret_cast<GByte *>(buf);
175
0
        tsize_t nRemainingBytes = size;
176
0
        while (true)
177
0
        {
178
0
            if (psGTH->nWriteBufferSize + nRemainingBytes <= BUFFER_SIZE)
179
0
            {
180
0
                memcpy(psGTH->abyWriteBuffer + psGTH->nWriteBufferSize,
181
0
                       pabyData, nRemainingBytes);
182
0
                psGTH->nWriteBufferSize += static_cast<int>(nRemainingBytes);
183
0
                if (psGTH->psShared->bAtEndOfFile)
184
0
                {
185
0
                    psGTH->psShared->nFileLength += size;
186
0
                }
187
0
                return size;
188
0
            }
189
190
0
            int nAppendable = BUFFER_SIZE - psGTH->nWriteBufferSize;
191
0
            memcpy(psGTH->abyWriteBuffer + psGTH->nWriteBufferSize, pabyData,
192
0
                   nAppendable);
193
0
            const tsize_t nRet = VSIFWriteL(psGTH->abyWriteBuffer, 1,
194
0
                                            BUFFER_SIZE, psGTH->psShared->fpL);
195
0
            psGTH->nWriteBufferSize = 0;
196
0
            if (nRet != BUFFER_SIZE)
197
0
            {
198
0
                TIFFErrorExt(th, "_tiffWriteProc", "%s", VSIStrerror(errno));
199
0
                return 0;
200
0
            }
201
202
0
            pabyData += nAppendable;
203
0
            nRemainingBytes -= nAppendable;
204
0
        }
205
0
    }
206
207
0
    const tsize_t nRet = VSIFWriteL(buf, 1, size, psGTH->psShared->fpL);
208
0
    if (nRet < size)
209
0
    {
210
0
        TIFFErrorExt(th, "_tiffWriteProc", "%s", VSIStrerror(errno));
211
0
    }
212
213
0
    if (psGTH->psShared->bAtEndOfFile)
214
0
    {
215
0
        psGTH->psShared->nFileLength += nRet;
216
0
    }
217
0
    return nRet;
218
0
}
219
220
static toff_t _tiffSeekProc(thandle_t th, toff_t off, int whence)
221
0
{
222
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
223
0
    SetActiveGTH(psGTH);
224
225
    // Optimization: if we are already at end, then no need to
226
    // issue a VSIFSeekL().
227
0
    if (whence == SEEK_END)
228
0
    {
229
0
        if (psGTH->psShared->bAtEndOfFile)
230
0
        {
231
0
            return static_cast<toff_t>(psGTH->psShared->nFileLength);
232
0
        }
233
234
0
        if (VSIFSeekL(psGTH->psShared->fpL, static_cast<vsi_l_offset>(off),
235
0
                      whence) != 0)
236
0
        {
237
0
            TIFFErrorExt(th, "_tiffSeekProc", "%s", VSIStrerror(errno));
238
0
            return static_cast<toff_t>(-1);
239
0
        }
240
0
        psGTH->psShared->bAtEndOfFile = true;
241
0
        psGTH->psShared->nFileLength = VSIFTellL(psGTH->psShared->fpL);
242
0
        return static_cast<toff_t>(psGTH->psShared->nFileLength);
243
0
    }
244
245
0
    GTHFlushBuffer(th);
246
0
    psGTH->psShared->bAtEndOfFile = false;
247
0
    psGTH->psShared->nFileLength = 0;
248
249
0
    if (VSIFSeekL(psGTH->psShared->fpL, static_cast<vsi_l_offset>(off),
250
0
                  whence) == 0)
251
0
    {
252
0
        return static_cast<toff_t>(VSIFTellL(psGTH->psShared->fpL));
253
0
    }
254
0
    else
255
0
    {
256
0
        TIFFErrorExt(th, "_tiffSeekProc", "%s", VSIStrerror(errno));
257
0
        return static_cast<toff_t>(-1);
258
0
    }
259
0
}
260
261
static void FreeGTH(GDALTiffHandle *psGTH)
262
0
{
263
0
    psGTH->psShared->nUserCounter--;
264
0
    if (psGTH->psParent == nullptr)
265
0
    {
266
0
        assert(psGTH->psShared->nUserCounter == 0);
267
0
        CPLFree(psGTH->psShared->pszName);
268
0
        CPLFree(psGTH->psShared);
269
0
    }
270
0
    else
271
0
    {
272
0
        if (psGTH->psShared->psActiveHandle == psGTH)
273
0
            psGTH->psShared->psActiveHandle = nullptr;
274
0
    }
275
0
    CPLFree(psGTH->abyWriteBuffer);
276
0
    CPLFree(psGTH->ppCachedData);
277
0
    CPLFree(psGTH->panCachedOffsets);
278
0
    CPLFree(psGTH->panCachedSizes);
279
0
    CPLFree(psGTH);
280
0
}
281
282
static int _tiffCloseProc(thandle_t th)
283
0
{
284
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
285
0
    SetActiveGTH(psGTH);
286
0
    GTHFlushBuffer(th);
287
0
    if (psGTH->bFree)
288
0
        FreeGTH(psGTH);
289
0
    return 0;
290
0
}
291
292
static toff_t _tiffSizeProc(thandle_t th)
293
0
{
294
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
295
0
    SetActiveGTH(psGTH);
296
297
0
    if (psGTH->psShared->bAtEndOfFile)
298
0
    {
299
0
        return static_cast<toff_t>(psGTH->psShared->nFileLength);
300
0
    }
301
302
0
    const vsi_l_offset old_off = VSIFTellL(psGTH->psShared->fpL);
303
0
    CPL_IGNORE_RET_VAL(VSIFSeekL(psGTH->psShared->fpL, 0, SEEK_END));
304
305
0
    const toff_t file_size =
306
0
        static_cast<toff_t>(VSIFTellL(psGTH->psShared->fpL));
307
0
    CPL_IGNORE_RET_VAL(VSIFSeekL(psGTH->psShared->fpL, old_off, SEEK_SET));
308
309
0
    return file_size;
310
0
}
311
312
static int _tiffMapProc(thandle_t th, tdata_t *pbase, toff_t *psize)
313
0
{
314
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
315
    // SetActiveGTH(psGTH);
316
317
0
    if (psGTH->pBase)
318
0
    {
319
0
        *pbase = psGTH->pBase;
320
0
        *psize = static_cast<toff_t>(psGTH->nDataLength);
321
0
        return 1;
322
0
    }
323
0
    return 0;
324
0
}
325
326
static void _tiffUnmapProc(thandle_t /* th */, tdata_t /* base */,
327
                           toff_t /* size */)
328
0
{
329
0
}
330
331
VSILFILE *VSI_TIFFGetVSILFile(thandle_t th)
332
0
{
333
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
334
0
    SetActiveGTH(psGTH);
335
0
    VSI_TIFFFlushBufferedWrite(th);
336
0
    return psGTH->psShared->fpL;
337
0
}
338
339
int VSI_TIFFFlushBufferedWrite(thandle_t th)
340
0
{
341
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
342
0
    SetActiveGTH(psGTH);
343
0
    psGTH->psShared->bAtEndOfFile = false;
344
0
    return GTHFlushBuffer(th);
345
0
}
346
347
int VSI_TIFFHasCachedRanges(thandle_t th)
348
0
{
349
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
350
0
    return psGTH->nCachedRanges != 0;
351
0
}
352
353
toff_t VSI_TIFFSeek(TIFF *tif, toff_t off, int whence)
354
0
{
355
0
    thandle_t th = TIFFClientdata(tif);
356
0
    return _tiffSeekProc(th, off, whence);
357
0
}
358
359
int VSI_TIFFWrite(TIFF *tif, const void *buffer, size_t buffersize)
360
0
{
361
0
    thandle_t th = TIFFClientdata(tif);
362
0
    return static_cast<size_t>(_tiffWriteProc(th, const_cast<tdata_t>(buffer),
363
0
                                              buffersize)) == buffersize;
364
0
}
365
366
void VSI_TIFFSetCachedRanges(thandle_t th, int nRanges, void **ppData,
367
                             const vsi_l_offset *panOffsets,
368
                             const size_t *panSizes)
369
0
{
370
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
371
0
    psGTH->nCachedRanges = nRanges;
372
0
    if (nRanges)
373
0
    {
374
0
        psGTH->ppCachedData = static_cast<void **>(
375
0
            CPLRealloc(psGTH->ppCachedData, nRanges * sizeof(void *)));
376
0
        memcpy(psGTH->ppCachedData, ppData, nRanges * sizeof(void *));
377
378
0
        psGTH->panCachedOffsets = static_cast<vsi_l_offset *>(CPLRealloc(
379
0
            psGTH->panCachedOffsets, nRanges * sizeof(vsi_l_offset)));
380
0
        memcpy(psGTH->panCachedOffsets, panOffsets,
381
0
               nRanges * sizeof(vsi_l_offset));
382
383
0
        psGTH->panCachedSizes = static_cast<size_t *>(
384
0
            CPLRealloc(psGTH->panCachedSizes, nRanges * sizeof(size_t)));
385
0
        memcpy(psGTH->panCachedSizes, panSizes, nRanges * sizeof(size_t));
386
0
    }
387
0
}
388
389
static bool IsReadOnly(const char *mode)
390
0
{
391
0
    bool bReadOnly = true;
392
0
    for (int i = 0; mode[i] != '\0'; i++)
393
0
    {
394
0
        if (mode[i] == 'w' || mode[i] == '+' || mode[i] == 'a')
395
0
        {
396
0
            bReadOnly = false;
397
0
        }
398
0
    }
399
0
    return bReadOnly;
400
0
}
401
402
static void InitializeWriteBuffer(GDALTiffHandle *psGTH, const char *pszMode)
403
0
{
404
    // No need to buffer on /vsimem/
405
0
    const bool bReadOnly = IsReadOnly(pszMode);
406
0
    bool bAllocBuffer = !bReadOnly;
407
0
    if (STARTS_WITH(psGTH->psShared->pszName, "/vsimem/"))
408
0
    {
409
0
        if (bReadOnly &&
410
0
            CPLTestBool(CPLGetConfigOption("GTIFF_USE_MMAP", "NO")))
411
0
        {
412
0
            psGTH->nDataLength = 0;
413
0
            psGTH->pBase = VSIGetMemFileBuffer(psGTH->psShared->pszName,
414
0
                                               &psGTH->nDataLength, FALSE);
415
0
        }
416
0
        bAllocBuffer = false;
417
0
    }
418
419
0
    psGTH->abyWriteBuffer =
420
0
        bAllocBuffer ? static_cast<GByte *>(VSIMalloc(BUFFER_SIZE)) : nullptr;
421
0
    psGTH->nWriteBufferSize = 0;
422
0
}
423
424
#ifdef SUPPORTS_LIBTIFF_OPEN_OPTIONS
425
static void VSI_TIFFSetOpenOptions(TIFFOpenOptions *opts)
426
0
{
427
0
    TIFFOpenOptionsSetErrorHandlerExtR(opts, GTiffErrorHandlerExt, nullptr);
428
0
    TIFFOpenOptionsSetWarningHandlerExtR(opts, GTiffWarningHandlerExt, nullptr);
429
0
#if defined(INTERNAL_LIBTIFF) || TIFFLIB_VERSION > 20230908
430
    // Read-once and stored in static storage otherwise affects
431
    // autotest/benchmark/test_gtiff.py::test_gtiff_byte
432
0
    static const GIntBig nMemLimit = []() -> GIntBig
433
0
    {
434
0
        if (const char *pszLimit =
435
0
                CPLGetConfigOption("GTIFF_MAX_CUMULATED_MEM_USAGE", nullptr))
436
0
            return CPLAtoGIntBig(pszLimit);
437
0
        else
438
0
        {
439
0
            const auto nUsableRAM = CPLGetUsablePhysicalRAM();
440
0
            if (nUsableRAM > 0)
441
0
            {
442
                // coverity[return_overflow]
443
0
                return nUsableRAM / 10 * 9;
444
0
            }
445
0
            else
446
0
                return 0;
447
0
        }
448
0
    }();
449
0
    if (nMemLimit > 0 && nMemLimit < std::numeric_limits<tmsize_t>::max())
450
0
    {
451
        //CPLDebug("GTiff", "TIFFOpenOptionsSetMaxCumulatedMemAlloc(%" PRIu64 ")",
452
        //         static_cast<uint64_t>(nMemLimit));
453
0
        TIFFOpenOptionsSetMaxCumulatedMemAlloc(
454
0
            opts, static_cast<tmsize_t>(nMemLimit));
455
0
    }
456
0
#endif
457
0
}
458
#endif
459
460
static TIFF *VSI_TIFFOpen_common(GDALTiffHandle *psGTH, const char *pszMode)
461
0
{
462
0
    InitializeWriteBuffer(psGTH, pszMode);
463
464
0
#ifdef SUPPORTS_LIBTIFF_OPEN_OPTIONS
465
0
    XTIFFInitialize();
466
0
    TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
467
0
    if (opts == nullptr)
468
0
    {
469
0
        FreeGTH(psGTH);
470
0
        return nullptr;
471
0
    }
472
0
    VSI_TIFFSetOpenOptions(opts);
473
0
    TIFF *tif = TIFFClientOpenExt(
474
0
        psGTH->psShared->pszName, pszMode, reinterpret_cast<thandle_t>(psGTH),
475
0
        _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
476
0
        _tiffSizeProc, _tiffMapProc, _tiffUnmapProc, opts);
477
0
    TIFFOpenOptionsFree(opts);
478
#else
479
    TIFF *tif = XTIFFClientOpen(
480
        psGTH->psShared->pszName, pszMode, reinterpret_cast<thandle_t>(psGTH),
481
        _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
482
        _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
483
#endif
484
0
    if (tif == nullptr)
485
0
        FreeGTH(psGTH);
486
487
0
    return tif;
488
0
}
489
490
// Open a TIFF file for read/writing.
491
TIFF *VSI_TIFFOpen(const char *name, const char *mode, VSILFILE *fpL)
492
0
{
493
494
0
    if (VSIFSeekL(fpL, 0, SEEK_SET) < 0)
495
0
        return nullptr;
496
497
0
    GDALTiffHandle *psGTH =
498
0
        static_cast<GDALTiffHandle *>(CPLCalloc(1, sizeof(GDALTiffHandle)));
499
0
    psGTH->bFree = true;
500
0
    psGTH->psParent = nullptr;
501
0
    psGTH->psShared = static_cast<GDALTiffHandleShared *>(
502
0
        CPLCalloc(1, sizeof(GDALTiffHandleShared)));
503
0
    psGTH->psShared->bReadOnly = (strchr(mode, '+') == nullptr);
504
0
    psGTH->psShared->bLazyStrileLoading = (strchr(mode, 'D') != nullptr);
505
0
    psGTH->psShared->pszName = CPLStrdup(name);
506
0
    psGTH->psShared->fpL = fpL;
507
0
    psGTH->psShared->psActiveHandle = psGTH;
508
0
    psGTH->psShared->nFileLength = 0;
509
0
    psGTH->psShared->bAtEndOfFile = false;
510
0
    psGTH->psShared->nUserCounter = 1;
511
512
0
    return VSI_TIFFOpen_common(psGTH, mode);
513
0
}
514
515
TIFF *VSI_TIFFOpenChild(TIFF *parent)
516
0
{
517
0
    GDALTiffHandle *psGTHParent =
518
0
        reinterpret_cast<GDALTiffHandle *>(TIFFClientdata(parent));
519
520
0
    GDALTiffHandle *psGTH =
521
0
        static_cast<GDALTiffHandle *>(CPLCalloc(1, sizeof(GDALTiffHandle)));
522
0
    psGTH->bFree = true;
523
0
    psGTH->psParent = psGTHParent;
524
0
    psGTH->psShared = psGTHParent->psShared;
525
0
    psGTH->psShared->nUserCounter++;
526
527
0
    SetActiveGTH(psGTH);
528
0
    VSIFSeekL(psGTH->psShared->fpL, 0, SEEK_SET);
529
0
    psGTH->psShared->bAtEndOfFile = false;
530
531
0
    const char *mode =
532
0
        psGTH->psShared->bReadOnly && psGTH->psShared->bLazyStrileLoading
533
0
            ? "rDO"
534
0
        : psGTH->psShared->bReadOnly          ? "r"
535
0
        : psGTH->psShared->bLazyStrileLoading ? "r+D"
536
0
                                              : "r+";
537
0
    return VSI_TIFFOpen_common(psGTH, mode);
538
0
}
539
540
// Re-open a TIFF handle (seeking to the appropriate directory is then needed)
541
TIFF *VSI_TIFFReOpen(TIFF *tif)
542
0
{
543
0
    thandle_t th = TIFFClientdata(tif);
544
0
    GDALTiffHandle *psGTH = reinterpret_cast<GDALTiffHandle *>(th);
545
546
    // Disable freeing of psGTH in _tiffCloseProc(), which could be called
547
    // if XTIFFClientOpen() fails, or obviously by XTIFFClose()
548
0
    psGTH->bFree = false;
549
550
0
    const char *mode =
551
0
        psGTH->psShared->bReadOnly && psGTH->psShared->bLazyStrileLoading
552
0
            ? "rDO"
553
0
        : psGTH->psShared->bReadOnly          ? "r"
554
0
        : psGTH->psShared->bLazyStrileLoading ? "r+D"
555
0
                                              : "r+";
556
557
0
    SetActiveGTH(psGTH);
558
0
    VSIFSeekL(psGTH->psShared->fpL, 0, SEEK_SET);
559
0
    psGTH->psShared->bAtEndOfFile = false;
560
561
0
#ifdef SUPPORTS_LIBTIFF_OPEN_OPTIONS
562
0
    TIFF *newHandle = nullptr;
563
0
    TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
564
0
    if (opts != nullptr)
565
0
    {
566
0
        VSI_TIFFSetOpenOptions(opts);
567
0
        newHandle = TIFFClientOpenExt(
568
0
            psGTH->psShared->pszName, mode, reinterpret_cast<thandle_t>(psGTH),
569
0
            _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
570
0
            _tiffSizeProc, _tiffMapProc, _tiffUnmapProc, opts);
571
0
        TIFFOpenOptionsFree(opts);
572
0
    }
573
#else
574
    TIFF *newHandle = XTIFFClientOpen(
575
        psGTH->psShared->pszName, mode, reinterpret_cast<thandle_t>(psGTH),
576
        _tiffReadProc, _tiffWriteProc, _tiffSeekProc, _tiffCloseProc,
577
        _tiffSizeProc, _tiffMapProc, _tiffUnmapProc);
578
#endif
579
0
    if (newHandle != nullptr)
580
0
        XTIFFClose(tif);
581
582
0
    psGTH->bFree = true;
583
584
0
    return newHandle;
585
0
}