roentgen b75cab
/* $Id: tif_unix.c,v 1.23 2012-06-01 21:40:59 fwarmerdam 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 UNIX-specific Routines. These are should also work with the
roentgen b75cab
 * Windows Common RunTime Library.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tif_config.h"
roentgen b75cab
roentgen b75cab
#ifdef HAVE_SYS_TYPES_H
roentgen b75cab
# include <sys types.h=""></sys>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#include <errno.h></errno.h>
roentgen b75cab
roentgen b75cab
#include <stdarg.h></stdarg.h>
roentgen b75cab
#include <stdlib.h></stdlib.h>
roentgen b75cab
#include <sys stat.h=""></sys>
roentgen b75cab
roentgen b75cab
#ifdef HAVE_UNISTD_H
roentgen b75cab
# include <unistd.h></unistd.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef HAVE_FCNTL_H
roentgen b75cab
# include <fcntl.h></fcntl.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef HAVE_IO_H
roentgen b75cab
# include <io.h></io.h>
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	size_t size_io = (size_t) size;
roentgen b75cab
	if ((tmsize_t) size_io != size)
roentgen b75cab
	{
roentgen b75cab
		errno=EINVAL;
roentgen b75cab
		return (tmsize_t) -1;
roentgen b75cab
	}
roentgen b75cab
	return ((tmsize_t) read((int) fd, buf, size_io));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	size_t size_io = (size_t) size;
roentgen b75cab
	if ((tmsize_t) size_io != size)
roentgen b75cab
	{
roentgen b75cab
		errno=EINVAL;
roentgen b75cab
		return (tmsize_t) -1;
roentgen b75cab
	}
roentgen b75cab
	return ((tmsize_t) write((int) fd, buf, size_io));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static uint64
roentgen b75cab
_tiffSeekProc(thandle_t fd, uint64 off, int whence)
roentgen b75cab
{
roentgen b75cab
	off_t off_io = (off_t) off;
roentgen b75cab
	if ((uint64) off_io != off)
roentgen b75cab
	{
roentgen b75cab
		errno=EINVAL;
roentgen b75cab
		return (uint64) -1; /* this is really gross */
roentgen b75cab
	}
roentgen b75cab
	return((uint64)lseek((int)fd,off_io,whence));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
_tiffCloseProc(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	return(close((int)fd));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static uint64
roentgen b75cab
_tiffSizeProc(thandle_t fd)
roentgen b75cab
{
roentgen b75cab
	struct stat sb;
roentgen b75cab
	if (fstat((int)fd,&sb)<0)
roentgen b75cab
		return(0);
roentgen b75cab
	else
roentgen b75cab
		return((uint64)sb.st_size);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef HAVE_MMAP
roentgen b75cab
#include <sys mman.h=""></sys>
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
roentgen b75cab
{
roentgen b75cab
	uint64 size64 = _tiffSizeProc(fd);
roentgen b75cab
	tmsize_t sizem = (tmsize_t)size64;
roentgen b75cab
	if ((uint64)sizem==size64) {
roentgen b75cab
		*pbase = (void*)
roentgen b75cab
		    mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, (int) fd, 0);
roentgen b75cab
		if (*pbase != (void*) -1) {
roentgen b75cab
			*psize = (tmsize_t)sizem;
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	return (0);
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) munmap(base, (off_t) size);
roentgen b75cab
}
roentgen b75cab
#else /* !HAVE_MMAP */
roentgen b75cab
static int
roentgen b75cab
_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
roentgen b75cab
{
roentgen b75cab
	(void) fd; (void) pbase; (void) psize;
roentgen b75cab
	return (0);
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; (void) base; (void) size;
roentgen b75cab
}
roentgen b75cab
#endif /* !HAVE_MMAP */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Open a TIFF file descriptor for read/writing.
roentgen b75cab
 */
roentgen b75cab
TIFF*
roentgen b75cab
TIFFFdOpen(int fd, const char* name, const char* mode)
roentgen b75cab
{
roentgen b75cab
	TIFF* tif;
roentgen b75cab
roentgen b75cab
	tif = TIFFClientOpen(name, mode,
roentgen b75cab
	    (thandle_t) fd,
roentgen b75cab
	    _tiffReadProc, _tiffWriteProc,
roentgen b75cab
	    _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
roentgen b75cab
	    _tiffMapProc, _tiffUnmapProc);
roentgen b75cab
	if (tif)
roentgen b75cab
		tif->tif_fd = fd;
roentgen b75cab
	return (tif);
roentgen b75cab
}
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
	int m, fd;
roentgen b75cab
	TIFF* tif;
roentgen b75cab
roentgen b75cab
	m = _TIFFgetMode(mode, module);
roentgen b75cab
	if (m == -1)
roentgen b75cab
		return ((TIFF*)0);
roentgen b75cab
roentgen b75cab
/* for cygwin and mingw */
roentgen b75cab
#ifdef O_BINARY
roentgen b75cab
	m |= O_BINARY;
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
	fd = open(name, m, 0666);
roentgen b75cab
	if (fd < 0) {
roentgen b75cab
		if (errno > 0 && strerror(errno) != NULL ) {
roentgen b75cab
			TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
roentgen b75cab
		} else {
roentgen b75cab
			TIFFErrorExt(0, module, "%s: Cannot open", name);
roentgen b75cab
		}
roentgen b75cab
		return ((TIFF *)0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	tif = TIFFFdOpen((int)fd, name, mode);
roentgen b75cab
	if(!tif)
roentgen b75cab
		close(fd);
roentgen b75cab
	return tif;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef __WIN32__
roentgen b75cab
#include <windows.h></windows.h>
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
	int m, fd;
roentgen b75cab
	int mbsize;
roentgen b75cab
	char *mbname;
roentgen b75cab
	TIFF* tif;
roentgen b75cab
roentgen b75cab
	m = _TIFFgetMode(mode, module);
roentgen b75cab
	if (m == -1)
roentgen b75cab
		return ((TIFF*)0);
roentgen b75cab
roentgen b75cab
/* for cygwin and mingw */
roentgen b75cab
#ifdef O_BINARY
roentgen b75cab
	m |= O_BINARY;
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
	fd = _wopen(name, m, 0666);
roentgen b75cab
	if (fd < 0) {
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 = _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, (mbname != NULL) ? mbname : "<unknown>",</unknown>
roentgen b75cab
			 mode);
roentgen b75cab
	
roentgen b75cab
	_TIFFfree(mbname);
roentgen b75cab
	
roentgen b75cab
	if(!tif)
roentgen b75cab
		close(fd);
roentgen b75cab
	return tif;
roentgen b75cab
}
roentgen b75cab
#endif
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
static void
roentgen b75cab
unixWarningHandler(const char* module, const char* fmt, va_list ap)
roentgen b75cab
{
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
}
roentgen b75cab
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
unixErrorHandler(const char* module, const char* fmt, va_list ap)
roentgen b75cab
{
roentgen b75cab
	if (module != NULL)
roentgen b75cab
		fprintf(stderr, "%s: ", module);
roentgen b75cab
	vfprintf(stderr, fmt, ap);
roentgen b75cab
	fprintf(stderr, ".\n");
roentgen b75cab
}
roentgen b75cab
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
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
 */