tif_unix.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* $Id: tif_unix.c,v 1.23 2012-06-01 21:40:59 fwarmerdam Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library UNIX-specific Routines. These are should also work with the
  27. * Windows Common RunTime Library.
  28. */
  29. #include "tif_config.h"
  30. #ifdef HAVE_SYS_TYPES_H
  31. # include <sys/types.h>
  32. #endif
  33. #include <errno.h>
  34. #include <stdarg.h>
  35. #include <stdlib.h>
  36. #include <sys/stat.h>
  37. #ifdef HAVE_UNISTD_H
  38. # include <unistd.h>
  39. #endif
  40. #ifdef HAVE_FCNTL_H
  41. # include <fcntl.h>
  42. #endif
  43. #ifdef HAVE_IO_H
  44. # include <io.h>
  45. #endif
  46. #include "tiffiop.h"
  47. static tmsize_t
  48. _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
  49. {
  50. size_t size_io = (size_t) size;
  51. if ((tmsize_t) size_io != size)
  52. {
  53. errno=EINVAL;
  54. return (tmsize_t) -1;
  55. }
  56. return ((tmsize_t) read((int) fd, buf, size_io));
  57. }
  58. static tmsize_t
  59. _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
  60. {
  61. size_t size_io = (size_t) size;
  62. if ((tmsize_t) size_io != size)
  63. {
  64. errno=EINVAL;
  65. return (tmsize_t) -1;
  66. }
  67. return ((tmsize_t) write((int) fd, buf, size_io));
  68. }
  69. static uint64
  70. _tiffSeekProc(thandle_t fd, uint64 off, int whence)
  71. {
  72. off_t off_io = (off_t) off;
  73. if ((uint64) off_io != off)
  74. {
  75. errno=EINVAL;
  76. return (uint64) -1; /* this is really gross */
  77. }
  78. return((uint64)lseek((int)fd,off_io,whence));
  79. }
  80. static int
  81. _tiffCloseProc(thandle_t fd)
  82. {
  83. return(close((int)fd));
  84. }
  85. static uint64
  86. _tiffSizeProc(thandle_t fd)
  87. {
  88. struct stat sb;
  89. if (fstat((int)fd,&sb)<0)
  90. return(0);
  91. else
  92. return((uint64)sb.st_size);
  93. }
  94. #ifdef HAVE_MMAP
  95. #include <sys/mman.h>
  96. static int
  97. _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
  98. {
  99. uint64 size64 = _tiffSizeProc(fd);
  100. tmsize_t sizem = (tmsize_t)size64;
  101. if ((uint64)sizem==size64) {
  102. *pbase = (void*)
  103. mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, (int) fd, 0);
  104. if (*pbase != (void*) -1) {
  105. *psize = (tmsize_t)sizem;
  106. return (1);
  107. }
  108. }
  109. return (0);
  110. }
  111. static void
  112. _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
  113. {
  114. (void) fd;
  115. (void) munmap(base, (off_t) size);
  116. }
  117. #else /* !HAVE_MMAP */
  118. static int
  119. _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
  120. {
  121. (void) fd; (void) pbase; (void) psize;
  122. return (0);
  123. }
  124. static void
  125. _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
  126. {
  127. (void) fd; (void) base; (void) size;
  128. }
  129. #endif /* !HAVE_MMAP */
  130. /*
  131. * Open a TIFF file descriptor for read/writing.
  132. */
  133. TIFF*
  134. TIFFFdOpen(int fd, const char* name, const char* mode)
  135. {
  136. TIFF* tif;
  137. tif = TIFFClientOpen(name, mode,
  138. (thandle_t) fd,
  139. _tiffReadProc, _tiffWriteProc,
  140. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  141. _tiffMapProc, _tiffUnmapProc);
  142. if (tif)
  143. tif->tif_fd = fd;
  144. return (tif);
  145. }
  146. /*
  147. * Open a TIFF file for read/writing.
  148. */
  149. TIFF*
  150. TIFFOpen(const char* name, const char* mode)
  151. {
  152. static const char module[] = "TIFFOpen";
  153. int m, fd;
  154. TIFF* tif;
  155. m = _TIFFgetMode(mode, module);
  156. if (m == -1)
  157. return ((TIFF*)0);
  158. /* for cygwin and mingw */
  159. #ifdef O_BINARY
  160. m |= O_BINARY;
  161. #endif
  162. fd = open(name, m, 0666);
  163. if (fd < 0) {
  164. if (errno > 0 && strerror(errno) != NULL ) {
  165. TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
  166. } else {
  167. TIFFErrorExt(0, module, "%s: Cannot open", name);
  168. }
  169. return ((TIFF *)0);
  170. }
  171. tif = TIFFFdOpen((int)fd, name, mode);
  172. if(!tif)
  173. close(fd);
  174. return tif;
  175. }
  176. #ifdef __WIN32__
  177. #include <windows.h>
  178. /*
  179. * Open a TIFF file with a Unicode filename, for read/writing.
  180. */
  181. TIFF*
  182. TIFFOpenW(const wchar_t* name, const char* mode)
  183. {
  184. static const char module[] = "TIFFOpenW";
  185. int m, fd;
  186. int mbsize;
  187. char *mbname;
  188. TIFF* tif;
  189. m = _TIFFgetMode(mode, module);
  190. if (m == -1)
  191. return ((TIFF*)0);
  192. /* for cygwin and mingw */
  193. #ifdef O_BINARY
  194. m |= O_BINARY;
  195. #endif
  196. fd = _wopen(name, m, 0666);
  197. if (fd < 0) {
  198. TIFFErrorExt(0, module, "%s: Cannot open", name);
  199. return ((TIFF *)0);
  200. }
  201. mbname = NULL;
  202. mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
  203. if (mbsize > 0) {
  204. mbname = _TIFFmalloc(mbsize);
  205. if (!mbname) {
  206. TIFFErrorExt(0, module,
  207. "Can't allocate space for filename conversion buffer");
  208. return ((TIFF*)0);
  209. }
  210. WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
  211. NULL, NULL);
  212. }
  213. tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
  214. mode);
  215. _TIFFfree(mbname);
  216. if(!tif)
  217. close(fd);
  218. return tif;
  219. }
  220. #endif
  221. void*
  222. _TIFFmalloc(tmsize_t s)
  223. {
  224. return (malloc((size_t) s));
  225. }
  226. void
  227. _TIFFfree(void* p)
  228. {
  229. free(p);
  230. }
  231. void*
  232. _TIFFrealloc(void* p, tmsize_t s)
  233. {
  234. return (realloc(p, (size_t) s));
  235. }
  236. void
  237. _TIFFmemset(void* p, int v, tmsize_t c)
  238. {
  239. memset(p, v, (size_t) c);
  240. }
  241. void
  242. _TIFFmemcpy(void* d, const void* s, tmsize_t c)
  243. {
  244. memcpy(d, s, (size_t) c);
  245. }
  246. int
  247. _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
  248. {
  249. return (memcmp(p1, p2, (size_t) c));
  250. }
  251. static void
  252. unixWarningHandler(const char* module, const char* fmt, va_list ap)
  253. {
  254. if (module != NULL)
  255. fprintf(stderr, "%s: ", module);
  256. fprintf(stderr, "Warning, ");
  257. vfprintf(stderr, fmt, ap);
  258. fprintf(stderr, ".\n");
  259. }
  260. TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
  261. static void
  262. unixErrorHandler(const char* module, const char* fmt, va_list ap)
  263. {
  264. if (module != NULL)
  265. fprintf(stderr, "%s: ", module);
  266. vfprintf(stderr, fmt, ap);
  267. fprintf(stderr, ".\n");
  268. }
  269. TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
  270. /* vim: set ts=8 sts=8 sw=8 noet: */
  271. /*
  272. * Local Variables:
  273. * mode: c
  274. * c-basic-offset: 8
  275. * fill-column: 78
  276. * End:
  277. */