#include <windows.h>
-static tsize_t
-_tiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
+/* This define is missing from VC6 headers. */
+#ifndef INVALID_SET_FILE_POINTER
+#define INVALID_SET_FILE_POINTER ((DWORD)-1)
+#endif
+
+static tmsize_t
+_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
{
- DWORD dwSizeRead;
- if (!ReadFile(fd, buf, size, &dwSizeRead, NULL))
- return(0);
- return ((tsize_t) dwSizeRead);
+ /* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
+ * 32bit sizes, so we loop through the data in suitable 32bit sized
+ * chunks */
+ uint8* ma;
+ uint64 mb;
+ DWORD n;
+ DWORD o;
+ tmsize_t p;
+ ma=(uint8*)buf;
+ mb=size;
+ p=0;
+ while (mb>0)
+ {
+ n=0x80000000UL;
+ if ((uint64)n>mb)
+ n=(DWORD)mb;
+ if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
+ return(0);
+ ma+=o;
+ mb-=o;
+ p+=o;
+ if (o!=n)
+ break;
+ }
+ return(p);
}
-static tsize_t
-_tiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
+static tmsize_t
+_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
{
- DWORD dwSizeWritten;
- if (!WriteFile(fd, buf, size, &dwSizeWritten, NULL))
- return(0);
- return ((tsize_t) dwSizeWritten);
+ /* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
+ * 32bit sizes, so we loop through the data in suitable 32bit sized
+ * chunks */
+ uint8* ma;
+ uint64 mb;
+ DWORD n;
+ DWORD o;
+ tmsize_t p;
+ ma=(uint8*)buf;
+ mb=size;
+ p=0;
+ while (mb>0)
+ {
+ n=0x80000000UL;
+ if ((uint64)n>mb)
+ n=(DWORD)mb;
+ if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
+ return(0);
+ ma+=o;
+ mb-=o;
+ p+=o;
+ if (o!=n)
+ break;
+ }
+ return(p);
}
-static toff_t
-_tiffSeekProc(thandle_t fd, toff_t off, int whence)
+static uint64
+_tiffSeekProc(thandle_t fd, uint64 off, int whence)
{
- DWORD dwMoveMethod, dwMoveHigh;
-
- /* we use this as a special code, so avoid accepting it */
- if( off == 0xFFFFFFFF )
- return 0xFFFFFFFF;
-
+ LARGE_INTEGER offli;
+ DWORD dwMoveMethod;
+ offli.QuadPart = off;
switch(whence)
{
- case SEEK_SET:
- dwMoveMethod = FILE_BEGIN;
- break;
- case SEEK_CUR:
- dwMoveMethod = FILE_CURRENT;
- break;
- case SEEK_END:
- dwMoveMethod = FILE_END;
- break;
- default:
- dwMoveMethod = FILE_BEGIN;
- break;
+ case SEEK_SET:
+ dwMoveMethod = FILE_BEGIN;
+ break;
+ case SEEK_CUR:
+ dwMoveMethod = FILE_CURRENT;
+ break;
+ case SEEK_END:
+ dwMoveMethod = FILE_END;
+ break;
+ default:
+ dwMoveMethod = FILE_BEGIN;
+ break;
}
- dwMoveHigh = 0;
- return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
- dwMoveMethod));
+ offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
+ if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
+ offli.QuadPart=0;
+ return(offli.QuadPart);
}
static int
return (CloseHandle(fd) ? 0 : -1);
}
-static toff_t
+static uint64
_tiffSizeProc(thandle_t fd)
{
- return ((toff_t)GetFileSize(fd, NULL));
+ ULARGE_INTEGER m;
+ m.LowPart=GetFileSize(fd,&m.HighPart);
+ return(m.QuadPart);
}
-#ifdef __BORLANDC__
-#pragma argsused
-#endif
static int
-_tiffDummyMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
+_tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
{
+ (void) fd;
+ (void) pbase;
+ (void) psize;
return (0);
}
* with Visual C++ 5.0
*/
static int
-_tiffMapProc(thandle_t fd, tdata_t* pbase, toff_t* psize)
+_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
{
- toff_t size;
+ uint64 size;
+ tmsize_t sizem;
HANDLE hMapFile;
- if ((size = _tiffSizeProc(fd)) == 0xFFFFFFFF)
+ size = _tiffSizeProc(fd);
+ sizem = (tmsize_t)size;
+ if ((uint64)sizem!=size)
return (0);
- hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, size, NULL);
+
+ /* By passing in 0 for the maximum file size, it specifies that we
+ create a file mapping object for the full file size. */
+ hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapFile == NULL)
return (0);
*pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
return(1);
}
-#ifdef __BORLANDC__
-#pragma argsused
-#endif
static void
-_tiffDummyUnmapProc(thandle_t fd, tdata_t base, toff_t size)
+_tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
{
+ (void) fd;
+ (void) base;
+ (void) size;
}
static void
-_tiffUnmapProc(thandle_t fd, tdata_t base, toff_t size)
+_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
{
+ (void) fd;
+ (void) size;
UnmapViewOfFile(base);
}
TIFFFdOpen(int ifd, const char* name, const char* mode)
{
TIFF* tif;
- BOOL fSuppressMap = (mode[1] == 'u' || (mode[1]!=0 && mode[2] == 'u'));
-
+ int fSuppressMap;
+ int m;
+ fSuppressMap=0;
+ for (m=0; mode[m]!=0; m++)
+ {
+ if (mode[m]=='u')
+ {
+ fSuppressMap=1;
+ break;
+ }
+ }
tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
_tiffReadProc, _tiffWriteProc,
_tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
return (tif);
}
+#ifndef _WIN32_WCE
+
/*
* Open a TIFF file for read/writing.
*/
m = _TIFFgetMode(mode, module);
- switch(m)
- {
- case O_RDONLY:
- dwMode = OPEN_EXISTING;
- break;
- case O_RDWR:
- dwMode = OPEN_ALWAYS;
- break;
- case O_RDWR|O_CREAT:
- dwMode = OPEN_ALWAYS;
- break;
- case O_RDWR|O_TRUNC:
- dwMode = CREATE_ALWAYS;
- break;
- case O_RDWR|O_CREAT|O_TRUNC:
- dwMode = CREATE_ALWAYS;
- break;
- default:
- return ((TIFF*)0);
+ switch(m) {
+ case O_RDONLY: dwMode = OPEN_EXISTING; break;
+ case O_RDWR: dwMode = OPEN_ALWAYS; break;
+ case O_RDWR|O_CREAT: dwMode = OPEN_ALWAYS; break;
+ case O_RDWR|O_TRUNC: dwMode = CREATE_ALWAYS; break;
+ case O_RDWR|O_CREAT|O_TRUNC: dwMode = CREATE_ALWAYS; break;
+ default: return ((TIFF*)0);
}
+
fd = (thandle_t)CreateFileA(name,
(m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
fd = (thandle_t)CreateFileW(name,
(m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
- FILE_SHARE_READ, NULL, dwMode,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
(m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
NULL);
if (fd == INVALID_HANDLE_VALUE) {
return tif;
}
-tdata_t
-_TIFFmalloc(tsize_t s)
+#endif /* ndef _WIN32_WCE */
+
+void*
+_TIFFmalloc(tmsize_t s)
{
- return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
+ return (malloc((size_t) s));
}
void
-_TIFFfree(tdata_t p)
+_TIFFfree(void* p)
{
- GlobalFree(p);
- return;
+ free(p);
}
-tdata_t
-_TIFFrealloc(tdata_t p, tsize_t s)
+void*
+_TIFFrealloc(void* p, tmsize_t s)
{
- void* pvTmp;
- tsize_t old;
-
- if(p == NULL)
- return ((tdata_t)GlobalAlloc(GMEM_FIXED, s));
-
- old = GlobalSize(p);
-
- if (old>=s) {
- if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
- CopyMemory(pvTmp, p, s);
- GlobalFree(p);
- }
- } else {
- if ((pvTmp = GlobalAlloc(GMEM_FIXED, s)) != NULL) {
- CopyMemory(pvTmp, p, old);
- GlobalFree(p);
- }
- }
- return ((tdata_t)pvTmp);
+ return (realloc(p, (size_t) s));
}
void
-_TIFFmemset(void* p, int v, tsize_t c)
+_TIFFmemset(void* p, int v, tmsize_t c)
{
- FillMemory(p, c, (BYTE)v);
+ memset(p, v, (size_t) c);
}
void
-_TIFFmemcpy(void* d, const tdata_t s, tsize_t c)
+_TIFFmemcpy(void* d, const void* s, tmsize_t c)
{
- CopyMemory(d, s, c);
+ memcpy(d, s, (size_t) c);
}
int
-_TIFFmemcmp(const tdata_t p1, const tdata_t p2, tsize_t c)
+_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
{
- register const BYTE *pb1 = (const BYTE *) p1;
- register const BYTE *pb2 = (const BYTE *) p2;
- register DWORD dwTmp = c;
- register int iTmp;
- for (iTmp = 0; dwTmp-- && !iTmp; iTmp = (int)*pb1++ - (int)*pb2++)
- ;
- return (iTmp);
+ return (memcmp(p1, p2, (size_t) c));
}
+#ifndef _WIN32_WCE
+
+#if defined(_MSC_VER) && (_MSC_VER < 1500)
+# define vsnprintf _vsnprintf
+#endif
+
static void
Win32WarningHandler(const char* module, const char* fmt, va_list ap)
{
#ifndef TIF_PLATFORM_CONSOLE
- LPTSTR szTitle;
- LPTSTR szTmp;
- LPCTSTR szTitleText = "%s Warning";
- LPCTSTR szDefaultModule = "LIBTIFF";
- LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
- if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
- strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
+ char *szTitle;
+ char *szTmp;
+ const char *szTitleText = "%s Warning";
+ const char *szDefaultModule = "LIBTIFF";
+ const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
+ SIZE_T nBufSize = (strlen(szTmpModule) +
+ strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
+
+ if ((szTitle = (char*)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
return;
sprintf(szTitle, szTitleText, szTmpModule);
szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
- vsprintf(szTmp, fmt, ap);
+ vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
LocalFree(szTitle);
+
return;
#else
if (module != NULL)
TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
static void
-Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
+Win32ErrorHandler(const char *module, const char *fmt, va_list ap)
{
#ifndef TIF_PLATFORM_CONSOLE
- LPTSTR szTitle;
- LPTSTR szTmp;
- LPCTSTR szTitleText = "%s Error";
- LPCTSTR szDefaultModule = "LIBTIFF";
- LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
- if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, (strlen(szTmpModule) +
- strlen(szTitleText) + strlen(fmt) + 128)*sizeof(char))) == NULL)
+ char *szTitle;
+ char *szTmp;
+ const char *szTitleText = "%s Error";
+ const char *szDefaultModule = "LIBTIFF";
+ const char *szTmpModule = (module == NULL) ? szDefaultModule : module;
+ SIZE_T nBufSize = (strlen(szTmpModule) +
+ strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
+
+ if ((szTitle = (char*)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
return;
sprintf(szTitle, szTitleText, szTmpModule);
szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
- vsprintf(szTmp, fmt, ap);
+ vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
LocalFree(szTitle);
return;
}
TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
+#endif /* ndef _WIN32_WCE */
+
/* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */