tif_open.c 32 KB

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