1 /* $Id: tif_win32.c,v 1.41 2015-08-23 20:12:44 bfriesen Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
29 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
33 CreateFileA/CreateFileW return type 'HANDLE'.
35 thandle_t is declared like
37 DECLARE_HANDLE(thandle_t);
41 Windows (from winnt.h) DECLARE_HANDLE logic looks like
45 #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
48 #define DECLARE_HANDLE(name) typedef HANDLE name
51 See http://bugzilla.maptools.org/show_bug.cgi?id=1941 for problems in WIN64
52 builds resulting from this. Unfortunately, the proposed patch was lost.
61 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
63 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
64 * 32bit sizes, so we loop through the data in suitable 32bit sized
79 if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
91 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
93 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
94 * 32bit sizes, so we loop through the data in suitable 32bit sized
109 if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
121 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
125 offli.QuadPart = off;
129 dwMoveMethod = FILE_BEGIN;
132 dwMoveMethod = FILE_CURRENT;
135 dwMoveMethod = FILE_END;
138 dwMoveMethod = FILE_BEGIN;
141 offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
142 if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
144 return(offli.QuadPart);
148 _tiffCloseProc(thandle_t fd)
150 return (CloseHandle(fd) ? 0 : -1);
154 _tiffSizeProc(thandle_t fd)
157 m.LowPart=GetFileSize(fd,&m.HighPart);
162 _tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
171 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
173 * Windows uses both a handle and a pointer for file mapping,
174 * but according to the SDK documentation and Richter's book
175 * "Advanced Windows Programming" it is safe to free the handle
176 * after obtaining the file mapping pointer
178 * This removes a nasty OS dependency and cures a problem
179 * with Visual C++ 5.0
182 _tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
188 size = _tiffSizeProc(fd);
189 sizem = (tmsize_t)size;
190 if ((uint64)sizem!=size)
193 /* By passing in 0 for the maximum file size, it specifies that we
194 create a file mapping object for the full file size. */
195 hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
196 if (hMapFile == NULL)
198 *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
199 CloseHandle(hMapFile);
207 _tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
215 _tiffUnmapProc(thandle_t fd, void* base, toff_t size)
219 UnmapViewOfFile(base);
223 * Open a TIFF file descriptor for read/writing.
224 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
225 * string, which forces the file to be opened unmapped.
228 TIFFFdOpen(int ifd, const char* name, const char* mode)
234 for (m=0; mode[m]!=0; m++)
242 tif = TIFFClientOpen(name, mode, (thandle_t)ifd, /* FIXME: WIN64 cast to pointer warning */
243 _tiffReadProc, _tiffWriteProc,
244 _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
245 fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
246 fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
255 * Open a TIFF file for read/writing.
258 TIFFOpen(const char* name, const char* mode)
260 static const char module[] = "TIFFOpen";
266 m = _TIFFgetMode(mode, module);
269 case O_RDONLY: dwMode = OPEN_EXISTING; break;
270 case O_RDWR: dwMode = OPEN_ALWAYS; break;
271 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
272 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
273 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
274 default: return ((TIFF*)0);
277 fd = (thandle_t)CreateFileA(name,
278 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
279 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
280 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
282 if (fd == INVALID_HANDLE_VALUE) {
283 TIFFErrorExt(0, module, "%s: Cannot open", name);
287 tif = TIFFFdOpen((int)fd, name, mode); /* FIXME: WIN64 cast from pointer to int warning */
294 * Open a TIFF file with a Unicode filename, for read/writing.
297 TIFFOpenW(const wchar_t* name, const char* mode)
299 static const char module[] = "TIFFOpenW";
307 m = _TIFFgetMode(mode, module);
310 case O_RDONLY: dwMode = OPEN_EXISTING; break;
311 case O_RDWR: dwMode = OPEN_ALWAYS; break;
312 case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
313 case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
314 case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
315 default: return ((TIFF*)0);
318 fd = (thandle_t)CreateFileW(name,
319 (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
320 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
321 (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
323 if (fd == INVALID_HANDLE_VALUE) {
324 TIFFErrorExt(0, module, "%S: Cannot open", name);
329 mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
331 mbname = (char *)_TIFFmalloc(mbsize);
333 TIFFErrorExt(0, module,
334 "Can't allocate space for filename conversion buffer");
338 WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
342 tif = TIFFFdOpen((int)fd, /* FIXME: WIN64 cast from pointer to int warning */
343 (mbname != NULL) ? mbname : "<unknown>", mode);
352 #endif /* ndef _WIN32_WCE */
355 _TIFFmalloc(tmsize_t s)
358 return ((void *) NULL);
360 return (malloc((size_t) s));
370 _TIFFrealloc(void* p, tmsize_t s)
372 return (realloc(p, (size_t) s));
376 _TIFFmemset(void* p, int v, tmsize_t c)
378 memset(p, v, (size_t) c);
382 _TIFFmemcpy(void* d, const void* s, tmsize_t c)
384 memcpy(d, s, (size_t) c);
388 _TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
390 return (memcmp(p1, p2, (size_t) c));
395 #if (_MSC_VER < 1500)
396 # define vsnprintf _vsnprintf
400 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
402 #ifndef TIF_PLATFORM_CONSOLE
405 LPCTSTR szTitleText = "%s Warning";
406 LPCTSTR szDefaultModule = "LIBTIFF";
407 LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
408 SIZE_T nBufSize = (strlen(szTmpModule) +
409 strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
411 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
413 sprintf(szTitle, szTitleText, szTmpModule);
414 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
415 vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
416 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
422 fprintf(stderr, "%s: ", module);
423 fprintf(stderr, "Warning, ");
424 vfprintf(stderr, fmt, ap);
425 fprintf(stderr, ".\n");
428 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
431 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
433 #ifndef TIF_PLATFORM_CONSOLE
436 LPCTSTR szTitleText = "%s Error";
437 LPCTSTR szDefaultModule = "LIBTIFF";
438 LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
439 SIZE_T nBufSize = (strlen(szTmpModule) +
440 strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
442 if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
444 sprintf(szTitle, szTitleText, szTmpModule);
445 szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
446 vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
447 MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
452 fprintf(stderr, "%s: ", module);
453 vfprintf(stderr, fmt, ap);
454 fprintf(stderr, ".\n");
457 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
459 #endif /* ndef _WIN32_WCE */
461 /* vim: set ts=8 sts=8 sw=8 noet: */