tif_open.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. * TIFF Library.
  26. */
  27. #include "tiffiop.h"
  28. #include <limits.h>
  29. /*
  30. * Dummy functions to fill the omitted client procedures.
  31. */
  32. static int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize)
  33. {
  34. (void)fd;
  35. (void)pbase;
  36. (void)psize;
  37. return (0);
  38. }
  39. static void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
  40. {
  41. (void)fd;
  42. (void)base;
  43. (void)size;
  44. }
  45. int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode,
  46. const char *module)
  47. {
  48. int m = -1;
  49. switch (mode[0])
  50. {
  51. case 'r':
  52. m = O_RDONLY;
  53. if (mode[1] == '+')
  54. m = O_RDWR;
  55. break;
  56. case 'w':
  57. case 'a':
  58. m = O_RDWR | O_CREAT;
  59. if (mode[0] == 'w')
  60. m |= O_TRUNC;
  61. break;
  62. default:
  63. _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
  64. break;
  65. }
  66. return (m);
  67. }
  68. TIFFOpenOptions *TIFFOpenOptionsAlloc()
  69. {
  70. TIFFOpenOptions *opts =
  71. (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions));
  72. return opts;
  73. }
  74. void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); }
  75. /** Define a limit in bytes for a single memory allocation done by libtiff.
  76. * If max_single_mem_alloc is set to 0, no other limit that the underlying
  77. * _TIFFmalloc() will be applied, which is the default.
  78. */
  79. void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts,
  80. tmsize_t max_single_mem_alloc)
  81. {
  82. opts->max_single_mem_alloc = max_single_mem_alloc;
  83. }
  84. void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts,
  85. TIFFErrorHandlerExtR handler,
  86. void *errorhandler_user_data)
  87. {
  88. opts->errorhandler = handler;
  89. opts->errorhandler_user_data = errorhandler_user_data;
  90. }
  91. void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts,
  92. TIFFErrorHandlerExtR handler,
  93. void *warnhandler_user_data)
  94. {
  95. opts->warnhandler = handler;
  96. opts->warnhandler_user_data = warnhandler_user_data;
  97. }
  98. static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif,
  99. const char *pszFunction,
  100. tmsize_t s)
  101. {
  102. TIFFErrorExtR(tif, pszFunction,
  103. "Memory allocation of %" PRIu64
  104. " bytes is beyond the %" PRIu64
  105. " byte limit defined in open options",
  106. (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc);
  107. }
  108. /** malloc() version that takes into account memory-specific open options */
  109. void *_TIFFmallocExt(TIFF *tif, tmsize_t s)
  110. {
  111. if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
  112. s > tif->tif_max_single_mem_alloc)
  113. {
  114. _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
  115. return NULL;
  116. }
  117. return _TIFFmalloc(s);
  118. }
  119. /** calloc() version that takes into account memory-specific open options */
  120. void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
  121. {
  122. if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
  123. {
  124. if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
  125. return NULL;
  126. if (nmemb * siz > tif->tif_max_single_mem_alloc)
  127. {
  128. _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
  129. nmemb * siz);
  130. return NULL;
  131. }
  132. }
  133. return _TIFFcalloc(nmemb, siz);
  134. }
  135. /** realloc() version that takes into account memory-specific open options */
  136. void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
  137. {
  138. if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
  139. s > tif->tif_max_single_mem_alloc)
  140. {
  141. _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
  142. return NULL;
  143. }
  144. return _TIFFrealloc(p, s);
  145. }
  146. /** free() version that takes into account memory-specific open options */
  147. void _TIFFfreeExt(TIFF *tif, void *p)
  148. {
  149. (void)tif;
  150. _TIFFfree(p);
  151. }
  152. TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata,
  153. TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc,
  154. TIFFSeekProc seekproc, TIFFCloseProc closeproc,
  155. TIFFSizeProc sizeproc, TIFFMapFileProc mapproc,
  156. TIFFUnmapFileProc unmapproc)
  157. {
  158. return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
  159. seekproc, closeproc, sizeproc, mapproc, unmapproc,
  160. NULL);
  161. }
  162. TIFF *TIFFClientOpenExt(const char *name, const char *mode,
  163. thandle_t clientdata, TIFFReadWriteProc readproc,
  164. TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
  165. TIFFCloseProc closeproc, TIFFSizeProc sizeproc,
  166. TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc,
  167. TIFFOpenOptions *opts)
  168. {
  169. static const char module[] = "TIFFClientOpenExt";
  170. TIFF *tif;
  171. int m;
  172. const char *cp;
  173. /* The following are configuration checks. They should be redundant, but
  174. * should not compile to any actual code in an optimised release build
  175. * anyway. If any of them fail, (makefile-based or other) configuration is
  176. * not correct */
  177. assert(sizeof(uint8_t) == 1);
  178. assert(sizeof(int8_t) == 1);
  179. assert(sizeof(uint16_t) == 2);
  180. assert(sizeof(int16_t) == 2);
  181. assert(sizeof(uint32_t) == 4);
  182. assert(sizeof(int32_t) == 4);
  183. assert(sizeof(uint64_t) == 8);
  184. assert(sizeof(int64_t) == 8);
  185. {
  186. union
  187. {
  188. uint8_t a8[2];
  189. uint16_t a16;
  190. } n;
  191. n.a8[0] = 1;
  192. n.a8[1] = 0;
  193. (void)n;
  194. #ifdef WORDS_BIGENDIAN
  195. assert(n.a16 == 256);
  196. #else
  197. assert(n.a16 == 1);
  198. #endif
  199. }
  200. m = _TIFFgetMode(opts, clientdata, mode, module);
  201. if (m == -1)
  202. goto bad2;
  203. tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
  204. if (opts && opts->max_single_mem_alloc > 0 &&
  205. size_to_alloc > opts->max_single_mem_alloc)
  206. {
  207. _TIFFErrorEarly(opts, clientdata, module,
  208. "%s: Memory allocation of %" PRIu64
  209. " bytes is beyond the %" PRIu64
  210. " byte limit defined in open options",
  211. name, (uint64_t)size_to_alloc,
  212. (uint64_t)opts->max_single_mem_alloc);
  213. goto bad2;
  214. }
  215. tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
  216. if (tif == NULL)
  217. {
  218. _TIFFErrorEarly(opts, clientdata, module,
  219. "%s: Out of memory (TIFF structure)", name);
  220. goto bad2;
  221. }
  222. _TIFFmemset(tif, 0, sizeof(*tif));
  223. tif->tif_name = (char *)tif + sizeof(TIFF);
  224. strcpy(tif->tif_name, name);
  225. tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
  226. tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; /* non-existent directory */
  227. tif->tif_curoff = 0;
  228. tif->tif_curstrip = (uint32_t)-1; /* invalid strip */
  229. tif->tif_row = (uint32_t)-1; /* read/write pre-increment */
  230. tif->tif_clientdata = clientdata;
  231. tif->tif_readproc = readproc;
  232. tif->tif_writeproc = writeproc;
  233. tif->tif_seekproc = seekproc;
  234. tif->tif_closeproc = closeproc;
  235. tif->tif_sizeproc = sizeproc;
  236. tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
  237. tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
  238. if (opts)
  239. {
  240. tif->tif_errorhandler = opts->errorhandler;
  241. tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
  242. tif->tif_warnhandler = opts->warnhandler;
  243. tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
  244. tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
  245. }
  246. if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
  247. {
  248. TIFFErrorExtR(tif, module,
  249. "One of the client procedures is NULL pointer.");
  250. _TIFFfreeExt(NULL, tif);
  251. goto bad2;
  252. }
  253. _TIFFSetDefaultCompressionState(tif); /* setup default state */
  254. /*
  255. * Default is to return data MSB2LSB and enable the
  256. * use of memory-mapped files and strip chopping when
  257. * a file is opened read-only.
  258. */
  259. tif->tif_flags = FILLORDER_MSB2LSB;
  260. if (m == O_RDONLY)
  261. tif->tif_flags |= TIFF_MAPPED;
  262. #ifdef STRIPCHOP_DEFAULT
  263. if (m == O_RDONLY || m == O_RDWR)
  264. tif->tif_flags |= STRIPCHOP_DEFAULT;
  265. #endif
  266. /*
  267. * Process library-specific flags in the open mode string.
  268. * The following flags may be used to control intrinsic library
  269. * behavior that may or may not be desirable (usually for
  270. * compatibility with some application that claims to support
  271. * TIFF but only supports some brain dead idea of what the
  272. * vendor thinks TIFF is):
  273. *
  274. * 'l' use little-endian byte order for creating a file
  275. * 'b' use big-endian byte order for creating a file
  276. * 'L' read/write information using LSB2MSB bit order
  277. * 'B' read/write information using MSB2LSB bit order
  278. * 'H' read/write information using host bit order
  279. * 'M' enable use of memory-mapped files when supported
  280. * 'm' disable use of memory-mapped files
  281. * 'C' enable strip chopping support when reading
  282. * 'c' disable strip chopping support
  283. * 'h' read TIFF header only, do not load the first IFD
  284. * '4' ClassicTIFF for creating a file (default)
  285. * '8' BigTIFF for creating a file
  286. * 'D' enable use of deferred strip/tile offset/bytecount array loading.
  287. * 'O' on-demand loading of values instead of whole array loading (implies
  288. * D)
  289. *
  290. * The use of the 'l' and 'b' flags is strongly discouraged.
  291. * These flags are provided solely because numerous vendors,
  292. * typically on the PC, do not correctly support TIFF; they
  293. * only support the Intel little-endian byte order. This
  294. * support is not configured by default because it supports
  295. * the violation of the TIFF spec that says that readers *MUST*
  296. * support both byte orders. It is strongly recommended that
  297. * you not use this feature except to deal with busted apps
  298. * that write invalid TIFF. And even in those cases you should
  299. * bang on the vendors to fix their software.
  300. *
  301. * The 'L', 'B', and 'H' flags are intended for applications
  302. * that can optimize operations on data by using a particular
  303. * bit order. By default the library returns data in MSB2LSB
  304. * bit order for compatibility with older versions of this
  305. * library. Returning data in the bit order of the native CPU
  306. * makes the most sense but also requires applications to check
  307. * the value of the FillOrder tag; something they probably do
  308. * not do right now.
  309. *
  310. * The 'M' and 'm' flags are provided because some virtual memory
  311. * systems exhibit poor behavior when large images are mapped.
  312. * These options permit clients to control the use of memory-mapped
  313. * files on a per-file basis.
  314. *
  315. * The 'C' and 'c' flags are provided because the library support
  316. * for chopping up large strips into multiple smaller strips is not
  317. * application-transparent and as such can cause problems. The 'c'
  318. * option permits applications that only want to look at the tags,
  319. * for example, to get the unadulterated TIFF tag information.
  320. */
  321. for (cp = mode; *cp; cp++)
  322. switch (*cp)
  323. {
  324. case 'b':
  325. #ifndef WORDS_BIGENDIAN
  326. if (m & O_CREAT)
  327. tif->tif_flags |= TIFF_SWAB;
  328. #endif
  329. break;
  330. case 'l':
  331. #ifdef WORDS_BIGENDIAN
  332. if ((m & O_CREAT))
  333. tif->tif_flags |= TIFF_SWAB;
  334. #endif
  335. break;
  336. case 'B':
  337. tif->tif_flags =
  338. (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
  339. break;
  340. case 'L':
  341. tif->tif_flags =
  342. (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
  343. break;
  344. case 'H':
  345. TIFFWarningExtR(tif, name,
  346. "H(ost) mode is deprecated. Since "
  347. "libtiff 4.5.1, it is an alias of 'B' / "
  348. "FILLORDER_MSB2LSB.");
  349. tif->tif_flags =
  350. (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
  351. break;
  352. case 'M':
  353. if (m == O_RDONLY)
  354. tif->tif_flags |= TIFF_MAPPED;
  355. break;
  356. case 'm':
  357. if (m == O_RDONLY)
  358. tif->tif_flags &= ~TIFF_MAPPED;
  359. break;
  360. case 'C':
  361. if (m == O_RDONLY)
  362. tif->tif_flags |= TIFF_STRIPCHOP;
  363. break;
  364. case 'c':
  365. if (m == O_RDONLY)
  366. tif->tif_flags &= ~TIFF_STRIPCHOP;
  367. break;
  368. case 'h':
  369. tif->tif_flags |= TIFF_HEADERONLY;
  370. break;
  371. case '8':
  372. if (m & O_CREAT)
  373. tif->tif_flags |= TIFF_BIGTIFF;
  374. break;
  375. case 'D':
  376. tif->tif_flags |= TIFF_DEFERSTRILELOAD;
  377. break;
  378. case 'O':
  379. if (m == O_RDONLY)
  380. tif->tif_flags |=
  381. (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);
  382. break;
  383. }
  384. #ifdef DEFER_STRILE_LOAD
  385. /* Compatibility with old DEFER_STRILE_LOAD compilation flag */
  386. /* Probably unneeded, since to the best of my knowledge (E. Rouault) */
  387. /* GDAL was the only user of this, and will now use the new 'D' flag */
  388. tif->tif_flags |= TIFF_DEFERSTRILELOAD;
  389. #endif
  390. /*
  391. * Read in TIFF header.
  392. */
  393. if ((m & O_TRUNC) ||
  394. !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
  395. {
  396. if (tif->tif_mode == O_RDONLY)
  397. {
  398. TIFFErrorExtR(tif, name, "Cannot read TIFF header");
  399. goto bad;
  400. }
  401. /*
  402. * Setup header and write.
  403. */
  404. #ifdef WORDS_BIGENDIAN
  405. tif->tif_header.common.tiff_magic =
  406. (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
  407. #else
  408. tif->tif_header.common.tiff_magic =
  409. (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
  410. #endif
  411. if (!(tif->tif_flags & TIFF_BIGTIFF))
  412. {
  413. tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
  414. tif->tif_header.classic.tiff_diroff = 0;
  415. if (tif->tif_flags & TIFF_SWAB)
  416. TIFFSwabShort(&tif->tif_header.common.tiff_version);
  417. tif->tif_header_size = sizeof(TIFFHeaderClassic);
  418. }
  419. else
  420. {
  421. tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
  422. tif->tif_header.big.tiff_offsetsize = 8;
  423. tif->tif_header.big.tiff_unused = 0;
  424. tif->tif_header.big.tiff_diroff = 0;
  425. if (tif->tif_flags & TIFF_SWAB)
  426. {
  427. TIFFSwabShort(&tif->tif_header.common.tiff_version);
  428. TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
  429. }
  430. tif->tif_header_size = sizeof(TIFFHeaderBig);
  431. }
  432. /*
  433. * The doc for "fopen" for some STD_C_LIBs says that if you
  434. * open a file for modify ("+"), then you must fseek (or
  435. * fflush?) between any freads and fwrites. This is not
  436. * necessary on most systems, but has been shown to be needed
  437. * on Solaris.
  438. */
  439. TIFFSeekFile(tif, 0, SEEK_SET);
  440. if (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size)))
  441. {
  442. TIFFErrorExtR(tif, name, "Error writing TIFF header");
  443. goto bad;
  444. }
  445. /*
  446. * Setup the byte order handling.
  447. */
  448. if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
  449. {
  450. #ifndef WORDS_BIGENDIAN
  451. tif->tif_flags |= TIFF_SWAB;
  452. #endif
  453. }
  454. else
  455. {
  456. #ifdef WORDS_BIGENDIAN
  457. tif->tif_flags |= TIFF_SWAB;
  458. #endif
  459. }
  460. /*
  461. * Setup default directory.
  462. */
  463. if (!TIFFDefaultDirectory(tif))
  464. goto bad;
  465. tif->tif_diroff = 0;
  466. tif->tif_lastdiroff = 0;
  467. tif->tif_setdirectory_force_absolute = FALSE;
  468. return (tif);
  469. }
  470. /*
  471. * Setup the byte order handling.
  472. */
  473. if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
  474. tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
  475. #if MDI_SUPPORT
  476. &&
  477. #if HOST_BIGENDIAN
  478. tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
  479. #else
  480. tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
  481. #endif
  482. )
  483. {
  484. TIFFErrorExtR(tif, name,
  485. "Not a TIFF or MDI file, bad magic number %" PRIu16
  486. " (0x%" PRIx16 ")",
  487. #else
  488. )
  489. {
  490. TIFFErrorExtR(tif, name,
  491. "Not a TIFF file, bad magic number %" PRIu16
  492. " (0x%" PRIx16 ")",
  493. #endif
  494. tif->tif_header.common.tiff_magic,
  495. tif->tif_header.common.tiff_magic);
  496. goto bad;
  497. }
  498. if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
  499. {
  500. #ifndef WORDS_BIGENDIAN
  501. tif->tif_flags |= TIFF_SWAB;
  502. #endif
  503. }
  504. else
  505. {
  506. #ifdef WORDS_BIGENDIAN
  507. tif->tif_flags |= TIFF_SWAB;
  508. #endif
  509. }
  510. if (tif->tif_flags & TIFF_SWAB)
  511. TIFFSwabShort(&tif->tif_header.common.tiff_version);
  512. if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
  513. (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
  514. {
  515. TIFFErrorExtR(tif, name,
  516. "Not a TIFF file, bad version number %" PRIu16
  517. " (0x%" PRIx16 ")",
  518. tif->tif_header.common.tiff_version,
  519. tif->tif_header.common.tiff_version);
  520. goto bad;
  521. }
  522. if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
  523. {
  524. if (tif->tif_flags & TIFF_SWAB)
  525. TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
  526. tif->tif_header_size = sizeof(TIFFHeaderClassic);
  527. }
  528. else
  529. {
  530. if (!ReadOK(tif,
  531. ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
  532. (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
  533. {
  534. TIFFErrorExtR(tif, name, "Cannot read TIFF header");
  535. goto bad;
  536. }
  537. if (tif->tif_flags & TIFF_SWAB)
  538. {
  539. TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
  540. TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
  541. }
  542. if (tif->tif_header.big.tiff_offsetsize != 8)
  543. {
  544. TIFFErrorExtR(tif, name,
  545. "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
  546. " (0x%" PRIx16 ")",
  547. tif->tif_header.big.tiff_offsetsize,
  548. tif->tif_header.big.tiff_offsetsize);
  549. goto bad;
  550. }
  551. if (tif->tif_header.big.tiff_unused != 0)
  552. {
  553. TIFFErrorExtR(tif, name,
  554. "Not a TIFF file, bad BigTIFF unused %" PRIu16
  555. " (0x%" PRIx16 ")",
  556. tif->tif_header.big.tiff_unused,
  557. tif->tif_header.big.tiff_unused);
  558. goto bad;
  559. }
  560. tif->tif_header_size = sizeof(TIFFHeaderBig);
  561. tif->tif_flags |= TIFF_BIGTIFF;
  562. }
  563. tif->tif_flags |= TIFF_MYBUFFER;
  564. tif->tif_rawcp = tif->tif_rawdata = 0;
  565. tif->tif_rawdatasize = 0;
  566. tif->tif_rawdataoff = 0;
  567. tif->tif_rawdataloaded = 0;
  568. switch (mode[0])
  569. {
  570. case 'r':
  571. if (!(tif->tif_flags & TIFF_BIGTIFF))
  572. tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
  573. else
  574. tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
  575. /*
  576. * Try to use a memory-mapped file if the client
  577. * has not explicitly suppressed usage with the
  578. * 'm' flag in the open mode (see above).
  579. */
  580. if (tif->tif_flags & TIFF_MAPPED)
  581. {
  582. toff_t n;
  583. if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
  584. {
  585. tif->tif_size = (tmsize_t)n;
  586. assert((toff_t)tif->tif_size == n);
  587. }
  588. else
  589. tif->tif_flags &= ~TIFF_MAPPED;
  590. }
  591. /*
  592. * Sometimes we do not want to read the first directory (for
  593. * example, it may be broken) and want to proceed to other
  594. * directories. I this case we use the TIFF_HEADERONLY flag to open
  595. * file and return immediately after reading TIFF header.
  596. */
  597. if (tif->tif_flags & TIFF_HEADERONLY)
  598. return (tif);
  599. /*
  600. * Setup initial directory.
  601. */
  602. if (TIFFReadDirectory(tif))
  603. {
  604. return (tif);
  605. }
  606. break;
  607. case 'a':
  608. /*
  609. * New directories are automatically append
  610. * to the end of the directory chain when they
  611. * are written out (see TIFFWriteDirectory).
  612. */
  613. if (!TIFFDefaultDirectory(tif))
  614. goto bad;
  615. return (tif);
  616. }
  617. bad:
  618. tif->tif_mode = O_RDONLY; /* XXX avoid flush */
  619. TIFFCleanup(tif);
  620. bad2:
  621. return ((TIFF *)0);
  622. }
  623. /*
  624. * Query functions to access private data.
  625. */
  626. /*
  627. * Return open file's name.
  628. */
  629. const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
  630. /*
  631. * Set the file name.
  632. */
  633. const char *TIFFSetFileName(TIFF *tif, const char *name)
  634. {
  635. const char *old_name = tif->tif_name;
  636. tif->tif_name = (char *)name;
  637. return (old_name);
  638. }
  639. /*
  640. * Return open file's I/O descriptor.
  641. */
  642. int TIFFFileno(TIFF *tif) { return (tif->tif_fd); }
  643. /*
  644. * Set open file's I/O descriptor, and return previous value.
  645. */
  646. int TIFFSetFileno(TIFF *tif, int fd)
  647. {
  648. int old_fd = tif->tif_fd;
  649. tif->tif_fd = fd;
  650. return old_fd;
  651. }
  652. /*
  653. * Return open file's clientdata.
  654. */
  655. thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
  656. /*
  657. * Set open file's clientdata, and return previous value.
  658. */
  659. thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
  660. {
  661. thandle_t m = tif->tif_clientdata;
  662. tif->tif_clientdata = newvalue;
  663. return m;
  664. }
  665. /*
  666. * Return read/write mode.
  667. */
  668. int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
  669. /*
  670. * Return read/write mode.
  671. */
  672. int TIFFSetMode(TIFF *tif, int mode)
  673. {
  674. int old_mode = tif->tif_mode;
  675. tif->tif_mode = mode;
  676. return (old_mode);
  677. }
  678. /*
  679. * Return nonzero if file is organized in
  680. * tiles; zero if organized as strips.
  681. */
  682. int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
  683. /*
  684. * Return current row being read/written.
  685. */
  686. uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); }
  687. /*
  688. * Return index of the current directory.
  689. */
  690. tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
  691. /*
  692. * Return current strip.
  693. */
  694. uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); }
  695. /*
  696. * Return current tile.
  697. */
  698. uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); }
  699. /*
  700. * Return nonzero if the file has byte-swapped data.
  701. */
  702. int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
  703. /*
  704. * Return nonzero if the data is returned up-sampled.
  705. */
  706. int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); }
  707. /*
  708. * Return nonzero if the data is returned in MSB-to-LSB bit order.
  709. */
  710. int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); }
  711. /*
  712. * Return nonzero if given file was written in big-endian order.
  713. */
  714. int TIFFIsBigEndian(TIFF *tif)
  715. {
  716. return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
  717. }
  718. /*
  719. * Return nonzero if given file is BigTIFF style.
  720. */
  721. int TIFFIsBigTIFF(TIFF *tif)
  722. {
  723. return (tif->tif_header.common.tiff_version == TIFF_VERSION_BIG);
  724. }
  725. /*
  726. * Return pointer to file read method.
  727. */
  728. TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
  729. /*
  730. * Return pointer to file write method.
  731. */
  732. TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
  733. /*
  734. * Return pointer to file seek method.
  735. */
  736. TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
  737. /*
  738. * Return pointer to file close method.
  739. */
  740. TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
  741. /*
  742. * Return pointer to file size requesting method.
  743. */
  744. TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
  745. /*
  746. * Return pointer to memory mapping method.
  747. */
  748. TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
  749. /*
  750. * Return pointer to memory unmapping method.
  751. */
  752. TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
  753. {
  754. return (tif->tif_unmapproc);
  755. }