roentgen b75cab
/* $Id: tif_win32.c,v 1.39 2011-12-22 17:07:57 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library Win32-specific Routines.  Adapted from tif_unix.c 4/5/95 by
roentgen b75cab
 * Scott Wagner (wagner@itek.com), Itek Graphix, Rochester, NY USA
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
roentgen b75cab
#include <windows.h></windows.h>
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	/* tmsize_t is 64bit on 64bit systems, but the WinAPI ReadFile takes
roentgen b75cab
	 * 32bit sizes, so we loop through the data in suitable 32bit sized
roentgen b75cab
	 * chunks */
roentgen b75cab
	uint8* ma;
roentgen b75cab
	uint64 mb;
roentgen b75cab
	DWORD n;
roentgen b75cab
	DWORD o;
roentgen b75cab
	tmsize_t p;
roentgen b75cab
	ma=(uint8*)buf;
roentgen b75cab
	mb=size;
roentgen b75cab
	p=0;
roentgen b75cab
	while (mb>0)
roentgen b75cab
	{
roentgen b75cab
		n=0x80000000UL;
roentgen b75cab
		if ((uint64)n>mb)
roentgen b75cab
			n=(DWORD)mb;
roentgen b75cab
		if (!ReadFile(fd,(LPVOID)ma,n,&o,NULL))
roentgen b75cab
			return(0);
roentgen b75cab
		ma+=o;
roentgen b75cab
		mb-=o;
roentgen b75cab
		p+=o;
roentgen b75cab
		if (o!=n)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	return(p);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	/* tmsize_t is 64bit on 64bit systems, but the WinAPI WriteFile takes
roentgen b75cab
	 * 32bit sizes, so we loop through the data in suitable 32bit sized
roentgen b75cab
	 * chunks */
roentgen b75cab
	uint8* ma;
roentgen b75cab
	uint64 mb;
roentgen b75cab
	DWORD n;
roentgen b75cab
	DWORD o;
roentgen b75cab
	tmsize_t p;
roentgen b75cab
	ma=(uint8*)buf;
roentgen b75cab
	mb=size;
roentgen b75cab
	p=0;
roentgen b75cab
	while (mb>0)
roentgen b75cab
	{
roentgen b75cab
		n=0x80000000UL;
roentgen b75cab
		if ((uint64)n>mb)
roentgen b75cab
			n=(DWORD)mb;
roentgen b75cab
		if (!WriteFile(fd,(LPVOID)ma,n,&o,NULL))
roentgen b75cab
			return(0);
roentgen b75cab
		ma+=o;
roentgen b75cab
		mb-=o;
roentgen b75cab
		p+=o;
roentgen b75cab
		if (o!=n)
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	return(p);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static uint64
roentgen b75cab
_tiffSeekProc(thandle_t fd, uint64 off, int whence)
roentgen b75cab
{
roentgen b75cab
	LARGE_INTEGER offli;
roentgen b75cab
	DWORD dwMoveMethod;
roentgen b75cab
	offli.QuadPart = off;
roentgen b75cab
	switch(whence)
roentgen b75cab
	{
roentgen b75cab
		case SEEK_SET:
roentgen b75cab
			dwMoveMethod = FILE_BEGIN;
roentgen b75cab
			break;
roentgen b75cab
		case SEEK_CUR:
roentgen b75cab
			dwMoveMethod = FILE_CURRENT;
roentgen b75cab
			break;
roentgen b75cab
		case SEEK_END:
roentgen b75cab
			dwMoveMethod = FILE_END;
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			dwMoveMethod = FILE_BEGIN;
roentgen b75cab
			break;
roentgen b75cab
	}
roentgen b75cab
	offli.LowPart=SetFilePointer(fd,offli.LowPart,&offli.HighPart,dwMoveMethod);
roentgen b75cab
	if ((offli.LowPart==INVALID_SET_FILE_POINTER)&&(GetLastError()!=NO_ERROR))
roentgen b75cab
		offli.QuadPart=0;
roentgen b75cab
	return(offli.QuadPart);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
_tiffCloseProc(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	return (CloseHandle(fd) ? 0 : -1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static uint64
roentgen b75cab
_tiffSizeProc(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	ULARGE_INTEGER m;
roentgen b75cab
	m.LowPart=GetFileSize(fd,&m.HighPart);
roentgen b75cab
	return(m.QuadPart);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
_tiffDummyMapProc(thandle_t fd, void** pbase, toff_t* psize)
roentgen b75cab
{
roentgen b75cab
	(void) fd;
roentgen b75cab
	(void) pbase;
roentgen b75cab
	(void) psize;
roentgen b75cab
	return (0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * From "Hermann Josef Hill" <lhill@rhein-zeitung.de>:</lhill@rhein-zeitung.de>
roentgen b75cab
 *
roentgen b75cab
 * Windows uses both a handle and a pointer for file mapping,
roentgen b75cab
 * but according to the SDK documentation and Richter's book
roentgen b75cab
 * "Advanced Windows Programming" it is safe to free the handle
roentgen b75cab
 * after obtaining the file mapping pointer
roentgen b75cab
 *
roentgen b75cab
 * This removes a nasty OS dependency and cures a problem
roentgen b75cab
 * with Visual C++ 5.0
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
roentgen b75cab
{
roentgen b75cab
	uint64 size;
roentgen b75cab
	tmsize_t sizem;
roentgen b75cab
	HANDLE hMapFile;
roentgen b75cab
roentgen b75cab
	size = _tiffSizeProc(fd);
roentgen b75cab
	sizem = (tmsize_t)size;
roentgen b75cab
	if ((uint64)sizem!=size)
roentgen b75cab
		return (0);
roentgen b75cab
roentgen b75cab
	/* By passing in 0 for the maximum file size, it specifies that we
roentgen b75cab
	   create a file mapping object for the full file size. */
roentgen b75cab
	hMapFile = CreateFileMapping(fd, NULL, PAGE_READONLY, 0, 0, NULL);
roentgen b75cab
	if (hMapFile == NULL)
roentgen b75cab
		return (0);
roentgen b75cab
	*pbase = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
roentgen b75cab
	CloseHandle(hMapFile);
roentgen b75cab
	if (*pbase == NULL)
roentgen b75cab
		return (0);
roentgen b75cab
	*psize = size;
roentgen b75cab
	return(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
_tiffDummyUnmapProc(thandle_t fd, void* base, toff_t size)
roentgen b75cab
{
roentgen b75cab
	(void) fd;
roentgen b75cab
	(void) base;
roentgen b75cab
	(void) size;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
roentgen b75cab
{
roentgen b75cab
	(void) fd;
roentgen b75cab
	(void) size;
roentgen b75cab
	UnmapViewOfFile(base);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Open a TIFF file descriptor for read/writing.
roentgen b75cab
 * Note that TIFFFdOpen and TIFFOpen recognise the character 'u' in the mode
roentgen b75cab
 * string, which forces the file to be opened unmapped.
roentgen b75cab
 */
roentgen b75cab
TIFF*
roentgen b75cab
TIFFFdOpen(int ifd, const char* name, const char* mode)
roentgen b75cab
{
roentgen b75cab
	TIFF* tif;
roentgen b75cab
	int fSuppressMap;
roentgen b75cab
	int m;
roentgen b75cab
	fSuppressMap=0;
roentgen b75cab
	for (m=0; mode[m]!=0; m++)
roentgen b75cab
	{
roentgen b75cab
		if (mode[m]=='u')
roentgen b75cab
		{
roentgen b75cab
			fSuppressMap=1;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	tif = TIFFClientOpen(name, mode, (thandle_t)ifd,
roentgen b75cab
			_tiffReadProc, _tiffWriteProc,
roentgen b75cab
			_tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
roentgen b75cab
			fSuppressMap ? _tiffDummyMapProc : _tiffMapProc,
roentgen b75cab
			fSuppressMap ? _tiffDummyUnmapProc : _tiffUnmapProc);
roentgen b75cab
	if (tif)
roentgen b75cab
		tif->tif_fd = ifd;
roentgen b75cab
	return (tif);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifndef _WIN32_WCE
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Open a TIFF file for read/writing.
roentgen b75cab
 */
roentgen b75cab
TIFF*
roentgen b75cab
TIFFOpen(const char* name, const char* mode)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFOpen";
roentgen b75cab
	thandle_t fd;
roentgen b75cab
	int m;
roentgen b75cab
	DWORD dwMode;
roentgen b75cab
	TIFF* tif;
roentgen b75cab
roentgen b75cab
	m = _TIFFgetMode(mode, module);
roentgen b75cab
roentgen b75cab
	switch(m) {
roentgen b75cab
		case O_RDONLY:			dwMode = OPEN_EXISTING; break;
roentgen b75cab
		case O_RDWR:			dwMode = OPEN_ALWAYS;   break;
roentgen b75cab
		case O_RDWR|O_CREAT:		dwMode = OPEN_ALWAYS;   break;
roentgen b75cab
		case O_RDWR|O_TRUNC:		dwMode = CREATE_ALWAYS; break;
roentgen b75cab
		case O_RDWR|O_CREAT|O_TRUNC:	dwMode = CREATE_ALWAYS; break;
roentgen b75cab
		default:			return ((TIFF*)0);
roentgen b75cab
	}
roentgen b75cab
        
roentgen b75cab
	fd = (thandle_t)CreateFileA(name,
roentgen b75cab
		(m == O_RDONLY)?GENERIC_READ:(GENERIC_READ | GENERIC_WRITE),
roentgen b75cab
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
roentgen b75cab
		(m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
roentgen b75cab
		NULL);
roentgen b75cab
	if (fd == INVALID_HANDLE_VALUE) {
roentgen b75cab
		TIFFErrorExt(0, module, "%s: Cannot open", name);
roentgen b75cab
		return ((TIFF *)0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	tif = TIFFFdOpen((int)fd, name, mode);
roentgen b75cab
	if(!tif)
roentgen b75cab
		CloseHandle(fd);
roentgen b75cab
	return tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Open a TIFF file with a Unicode filename, for read/writing.
roentgen b75cab
 */
roentgen b75cab
TIFF*
roentgen b75cab
TIFFOpenW(const wchar_t* name, const char* mode)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFOpenW";
roentgen b75cab
	thandle_t fd;
roentgen b75cab
	int m;
roentgen b75cab
	DWORD dwMode;
roentgen b75cab
	int mbsize;
roentgen b75cab
	char *mbname;
roentgen b75cab
	TIFF *tif;
roentgen b75cab
roentgen b75cab
	m = _TIFFgetMode(mode, module);
roentgen b75cab
roentgen b75cab
	switch(m) {
roentgen b75cab
		case O_RDONLY:			dwMode = OPEN_EXISTING; break;
roentgen b75cab
		case O_RDWR:			dwMode = OPEN_ALWAYS;   break;
roentgen b75cab
		case O_RDWR|O_CREAT:		dwMode = OPEN_ALWAYS;   break;
roentgen b75cab
		case O_RDWR|O_TRUNC:		dwMode = CREATE_ALWAYS; break;
roentgen b75cab
		case O_RDWR|O_CREAT|O_TRUNC:	dwMode = CREATE_ALWAYS; break;
roentgen b75cab
		default:			return ((TIFF*)0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	fd = (thandle_t)CreateFileW(name,
roentgen b75cab
		(m == O_RDONLY)?GENERIC_READ:(GENERIC_READ|GENERIC_WRITE),
roentgen b75cab
		FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, dwMode,
roentgen b75cab
		(m == O_RDONLY)?FILE_ATTRIBUTE_READONLY:FILE_ATTRIBUTE_NORMAL,
roentgen b75cab
		NULL);
roentgen b75cab
	if (fd == INVALID_HANDLE_VALUE) {
roentgen b75cab
		TIFFErrorExt(0, module, "%S: Cannot open", name);
roentgen b75cab
		return ((TIFF *)0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	mbname = NULL;
roentgen b75cab
	mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
roentgen b75cab
	if (mbsize > 0) {
roentgen b75cab
		mbname = (char *)_TIFFmalloc(mbsize);
roentgen b75cab
		if (!mbname) {
roentgen b75cab
			TIFFErrorExt(0, module,
roentgen b75cab
			"Can't allocate space for filename conversion buffer");
roentgen b75cab
			return ((TIFF*)0);
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
roentgen b75cab
				    NULL, NULL);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	tif = TIFFFdOpen((int)fd,
roentgen b75cab
			 (mbname != NULL) ? mbname : "<unknown>", mode);</unknown>
roentgen b75cab
	if(!tif)
roentgen b75cab
		CloseHandle(fd);
roentgen b75cab
roentgen b75cab
	_TIFFfree(mbname);
roentgen b75cab
roentgen b75cab
	return tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#endif /* ndef _WIN32_WCE */
roentgen b75cab
roentgen b75cab
void*
roentgen b75cab
_TIFFmalloc(tmsize_t s)
roentgen b75cab
{
roentgen b75cab
	return (malloc((size_t) s));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFfree(void* p)
roentgen b75cab
{
roentgen b75cab
	free(p);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void*
roentgen b75cab
_TIFFrealloc(void* p, tmsize_t s)
roentgen b75cab
{
roentgen b75cab
	return (realloc(p, (size_t) s));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFmemset(void* p, int v, tmsize_t c)
roentgen b75cab
{
roentgen b75cab
	memset(p, v, (size_t) c);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFmemcpy(void* d, const void* s, tmsize_t c)
roentgen b75cab
{
roentgen b75cab
	memcpy(d, s, (size_t) c);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
roentgen b75cab
{
roentgen b75cab
	return (memcmp(p1, p2, (size_t) c));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifndef _WIN32_WCE
roentgen b75cab
roentgen b75cab
#if (_MSC_VER < 1500)
roentgen b75cab
#  define vsnprintf _vsnprintf
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Win32WarningHandler(const char* module, const char* fmt, va_list ap)
roentgen b75cab
{
roentgen b75cab
#ifndef TIF_PLATFORM_CONSOLE
roentgen b75cab
	LPTSTR szTitle;
roentgen b75cab
	LPTSTR szTmp;
roentgen b75cab
	LPCTSTR szTitleText = "%s Warning";
roentgen b75cab
	LPCTSTR szDefaultModule = "LIBTIFF";
roentgen b75cab
	LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
roentgen b75cab
        SIZE_T nBufSize = (strlen(szTmpModule) +
roentgen b75cab
                        strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
roentgen b75cab
roentgen b75cab
	if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
roentgen b75cab
		return;
roentgen b75cab
	sprintf(szTitle, szTitleText, szTmpModule);
roentgen b75cab
	szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
roentgen b75cab
	vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
roentgen b75cab
	MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONINFORMATION);
roentgen b75cab
	LocalFree(szTitle);
roentgen b75cab
roentgen b75cab
	return;
roentgen b75cab
#else
roentgen b75cab
	if (module != NULL)
roentgen b75cab
		fprintf(stderr, "%s: ", module);
roentgen b75cab
	fprintf(stderr, "Warning, ");
roentgen b75cab
	vfprintf(stderr, fmt, ap);
roentgen b75cab
	fprintf(stderr, ".\n");
roentgen b75cab
#endif        
roentgen b75cab
}
roentgen b75cab
TIFFErrorHandler _TIFFwarningHandler = Win32WarningHandler;
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
Win32ErrorHandler(const char* module, const char* fmt, va_list ap)
roentgen b75cab
{
roentgen b75cab
#ifndef TIF_PLATFORM_CONSOLE
roentgen b75cab
	LPTSTR szTitle;
roentgen b75cab
	LPTSTR szTmp;
roentgen b75cab
	LPCTSTR szTitleText = "%s Error";
roentgen b75cab
	LPCTSTR szDefaultModule = "LIBTIFF";
roentgen b75cab
	LPCTSTR szTmpModule = (module == NULL) ? szDefaultModule : module;
roentgen b75cab
        SIZE_T nBufSize = (strlen(szTmpModule) +
roentgen b75cab
                        strlen(szTitleText) + strlen(fmt) + 256)*sizeof(char);
roentgen b75cab
roentgen b75cab
	if ((szTitle = (LPTSTR)LocalAlloc(LMEM_FIXED, nBufSize)) == NULL)
roentgen b75cab
		return;
roentgen b75cab
	sprintf(szTitle, szTitleText, szTmpModule);
roentgen b75cab
	szTmp = szTitle + (strlen(szTitle)+2)*sizeof(char);
roentgen b75cab
	vsnprintf(szTmp, nBufSize-(strlen(szTitle)+2)*sizeof(char), fmt, ap);
roentgen b75cab
	MessageBoxA(GetFocus(), szTmp, szTitle, MB_OK | MB_ICONEXCLAMATION);
roentgen b75cab
	LocalFree(szTitle);
roentgen b75cab
	return;
roentgen b75cab
#else
roentgen b75cab
	if (module != NULL)
roentgen b75cab
		fprintf(stderr, "%s: ", module);
roentgen b75cab
	vfprintf(stderr, fmt, ap);
roentgen b75cab
	fprintf(stderr, ".\n");
roentgen b75cab
#endif        
roentgen b75cab
}
roentgen b75cab
TIFFErrorHandler _TIFFerrorHandler = Win32ErrorHandler;
roentgen b75cab
roentgen b75cab
#endif /* ndef _WIN32_WCE */
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */