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
, tdata_t buf
, tsize_t size
)
39 if (!ReadFile(fd
, buf
, size
, &dwSizeRead
, NULL
))
41 return ((tsize_t
) dwSizeRead
);
45 _tiffWriteProc(thandle_t fd
, tdata_t buf
, tsize_t size
)
48 if (!WriteFile(fd
, buf
, size
, &dwSizeWritten
, NULL
))
50 return ((tsize_t
) dwSizeWritten
);
54 _tiffSeekProc(thandle_t fd
, toff_t off
, int whence
)
56 DWORD dwMoveMethod
, dwMoveHigh
;
58 /* we use this as a special code, so avoid accepting it */
59 if( off
== 0xFFFFFFFF )
65 dwMoveMethod
= FILE_BEGIN
;
68 dwMoveMethod
= FILE_CURRENT
;
71 dwMoveMethod
= FILE_END
;
74 dwMoveMethod
= FILE_BEGIN
;
78 return ((toff_t
)SetFilePointer(fd
, (LONG
) off
, (PLONG
)&dwMoveHigh
,
83 _tiffCloseProc(thandle_t fd
)
85 return (CloseHandle(fd
) ? 0 : -1);
89 _tiffSizeProc(thandle_t fd
)
91 return ((toff_t
)GetFileSize(fd
, NULL
));
98 _tiffDummyMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
104 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
106 * Windows uses both a handle and a pointer for file mapping,
107 * but according to the SDK documentation and Richter's book
108 * "Advanced Windows Programming" it is safe to free the handle
109 * after obtaining the file mapping pointer
111 * This removes a nasty OS dependency and cures a problem
112 * with Visual C++ 5.0
115 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
)
120 if ((size
= _tiffSizeProc(fd
)) == 0xFFFFFFFF)
122 hMapFile
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, size
, NULL
);
123 if (hMapFile
== NULL
)
125 *pbase
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0);
126 CloseHandle(hMapFile
);
137 _tiffDummyUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
142 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
)
144 UnmapViewOfFile(base
);
148 * Open a TIFF file descriptor for read/writing.
149 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
150 * string, which forces the file to be opened unmapped.
153 TIFFFdOpen(int ifd
, const char* name
, const char* mode
)
156 BOOL fSuppressMap
= (mode
[1] == 'u' || (mode
[1]!=0 && mode
[2] == 'u'));
158 tif
= TIFFClientOpen(name
, mode
, (thandle_t
)ifd
,
159 _tiffReadProc
, _tiffWriteProc
,
160 _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
,
161 fSuppressMap
? _tiffDummyMapProc
: _tiffMapProc
,
162 fSuppressMap
? _tiffDummyUnmapProc
: _tiffUnmapProc
);
169 * Open a TIFF file for read/writing.
172 TIFFOpen(const char* name
, const char* mode
)
174 static const char module[] = "TIFFOpen";
180 m
= _TIFFgetMode(mode
, module);
185 dwMode
= OPEN_EXISTING
;
188 dwMode
= OPEN_ALWAYS
;
191 dwMode
= OPEN_ALWAYS
;
194 dwMode
= CREATE_ALWAYS
;
196 case O_RDWR
|O_CREAT
|O_TRUNC
:
197 dwMode
= CREATE_ALWAYS
;
202 fd
= (thandle_t
)CreateFileA(name
,
203 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
| GENERIC_WRITE
),
204 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, dwMode
,
205 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
207 if (fd
== INVALID_HANDLE_VALUE
) {
208 TIFFErrorExt(0, module, "%s: Cannot open", name
);
212 tif
= TIFFFdOpen((int)fd
, name
, mode
);
219 * Open a TIFF file with a Unicode filename, for read/writing.
222 TIFFOpenW(const wchar_t* name
, const char* mode
)
224 static const char module[] = "TIFFOpenW";
232 m
= _TIFFgetMode(mode
, module);
235 case O_RDONLY
: dwMode
= OPEN_EXISTING
; break;
236 case O_RDWR
: dwMode
= OPEN_ALWAYS
; break;
237 case O_RDWR
|O_CREAT
: dwMode
= OPEN_ALWAYS
; break;
238 case O_RDWR
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
239 case O_RDWR
|O_CREAT
|O_TRUNC
: dwMode
= CREATE_ALWAYS
; break;
240 default: return ((TIFF
*)0);
243 fd
= (thandle_t
)CreateFileW(name
,
244 (m
== O_RDONLY
)?GENERIC_READ
:(GENERIC_READ
|GENERIC_WRITE
),
245 FILE_SHARE_READ
, NULL
, dwMode
,
246 (m
== O_RDONLY
)?FILE_ATTRIBUTE_READONLY
:FILE_ATTRIBUTE_NORMAL
,
248 if (fd
== INVALID_HANDLE_VALUE
) {
249 TIFFErrorExt(0, module, "%S: Cannot open", name
);
254 mbsize
= WideCharToMultiByte(CP_ACP
, 0, name
, -1, NULL
, 0, NULL
, NULL
);
256 mbname
= (char *)_TIFFmalloc(mbsize
);
258 TIFFErrorExt(0, module,
259 "Can't allocate space for filename conversion buffer");
263 WideCharToMultiByte(CP_ACP
, 0, name
, -1, mbname
, mbsize
,
267 tif
= TIFFFdOpen((int)fd
,
268 (mbname
!= NULL
) ? mbname
: "<unknown>", mode
);
278 _TIFFmalloc(tsize_t s
)
280 return ((tdata_t
)GlobalAlloc(GMEM_FIXED
, s
));
291 _TIFFrealloc(tdata_t p
, tsize_t s
)
297 return ((tdata_t
)GlobalAlloc(GMEM_FIXED
, s
));
302 if ((pvTmp
= GlobalAlloc(GMEM_FIXED
, s
)) != NULL
) {
303 CopyMemory(pvTmp
, p
, s
);
307 if ((pvTmp
= GlobalAlloc(GMEM_FIXED
, s
)) != NULL
) {
308 CopyMemory(pvTmp
, p
, old
);
312 return ((tdata_t
)pvTmp
);
316 _TIFFmemset(void* p
, int v
, tsize_t c
)
318 FillMemory(p
, c
, (BYTE
)v
);
322 _TIFFmemcpy(void* d
, const tdata_t s
, tsize_t c
)
328 _TIFFmemcmp(const tdata_t p1
, const tdata_t p2
, tsize_t c
)
330 register const BYTE
*pb1
= (const BYTE
*) p1
;
331 register const BYTE
*pb2
= (const BYTE
*) p2
;
332 register DWORD dwTmp
= c
;
334 for (iTmp
= 0; dwTmp
-- && !iTmp
; iTmp
= (int)*pb1
++ - (int)*pb2
++)
340 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
)
342 #ifndef TIF_PLATFORM_CONSOLE
345 const char *szTitleText
= "%s Warning";
346 const char *szDefaultModule
= "LIBTIFF";
347 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
348 if ((szTitle
= (char *)LocalAlloc(LMEM_FIXED
, (strlen(szTmpModule
) +
349 strlen(szTitleText
) + strlen(fmt
) + 128)*sizeof(char))) == NULL
)
351 sprintf(szTitle
, szTitleText
, szTmpModule
);
352 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
353 vsprintf(szTmp
, fmt
, ap
);
354 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONINFORMATION
);
359 fprintf(stderr
, "%s: ", module);
360 fprintf(stderr
, "Warning, ");
361 vfprintf(stderr
, fmt
, ap
);
362 fprintf(stderr
, ".\n");
365 TIFFErrorHandler _TIFFwarningHandler
= Win32WarningHandler
;
368 Win32ErrorHandler(const char* module, const char* fmt
, va_list ap
)
370 #ifndef TIF_PLATFORM_CONSOLE
373 const char *szTitleText
= "%s Error";
374 const char *szDefaultModule
= "LIBTIFF";
375 const char *szTmpModule
= (module == NULL
) ? szDefaultModule
: module;
376 if ((szTitle
= (char *)LocalAlloc(LMEM_FIXED
, (strlen(szTmpModule
) +
377 strlen(szTitleText
) + strlen(fmt
) + 128)*sizeof(char))) == NULL
)
379 sprintf(szTitle
, szTitleText
, szTmpModule
);
380 szTmp
= szTitle
+ (strlen(szTitle
)+2)*sizeof(char);
381 vsprintf(szTmp
, fmt
, ap
);
382 MessageBoxA(GetFocus(), szTmp
, szTitle
, MB_OK
| MB_ICONEXCLAMATION
);
387 fprintf(stderr
, "%s: ", module);
388 vfprintf(stderr
, fmt
, ap
);
389 fprintf(stderr
, ".\n");
392 TIFFErrorHandler _TIFFerrorHandler
= Win32ErrorHandler
;
394 /* vim: set ts=8 sts=8 sw=8 noet: */