ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

tif_win32.c
Go to the documentation of this file.
00001 /* $Id: tif_win32.c,v 1.21.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
00002 
00003 /*
00004  * Copyright (c) 1988-1997 Sam Leffler
00005  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
00006  *
00007  * Permission to use, copy, modify, distribute, and sell this software and 
00008  * its documentation for any purpose is hereby granted without fee, provided
00009  * that (i) the above copyright notices and this permission notice appear in
00010  * all copies of the software and related documentation, and (ii) the names of
00011  * Sam Leffler and Silicon Graphics may not be used in any advertising or
00012  * publicity relating to the software without the specific, prior written
00013  * permission of Sam Leffler and Silicon Graphics.
00014  * 
00015  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
00016  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
00017  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
00018  * 
00019  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
00020  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
00021  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00022  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
00023  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
00024  * OF THIS SOFTWARE.
00025  */
00026 
00027 /*
00028  * TIFF Library Win32-specific Routines.  Adapted from tif_unix.c 4/5/95 by
00029  * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
00030  */
00031 #include "tiffiop.h"
00032 
00033 #include <windows.h>
00034 
00035 static tsize_t
00036 _tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
00037 {
00038     DWORD dwSizeRead;
00039     if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
00040         return(0);
00041     return ((tsize_t) dwSizeRead);
00042 }
00043 
00044 static tsize_t
00045 _tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
00046 {
00047     DWORD dwSizeWritten;
00048     if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
00049         return(0);
00050     return ((tsize_t) dwSizeWritten);
00051 }
00052 
00053 static toff_t
00054 _tiffSeekProc(thandle_t fd, toff_t off, int whence)
00055 {
00056         ULARGE_INTEGER li;
00057     DWORD dwMoveMethod;
00058 
00059     li.QuadPart = off;
00060         
00061     switch(whence)
00062     {
00063     case SEEK_SET:
00064         dwMoveMethod = FILE_BEGIN;
00065         break;
00066     case SEEK_CUR:
00067         dwMoveMethod = FILE_CURRENT;
00068         break;
00069     case SEEK_END:
00070         dwMoveMethod = FILE_END;
00071         break;
00072     default:
00073         dwMoveMethod = FILE_BEGIN;
00074         break;
00075     }
00076     return ((toff_t)SetFilePointer(fd, (LONG) li.LowPart,
00077                        (PLONG)&li.HighPart, dwMoveMethod));
00078 }
00079 
00080 static int
00081 _tiffCloseProc(thandle_t fd)
00082 {
00083     return (CloseHandle(fd) ? 0 : -1);
00084 }
00085 
00086 static toff_t
00087 _tiffSizeProc(thandle_t fd)
00088 {
00089     return ((toff_t)GetFileSize(fd, NULL));
00090 }
00091 
00092 static int
00093 _tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
00094 {
00095     (void) fd;
00096     (void) pbase;
00097     (void) psize;
00098     return (0);
00099 }
00100 
00101 /*
00102  * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:
00103  *
00104  * Windows uses both a handle and a pointer for file mapping,
00105  * but according to the SDK documentation and Richter's book
00106  * "Advanced Windows Programming" it is safe to free the handle
00107  * after obtaining the file mapping pointer
00108  *
00109  * This removes a nasty OS dependency and cures a problem
00110  * with Visual C++ 5.0
00111  */
00112 static int
00113 _tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
00114 {
00115     toff_t size;
00116     HANDLE hMapFile;
00117 
00118     if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
00119         return (0);
00120     hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
00121     if (hMapFile == NULL)
00122         return (0);
00123     *pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
00124     CloseHandle(hMapFile);
00125     if (*pbase == NULL)
00126         return (0);
00127     *psize = size;
00128     return(1);
00129 }
00130 
00131 static void
00132 _tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
00133 {
00134     (void) fd;
00135     (void) base;
00136     (void) size;
00137 }
00138 
00139 static void
00140 _tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
00141 {
00142     UnmapViewOfFile(base);
00143 }
00144 
00145 /*
00146  * Open a TIFF file descriptor for read/writing.
00147  * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
00148  * string, which forces the file to be opened unmapped.
00149  */
00150 TIFF*
00151 TIFFFdOpen(int ifd, const char* name, const char* mode)
00152 {
00153     TIFF* tif;
00154     BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
00155 
00156     tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
00157             _tiffReadProc, _tiffWriteProc,
00158             _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
00159             fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
00160             fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
00161     if (tif)
00162         tif->tif_fd = ifd;
00163     return (tif);
00164 }
00165 
00166 #ifndef _WIN32_WCE
00167 
00168 /*
00169  * Open a TIFF file for read/writing.
00170  */
00171 TIFF*
00172 TIFFOpen(const char* name, const char* mode)
00173 {
00174     static const char module[] = "TIFFOpen";
00175     thandle_t fd;
00176     int m;
00177     DWORD dwMode;
00178     TIFF* tif;
00179 
00180     m = _TIFFgetMode(mode, module);
00181 
00182     switch(m)
00183     {
00184     case O_RDONLY:
00185         dwMode = OPEN_EXISTING;
00186         break;
00187     case O_RDWR:
00188         dwMode = OPEN_ALWAYS;
00189         break;
00190     case O_RDWR|O_CREAT:
00191         dwMode = OPEN_ALWAYS;
00192         break;
00193     case O_RDWR|O_TRUNC:
00194         dwMode = CREATE_ALWAYS;
00195         break;
00196     case O_RDWR|O_CREAT|O_TRUNC:
00197         dwMode = CREATE_ALWAYS;
00198         break;
00199     default:
00200         return ((TIFF*)0);
00201     }
00202     fd = (thandle_t)CreateFileA(name,
00203         (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
00204         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
00205         (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
00206         NULL);
00207     if (fd == INVALID_HANDLE_VALUE) {
00208         TIFFErrorExt(0, module, "%s: Cannot open", name);
00209         return ((TIFF *)0);
00210     }
00211 
00212     tif = TIFFFdOpen((int)fd, name, mode);
00213     if(!tif)
00214         CloseHandle(fd);
00215     return tif;
00216 }
00217 
00218 /*
00219  * Open a TIFF file with a Unicode filename, for read/writing.
00220  */
00221 TIFF*
00222 TIFFOpenW(const wchar_t* name, const char* mode)
00223 {
00224     static const char module[] = "TIFFOpenW";
00225     thandle_t fd;
00226     int m;
00227     DWORD dwMode;
00228     int mbsize;
00229     char *mbname;
00230     TIFF *tif;
00231 
00232     m = _TIFFgetMode(mode, module);
00233 
00234     switch(m) {
00235         case O_RDONLY:          dwMode = OPEN_EXISTING; break;
00236         case O_RDWR:            dwMode = OPEN_ALWAYS;   break;
00237         case O_RDWR|O_CREAT:        dwMode = OPEN_ALWAYS;   break;
00238         case O_RDWR|O_TRUNC:        dwMode = CREATE_ALWAYS; break;
00239         case O_RDWR|O_CREAT|O_TRUNC:    dwMode = CREATE_ALWAYS; break;
00240         default:            return ((TIFF*)0);
00241     }
00242 
00243     fd = (thandle_t)CreateFileW(name,
00244         (m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
00245         FILE_SHARE_READ, NULL, dwMode,
00246         (m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
00247         NULL);
00248     if (fd == INVALID_HANDLE_VALUE) {
00249         TIFFErrorExt(0, module, "%S: Cannot open", name);
00250         return ((TIFF *)0);
00251     }
00252 
00253     mbname = NULL;
00254     mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
00255     if (mbsize > 0) {
00256         mbname = (char *)_TIFFmalloc(mbsize);
00257         if (!mbname) {
00258             TIFFErrorExt(0, module,
00259             "Can't allocate space for filename conversion buffer");
00260             return ((TIFF*)0);
00261         }
00262 
00263         WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
00264                     NULL, NULL);
00265     }
00266 
00267     tif = TIFFFdOpen((int)fd,
00268              (mbname != NULL) ? mbname : "<unknown>", mode);
00269     if(!tif)
00270         CloseHandle(fd);
00271 
00272     _TIFFfree(mbname);
00273 
00274     return tif;
00275 }
00276 
00277 #endif /* ndef _WIN32_WCE */
00278 
00279 
00280 tdata_t
00281 _TIFFmalloc(tsize_t s)
00282 {
00283     return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
00284 }
00285 
00286 void
00287 _TIFFfree(tdata_t p)
00288 {
00289     GlobalFree(p);
00290     return;
00291 }
00292 
00293 tdata_t
00294 _TIFFrealloc(tdata_t p, tsize_t s)
00295 {
00296     void* pvTmp;
00297     tsize_t old;
00298 
00299     if(p == NULL)
00300         return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
00301 
00302     old = GlobalSize(p);
00303 
00304     if (old>=s) {
00305         if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
00306             CopyMemory(pvTmp, p, s);
00307             GlobalFree(p);
00308         }
00309     } else {
00310         if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
00311             CopyMemory(pvTmp, p, old);
00312             GlobalFree(p);
00313         }
00314     }
00315     return ((tdata_t)pvTmp);
00316 }
00317 
00318 void
00319 _TIFFmemset(void* p, int v, tsize_t c)
00320 {
00321     FillMemory(p, c, (BYTE)v);
00322 }
00323 
00324 void
00325 _TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
00326 {
00327     CopyMemory(d, s, c);
00328 }
00329 
00330 int
00331 _TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
00332 {
00333     register const BYTE *pb1 = (const BYTE *) p1;
00334     register const BYTE *pb2 = (const BYTE *) p2;
00335     register DWORD dwTmp = c;
00336     register int iTmp;
00337     for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
00338         ;
00339     return (iTmp);
00340 }
00341 
00342 #ifndef _WIN32_WCE
00343 
00344 static void
00345 Win32WarningHandler(const char* module, const char* fmt, va_list ap)
00346 {
00347 #ifndef TIF_PLATFORM_CONSOLE
00348     LPTSTR szTitle;
00349     LPTSTR szTmp;
00350     LPCTSTR szTitleText = "%s Warning";
00351     LPCTSTR szDefaultModule = "LIBTIFF";
00352     LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
00353     if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
00354         strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
00355         return;
00356     sprintf(szTitle, szTitleText, szTmpModule);
00357     szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
00358     vsprintf(szTmp, fmt, ap);
00359     MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
00360     LocalFree(szTitle);
00361     return;
00362 #else
00363     if (module != NULL)
00364         fprintf(stderr, "%s: ", module);
00365     fprintf(stderr, "Warning, ");
00366     vfprintf(stderr, fmt, ap);
00367     fprintf(stderr, ".\n");
00368 #endif        
00369 }
00370 TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
00371 
00372 static void
00373 Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
00374 {
00375 #ifndef TIF_PLATFORM_CONSOLE
00376     LPTSTR szTitle;
00377     LPTSTR szTmp;
00378     LPCTSTR szTitleText = "%s Error";
00379     LPCTSTR szDefaultModule = "LIBTIFF";
00380     LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
00381     if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
00382         strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
00383         return;
00384     sprintf(szTitle, szTitleText, szTmpModule);
00385     szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
00386     vsprintf(szTmp, fmt, ap);
00387     MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
00388     LocalFree(szTitle);
00389     return;
00390 #else
00391     if (module != NULL)
00392         fprintf(stderr, "%s: ", module);
00393     vfprintf(stderr, fmt, ap);
00394     fprintf(stderr, ".\n");
00395 #endif        
00396 }
00397 TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
00398 
00399 #endif /* ndef _WIN32_WCE */
00400 
00401 /* vim: set ts=8 sts=8 sw=8 noet: */
00402 /*
00403  * Local Variables:
00404  * mode: c
00405  * c-basic-offset: 8
00406  * fill-column: 78
00407  * End:
00408  */

Generated on Fri May 25 2012 04:17:44 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.