tif_win32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
  26. * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
  27. */
  28. #include "tiffiop.h"
  29. #include <windows.h>
  30. /*
  31. CreateFileA/CreateFileW return type 'HANDLE' while TIFFFdOpen() takes 'int',
  32. which is formally incompatible and can even seemingly be of different size:
  33. HANDLE is 64 bit under Win64, while int is still 32 bits there.
  34. However, only the lower 32 bits of a HANDLE are significant under Win64 as,
  35. for interoperability reasons, they must have the same values in 32- and
  36. 64-bit programs running on the same system, see
  37. https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
  38. Because of this, it is safe to define the following trivial functions for
  39. casting between ints and HANDLEs, which are only really needed to avoid
  40. compiler warnings (and, perhaps, to make the code slightly more clear).
  41. Note that using the intermediate cast to "intptr_t" is crucial for warning
  42. avoidance, as this integer type has the same size as HANDLE in all builds.
  43. */
  44. static inline thandle_t thandle_from_int(int ifd)
  45. {
  46. return (thandle_t)(intptr_t)ifd;
  47. }
  48. static inline int thandle_to_int(thandle_t fd)
  49. {
  50. return (int)(intptr_t)fd;
  51. }
  52. static tmsize_t
  53. _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
  54. {
  55. /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
  56. * 32bit sizes, so we loop through the data in suitable 32bit sized
  57. * chunks */
  58. uint8_t* ma;
  59. uint64_t mb;
  60. DWORD n;
  61. DWORD o;
  62. tmsize_t p;
  63. ma=(uint8_t*)buf;
  64. mb=size;
  65. p=0;
  66. while (mb>0)
  67. {
  68. n=0x80000000UL;
  69. if ((uint64_t)n>mb)
  70. n=(DWORD)mb;
  71. if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
  72. return(0);
  73. ma+=o;
  74. mb-=o;
  75. p+=o;
  76. if (o!=n)
  77. break;
  78. }
  79. return(p);
  80. }
  81. static tmsize_t
  82. _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
  83. {
  84. /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
  85. * 32bit sizes, so we loop through the data in suitable 32bit sized
  86. * chunks */
  87. uint8_t* ma;
  88. uint64_t mb;
  89. DWORD n;
  90. DWORD o;
  91. tmsize_t p;
  92. ma=(uint8_t*)buf;
  93. mb=size;
  94. p=0;
  95. while (mb>0)
  96. {
  97. n=0x80000000UL;
  98. if ((uint64_t)n>mb)
  99. n=(DWORD)mb;
  100. if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
  101. return(0);
  102. ma+=o;
  103. mb-=o;
  104. p+=o;
  105. if (o!=n)
  106. break;
  107. }
  108. return(p);
  109. }
  110. static uint64_t
  111. _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
  112. {
  113. LARGE_INTEGER offli;
  114. DWORD dwMoveMethod;
  115. offli.QuadPart = off;
  116. switch(whence)
  117. {
  118. case SEEK_SET:
  119. dwMoveMethod = FILE_BEGIN;
  120. break;
  121. case SEEK_CUR:
  122. dwMoveMethod = FILE_CURRENT;
  123. break;
  124. case SEEK_END:
  125. dwMoveMethod = FILE_END;
  126. break;
  127. default:
  128. dwMoveMethod = FILE_BEGIN;
  129. break;
  130. }
  131. offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
  132. if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
  133. offli.QuadPart=0;
  134. return(offli.QuadPart);
  135. }
  136. static int
  137. _tiffCloseProc(thandle_t fd)
  138. {
  139. return (CloseHandle(fd) ? 0 : -1);
  140. }
  141. static uint64_t
  142. _tiffSizeProc(thandle_t fd)
  143. {
  144. LARGE_INTEGER m;
  145. if (GetFileSizeEx(fd,&m))
  146. return(m.QuadPart);
  147. else
  148. return(0);
  149. }
  150. static int
  151. _tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
  152. {
  153. (void) fd;
  154. (void) pbase;
  155. (void) psize;
  156. return (0);
  157. }
  158. /*
  159. * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
  160. *
  161. * Windows uses both a handle and a pointer for file mapping,
  162. * but according to the SDK documentation and Richter's book
  163. * "Advanced Windows Programming" it is safe to free the handle
  164. * after obtaining the file mapping pointer
  165. *
  166. * This removes a nasty OS dependency and cures a problem
  167. * with Visual C++ 5.0
  168. */
  169. static int
  170. _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
  171. {
  172. uint64_t size;
  173. tmsize_t sizem;
  174. HANDLE hMapFile;
  175. size = _tiffSizeProc(fd);
  176. sizem = (tmsize_t)size;
  177. if (!size || (uint64_t)sizem!=size)
  178. return (0);
  179. /* By passing in 0 for the maximum file size, it specifies that we
  180. create a file mapping object for the full file size. */
  181. hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
  182. if (hMapFile == NULL)
  183. return (0);
  184. *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
  185. CloseHandle(hMapFile);
  186. if (*pbase == NULL)
  187. return (0);
  188. *psize = size;
  189. return(1);
  190. }
  191. static void
  192. _tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
  193. {
  194. (void) fd;
  195. (void) base;
  196. (void) size;
  197. }
  198. static void
  199. _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
  200. {
  201. (void) fd;
  202. (void) size;
  203. UnmapViewOfFile(base);
  204. }
  205. /*
  206. * Open a TIFF file descriptor for read/writing.
  207. * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
  208. * string, which forces the file to be opened unmapped.
  209. */
  210. TIFF*
  211. TIFFFdOpen(int ifd, const char* name, const char* mode)
  212. {
  213. TIFF* tif;
  214. int fSuppressMap;
  215. int m;
  216. fSuppressMap=0;
  217. for (m=0; mode[m]!=0; m++)
  218. {
  219. if (mode[m]=='u')
  220. {
  221. fSuppressMap=1;
  222. break;
  223. }
  224. }
  225. tif = TIFFClientOpen(name, mode, thandle_from_int(ifd),
  226. _tiffReadProc, _tiffWriteProc,
  227. _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
  228. fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
  229. fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
  230. if (tif)
  231. tif->tif_fd = ifd;
  232. return (tif);
  233. }
  234. #ifndef _WIN32_WCE
  235. /*
  236. * Open a TIFF file for read/writing.
  237. */
  238. TIFF*
  239. TIFFOpen(const char* name, const char* mode)
  240. {
  241. static const char module[] = "TIFFOpen";
  242. thandle_t fd;
  243. int m;
  244. DWORD dwMode;
  245. TIFF* tif;
  246. m = _TIFFgetMode(mode, module);
  247. switch(m) {
  248. case O_RDONLY: dwMode = OPEN_EXISTING; break;
  249. case O_RDWR: dwMode = OPEN_ALWAYS; break;
  250. case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
  251. case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  252. case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  253. default: return ((TIFF*)0);
  254. }
  255. fd = (thandle_t)CreateFileA(name,
  256. (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
  257. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
  258. (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
  259. NULL);
  260. if (fd == INVALID_HANDLE_VALUE) {
  261. TIFFErrorExt(0, module, "%s: Cannot open", name);
  262. return ((TIFF *)0);
  263. }
  264. tif = TIFFFdOpen(thandle_to_int(fd), name, mode);
  265. if(!tif)
  266. CloseHandle(fd);
  267. return tif;
  268. }
  269. /*
  270. * Open a TIFF file with a Unicode filename, for read/writing.
  271. */
  272. TIFF*
  273. TIFFOpenW(const wchar_t* name, const char* mode)
  274. {
  275. static const char module[] = "TIFFOpenW";
  276. thandle_t fd;
  277. int m;
  278. DWORD dwMode;
  279. int mbsize;
  280. char *mbname;
  281. TIFF *tif;
  282. m = _TIFFgetMode(mode, module);
  283. switch(m) {
  284. case O_RDONLY: dwMode = OPEN_EXISTING; break;
  285. case O_RDWR: dwMode = OPEN_ALWAYS; break;
  286. case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
  287. case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  288. case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
  289. default: return ((TIFF*)0);
  290. }
  291. fd = (thandle_t)CreateFileW(name,
  292. (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
  293. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
  294. (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
  295. NULL);
  296. if (fd == INVALID_HANDLE_VALUE) {
  297. TIFFErrorExt(0, module, "%S: Cannot open", name);
  298. return ((TIFF *)0);
  299. }
  300. mbname = NULL;
  301. mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
  302. if (mbsize > 0) {
  303. mbname = (char *)_TIFFmalloc(mbsize);
  304. if (!mbname) {
  305. TIFFErrorExt(0, module,
  306. "Can't allocate space for filename conversion buffer");
  307. return ((TIFF*)0);
  308. }
  309. WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
  310. NULL, NULL);
  311. }
  312. tif = TIFFFdOpen(thandle_to_int(fd),
  313. (mbname != NULL) ? mbname : "<unknown>", mode);
  314. if(!tif)
  315. CloseHandle(fd);
  316. _TIFFfree(mbname);
  317. return tif;
  318. }
  319. #endif /* ndef _WIN32_WCE */
  320. void*
  321. _TIFFmalloc(tmsize_t s)
  322. {
  323. if (s == 0)
  324. return ((void *) NULL);
  325. return (malloc((size_t) s));
  326. }
  327. void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
  328. {
  329. if( nmemb == 0 || siz == 0 )
  330. return ((void *) NULL);
  331. return calloc((size_t) nmemb, (size_t)siz);
  332. }
  333. void
  334. _TIFFfree(void* p)
  335. {
  336. free(p);
  337. }
  338. void*
  339. _TIFFrealloc(void* p, tmsize_t s)
  340. {
  341. return (realloc(p, (size_t) s));
  342. }
  343. void
  344. _TIFFmemset(void* p, int v, tmsize_t c)
  345. {
  346. memset(p, v, (size_t) c);
  347. }
  348. void
  349. _TIFFmemcpy(void* d, const void* s, tmsize_t c)
  350. {
  351. memcpy(d, s, (size_t) c);
  352. }
  353. int
  354. _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
  355. {
  356. return (memcmp(p1, p2, (size_t) c));
  357. }
  358. #ifndef _WIN32_WCE
  359. static void
  360. Win32WarningHandler(const char* module, const char* fmt, va_list ap)
  361. {
  362. if (module != NULL)
  363. fprintf(stderr, "%s: ", module);
  364. fprintf(stderr, "Warning, ");
  365. vfprintf(stderr, fmt, ap);
  366. fprintf(stderr, ".\n");
  367. }
  368. TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
  369. static void
  370. Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
  371. {
  372. if (module != NULL)
  373. fprintf(stderr, "%s: ", module);
  374. vfprintf(stderr, fmt, ap);
  375. fprintf(stderr, ".\n");
  376. }
  377. TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
  378. #endif /* ndef _WIN32_WCE */
  379. /* vim: set ts=8 sts=8 sw=8 noet: */
  380. /*
  381. * Local Variables:
  382. * mode: c
  383. * c-basic-offset: 8
  384. * fill-column: 78
  385. * End:
  386. */