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 _tiffReadProc(thandle_t fd
, tdata_t buf
, tsize_t size
) 
  38         if (!ReadFile(fd
, buf
, size
, &dwSizeRead
, NULL
)) 
  40         return ((tsize_t
) dwSizeRead
); 
  44 _tiffWriteProc(thandle_t fd
, tdata_t buf
, tsize_t size
) 
  47         if (!WriteFile(fd
, buf
, size
, &dwSizeWritten
, NULL
)) 
  49         return ((tsize_t
) dwSizeWritten
); 
  53 _tiffSeekProc(thandle_t fd
, toff_t off
, int whence
) 
  59                 dwMoveMethod 
= FILE_BEGIN
; 
  62                 dwMoveMethod 
= FILE_CURRENT
; 
  65                 dwMoveMethod 
= FILE_END
; 
  68                 dwMoveMethod 
= FILE_BEGIN
; 
  71         return ((toff_t
)SetFilePointer(fd
, off
, NULL
, dwMoveMethod
)); 
  75 _tiffCloseProc(thandle_t fd
) 
  77         return (CloseHandle(fd
) ? 0 : -1); 
  81 _tiffSizeProc(thandle_t fd
) 
  83         return ((toff_t
)GetFileSize(fd
, NULL
)); 
  87 _tiffDummyMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
) 
  93  * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>: 
  95  * Windows uses both a handle and a pointer for file mapping, 
  96  * but according to the SDK documentation and Richter's book 
  97  * "Advanced Windows Programming" it is safe to free the handle 
  98  * after obtaining the file mapping pointer 
 100  * This removes a nasty OS dependency and cures a problem 
 101  * with Visual C++ 5.0 
 104 _tiffMapProc(thandle_t fd
, tdata_t
* pbase
, toff_t
* psize
) 
 109         if ((size 
= _tiffSizeProc(fd
)) == (toff_t
)-1) 
 111         hMapFile 
= CreateFileMapping(fd
, NULL
, PAGE_READONLY
, 0, size
, NULL
); 
 112         if (hMapFile 
== NULL
) 
 114         *pbase 
= MapViewOfFile(hMapFile
, FILE_MAP_READ
, 0, 0, 0); 
 115         CloseHandle(hMapFile
); 
 123 _tiffDummyUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
) 
 128 _tiffUnmapProc(thandle_t fd
, tdata_t base
, toff_t size
) 
 130         UnmapViewOfFile(base
); 
 134  * Open a TIFF file descriptor for read/writing. 
 135  * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode 
 136  * string, which forces the file to be opened unmapped. 
 139 TIFFFdOpen(int ifd
, const char* name
, const char* mode
) 
 142         BOOL fSuppressMap 
= (mode
[1] == 'u' || mode
[2] == 'u'); 
 144         tif 
= TIFFClientOpen(name
, mode
, 
 146             _tiffReadProc
, _tiffWriteProc
, 
 147             _tiffSeekProc
, _tiffCloseProc
, _tiffSizeProc
, 
 148                  fSuppressMap 
? _tiffDummyMapProc 
: _tiffMapProc
, 
 149                  fSuppressMap 
? _tiffDummyUnmapProc 
: _tiffUnmapProc
); 
 156  * Open a TIFF file for read/writing. 
 159 TIFFOpen(const char* name
, const char* mode
) 
 161         static const char module[] = "TIFFOpen"; 
 166         m 
= _TIFFgetMode(mode
, module); 
 171                 dwMode 
= OPEN_EXISTING
; 
 174                 dwMode 
= OPEN_ALWAYS
; 
 177                 dwMode 
= OPEN_ALWAYS
; 
 180                 dwMode 
= CREATE_ALWAYS
; 
 182         case O_RDWR
|O_CREAT
|O_TRUNC
: 
 183                 dwMode 
= CREATE_ALWAYS
; 
 188         fd 
