Line data Source code
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 : /**
34 : * @file tif_open.c
35 : * @brief Initialise handles and open options, with shared allocation helpers.
36 : */
37 :
38 : #include "tiffiop.h"
39 : #include <assert.h>
40 : #include <limits.h>
41 :
42 : /*
43 : * Dummy functions to fill the omitted client procedures.
44 : */
45 0 : int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize)
46 : {
47 : (void)fd;
48 : (void)pbase;
49 : (void)psize;
50 0 : return (0);
51 : }
52 :
53 0 : void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
54 : {
55 : (void)fd;
56 : (void)base;
57 : (void)size;
58 0 : }
59 :
60 72329 : int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode,
61 : const char *module)
62 : {
63 72329 : int m = -1;
64 :
65 72329 : switch (mode[0])
66 : {
67 31435 : case 'r':
68 31435 : m = O_RDONLY;
69 31435 : if (mode[1] == '+')
70 3992 : m = O_RDWR;
71 31435 : break;
72 40825 : case 'w':
73 : case 'a':
74 40825 : m = O_RDWR | O_CREAT;
75 40825 : if (mode[0] == 'w')
76 40825 : m |= O_TRUNC;
77 40825 : break;
78 69 : default:
79 69 : _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
80 0 : break;
81 : }
82 72260 : return (m);
83 : }
84 :
85 72428 : TIFFOpenOptions *TIFFOpenOptionsAlloc(void)
86 : {
87 : TIFFOpenOptions *opts =
88 72428 : (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions));
89 72402 : return opts;
90 : }
91 :
92 72308 : void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); }
93 :
94 0 : void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts,
95 : tmsize_t max_single_mem_alloc)
96 : {
97 0 : opts->max_single_mem_alloc = max_single_mem_alloc;
98 0 : }
99 :
100 72364 : void TIFFOpenOptionsSetMaxCumulatedMemAlloc(TIFFOpenOptions *opts,
101 : tmsize_t max_cumulated_mem_alloc)
102 : {
103 72364 : opts->max_cumulated_mem_alloc = max_cumulated_mem_alloc;
104 72364 : }
105 :
106 0 : void TIFFOpenOptionsSetWarnAboutUnknownTags(TIFFOpenOptions *opts,
107 : int warn_about_unknown_tags)
108 : {
109 0 : opts->warn_about_unknown_tags = warn_about_unknown_tags;
110 0 : }
111 :
112 72382 : void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts,
113 : TIFFErrorHandlerExtR handler,
114 : void *errorhandler_user_data)
115 : {
116 72382 : opts->errorhandler = handler;
117 72382 : opts->errorhandler_user_data = errorhandler_user_data;
118 72382 : }
119 :
120 72407 : void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts,
121 : TIFFErrorHandlerExtR handler,
122 : void *warnhandler_user_data)
123 : {
124 72407 : opts->warnhandler = handler;
125 72407 : opts->warnhandler_user_data = warnhandler_user_data;
126 72407 : }
127 :
128 : /**
129 : * @brief Report a rejected single allocation-policy check.
130 : * @ingroup libtiff_private_memory
131 : *
132 : * Formats the stored limit and size values through TIFFErrorExtR(). Does not
133 : * allocate, free, update accounting, or decide the caller's return status.
134 : * Diagnostic callbacks may have their normal side effects.
135 : *
136 : * @param[in] tif Non-NULL handle whose tif_max_single_mem_alloc is reported.
137 : * @param[in] pszFunction Borrowed diagnostic module name.
138 : * @param[in] s Requested payload bytes that exceeded the single-allocation
139 : * limit.
140 : */
141 0 : static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif,
142 : const char *pszFunction,
143 : tmsize_t s)
144 : {
145 0 : TIFFErrorExtR(tif, pszFunction,
146 : "Memory allocation of %" PRIu64
147 : " bytes is beyond the %" PRIu64
148 : " byte limit defined in open options",
149 0 : (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc);
150 0 : }
151 :
152 : /**
153 : * @brief Report a rejected cumulative allocation-policy check.
154 : * @ingroup libtiff_private_memory
155 : *
156 : * Formats the stored limit and size values through TIFFErrorExtR(). Does not
157 : * allocate, free, update accounting, or decide the caller's return status.
158 : * Diagnostic callbacks may have their normal side effects.
159 : *
160 : * @param[in] tif Non-NULL handle whose current cumulated total and
161 : * cumulated limit are reported.
162 : * @param[in] pszFunction Borrowed diagnostic module name.
163 : * @param[in] s Requested payload bytes, or additional payload growth for
164 : * cumulative realloc reporting.
165 : */
166 0 : static void _TIFFEmitErrorAboveMaxCumulatedMemAlloc(TIFF *tif,
167 : const char *pszFunction,
168 : tmsize_t s)
169 : {
170 0 : TIFFErrorExtR(tif, pszFunction,
171 : "Cumulated memory allocation of %" PRIu64 " + %" PRIu64
172 : " bytes is beyond the %" PRIu64
173 : " cumulated byte limit defined in open options",
174 0 : (uint64_t)tif->tif_cur_cumulated_mem_alloc, (uint64_t)s,
175 0 : (uint64_t)tif->tif_max_cumulated_mem_alloc);
176 0 : }
177 :
178 : /* When allocating memory, we write at the beginning of the buffer it size.
179 : * This allows us to keep track of the total memory allocated when we
180 : * malloc/calloc/realloc and free. In theory we need just SIZEOF_SIZE_T bytes
181 : * for that, but on x86_64, allocations of more than 16 bytes are aligned on
182 : * 16 bytes. Hence using 2 * SIZEOF_SIZE_T.
183 : * It is critical that _TIFFmallocExt/_TIFFcallocExt/_TIFFreallocExt are
184 : * paired with _TIFFfreeExt.
185 : * CMakeLists.txt defines TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS, which in
186 : * turn disables the definition of the non Ext version in tiffio.h
187 : */
188 : /** @brief Reserve two size_t widths before a cumulatively tracked payload.
189 : * @ingroup libtiff_private_memory
190 : *
191 : * Private accounting/alignment prefix; callers must not access or infer it.
192 : */
193 : #define LEADING_AREA_TO_STORE_ALLOC_SIZE (2 * SIZEOF_SIZE_T)
194 :
195 738096 : void *_TIFFmallocExt(TIFF *tif, tmsize_t s)
196 : {
197 738096 : if (s <= 0)
198 0 : return NULL;
199 738096 : if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
200 0 : s > tif->tif_max_single_mem_alloc)
201 : {
202 0 : _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
203 0 : return NULL;
204 : }
205 738096 : if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
206 : {
207 663476 : if (s > tif->tif_max_cumulated_mem_alloc -
208 663476 : tif->tif_cur_cumulated_mem_alloc ||
209 663891 : s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
210 : {
211 0 : _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s);
212 0 : return NULL;
213 : }
214 663555 : void *ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
215 664669 : if (!ptr)
216 0 : return NULL;
217 664669 : tif->tif_cur_cumulated_mem_alloc += s;
218 664669 : memcpy(ptr, &s, sizeof(s));
219 664669 : return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
220 : }
221 74620 : return _TIFFmalloc(s);
222 : }
223 :
224 155075 : void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
225 : {
226 155075 : if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
227 232 : return NULL;
228 154843 : if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
229 : {
230 0 : if (nmemb * siz > tif->tif_max_single_mem_alloc)
231 : {
232 0 : _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
233 : nmemb * siz);
234 0 : return NULL;
235 : }
236 : }
237 154843 : if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
238 : {
239 154857 : const tmsize_t s = nmemb * siz;
240 154857 : if (s > tif->tif_max_cumulated_mem_alloc -
241 154857 : tif->tif_cur_cumulated_mem_alloc ||
242 155051 : s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)
243 : {
244 98 : _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s);
245 0 : return NULL;
246 : }
247 154759 : void *ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1);
248 154930 : if (!ptr)
249 0 : return NULL;
250 154930 : tif->tif_cur_cumulated_mem_alloc += s;
251 154930 : memcpy(ptr, &s, sizeof(s));
252 154930 : return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
253 : }
254 0 : return _TIFFcalloc(nmemb, siz);
255 : }
256 :
257 2269700 : void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
258 : {
259 2269700 : if (s <= 0)
260 : {
261 0 : _TIFFfreeExt(tif, p);
262 0 : return NULL;
263 : }
264 2269700 : if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
265 0 : s > tif->tif_max_single_mem_alloc)
266 : {
267 0 : _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
268 0 : return NULL;
269 : }
270 2269700 : if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
271 : {
272 2187260 : void *oldPtr = p;
273 2187260 : tmsize_t oldSize = 0;
274 2187260 : if (p)
275 : {
276 584647 : oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
277 584647 : memcpy(&oldSize, oldPtr, sizeof(oldSize));
278 584647 : assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
279 : }
280 2187260 : if (s > oldSize &&
281 2185360 : (s > tif->tif_max_cumulated_mem_alloc -
282 2185360 : (tif->tif_cur_cumulated_mem_alloc - oldSize) ||
283 2186220 : s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE))
284 : {
285 0 : _TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt",
286 : s - oldSize);
287 0 : return NULL;
288 : }
289 : void *newPtr =
290 2188120 : _TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s);
291 2189520 : if (newPtr == NULL)
292 0 : return NULL;
293 2189520 : tif->tif_cur_cumulated_mem_alloc -= oldSize;
294 2189520 : tif->tif_cur_cumulated_mem_alloc += s;
295 2189520 : memcpy(newPtr, &s, sizeof(s));
296 2189520 : return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE;
297 : }
298 82441 : return _TIFFrealloc(p, s);
299 : }
300 :
301 2550480 : void _TIFFfreeExt(TIFF *tif, void *p)
302 : {
303 2550480 : if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0)
304 : {
305 2424620 : void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE;
306 : tmsize_t oldSize;
307 2424620 : memcpy(&oldSize, oldPtr, sizeof(oldSize));
308 2424620 : assert(oldSize <= tif->tif_cur_cumulated_mem_alloc);
309 2424620 : tif->tif_cur_cumulated_mem_alloc -= oldSize;
310 2424620 : p = oldPtr;
311 : }
312 2550480 : _TIFFfree(p);
313 2550170 : }
314 :
315 0 : TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata,
316 : TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc,
317 : TIFFSeekProc seekproc, TIFFCloseProc closeproc,
318 : TIFFSizeProc sizeproc, TIFFMapFileProc mapproc,
319 : TIFFUnmapFileProc unmapproc)
320 : {
321 0 : return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
322 : seekproc, closeproc, sizeproc, mapproc, unmapproc,
323 : NULL);
324 : }
325 :
326 72408 : TIFF *TIFFClientOpenExt(const char *name, const char *mode,
327 : thandle_t clientdata, TIFFReadWriteProc readproc,
328 : TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
329 : TIFFCloseProc closeproc, TIFFSizeProc sizeproc,
330 : TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc,
331 : TIFFOpenOptions *opts)
332 : {
333 : static const char module[] = "TIFFClientOpenExt";
334 : TIFF *tif;
335 : int m;
336 : const char *cp;
337 : size_t name_len;
338 : tmsize_t size_to_alloc;
339 :
340 : /* The following are configuration checks. They should be redundant, but
341 : * should not compile to any actual code in an optimised release build
342 : * anyway. If any of them fail, (makefile-based or other) configuration is
343 : * not correct */
344 : assert(sizeof(uint8_t) == 1);
345 : assert(sizeof(int8_t) == 1);
346 : assert(sizeof(uint16_t) == 2);
347 : assert(sizeof(int16_t) == 2);
348 : assert(sizeof(uint32_t) == 4);
349 : assert(sizeof(int32_t) == 4);
350 : assert(sizeof(uint64_t) == 8);
351 : assert(sizeof(int64_t) == 8);
352 : {
353 : union
354 : {
355 : uint8_t a8[2];
356 : uint16_t a16;
357 : } n;
358 72408 : n.a8[0] = 1;
359 72408 : n.a8[1] = 0;
360 : (void)n;
361 : #if WORDS_BIGENDIAN
362 : assert(n.a16 == 256);
363 : #else
364 72408 : assert(n.a16 == 1);
365 : #endif
366 : }
367 :
368 72408 : m = _TIFFgetMode(opts, clientdata, mode, module);
369 72319 : if (m == -1)
370 0 : goto bad2;
371 : /* Keep the length used for the allocation and the one used for the copy
372 : * in a single variable, so they cannot drift apart. */
373 72319 : name_len = strlen(name) + 1;
374 72319 : size_to_alloc = (tmsize_t)(sizeof(TIFF) + name_len);
375 72319 : if (opts && opts->max_single_mem_alloc > 0 &&
376 0 : size_to_alloc > opts->max_single_mem_alloc)
377 : {
378 0 : _TIFFErrorEarly(opts, clientdata, module,
379 : "%s: Memory allocation of %" PRIu64
380 : " bytes is beyond the %" PRIu64
381 : " byte limit defined in open options",
382 : name, (uint64_t)size_to_alloc,
383 0 : (uint64_t)opts->max_single_mem_alloc);
384 0 : goto bad2;
385 : }
386 72319 : if (opts && opts->max_cumulated_mem_alloc > 0 &&
387 72332 : size_to_alloc > opts->max_cumulated_mem_alloc)
388 : {
389 0 : _TIFFErrorEarly(opts, clientdata, module,
390 : "%s: Memory allocation of %" PRIu64
391 : " bytes is beyond the %" PRIu64
392 : " cumulated byte limit defined in open options",
393 : name, (uint64_t)size_to_alloc,
394 0 : (uint64_t)opts->max_cumulated_mem_alloc);
395 0 : goto bad2;
396 : }
397 72319 : tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
398 72339 : if (tif == NULL)
399 : {
400 0 : _TIFFErrorEarly(opts, clientdata, module,
401 : "%s: Out of memory (TIFF structure)", name);
402 0 : goto bad2;
403 : }
404 72339 : _TIFFmemset(tif, 0, sizeof(*tif));
405 72285 : tif->tif_name = (char *)tif + sizeof(TIFF);
406 72285 : _TIFFmemcpy(tif->tif_name, name, (tmsize_t)name_len);
407 72346 : tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
408 72346 : tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */
409 72346 : tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER;
410 72346 : tif->tif_curoff = 0;
411 72346 : tif->tif_clientdata = clientdata;
412 72346 : tif->tif_readproc = readproc;
413 72346 : tif->tif_writeproc = writeproc;
414 72346 : tif->tif_seekproc = seekproc;
415 72346 : tif->tif_closeproc = closeproc;
416 72346 : tif->tif_sizeproc = sizeproc;
417 72346 : tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
418 72346 : tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
419 72346 : if (opts)
420 : {
421 72243 : tif->tif_errorhandler = opts->errorhandler;
422 72243 : tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
423 72243 : tif->tif_warnhandler = opts->warnhandler;
424 72243 : tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
425 72243 : tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
426 72243 : tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc;
427 72243 : tif->tif_warn_about_unknown_tags = opts->warn_about_unknown_tags;
428 : }
429 :
430 : /* Reset tif->tif_dir structure to zero and
431 : * initialize some IFD strile counter and index parameters. */
432 72346 : _TIFFResetTifDirAndInitStrileCounters(&tif->tif_dir);
433 :
434 72288 : if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
435 : {
436 127 : TIFFErrorExtR(tif, module,
437 : "One of the client procedures is NULL pointer.");
438 0 : _TIFFfreeExt(NULL, tif);
439 0 : goto bad2;
440 : }
441 :
442 72161 : _TIFFSetDefaultCompressionState(tif); /* setup default state */
443 : /*
444 : * Default is to return data MSB2LSB and enable the
445 : * use of memory-mapped files and strip chopping when
446 : * a file is opened read-only.
447 : */
448 72337 : tif->tif_flags = FILLORDER_MSB2LSB;
449 72337 : if (m == O_RDONLY)
450 27347 : tif->tif_flags |= TIFF_MAPPED;
451 :
452 : #ifdef STRIPCHOP_DEFAULT
453 72337 : if (m == O_RDONLY || m == O_RDWR)
454 31327 : tif->tif_flags |= STRIPCHOP_DEFAULT;
455 : #endif
456 :
457 : /*
458 : * Process library-specific flags in the open mode string.
459 : * The following flags may be used to control intrinsic library
460 : * behavior that may or may not be desirable (usually for
461 : * compatibility with some application that claims to support
462 : * TIFF but only supports some brain dead idea of what the
463 : * vendor thinks TIFF is):
464 : *
465 : * 'l' use little-endian byte order for creating a file
466 : * 'b' use big-endian byte order for creating a file
467 : * 'L' read/write information using LSB2MSB bit order
468 : * 'B' read/write information using MSB2LSB bit order
469 : * 'H' deprecated alias of 'B' (emits a warning)
470 : * 'M' enable use of memory-mapped files when supported
471 : * 'm' disable use of memory-mapped files
472 : * 'C' enable strip chopping support when reading
473 : * 'c' disable strip chopping support
474 : * 'h' read TIFF header only, do not load the first IFD
475 : * '4' ClassicTIFF for creating a file (default)
476 : * '8' BigTIFF for creating a file
477 : * 'D' enable use of deferred strip/tile offset/bytecount array loading.
478 : * 'O' on-demand loading of values instead of whole array loading (implies
479 : * D)
480 : *
481 : * The use of the 'l' and 'b' flags is strongly discouraged.
482 : * These flags are provided solely because numerous vendors,
483 : * typically on the PC, do not correctly support TIFF; they
484 : * only support the Intel little-endian byte order. This
485 : * support is not configured by default because it supports
486 : * the violation of the TIFF spec that says that readers *MUST*
487 : * support both byte orders. It is strongly recommended that
488 : * you not use this feature except to deal with busted apps
489 : * that write invalid TIFF. And even in those cases you should
490 : * bang on the vendors to fix their software.
491 : *
492 : * The 'L', 'B', and 'H' flags are intended for applications
493 : * that can optimize operations on data by using a particular
494 : * bit order. By default the library returns data in MSB2LSB
495 : * bit order for compatibility with older versions of this
496 : * library. Returning data in the bit order of the native CPU
497 : * makes the most sense but also requires applications to check
498 : * the value of the FillOrder tag; something they probably do
499 : * not do right now.
500 : *
501 : * The 'M' and 'm' flags are provided because some virtual memory
502 : * systems exhibit poor behavior when large images are mapped.
503 : * These options permit clients to control the use of memory-mapped
504 : * files on a per-file basis.
505 : *
506 : * The 'C' and 'c' flags are provided because the library support
507 : * for chopping up large strips into multiple smaller strips is not
508 : * application-transparent and as such can cause problems. The 'c'
509 : * option permits applications that only want to look at the tags,
510 : * for example, to get the unadulterated TIFF tag information.
511 : */
512 293587 : for (cp = mode; *cp; cp++)
513 221250 : switch (*cp)
514 : {
515 83 : case 'b':
516 : #if !WORDS_BIGENDIAN
517 83 : if (m & O_CREAT)
518 83 : tif->tif_flags |= TIFF_SWAB;
519 : #endif
520 83 : break;
521 30230 : case 'l':
522 : #if WORDS_BIGENDIAN
523 : if ((m & O_CREAT))
524 : tif->tif_flags |= TIFF_SWAB;
525 : #endif
526 30230 : break;
527 0 : case 'B':
528 0 : tif->tif_flags =
529 0 : (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
530 0 : break;
531 0 : case 'L':
532 0 : tif->tif_flags =
533 0 : (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
534 0 : break;
535 0 : case 'H':
536 0 : TIFFWarningExtR(tif, name,
537 : "H(ost) mode is deprecated. Since "
538 : "libtiff 4.5.1, it is an alias of 'B' / "
539 : "FILLORDER_MSB2LSB.");
540 0 : tif->tif_flags =
541 0 : (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
542 0 : break;
543 0 : case 'M':
544 0 : if (m == O_RDONLY)
545 0 : tif->tif_flags |= TIFF_MAPPED;
546 0 : break;
547 0 : case 'm':
548 0 : if (m == O_RDONLY)
549 0 : tif->tif_flags &= ~TIFF_MAPPED;
550 0 : break;
551 23877 : case 'C':
552 23877 : if (m == O_RDONLY)
553 23467 : tif->tif_flags |= TIFF_STRIPCHOP;
554 23877 : break;
555 579 : case 'c':
556 579 : if (m == O_RDONLY)
557 579 : tif->tif_flags &= ~TIFF_STRIPCHOP;
558 579 : break;
559 0 : case 'h':
560 0 : tif->tif_flags |= TIFF_HEADERONLY;
561 0 : break;
562 120 : case '8':
563 120 : if (m & O_CREAT)
564 120 : tif->tif_flags |= TIFF_BIGTIFF;
565 120 : break;
566 25193 : case 'D':
567 25193 : tif->tif_flags |= TIFF_DEFERSTRILELOAD;
568 25193 : break;
569 24414 : case 'O':
570 24414 : if (m == O_RDONLY)
571 24237 : tif->tif_flags |=
572 : (TIFF_LAZYSTRILELOAD_ASKED | TIFF_DEFERSTRILELOAD);
573 24414 : break;
574 116754 : default:
575 116754 : break;
576 : }
577 :
578 : #ifdef DEFER_STRILE_LOAD
579 : /* Compatibility with old DEFER_STRILE_LOAD compilation flag */
580 : /* Probably unneeded, since to the best of my knowledge (E. Rouault) */
581 : /* GDAL was the only user of this, and will now use the new 'D' flag */
582 : tif->tif_flags |= TIFF_DEFERSTRILELOAD;
583 : #endif
584 :
585 : /*
586 : * Read in TIFF header.
587 : */
588 103840 : if ((m & O_TRUNC) ||
589 31217 : !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
590 : {
591 40819 : if (tif->tif_mode == O_RDONLY)
592 : {
593 0 : TIFFErrorExtR(tif, name, "Cannot read TIFF header");
594 2 : goto bad;
595 : }
596 : /*
597 : * Setup header and write.
598 : */
599 : #if WORDS_BIGENDIAN
600 : tif->tif_header.common.tiff_magic =
601 : (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
602 : #else
603 40819 : tif->tif_header.common.tiff_magic =
604 40819 : (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
605 : #endif
606 : TIFFHeaderUnion tif_header_swapped;
607 40819 : if (!(tif->tif_flags & TIFF_BIGTIFF))
608 : {
609 40705 : tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
610 40705 : tif->tif_header.classic.tiff_diroff = 0;
611 40705 : tif->tif_header_size = sizeof(TIFFHeaderClassic);
612 : /* Swapped copy for writing */
613 40705 : _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
614 : sizeof(TIFFHeaderUnion));
615 40705 : if (tif->tif_flags & TIFF_SWAB)
616 79 : TIFFSwabShort(&tif_header_swapped.common.tiff_version);
617 : }
618 : else
619 : {
620 114 : tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
621 114 : tif->tif_header.big.tiff_offsetsize = 8;
622 114 : tif->tif_header.big.tiff_unused = 0;
623 114 : tif->tif_header.big.tiff_diroff = 0;
624 114 : tif->tif_header_size = sizeof(TIFFHeaderBig);
625 : /* Swapped copy for writing */
626 114 : _TIFFmemcpy(&tif_header_swapped, &tif->tif_header,
627 : sizeof(TIFFHeaderUnion));
628 120 : if (tif->tif_flags & TIFF_SWAB)
629 : {
630 4 : TIFFSwabShort(&tif_header_swapped.common.tiff_version);
631 4 : TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize);
632 : }
633 : }
634 : /*
635 : * The doc for "fopen" for some STD_C_LIBs says that if you
636 : * open a file for modify ("+"), then you must fseek (or
637 : * fflush?) between any freads and fwrites. This is not
638 : * necessary on most systems, but has been shown to be needed
639 : * on Solaris.
640 : */
641 40825 : TIFFSeekFile(tif, 0, SEEK_SET);
642 40825 : if (!WriteOK(tif, &tif_header_swapped,
643 : (tmsize_t)(tif->tif_header_size)))
644 : {
645 2 : TIFFErrorExtR(tif, name, "Error writing TIFF header");
646 2 : goto bad;
647 : }
648 : /*
649 : * Setup default directory.
650 : */
651 40823 : if (!TIFFDefaultDirectory(tif))
652 0 : goto bad;
653 40823 : tif->tif_diroff = 0;
654 40823 : tif->tif_lastdiroff = 0;
655 40823 : tif->tif_setdirectory_force_absolute = FALSE;
656 : /* tif_curdircount = 0 means 'empty file opened for writing, but no IFD
657 : * written yet' */
658 40823 : tif->tif_curdircount = 0;
659 40823 : return (tif);
660 : }
661 :
662 : /*
663 : * Setup the byte order handling according to the opened file for reading.
664 : */
665 31804 : if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
666 31031 : tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
667 : #if MDI_SUPPORT
668 : &&
669 : #if HOST_BIGENDIAN
670 : tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
671 : #else
672 : tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
673 : #endif
674 : )
675 : {
676 : TIFFErrorExtR(tif, name,
677 : "Not a TIFF or MDI file, bad magic number %" PRIu16
678 : " (0x%" PRIx16 ")",
679 : #else
680 : )
681 : {
682 0 : TIFFErrorExtR(tif, name,
683 : "Not a TIFF file, bad magic number %" PRIu16
684 : " (0x%" PRIx16 ")",
685 : #endif
686 0 : tif->tif_header.common.tiff_magic,
687 0 : tif->tif_header.common.tiff_magic);
688 0 : goto bad;
689 : }
690 31804 : if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
691 : {
692 : #if !WORDS_BIGENDIAN
693 222 : tif->tif_flags |= TIFF_SWAB;
694 : #endif
695 : }
696 : else
697 : {
698 : #if WORDS_BIGENDIAN
699 : tif->tif_flags |= TIFF_SWAB;
700 : #endif
701 : }
702 31804 : if (tif->tif_flags & TIFF_SWAB)
703 222 : TIFFSwabShort(&tif->tif_header.common.tiff_version);
704 31413 : if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
705 298 : (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
706 : {
707 0 : TIFFErrorExtR(tif, name,
708 : "Not a TIFF file, bad version number %" PRIu16
709 : " (0x%" PRIx16 ")",
710 0 : tif->tif_header.common.tiff_version,
711 0 : tif->tif_header.common.tiff_version);
712 0 : goto bad;
713 : }
714 31413 : if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
715 : {
716 31115 : if (tif->tif_flags & TIFF_SWAB)
717 211 : TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
718 30820 : tif->tif_header_size = sizeof(TIFFHeaderClassic);
719 : }
720 : else
721 : {
722 298 : if (!ReadOK(tif,
723 : ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
724 : (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
725 : {
726 0 : TIFFErrorExtR(tif, name, "Cannot read TIFF header");
727 0 : goto bad;
728 : }
729 298 : if (tif->tif_flags & TIFF_SWAB)
730 : {
731 11 : TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
732 11 : TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
733 : }
734 298 : if (tif->tif_header.big.tiff_offsetsize != 8)
735 : {
736 0 : TIFFErrorExtR(tif, name,
737 : "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
738 : " (0x%" PRIx16 ")",
739 0 : tif->tif_header.big.tiff_offsetsize,
740 0 : tif->tif_header.big.tiff_offsetsize);
741 0 : goto bad;
742 : }
743 298 : if (tif->tif_header.big.tiff_unused != 0)
744 : {
745 0 : TIFFErrorExtR(tif, name,
746 : "Not a TIFF file, bad BigTIFF unused %" PRIu16
747 : " (0x%" PRIx16 ")",
748 0 : tif->tif_header.big.tiff_unused,
749 0 : tif->tif_header.big.tiff_unused);
750 0 : goto bad;
751 : }
752 298 : tif->tif_header_size = sizeof(TIFFHeaderBig);
753 298 : tif->tif_flags |= TIFF_BIGTIFF;
754 : }
755 31118 : tif->tif_flags |= TIFF_MYBUFFER;
756 31118 : tif->tif_rawcp = tif->tif_rawdata = 0;
757 31118 : tif->tif_rawdatasize = 0;
758 31118 : tif->tif_rawdataoff = 0;
759 31118 : tif->tif_rawdataloaded = 0;
760 :
761 31118 : switch (mode[0])
762 : {
763 31468 : case 'r':
764 31468 : if (!(tif->tif_flags & TIFF_BIGTIFF))
765 31101 : tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
766 : else
767 367 : tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
768 : /*
769 : * Try to use a memory-mapped file if the client
770 : * has not explicitly suppressed usage with the
771 : * 'm' flag in the open mode (see above).
772 : */
773 31468 : if (tif->tif_flags & TIFF_MAPPED)
774 : {
775 : toff_t n;
776 27176 : if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
777 : {
778 179 : tif->tif_size = (tmsize_t)n;
779 179 : assert((toff_t)tif->tif_size == n);
780 : }
781 : else
782 27165 : tif->tif_flags &= ~TIFF_MAPPED;
783 : }
784 : /*
785 : * Sometimes we do not want to read the first directory (for
786 : * example, it may be broken) and want to proceed to other
787 : * directories. I this case we use the TIFF_HEADERONLY flag to open
788 : * file and return immediately after reading TIFF header.
789 : * However, the pointer to TIFFSetField() and TIFFGetField()
790 : * (i.e. tif->tif_tagmethods.vsetfield and
791 : * tif->tif_tagmethods.vgetfield) need to be initialized, which is
792 : * done in TIFFDefaultDirectory().
793 : */
794 31636 : if (tif->tif_flags & TIFF_HEADERONLY)
795 : {
796 0 : if (!TIFFDefaultDirectory(tif))
797 0 : goto bad;
798 0 : return (tif);
799 : }
800 :
801 : /*
802 : * Setup initial directory.
803 : */
804 31636 : if (TIFFReadDirectory(tif))
805 : {
806 31374 : return (tif);
807 : }
808 137 : break;
809 0 : case 'a':
810 : /*
811 : * New directories are automatically append
812 : * to the end of the directory chain when they
813 : * are written out (see TIFFWriteDirectory).
814 : */
815 0 : if (!TIFFDefaultDirectory(tif))
816 0 : goto bad;
817 0 : return (tif);
818 0 : default:
819 0 : break;
820 : }
821 0 : bad:
822 0 : tif->tif_mode = O_RDONLY; /* XXX avoid flush */
823 0 : TIFFCleanup(tif);
824 15 : bad2:
825 15 : return ((TIFF *)0);
826 : }
827 :
828 : /*
829 : * Query functions to access private data.
830 : */
831 :
832 : /*
833 : * Return open file's name.
834 : */
835 6 : const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
836 :
837 : /*
838 : * Set the file name.
839 : */
840 0 : const char *TIFFSetFileName(TIFF *tif, const char *name)
841 : {
842 0 : const char *old_name = tif->tif_name;
843 0 : tif->tif_name = (char *)name;
844 0 : return (old_name);
845 : }
846 :
847 : /*
848 : * Return open file's I/O descriptor.
849 : */
850 0 : int TIFFFileno(TIFF *tif) { return (tif->tif_fd); }
851 :
852 : /*
853 : * Set open file's I/O descriptor, and return previous value.
854 : */
855 0 : int TIFFSetFileno(TIFF *tif, int fd)
856 : {
857 0 : int old_fd = tif->tif_fd;
858 0 : tif->tif_fd = fd;
859 0 : return old_fd;
860 : }
861 :
862 : /*
863 : * Return open file's clientdata.
864 : */
865 104220 : thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
866 :
867 : /*
868 : * Set open file's clientdata, and return previous value.
869 : */
870 0 : thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
871 : {
872 0 : thandle_t m = tif->tif_clientdata;
873 0 : tif->tif_clientdata = newvalue;
874 0 : return m;
875 : }
876 :
877 : /*
878 : * Return read/write mode.
879 : */
880 0 : int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
881 :
882 : /*
883 : * Return read/write mode.
884 : */
885 0 : int TIFFSetMode(TIFF *tif, int mode)
886 : {
887 0 : int old_mode = tif->tif_mode;
888 0 : tif->tif_mode = mode;
889 0 : return (old_mode);
890 : }
891 :
892 : /*
893 : * Return nonzero if file is organized in
894 : * tiles; zero if organized as strips.
895 : */
896 5120300 : int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
897 :
898 : /*
899 : * Return current row being read/written.
900 : */
901 0 : uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_dir.td_row); }
902 :
903 : /*
904 : * Return index of the current directory.
905 : */
906 0 : tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
907 :
908 : /*
909 : * Return current strip.
910 : */
911 0 : uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_dir.td_curstrip); }
912 :
913 : /*
914 : * Return current tile.
915 : */
916 0 : uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_dir.td_curtile); }
917 :
918 : /*
919 : * Return nonzero if the file has byte-swapped data.
920 : */
921 109475 : int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
922 :
923 : /*
924 : * Return nonzero if the data is returned up-sampled.
925 : */
926 0 : int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); }
927 :
928 : /*
929 : * Return nonzero if the data is returned in MSB-to-LSB bit order.
930 : */
931 0 : int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); }
932 :
933 : /*
934 : * Return nonzero if given file was written in big-endian order.
935 : */
936 27851 : int TIFFIsBigEndian(TIFF *tif)
937 : {
938 27851 : return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
939 : }
940 :
941 : /*
942 : * Return nonzero if given file is BigTIFF style.
943 : */
944 3093 : int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); }
945 :
946 : /*
947 : * Return pointer to file read method.
948 : */
949 0 : TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
950 :
951 : /*
952 : * Return pointer to file write method.
953 : */
954 0 : TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
955 :
956 : /*
957 : * Return pointer to file seek method.
958 : */
959 0 : TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
960 :
961 : /*
962 : * Return pointer to file close method.
963 : */
964 0 : TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
965 :
966 : /*
967 : * Return pointer to file size requesting method.
968 : */
969 16407 : TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
970 :
971 : /*
972 : * Return pointer to memory mapping method.
973 : */
974 0 : TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
975 :
976 : /*
977 : * Return pointer to memory unmapping method.
978 : */
979 0 : TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
980 : {
981 0 : return (tif->tif_unmapproc);
982 : }
|