tif_unix.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 UNIX-specific Routines. These are should also work with the
  26. * Windows Common RunTime Library.
  27. */
  28. #ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
  29. #undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
  30. #endif
  31. #include "tif_config.h"
  32. #ifdef HAVE_SYS_TYPES_H
  33. #include <sys/types.h>
  34. #endif
  35. #include <errno.h>
  36. #include <stdarg.h>
  37. #include <stdlib.h>
  38. #include <sys/stat.h>
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. #ifdef HAVE_FCNTL_H
  43. #include <fcntl.h>
  44. #endif
  45. #ifdef HAVE_IO_H
  46. #include <io.h>
  47. #endif
  48. #include "tiffiop.h"
  49. #define TIFF_IO_MAX 2147483647U
  50. typedef union fd_as_handle_union
  51. {
  52. int fd;
  53. thandle_t h;
  54. } fd_as_handle_union_t;
  55. static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
  56. {
  57. fd_as_handle_union_t fdh;
  58. const size_t bytes_total = (size_t)size;
  59. size_t bytes_read;
  60. tmsize_t count = -1;
  61. if ((tmsize_t)bytes_total != size)
  62. {
  63. errno = EINVAL;
  64. return (tmsize_t)-1;
  65. }
  66. fdh.h = fd;
  67. for (bytes_read = 0; bytes_read < bytes_total; bytes_read += count)
  68. {
  69. char *buf_offset = (char *)buf + bytes_read;
  70. size_t io_size = bytes_total - bytes_read;
  71. if (io_size > TIFF_IO_MAX)
  72. io_size = TIFF_IO_MAX;
  73. /* Below is an obvious false positive of Coverity Scan */
  74. /* coverity[overflow_sink] */
  75. count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
  76. if (count <= 0)
  77. break;
  78. }
  79. if (count < 0)
  80. return (tmsize_t)-1;
  81. /* Silence Coverity Scan warning about unsigned to signed underflow. */
  82. /* coverity[return_overflow:SUPPRESS] */
  83. return (tmsize_t)bytes_read;
  84. }
  85. static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
  86. {
  87. fd_as_handle_union_t fdh;
  88. const size_t bytes_total = (size_t)size;
  89. size_t bytes_written;
  90. tmsize_t count = -1;
  91. if ((tmsize_t)bytes_total != size)
  92. {
  93. errno = EINVAL;
  94. return (tmsize_t)-1;
  95. }
  96. fdh.h = fd;
  97. for (bytes_written = 0; bytes_written < bytes_total; bytes_written += count)
  98. {
  99. const char *buf_offset = (char *)buf + bytes_written;
  100. size_t io_size = bytes_total - bytes_written;
  101. if (io_size > TIFF_IO_MAX)
  102. io_size = TIFF_IO_MAX;
  103. /* Below is an obvious false positive of Coverity Scan */
  104. /* coverity[overflow_sink] */
  105. count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
  106. if (count <= 0)
  107. break;
  108. }
  109. if (count < 0)
  110. return (tmsize_t)-1;
  111. /* Silence Coverity Scan warning about unsigned to signed underflow. */
  112. /* coverity[return_overflow:SUPPRESS] */
  113. return (tmsize_t)bytes_written;
  114. /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
  115. }
  116. static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
  117. {
  118. fd_as_handle_union_t fdh;
  119. _TIFF_off_t off_io = (_TIFF_off_t)off;
  120. if ((uint64_t)off_io != off)
  121. {
  122. errno = EINVAL;
  123. return (uint64_t)-1; /* this is really gross */
  124. }
  125. fdh.h = fd;
  126. return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
  127. }
  128. static int _tiffCloseProc(thandle_t fd)
  129. {
  130. fd_as_handle_union_t fdh;
  131. fdh.h = fd;
  132. return (close(fdh.fd));
  133. }
  134. static uint64_t _tiffSizeProc(thandle_t fd)
  135. {
  136. _TIFF_stat_s sb;
  137. fd_as_handle_union_t fdh;
  138. fdh.h = fd;
  139. if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
  140. return (0);
  141. else
  142. return ((uint64_t)sb.st_size);
  143. }
  144. #ifdef HAVE_MMAP
  145. #include <sys/mman.h>
  146. static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
  147. {
  148. uint64_t size64 = _tiffSizeProc(fd);
  149. tmsize_t sizem = (tmsize_t)size64;
  150. if (size64 && (uint64_t)sizem == size64)
  151. {
  152. fd_as_handle_union_t fdh;
  153. fdh.h = fd;
  154. *pbase =
  155. (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
  156. if (*pbase != (void *)-1)
  157. {
  158. *psize = (tmsize_t)sizem;
  159. return (1);
  160. }
  161. }
  162. return (0);
  163. }
  164. static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
  165. {
  166. (void)fd;
  167. (void)munmap(base, (off_t)size);
  168. }
  169. #else /* !HAVE_MMAP */
  170. static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
  171. {
  172. (void)fd;
  173. (void)pbase;
  174. (void)psize;
  175. return (0);
  176. }
  177. static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
  178. {
  179. (void)fd;
  180. (void)base;
  181. (void)size;
  182. }
  183. #endif /* !HAVE_MMAP */
  184. /*
  185. * Open a TIFF file descriptor for read/writing.
  186. */
  187. TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
  188. {
  189. return TIFFFdOpenExt(fd, name, mode, NULL);
  190. }
  191. TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
  192. TIFFOpenOptions *opts)
  193. {
  194. TIFF *tif;
  195. fd_as_handle_union_t fdh;
  196. fdh.fd = fd;
  197. tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
  198. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  199. _tiffMapProc, _tiffUnmapProc, opts);
  200. if (tif)
  201. tif->tif_fd = fd;
  202. return (tif);
  203. }
  204. /*
  205. * Open a TIFF file for read/writing.
  206. */
  207. TIFF *TIFFOpen(const char *name, const char *mode)
  208. {
  209. return TIFFOpenExt(name, mode, NULL);
  210. }
  211. TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
  212. {
  213. static const char module[] = "TIFFOpen";
  214. int m, fd;
  215. TIFF *tif;
  216. m = _TIFFgetMode(opts, NULL, mode, module);
  217. if (m == -1)
  218. return ((TIFF *)0);
  219. /* for cygwin and mingw */
  220. #ifdef O_BINARY
  221. m |= O_BINARY;
  222. #endif
  223. fd = open(name, m, 0666);
  224. if (fd < 0)
  225. {
  226. if (errno > 0 && strerror(errno) != NULL)
  227. {
  228. _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
  229. strerror(errno));
  230. }
  231. else
  232. {
  233. _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
  234. }
  235. return ((TIFF *)0);
  236. }
  237. tif = TIFFFdOpenExt((int)fd, name, mode, opts);
  238. if (!tif)
  239. close(fd);
  240. return tif;
  241. }
  242. #ifdef _WIN32
  243. #include <windows.h>
  244. /*
  245. * Open a TIFF file with a Unicode filename, for read/writing.
  246. */
  247. TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
  248. {
  249. return TIFFOpenWExt(name, mode, NULL);
  250. }
  251. TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
  252. {
  253. static const char module[] = "TIFFOpenW";
  254. int m, fd;
  255. int mbsize;
  256. char *mbname;
  257. TIFF *tif;
  258. m = _TIFFgetMode(opts, NULL, mode, module);
  259. if (m == -1)
  260. return ((TIFF *)0);
  261. /* for cygwin and mingw */
  262. #ifdef O_BINARY
  263. m |= O_BINARY;
  264. #endif
  265. fd = _wopen(name, m, 0666);
  266. if (fd < 0)
  267. {
  268. _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
  269. return ((TIFF *)0);
  270. }
  271. mbname = NULL;
  272. mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
  273. if (mbsize > 0)
  274. {
  275. mbname = _TIFFmalloc(mbsize);
  276. if (!mbname)
  277. {
  278. _TIFFErrorEarly(
  279. opts, NULL, module,
  280. "Can't allocate space for filename conversion buffer");
  281. return ((TIFF *)0);
  282. }
  283. WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
  284. }
  285. tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
  286. opts);
  287. _TIFFfree(mbname);
  288. if (!tif)
  289. close(fd);
  290. return tif;
  291. }
  292. #endif
  293. void *_TIFFmalloc(tmsize_t s)
  294. {
  295. if (s == 0)
  296. return ((void *)NULL);
  297. return (malloc((size_t)s));
  298. }
  299. void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
  300. {
  301. if (nmemb == 0 || siz == 0)
  302. return ((void *)NULL);
  303. return calloc((size_t)nmemb, (size_t)siz);
  304. }
  305. void _TIFFfree(void *p) { free(p); }
  306. void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
  307. void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
  308. void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
  309. {
  310. memcpy(d, s, (size_t)c);
  311. }
  312. int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
  313. {
  314. return (memcmp(p1, p2, (size_t)c));
  315. }
  316. static void unixWarningHandler(const char *module, const char *fmt, va_list ap)
  317. {
  318. if (module != NULL)
  319. fprintf(stderr, "%s: ", module);
  320. fprintf(stderr, "Warning, ");
  321. vfprintf(stderr, fmt, ap);
  322. fprintf(stderr, ".\n");
  323. }
  324. TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
  325. static void unixErrorHandler(const char *module, const char *fmt, va_list ap)
  326. {
  327. if (module != NULL)
  328. fprintf(stderr, "%s: ", module);
  329. vfprintf(stderr, fmt, ap);
  330. fprintf(stderr, ".\n");
  331. }
  332. TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;