3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
27 * TIFF Library Win32-specific Routines. Adapted from tif_unix.c 4/5/95 by
28 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
34 /* This define is missing from VC6 headers. */
35 #ifndef INVALID_SET_FILE_POINTER
36 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
40 _tiffReadProc(thandle_t fd
, void* buf
, tmsize_t size
)
42 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
43 * 32bit sizes, so we loop through the data in suitable 32bit sized
58 if (!ReadFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
70 _tiffWriteProc(thandle_t fd
, void* buf
, tmsize_t size
)
72 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
73 * 32bit sizes, so we loop through the data in suitable 32bit sized
88 if (!WriteFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
100 _tiffSeekProc(thandle_t fd
, uint64 off
, int whence
)
104 offli
.QuadPart
= off
;
108 dwMoveMethod
= FILE_BEGIN
;
111 dwMoveMethod
= FILE_CURRENT
;
114 dwMoveMethod
= FILE_END
;
117 dwMoveMethod
= FILE_BEGIN
;
120 offli
.LowPart
=SetFilePointer(fd
,offli
.LowPart
,&offli
.HighPart
,dwMoveMethod
);
121 if ((offli
.LowPart
==INVALID_SET_FILE_POINTER
)&&(GetLastError()!=NO_ERROR
))
123 return(offli
.QuadPart
);
127 _tiffCloseProc(thandle_t fd
)
129 return (CloseHandle(fd
) ? 0 : -1);
133 _tiffSizeProc(thandle_t fd
)
136 m
.LowPart
=GetFileSize(fd
,&m
.HighPart
);
141 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
150 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
152 * Windows uses both a handle and a pointer for file mapping,
153 * but according to the SDK documentation and Richter's book
154 * "Advanced Windows Programming" it is safe to free the handle
155 * after obtaining the file mapping pointer
157 * This removes a nasty OS dependency and cures a problem
158 * with Visual C++ 5.0
161 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
167 size
= _tiffSizeProc(fd
);
168 sizem
= (tmsize_t
)size
;
169 if ((uint64
)sizem
!=size
)
172 /* By passing in 0 for the maximum file size, it specifies that we
173 create a file mapping object for the full file size. */
174 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
175 if (hMapFile
== NULL
)
177 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
178 CloseHandle(hMapFile
);
186 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
194 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
198 UnmapViewOfFile(base
);
202 * Open a TIFF file descriptor for read/writing.
203 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
204 * string, which forces the file to be opened unmapped.
207 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
213 for (m
=0; mode
[m
]!=0; m
++)
221 tif
= TIFFClientOpen(name
, mode
, (thandle_t
)ifd
,
222 _tiffReadProc
, _tiffWriteProc
,
223 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
224 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
225 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
234 * Open a TIFF file for read/writing.
237 TIFFOpen(const char* name
, const char* mode
)
239 static const char module[] = "TIFFOpen";
245 m
= _TIFFgetMode(mode
, module);
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);
256 fd
= (thandle_t
)CreateFileA(name
,
257 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
| GENERIC_WRITE
),
258 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
259 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
261 if (fd
== INVALID_HANDLE_VALUE
) {
262 TIFFErrorExt(0, module, "%s: Cannot open", name
);
266 tif
= TIFFFdOpen((int)fd
, name
, mode
);
273 * Open a TIFF file with a Unicode filename, for read/writing.
276 TIFFOpenW(const wchar_t* name
, const char* mode
)
278 static const char module[] = "TIFFOpenW";
286 m
= _TIFFgetMode(mode
, module);
289 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
290 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
291 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
292 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
293 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
294 default: return ((TIFF
*)0);
297 fd
= (thandle_t
)CreateFileW(name
,
298 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
|GENERIC_WRITE
),
299 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
300 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
302 if (fd
== INVALID_HANDLE_VALUE
) {
303 TIFFErrorExt(0, module, "%S: Cannot open", name
);
308 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
310 mbname
= (char *)_TIFFmalloc(mbsize
);
312 TIFFErrorExt(0, module,
313 "Can't allocate space for filename conversion buffer");
317 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
321 tif
= TIFFFdOpen((int)fd
,
322 (mbname
!= NULL
) ? mbname
: "<unknown>", mode
);
331 #endif /* ndef _WIN32_WCE */
334 _TIFFmalloc(tmsize_t s
)
336 return (malloc((size_t) s
));
346 _TIFFrealloc(void* p
, tmsize_t s
)
348 return (realloc(p
, (size_t) s
));
352 _TIFFmemset(void* p
, int v
, tmsize_t c
)
354 memset(p
, v
, (size_t) c
);
358 _TIFFmemcpy(void* d
, const void* s
, tmsize_t c
)
360 memcpy(d
, s
, (size_t) c
);
364 _TIFFmemcmp(const void* p1
, const void* p2
, tmsize_t c
)
366 return (memcmp(p1
, p2
, (size_t) c
));
371 #if defined(_MSC_VER) && (_MSC_VER < 1500)
372 # define vsnprintf _vsnprintf
376 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
)
378 #ifndef TIF_PLATFORM_CONSOLE
381 const char *szTitleText
= "%s Warning";
382 const char *szDefaultModule
= "LIBTIFF";
383 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
384 SIZE_T nBufSize
= (strlen(szTmpModule
) +
385 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
387 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
389 sprintf(szTitle
, szTitleText
, szTmpModule
);
390 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
391 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
392 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONINFORMATION
);
398 fprintf(stderr
, "%s: ", module);
399 fprintf(stderr
, "Warning, ");
400 vfprintf(stderr
, fmt
, ap
);
401 fprintf(stderr
, ".\n");
404 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
407 Win32ErrorHandler(const char *module, const char *fmt
, va_list ap
)
409 #ifndef TIF_PLATFORM_CONSOLE
412 const char *szTitleText
= "%s Error";
413 const char *szDefaultModule
= "LIBTIFF";
414 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
415 SIZE_T nBufSize
= (strlen(szTmpModule
) +
416 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
418 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
420 sprintf(szTitle
, szTitleText
, szTmpModule
);
421 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
422 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
423 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONEXCLAMATION
);
428 fprintf(stderr
, "%s: ", module);
429 vfprintf(stderr
, fmt
, ap
);
430 fprintf(stderr
, ".\n");
433 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;
435 #endif /* ndef _WIN32_WCE */
437 /* vim: set ts=8 sts=8 sw=8 noet: */