tif_unix.c 8.1 KB

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