Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/libtiff-src/tif_open.c

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