tif_unix.c 9.3 KB

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