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
35 /* This define is missing from VC6 headers. */
36 #ifndef INVALID_SET_FILE_POINTER
37 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
41 _tiffReadProc(thandle_t fd
, void* buf
, tmsize_t size
)
43 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
44 * 32bit sizes, so we loop through the data in suitable 32bit sized
59 if (!ReadFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
71 _tiffWriteProc(thandle_t fd
, void* buf
, tmsize_t size
)
73 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
74 * 32bit sizes, so we loop through the data in suitable 32bit sized
89 if (!WriteFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
101 _tiffSeekProc(thandle_t fd
, uint64 off
, int whence
)
105 offli
.QuadPart
= off
;
109 dwMoveMethod
= FILE_BEGIN
;
112 dwMoveMethod
= FILE_CURRENT
;
115 dwMoveMethod
= FILE_END
;
118 dwMoveMethod
= FILE_BEGIN
;
121 offli
.LowPart
=SetFilePointer(fd
,offli
.LowPart
,&offli
.HighPart
,dwMoveMethod
);
122 if ((offli
.LowPart
==INVALID_SET_FILE_POINTER
)&&(GetLastError()!=NO_ERROR
))
124 return(offli
.QuadPart
);
128 _tiffCloseProc(thandle_t fd
)
130 return (CloseHandle(fd
) ? 0 : -1);
134 _tiffSizeProc(thandle_t fd
)
137 m
.LowPart
=GetFileSize(fd
,&m
.HighPart
);
142 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
151 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
153 * Windows uses both a handle and a pointer for file mapping,
154 * but according to the SDK documentation and Richter's book
155 * "Advanced Windows Programming" it is safe to free the handle
156 * after obtaining the file mapping pointer
158 * This removes a nasty OS dependency and cures a problem
159 * with Visual C++ 5.0
162 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
168 size
= _tiffSizeProc(fd
);
169 sizem
= (tmsize_t
)size
;
170 if ((uint64
)sizem
!=size
)
173 /* By passing in 0 for the maximum file size, it specifies that we
174 create a file mapping object for the full file size. */
175 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
176 if (hMapFile
== NULL
)
178 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
179 CloseHandle(hMapFile
);
187 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
195 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
199 UnmapViewOfFile(base
);
203 * Open a TIFF file descriptor for read/writing.
204 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
205 * string, which forces the file to be opened unmapped.
208 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
214 for (m
=0; mode
[m
]!=0; m
++)
222 tif
= TIFFClientOpen(name
, mode
, (thandle_t
)ifd
,
223 _tiffReadProc
, _tiffWriteProc
,
224 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
225 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
226 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
235 * Open a TIFF file for read/writing.
238 TIFFOpen(const char* name
, const char* mode
)
240 static const char module[] = "TIFFOpen";
246 m
= _TIFFgetMode(mode
, module);
249 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
250 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
251 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
252 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
253 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
254 default: return ((TIFF
*)0);
257 fd
= (thandle_t
)CreateFileA(name
,
258 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
| GENERIC_WRITE
),
259 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
260 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
262 if (fd
== INVALID_HANDLE_VALUE
) {
263 TIFFErrorExt(0, module, "%s: Cannot open", name
);
267 tif
= TIFFFdOpen((int)fd
, name
, mode
);
274 * Open a TIFF file with a Unicode filename, for read/writing.
277 TIFFOpenW(const wchar_t* name
, const char* mode
)
279 static const char module[] = "TIFFOpenW";
287 m
= _TIFFgetMode(mode
, module);
290 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
291 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
292 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
293 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
294 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
295 default: return ((TIFF
*)0);
298 fd
= (thandle_t
)CreateFileW(name
,
299 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
|GENERIC_WRITE
),
300 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
301 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
303 if (fd
== INVALID_HANDLE_VALUE
) {
304 TIFFErrorExt(0, module, "%S: Cannot open", name
);
309 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
311 mbname
= (char *)_TIFFmalloc(mbsize
);
313 TIFFErrorExt(0, module,
314 "Can't allocate space for filename conversion buffer");
318 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
322 tif
= TIFFFdOpen((int)fd
,
323 (mbname
!= NULL
) ? mbname
: "<unknown>", mode
);
332 #endif /* ndef _WIN32_WCE */
335 _TIFFmalloc(tmsize_t s
)
337 return (malloc((size_t) s
));
347 _TIFFrealloc(void* p
, tmsize_t s
)
349 return (realloc(p
, (size_t) s
));
353 _TIFFmemset(void* p
, int v
, tmsize_t c
)
355 memset(p
, v
, (size_t) c
);
359 _TIFFmemcpy(void* d
, const void* s
, tmsize_t c
)
361 memcpy(d
, s
, (size_t) c
);
365 _TIFFmemcmp(const void* p1
, const void* p2
, tmsize_t c
)
367 return (memcmp(p1
, p2
, (size_t) c
));
372 #if (_MSC_VER < 1500)
373 # define vsnprintf _vsnprintf
377 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
)
379 #ifndef TIF_PLATFORM_CONSOLE
382 const char *szTitleText
= "%s Warning";
383 const char *szDefaultModule
= "LIBTIFF";
384 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
385 SIZE_T nBufSize
= (strlen(szTmpModule
) +
386 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
388 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
390 sprintf(szTitle
, szTitleText
, szTmpModule
);
391 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
392 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
393 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONINFORMATION
);
399 fprintf(stderr
, "%s: ", module);
400 fprintf(stderr
, "Warning, ");
401 vfprintf(stderr
, fmt
, ap
);
402 fprintf(stderr
, ".\n");
405 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
408 Win32ErrorHandler(const char *module, const char *fmt
, va_list ap
)
410 #ifndef TIF_PLATFORM_CONSOLE
413 const char *szTitleText
= "%s Error";
414 const char *szDefaultModule
= "LIBTIFF";
415 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
416 SIZE_T nBufSize
= (strlen(szTmpModule
) +
417 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
419 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
421 sprintf(szTitle
, szTitleText
, szTmpModule
);
422 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
423 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
424 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONEXCLAMATION
);
429 fprintf(stderr
, "%s: ", module);
430 vfprintf(stderr
, fmt
, ap
);
431 fprintf(stderr
, ".\n");
434 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;
436 #endif /* ndef _WIN32_WCE */
438 /* vim: set ts=8 sts=8 sw=8 noet: */