/src/FreeImage/Source/LibTIFF/tif_unix.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 300 lines · 212 code · 42 blank · 46 comment · 20 complexity · f4b8adfea557839cd730e2d5dac3c431 MD5 · raw file

  1. /* $Id: tif_unix.c,v 1.35 2011/04/10 17:14:09 drolon 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 <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. static tsize_t
  47. _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
  48. {
  49. return ((tsize_t) read((int) fd, buf, (size_t) size));
  50. }
  51. static tsize_t
  52. _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
  53. {
  54. return ((tsize_t) write((int) fd, buf, (size_t) size));
  55. }
  56. static toff_t
  57. _tiffSeekProc(thandle_t fd, toff_t off, int whence)
  58. {
  59. return ((toff_t) lseek((int) fd, (off_t) off, whence));
  60. }
  61. static int
  62. _tiffCloseProc(thandle_t fd)
  63. {
  64. return (close((int) fd));
  65. }
  66. static toff_t
  67. _tiffSizeProc(thandle_t fd)
  68. {
  69. #ifdef _AM29K
  70. long fsize;
  71. return ((fsize = lseek((int) fd, 0, SEEK_END)) < 0 ? 0 : fsize);
  72. #else
  73. struct stat sb;
  74. return (toff_t) (fstat((int) fd, &sb) < 0 ? 0 : sb.st_size);
  75. #endif
  76. }
  77. #ifdef HAVE_MMAP
  78. #include <sys/mman.h>
  79. static int
  80. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  81. {
  82. toff_t size = _tiffSizeProc(fd);
  83. if (size != (toff_t) -1) {
  84. *pbase = (tdata_t)
  85. mmap(0, size, PROT_READ, MAP_SHARED, (int) fd, 0);
  86. if (*pbase != (tdata_t) -1) {
  87. *psize = size;
  88. return (1);
  89. }
  90. }
  91. return (0);
  92. }
  93. static void
  94. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  95. {
  96. (void) fd;
  97. (void) munmap(base, (off_t) size);
  98. }
  99. #else /* !HAVE_MMAP */
  100. static int
  101. _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
  102. {
  103. (void) fd; (void) pbase; (void) psize;
  104. return (0);
  105. }
  106. static void
  107. _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
  108. {
  109. (void) fd; (void) base; (void) size;
  110. }
  111. #endif /* !HAVE_MMAP */
  112. /*
  113. * Open a TIFF file descriptor for read/writing.
  114. */
  115. TIFF*
  116. TIFFFdOpen(int fd, const char* name, const char* mode)
  117. {
  118. TIFF* tif;
  119. tif = TIFFClientOpen(name, mode,
  120. (thandle_t) fd,
  121. _tiffReadProc, _tiffWriteProc,
  122. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  123. _tiffMapProc, _tiffUnmapProc);
  124. if (tif)
  125. tif->tif_fd = fd;
  126. return (tif);
  127. }
  128. /*
  129. * Open a TIFF file for read/writing.
  130. */
  131. TIFF*
  132. TIFFOpen(const char* name, const char* mode)
  133. {
  134. static const char module[] = "TIFFOpen";
  135. int m, fd;
  136. TIFF* tif;
  137. m = _TIFFgetMode(mode, module);
  138. if (m == -1)
  139. return ((TIFF*)0);
  140. /* for cygwin and mingw */
  141. #ifdef O_BINARY
  142. m |= O_BINARY;
  143. #endif
  144. #ifdef _AM29K
  145. fd = open(name, m);
  146. #else
  147. fd = open(name, m, 0666);
  148. #endif
  149. if (fd < 0) {
  150. TIFFErrorExt(0, module, "%s: Cannot open", name);
  151. return ((TIFF *)0);
  152. }
  153. tif = TIFFFdOpen((int)fd, name, mode);
  154. if(!tif)
  155. close(fd);
  156. return tif;
  157. }
  158. #ifdef __WIN32__
  159. #include <windows.h>
  160. /*
  161. * Open a TIFF file with a Unicode filename, for read/writing.
  162. */
  163. TIFF*
  164. TIFFOpenW(const wchar_t* name, const char* mode)
  165. {
  166. static const char module[] = "TIFFOpenW";
  167. int m, fd;
  168. int mbsize;
  169. char *mbname;
  170. TIFF* tif;
  171. m = _TIFFgetMode(mode, module);
  172. if (m == -1)
  173. return ((TIFF*)0);
  174. /* for cygwin and mingw */
  175. #ifdef O_BINARY
  176. m |= O_BINARY;
  177. #endif
  178. fd = _wopen(name, m, 0666);
  179. if (fd < 0) {
  180. TIFFErrorExt(0, module, "%s: Cannot open", name);
  181. return ((TIFF *)0);
  182. }
  183. mbname = NULL;
  184. mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
  185. if (mbsize > 0) {
  186. mbname = _TIFFmalloc(mbsize);
  187. if (!mbname) {
  188. TIFFErrorExt(0, module,
  189. "Can't allocate space for filename conversion buffer");
  190. return ((TIFF*)0);
  191. }
  192. WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
  193. NULL, NULL);
  194. }
  195. tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
  196. mode);
  197. _TIFFfree(mbname);
  198. if(!tif)
  199. close(fd);
  200. return tif;
  201. }
  202. #endif
  203. void*
  204. _TIFFmalloc(tsize_t s)
  205. {
  206. return (malloc((size_t) s));
  207. }
  208. void
  209. _TIFFfree(tdata_t p)
  210. {
  211. free(p);
  212. }
  213. void*
  214. _TIFFrealloc(tdata_t p, tsize_t s)
  215. {
  216. return (realloc(p, (size_t) s));
  217. }
  218. void
  219. _TIFFmemset(tdata_t p, int v, tsize_t c)
  220. {
  221. memset(p, v, (size_t) c);
  222. }
  223. void
  224. _TIFFmemcpy(tdata_t d, const tdata_t s, tsize_t c)
  225. {
  226. memcpy(d, s, (size_t) c);
  227. }
  228. int
  229. _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
  230. {
  231. return (memcmp(p1, p2, (size_t) c));
  232. }
  233. static void
  234. unixWarningHandler(const char* module, const char* fmt, va_list ap)
  235. {
  236. if (module != NULL)
  237. fprintf(stderr, "%s: ", module);
  238. fprintf(stderr, "Warning, ");
  239. vfprintf(stderr, fmt, ap);
  240. fprintf(stderr, ".\n");
  241. }
  242. TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
  243. static void
  244. unixErrorHandler(const char* module, const char* fmt, va_list ap)
  245. {
  246. if (module != NULL)
  247. fprintf(stderr, "%s: ", module);
  248. vfprintf(stderr, fmt, ap);
  249. fprintf(stderr, ".\n");
  250. }
  251. TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
  252. /*
  253. * Local Variables:
  254. * mode: c
  255. * c-basic-offset: 8
  256. * fill-column: 78
  257. * End:
  258. */