= (thandle_t
)CreateFile(name
, (m 
== O_RDONLY
) ? GENERIC_READ 
: 
 189                         (GENERIC_READ 
| GENERIC_WRITE
), FILE_SHARE_READ
, NULL
, dwMode
, 
 190                         (m 
== O_RDONLY
) ? FILE_ATTRIBUTE_READONLY 
: FILE_ATTRIBUTE_NORMAL
, NULL
); 
 191         if (fd 
== INVALID_HANDLE_VALUE
) { 
 192                 TIFFError(module, "%s: Cannot open", name
); 
 195         return (TIFFFdOpen((int)fd
, name
, mode
)); 
 199 _TIFFmalloc(tsize_t s
) 
 201         return ((tdata_t
)GlobalAlloc(GMEM_FIXED
, s
)); 
 212 _TIFFrealloc(tdata_t p
, tsize_t s
) 
 215         if ((pvTmp 
= GlobalReAlloc(p
, s
, 0)) == NULL
) { 
 216                 if ((pvTmp 
= GlobalAlloc(GMEM_FIXED
, s
)) != NULL
) { 
 217                         CopyMemory(pvTmp
, p
, GlobalSize(p
)); 
 221         return ((tdata_t
)pvTmp
); 
 225 _TIFFmemset(void* p
, int v
, tsize_t c
) 
 227         FillMemory(p
, c
, (BYTE
)v
); 
 231 _TIFFmemcpy(void* d
, const tdata_t s
, tsize_t c
) 
 237 _TIFFmemcmp(const tdata_t p1
, const tdata_t p2
, tsize_t c
) 
 239         register const BYTE 
*pb1 
= p1
; 
 240         register const BYTE 
*pb2 
= p2
; 
 241         register DWORD dwTmp 
= c
; 
 243         for (iTmp 
= 0; dwTmp
-- && !iTmp
; iTmp 
= (int)*pb1
++ - (int)*pb2
++) 
 249 Win32WarningHandler(const char* module, const char* fmt
, va_list ap
) 
 251 #ifndef TIF_PLATFORM_CONSOLE 
 254         LPCTSTR szTitleText 
= "%s Warning"; 
 255         LPCTSTR szDefaultModule 
= "TIFFLIB"; 
 256         szTmp 
= (module == NULL
) ? (LPTSTR
)szDefaultModule 
: (LPTSTR
)module; 
 257         if ((szTitle 
= (LPTSTR
)LocalAlloc(LMEM_FIXED
, (lstrlen(szTmp
) + 
 258                         lstrlen(szTitleText
) + lstrlen(fmt
) + 128)*sizeof(TCHAR
))) == NULL
) 
 260         wsprintf(szTitle
, szTitleText
, szTmp
); 
 261         szTmp 
= szTitle 
+ (lstrlen(szTitle
)+2)*sizeof(TCHAR
); 
 262         wvsprintf(szTmp
, fmt
, ap
); 
 263         MessageBox(GetFocus(), szTmp
, szTitle
, MB_OK 
| MB_ICONINFORMATION
); 
 268                 fprintf(stderr
, "%s: ", module); 
 269         fprintf(stderr
, "Warning, "); 
 270         vfprintf(stderr
, fmt
, ap
); 
 271         fprintf(stderr
, ".\n"); 
 274 TIFFErrorHandler _TIFFwarningHandler 
= Win32WarningHandler
; 
 277 Win32ErrorHandler(const char* module, const char* fmt
, va_list ap
) 
 279 #ifndef TIF_PLATFORM_CONSOLE 
 282         LPCTSTR szTitleText 
= "%s Error"; 
 283         LPCTSTR szDefaultModule 
= "TIFFLIB"; 
 284         szTmp 
= (module == NULL
) ? (LPTSTR
)szDefaultModule 
: (LPTSTR
)module; 
 285         if ((szTitle 
= (LPTSTR
)LocalAlloc(LMEM_FIXED
, (lstrlen(szTmp
) + 
 286                         lstrlen(szTitleText
) + lstrlen(fmt
) + 128)*sizeof(TCHAR
))) == NULL
) 
 288         wsprintf(szTitle
, szTitleText
, szTmp
); 
 289         szTmp 
= szTitle 
+ (lstrlen(szTitle
)+2)*sizeof(TCHAR
); 
 290         wvsprintf(szTmp
, fmt
, ap
); 
 291         MessageBox(GetFocus(), szTmp
, szTitle
, MB_OK 
| MB_ICONEXCLAMATION
); 
 296                 fprintf(stderr
, "%s: ", module); 
 297         vfprintf(stderr
, fmt
, ap
); 
 298         fprintf(stderr
, ".\n"); 
 301 TIFFErrorHandler _TIFFerrorHandler 
= Win32ErrorHandler
;