2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
26 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
27 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
31 CreateFileA/CreateFileW return type 'HANDLE'.
33 thandle_t is declared like
35 DECLARE_HANDLE(thandle_t);
39 Windows (from winnt.h) DECLARE_HANDLE logic looks like
43 #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
46 #define DECLARE_HANDLE(name) typedef HANDLE name
49 See http://bugzilla.maptools.org/show_bug.cgi?id=1941 for problems in WIN64
50 builds resulting from this. Unfortunately, the proposed patch was lost.
63 _tiffReadProc(thandle_t fd
, void* buf
, tmsize_t size
)
65 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
66 * 32bit sizes, so we loop through the data in suitable 32bit sized
81 if (!ReadFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
93 _tiffWriteProc(thandle_t fd
, void* buf
, tmsize_t size
)
95 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
96 * 32bit sizes, so we loop through the data in suitable 32bit sized
111 if (!WriteFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
123 _tiffSeekProc(thandle_t fd
, uint64 off
, int whence
)
127 offli
.QuadPart
= off
;
131 dwMoveMethod
= FILE_BEGIN
;
134 dwMoveMethod
= FILE_CURRENT
;
137 dwMoveMethod
= FILE_END
;
140 dwMoveMethod
= FILE_BEGIN
;
143 offli
.LowPart
=SetFilePointer(fd
,offli
.LowPart
,&offli
.HighPart
,dwMoveMethod
);
144 if ((offli
.LowPart
==INVALID_SET_FILE_POINTER
)&&(GetLastError()!=NO_ERROR
))
146 return(offli
.QuadPart
);
150 _tiffCloseProc(thandle_t fd
)
152 return (CloseHandle(fd
) ? 0 : -1);
156 _tiffSizeProc(thandle_t fd
)
159 m
.LowPart
=GetFileSize(fd
,&m
.HighPart
);
164 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
173 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
175 * Windows uses both a handle and a pointer for file mapping,
176 * but according to the SDK documentation and Richter's book
177 * "Advanced Windows Programming" it is safe to free the handle
178 * after obtaining the file mapping pointer
180 * This removes a nasty OS dependency and cures a problem
181 * with Visual C++ 5.0
184 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
190 size
= _tiffSizeProc(fd
);
191 sizem
= (tmsize_t
)size
;
192 if ((uint64
)sizem
!=size
)
195 /* By passing in 0 for the maximum file size, it specifies that we
196 create a file mapping object for the full file size. */
197 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
198 if (hMapFile
== NULL
)
200 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
201 CloseHandle(hMapFile
);
209 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
217 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
221 UnmapViewOfFile(base
);
225 * Open a TIFF file descriptor for read/writing.
226 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
227 * string, which forces the file to be opened unmapped.
230 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
236 for (m
=0; mode
[m
]!=0; m
++)
244 tif
= TIFFClientOpen(name
, mode
, (thandle_t
)ifd
, /* FIXME: WIN64 cast to pointer warning */
245 _tiffReadProc
, _tiffWriteProc
,
246 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
247 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
248 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
257 * Open a TIFF file for read/writing.
260 TIFFOpen(const char* name
, const char* mode
)
262 static const char module
[] = "TIFFOpen";
268 m
= _TIFFgetMode(mode
, module
);
271 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
272 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
273 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
274 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
275 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
276 default: return ((TIFF
*)0);
279 fd
= (thandle_t
)CreateFileA(name
,
280 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
| GENERIC_WRITE
),
281 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
282 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
284 if (fd
== INVALID_HANDLE_VALUE
) {
285 TIFFErrorExt(0, module
, "%s: Cannot open", name
);
289 tif
= TIFFFdOpen((int)fd
, name
, mode
); /* FIXME: WIN64 cast from pointer to int warning */
296 * Open a TIFF file with a Unicode filename, for read/writing.
299 TIFFOpenW(const wchar_t* name
, const char* mode
)
301 static const char module
[] = "TIFFOpenW";
309 m
= _TIFFgetMode(mode
, module
);
312 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
313 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
314 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
315 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
316 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
317 default: return ((TIFF
*)0);
320 fd
= (thandle_t
)CreateFileW(name
,
321 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
|GENERIC_WRITE
),
322 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
323 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
325 if (fd
== INVALID_HANDLE_VALUE
) {
326 TIFFErrorExt(0, module
, "%S: Cannot open", name
);
331 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
333 mbname
= (char *)_TIFFmalloc(mbsize
);
335 TIFFErrorExt(0, module
,
336 "Can't allocate space for filename conversion buffer");
340 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
344 tif
= TIFFFdOpen((int)fd
, /* FIXME: WIN64 cast from pointer to int warning */
345 (mbname
!= NULL
) ? mbname
: "<unknown>", mode
);
354 #endif /* ndef _WIN32_WCE */
357 _TIFFmalloc(tmsize_t s
)
360 return ((void *) NULL
);
362 return (malloc((size_t) s
));
365 void* _TIFFcalloc(tmsize_t nmemb
, tmsize_t siz
)
367 if( nmemb
== 0 || siz
== 0 )
368 return ((void *) NULL
);
370 return calloc((size_t) nmemb
, (size_t)siz
);
380 _TIFFrealloc(void* p
, tmsize_t s
)
382 return (realloc(p
, (size_t) s
));
386 _TIFFmemset(void* p
, int v
, tmsize_t c
)
388 memset(p
, v
, (size_t) c
);
392 _TIFFmemcpy(void* d
, const void* s
, tmsize_t c
)
394 memcpy(d
, s
, (size_t) c
);
398 _TIFFmemcmp(const void* p1
, const void* p2
, tmsize_t c
)
400 return (memcmp(p1
, p2
, (size_t) c
));
405 #if (_MSC_VER < 1500)
406 # define vsnprintf _vsnprintf
410 Win32WarningHandler(const char* module
, const char* fmt
, va_list ap
)
413 fprintf(stderr
, "%s: ", module
);
414 fprintf(stderr
, "Warning, ");
415 vfprintf(stderr
, fmt
, ap
);
416 fprintf(stderr
, ".\n");
418 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
421 Win32ErrorHandler(const char* module
, const char* fmt
, va_list ap
)
424 fprintf(stderr
, "%s: ", module
);
425 vfprintf(stderr
, fmt
, ap
);
426 fprintf(stderr
, ".\n");
428 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;
430 #endif /* ndef _WIN32_WCE */
432 /* vim: set ts=8 sts=8 sw=8 noet: */