tif_open.c 32 KB

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