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
36 _tiffReadProc(thandle_t fd
, void* buf
, tmsize_t size
)
38 /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
39 * 32bit sizes, so we loop through the data in suitable 32bit sized
54 if (!ReadFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
66 _tiffWriteProc(thandle_t fd
, void* buf
, tmsize_t size
)
68 /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
69 * 32bit sizes, so we loop through the data in suitable 32bit sized
84 if (!WriteFile(fd
,(LPVOID
)ma
,n
,&o
,NULL
))
96 _tiffSeekProc(thandle_t fd
, uint64 off
, int whence
)
100 offli
.QuadPart
= off
;
104 dwMoveMethod
= FILE_BEGIN
;
107 dwMoveMethod
= FILE_CURRENT
;
110 dwMoveMethod
= FILE_END
;
113 dwMoveMethod
= FILE_BEGIN
;
116 offli
.LowPart
=SetFilePointer(fd
,offli
.LowPart
,&offli
.HighPart
,dwMoveMethod
);
117 if ((offli
.LowPart
==INVALID_SET_FILE_POINTER
)&&(GetLastError()!=NO_ERROR
))
119 return(offli
.QuadPart
);
123 _tiffCloseProc(thandle_t fd
)
125 return (CloseHandle(fd
) ? 0 : -1);
129 _tiffSizeProc(thandle_t fd
)
132 m
.LowPart
=GetFileSize(fd
,&m
.HighPart
);
137 _tiffDummyMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
146 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
148 * Windows uses both a handle and a pointer for file mapping,
149 * but according to the SDK documentation and Richter's book
150 * "Advanced Windows Programming" it is safe to free the handle
151 * after obtaining the file mapping pointer
153 * This removes a nasty OS dependency and cures a problem
154 * with Visual C++ 5.0
157 _tiffMapProc(thandle_t fd
, void** pbase
, toff_t
* psize
)
163 size
= _tiffSizeProc(fd
);
164 sizem
= (tmsize_t
)size
;
165 if ((uint64
)sizem
!=size
)
168 /* By passing in 0 for the maximum file size, it specifies that we
169 create a file mapping object for the full file size. */
170 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
171 if (hMapFile
== NULL
)
173 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
174 CloseHandle(hMapFile
);
182 _tiffDummyUnmapProc(thandle_t fd
, void* base
, toff_t size
)
190 _tiffUnmapProc(thandle_t fd
, void* base
, toff_t size
)
194 UnmapViewOfFile(base
);
198 * Open a TIFF file descriptor for read/writing.
199 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
200 * string, which forces the file to be opened unmapped.
203 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
209 for (m
=0; mode
[m
]!=0; m
++)
217 tif
= TIFFClientOpen(name
, mode
, (thandle_t
)ifd
,
218 _tiffReadProc
, _tiffWriteProc
,
219 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
220 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
221 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
230 * Open a TIFF file for read/writing.
233 TIFFOpen(const char* name
, const char* mode
)
235 static const char module[] = "TIFFOpen";
241 m
= _TIFFgetMode(mode
, module);
244 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
245 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
246 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
247 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
248 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
249 default: return ((TIFF
*)0);
252 fd
= (thandle_t
)CreateFileA(name
,
253 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
| GENERIC_WRITE
),
254 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
255 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
257 if (fd
== INVALID_HANDLE_VALUE
) {
258 TIFFErrorExt(0, module, "%s: Cannot open", name
);
262 tif
= TIFFFdOpen((int)fd
, name
, mode
);
269 * Open a TIFF file with a Unicode filename, for read/writing.
272 TIFFOpenW(const wchar_t* name
, const char* mode
)
274 static const char module[] = "TIFFOpenW";
282 m
= _TIFFgetMode(mode
, module);
285 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
286 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
287 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
288 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
289 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
290 default: return ((TIFF
*)0);
293 fd
= (thandle_t
)CreateFileW(name
,
294 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
|GENERIC_WRITE
),
295 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
296 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
298 if (fd
== INVALID_HANDLE_VALUE
) {
299 TIFFErrorExt(0, module, "%S: Cannot open", name
);
304 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
306 mbname
= (char *)_TIFFmalloc(mbsize
);
308 TIFFErrorExt(0, module,
309 "Can't allocate space for filename conversion buffer");
313 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
317 tif
= TIFFFdOpen((int)fd
,
318 (mbname
!= NULL
) ? mbname
: "<unknown>", mode
);
327 #endif /* ndef _WIN32_WCE */
330 _TIFFmalloc(tmsize_t s
)
332 return (malloc((size_t) s
));
342 _TIFFrealloc(void* p
, tmsize_t s
)
344 return (realloc(p
, (size_t) s
));
348 _TIFFmemset(void* p
, int v
, tmsize_t c
)
350 memset(p
, v
, (size_t) c
);
354 _TIFFmemcpy(void* d
, const void* s
, tmsize_t c
)
356 memcpy(d
, s
, (size_t) c
);
360 _TIFFmemcmp(const void* p1
, const void* p2
, tmsize_t c
)
362 return (memcmp(p1
, p2
, (size_t) c
));
367 #if (_MSC_VER < 1500)
368 # define vsnprintf _vsnprintf
372 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
)
374 #ifndef TIF_PLATFORM_CONSOLE
377 const char *szTitleText
= "%s Warning";
378 const char *szDefaultModule
= "LIBTIFF";
379 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
380 SIZE_T nBufSize
= (strlen(szTmpModule
) +
381 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
383 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
385 sprintf(szTitle
, szTitleText
, szTmpModule
);
386 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
387 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
388 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONINFORMATION
);
394 fprintf(stderr
, "%s: ", module);
395 fprintf(stderr
, "Warning, ");
396 vfprintf(stderr
, fmt
, ap
);
397 fprintf(stderr
, ".\n");
400 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
403 Win32ErrorHandler(const char *module, const char *fmt
, va_list ap
)
405 #ifndef TIF_PLATFORM_CONSOLE
408 const char *szTitleText
= "%s Error";
409 const char *szDefaultModule
= "LIBTIFF";
410 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
411 SIZE_T nBufSize
= (strlen(szTmpModule
) +
412 strlen(szTitleText
) + strlen(fmt
) + 256)*sizeof(char);
414 if ((szTitle
= (char*)LocalAlloc(LMEM_FIXED
, nBufSize
)) == NULL
)
416 sprintf(szTitle
, szTitleText
, szTmpModule
);
417 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
418 vsnprintf(szTmp
, nBufSize
-(strlen(szTitle
)+2)*sizeof(char), fmt
, ap
);
419 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONEXCLAMATION
);
424 fprintf(stderr
, "%s: ", module);
425 vfprintf(stderr
, fmt
, ap
);
426 fprintf(stderr
, ".\n");
429 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;
431 #endif /* ndef _WIN32_WCE */
433 /* vim: set ts=8 sts=8 sw=8 noet: */