Blame libtiff/tif_unix.c

Packit Service 2594b8
/* $Id: tif_unix.c,v 1.28 2017-01-11 19:02:49 erouault Exp $ */
Packit Service 2594b8
Packit Service 2594b8
/*
Packit Service 2594b8
 * Copyright (c) 1988-1997 Sam Leffler
Packit Service 2594b8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit Service 2594b8
 *
Packit Service 2594b8
 * Permission to use, copy, modify, distribute, and sell this software and
Packit Service 2594b8
 * its documentation for any purpose is hereby granted without fee, provided
Packit Service 2594b8
 * that (i) the above copyright notices and this permission notice appear in
Packit Service 2594b8
 * all copies of the software and related documentation, and (ii) the names of
Packit Service 2594b8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit Service 2594b8
 * publicity relating to the software without the specific, prior written
Packit Service 2594b8
 * permission of Sam Leffler and Silicon Graphics.
Packit Service 2594b8
 *
Packit Service 2594b8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
Packit Service 2594b8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
Packit Service 2594b8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 2594b8
 *
Packit Service 2594b8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit Service 2594b8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit Service 2594b8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit Service 2594b8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
Packit Service 2594b8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
Packit Service 2594b8
 * OF THIS SOFTWARE.
Packit Service 2594b8
 */
Packit Service 2594b8
Packit Service 2594b8
/*
Packit Service 2594b8
 * TIFF Library UNIX-specific Routines. These are should also work with the
Packit Service 2594b8
 * Windows Common RunTime Library.
Packit Service 2594b8
 */
Packit Service 2594b8
Packit Service 2594b8
#include "tif_config.h"
Packit Service 2594b8
Packit Service 2594b8
#ifdef HAVE_SYS_TYPES_H
Packit Service 2594b8
# include <sys/types.h>
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
#include <errno.h>
Packit Service 2594b8
Packit Service 2594b8
#include <stdarg.h>
Packit Service 2594b8
#include <stdlib.h>
Packit Service 2594b8
#include <sys/stat.h>
Packit Service 2594b8
Packit Service 2594b8
#ifdef HAVE_UNISTD_H
Packit Service 2594b8
# include <unistd.h>
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
#ifdef HAVE_FCNTL_H
Packit Service 2594b8
# include <fcntl.h>
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
#ifdef HAVE_IO_H
Packit Service 2594b8
# include <io.h>
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
#include "tiffiop.h"
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
#define TIFF_IO_MAX 2147483647U
Packit Service 2594b8
Packit Service 2594b8
Packit Service 2594b8
typedef union fd_as_handle_union
Packit Service 2594b8
{
Packit Service 2594b8
	int fd;
Packit Service 2594b8
	thandle_t h;
Packit Service 2594b8
} fd_as_handle_union_t;
Packit Service 2594b8
Packit Service 2594b8
static tmsize_t
Packit Service 2594b8
_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
Packit Service 2594b8
{
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
        const size_t bytes_total = (size_t) size;
Packit Service 2594b8
        size_t bytes_read;
Packit Service 2594b8
        tmsize_t count = -1;
Packit Service 2594b8
	if ((tmsize_t) bytes_total != size)
Packit Service 2594b8
	{
Packit Service 2594b8
		errno=EINVAL;
Packit Service 2594b8
		return (tmsize_t) -1;
Packit Service 2594b8
	}
Packit Service 2594b8
	fdh.h = fd;
Packit Service 2594b8
        for (bytes_read=0; bytes_read < bytes_total; bytes_read+=count)
Packit Service 2594b8
        {
Packit Service 2594b8
                char *buf_offset = (char *) buf+bytes_read;
Packit Service 2594b8
                size_t io_size = bytes_total-bytes_read;
Packit Service 2594b8
                if (io_size > TIFF_IO_MAX)
Packit Service 2594b8
                        io_size = TIFF_IO_MAX;
Packit Service 2594b8
                count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
Packit Service 2594b8
                if (count <= 0)
Packit Service 2594b8
                        break;
Packit Service 2594b8
        }
Packit Service 2594b8
        if (count < 0)
Packit Service 2594b8
                return (tmsize_t)-1;
Packit Service 2594b8
        return (tmsize_t) bytes_read;
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static tmsize_t
Packit Service 2594b8
_tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
Packit Service 2594b8
{
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
	const size_t bytes_total = (size_t) size;
Packit Service 2594b8
        size_t bytes_written;
Packit Service 2594b8
        tmsize_t count = -1;
Packit Service 2594b8
	if ((tmsize_t) bytes_total != size)
Packit Service 2594b8
	{
Packit Service 2594b8
		errno=EINVAL;
Packit Service 2594b8
		return (tmsize_t) -1;
Packit Service 2594b8
	}
Packit Service 2594b8
	fdh.h = fd;
Packit Service 2594b8
        for (bytes_written=0; bytes_written < bytes_total; bytes_written+=count)
Packit Service 2594b8
        {
Packit Service 2594b8
                const char *buf_offset = (char *) buf+bytes_written;
Packit Service 2594b8
                size_t io_size = bytes_total-bytes_written;
Packit Service 2594b8
                if (io_size > TIFF_IO_MAX)
Packit Service 2594b8
                        io_size = TIFF_IO_MAX;
Packit Service 2594b8
                count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
Packit Service 2594b8
                if (count <= 0)
Packit Service 2594b8
                        break;
Packit Service 2594b8
        }
Packit Service 2594b8
        if (count < 0)
Packit Service 2594b8
                return (tmsize_t)-1;
Packit Service 2594b8
        return (tmsize_t) bytes_written;
Packit Service 2594b8
	/* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static uint64
Packit Service 2594b8
_tiffSeekProc(thandle_t fd, uint64 off, int whence)
Packit Service 2594b8
{
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
	_TIFF_off_t off_io = (_TIFF_off_t) off;
Packit Service 2594b8
	if ((uint64) off_io != off)
Packit Service 2594b8
	{
Packit Service 2594b8
		errno=EINVAL;
Packit Service 2594b8
		return (uint64) -1; /* this is really gross */
Packit Service 2594b8
	}
Packit Service 2594b8
	fdh.h = fd;
Packit Service 2594b8
	return((uint64)_TIFF_lseek_f(fdh.fd,off_io,whence));
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static int
Packit Service 2594b8
_tiffCloseProc(thandle_t fd)
Packit Service 2594b8
{
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
	fdh.h = fd;
Packit Service 2594b8
	return(close(fdh.fd));
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static uint64
Packit Service 2594b8
_tiffSizeProc(thandle_t fd)
Packit Service 2594b8
{
Packit Service 2594b8
	_TIFF_stat_s sb;
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
	fdh.h = fd;
Packit Service 2594b8
	if (_TIFF_fstat_f(fdh.fd,&sb)<0)
Packit Service 2594b8
		return(0);
Packit Service 2594b8
	else
Packit Service 2594b8
		return((uint64)sb.st_size);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
#ifdef HAVE_MMAP
Packit Service 2594b8
#include <sys/mman.h>
Packit Service 2594b8
Packit Service 2594b8
static int
Packit Service 2594b8
_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
Packit Service 2594b8
{
Packit Service 2594b8
	uint64 size64 = _tiffSizeProc(fd);
Packit Service 2594b8
	tmsize_t sizem = (tmsize_t)size64;
Packit Service 2594b8
	if ((uint64)sizem==size64) {
Packit Service 2594b8
		fd_as_handle_union_t fdh;
Packit Service 2594b8
		fdh.h = fd;
Packit Service 2594b8
		*pbase = (void*)
Packit Service 2594b8
		    mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
Packit Service 2594b8
		if (*pbase != (void*) -1) {
Packit Service 2594b8
			*psize = (tmsize_t)sizem;
Packit Service 2594b8
			return (1);
Packit Service 2594b8
		}
Packit Service 2594b8
	}
Packit Service 2594b8
	return (0);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static void
Packit Service 2594b8
_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
Packit Service 2594b8
{
Packit Service 2594b8
	(void) fd;
Packit Service 2594b8
	(void) munmap(base, (off_t) size);
Packit Service 2594b8
}
Packit Service 2594b8
#else /* !HAVE_MMAP */
Packit Service 2594b8
static int
Packit Service 2594b8
_tiffMapProc(thandle_t fd, void** pbase, toff_t* psize)
Packit Service 2594b8
{
Packit Service 2594b8
	(void) fd; (void) pbase; (void) psize;
Packit Service 2594b8
	return (0);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static void
Packit Service 2594b8
_tiffUnmapProc(thandle_t fd, void* base, toff_t size)
Packit Service 2594b8
{
Packit Service 2594b8
	(void) fd; (void) base; (void) size;
Packit Service 2594b8
}
Packit Service 2594b8
#endif /* !HAVE_MMAP */
Packit Service 2594b8
Packit Service 2594b8
/*
Packit Service 2594b8
 * Open a TIFF file descriptor for read/writing.
Packit Service 2594b8
 */
Packit Service 2594b8
TIFF*
Packit Service 2594b8
TIFFFdOpen(int fd, const char* name, const char* mode)
Packit Service 2594b8
{
Packit Service 2594b8
	TIFF* tif;
Packit Service 2594b8
Packit Service 2594b8
	fd_as_handle_union_t fdh;
Packit Service 2594b8
	fdh.fd = fd;
Packit Service 2594b8
	tif = TIFFClientOpen(name, mode,
Packit Service 2594b8
	    fdh.h,
Packit Service 2594b8
	    _tiffReadProc, _tiffWriteProc,
Packit Service 2594b8
	    _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
Packit Service 2594b8
	    _tiffMapProc, _tiffUnmapProc);
Packit Service 2594b8
	if (tif)
Packit Service 2594b8
		tif->tif_fd = fd;
Packit Service 2594b8
	return (tif);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
/*
Packit Service 2594b8
 * Open a TIFF file for read/writing.
Packit Service 2594b8
 */
Packit Service 2594b8
TIFF*
Packit Service 2594b8
TIFFOpen(const char* name, const char* mode)
Packit Service 2594b8
{
Packit Service 2594b8
	static const char module[] = "TIFFOpen";
Packit Service 2594b8
	int m, fd;
Packit Service 2594b8
	TIFF* tif;
Packit Service 2594b8
Packit Service 2594b8
	m = _TIFFgetMode(mode, module);
Packit Service 2594b8
	if (m == -1)
Packit Service 2594b8
		return ((TIFF*)0);
Packit Service 2594b8
Packit Service 2594b8
/* for cygwin and mingw */
Packit Service 2594b8
#ifdef O_BINARY
Packit Service 2594b8
	m |= O_BINARY;
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
	fd = open(name, m, 0666);
Packit Service 2594b8
	if (fd < 0) {
Packit Service 2594b8
		if (errno > 0 && strerror(errno) != NULL ) {
Packit Service 2594b8
			TIFFErrorExt(0, module, "%s: %s", name, strerror(errno) );
Packit Service 2594b8
		} else {
Packit Service 2594b8
			TIFFErrorExt(0, module, "%s: Cannot open", name);
Packit Service 2594b8
		}
Packit Service 2594b8
		return ((TIFF *)0);
Packit Service 2594b8
	}
Packit Service 2594b8
Packit Service 2594b8
	tif = TIFFFdOpen((int)fd, name, mode);
Packit Service 2594b8
	if(!tif)
Packit Service 2594b8
		close(fd);
Packit Service 2594b8
	return tif;
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
#ifdef __WIN32__
Packit Service 2594b8
#include <windows.h>
Packit Service 2594b8
/*
Packit Service 2594b8
 * Open a TIFF file with a Unicode filename, for read/writing.
Packit Service 2594b8
 */
Packit Service 2594b8
TIFF*
Packit Service 2594b8
TIFFOpenW(const wchar_t* name, const char* mode)
Packit Service 2594b8
{
Packit Service 2594b8
	static const char module[] = "TIFFOpenW";
Packit Service 2594b8
	int m, fd;
Packit Service 2594b8
	int mbsize;
Packit Service 2594b8
	char *mbname;
Packit Service 2594b8
	TIFF* tif;
Packit Service 2594b8
Packit Service 2594b8
	m = _TIFFgetMode(mode, module);
Packit Service 2594b8
	if (m == -1)
Packit Service 2594b8
		return ((TIFF*)0);
Packit Service 2594b8
Packit Service 2594b8
/* for cygwin and mingw */
Packit Service 2594b8
#ifdef O_BINARY
Packit Service 2594b8
	m |= O_BINARY;
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
	fd = _wopen(name, m, 0666);
Packit Service 2594b8
	if (fd < 0) {
Packit Service 2594b8
		TIFFErrorExt(0, module, "%ls: Cannot open", name);
Packit Service 2594b8
		return ((TIFF *)0);
Packit Service 2594b8
	}
Packit Service 2594b8
Packit Service 2594b8
	mbname = NULL;
Packit Service 2594b8
	mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
Packit Service 2594b8
	if (mbsize > 0) {
Packit Service 2594b8
		mbname = _TIFFmalloc(mbsize);
Packit Service 2594b8
		if (!mbname) {
Packit Service 2594b8
			TIFFErrorExt(0, module,
Packit Service 2594b8
			"Can't allocate space for filename conversion buffer");
Packit Service 2594b8
			return ((TIFF*)0);
Packit Service 2594b8
		}
Packit Service 2594b8
Packit Service 2594b8
		WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize,
Packit Service 2594b8
				    NULL, NULL);
Packit Service 2594b8
	}
Packit Service 2594b8
Packit Service 2594b8
	tif = TIFFFdOpen((int)fd, (mbname != NULL) ? mbname : "<unknown>",
Packit Service 2594b8
			 mode);
Packit Service 2594b8
	
Packit Service 2594b8
	_TIFFfree(mbname);
Packit Service 2594b8
	
Packit Service 2594b8
	if(!tif)
Packit Service 2594b8
		close(fd);
Packit Service 2594b8
	return tif;
Packit Service 2594b8
}
Packit Service 2594b8
#endif
Packit Service 2594b8
Packit Service 2594b8
void*
Packit Service 2594b8
_TIFFmalloc(tmsize_t s)
Packit Service 2594b8
{
Packit Service 2594b8
        if (s == 0)
Packit Service 2594b8
                return ((void *) NULL);
Packit Service 2594b8
Packit Service 2594b8
	return (malloc((size_t) s));
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
void* _TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
Packit Service 2594b8
{
Packit Service 2594b8
    if( nmemb == 0 || siz == 0 )
Packit Service 2594b8
        return ((void *) NULL);
Packit Service 2594b8
Packit Service 2594b8
    return calloc((size_t) nmemb, (size_t)siz);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
void
Packit Service 2594b8
_TIFFfree(void* p)
Packit Service 2594b8
{
Packit Service 2594b8
	free(p);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
void*
Packit Service 2594b8
_TIFFrealloc(void* p, tmsize_t s)
Packit Service 2594b8
{
Packit Service 2594b8
	return (realloc(p, (size_t) s));
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
void
Packit Service 2594b8
_TIFFmemset(void* p, int v, tmsize_t c)
Packit Service 2594b8
{
Packit Service 2594b8
	memset(p, v, (size_t) c);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
void
Packit Service 2594b8
_TIFFmemcpy(void* d, const void* s, tmsize_t c)
Packit Service 2594b8
{
Packit Service 2594b8
	memcpy(d, s, (size_t) c);
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
int
Packit Service 2594b8
_TIFFmemcmp(const void* p1, const void* p2, tmsize_t c)
Packit Service 2594b8
{
Packit Service 2594b8
	return (memcmp(p1, p2, (size_t) c));
Packit Service 2594b8
}
Packit Service 2594b8
Packit Service 2594b8
static void
Packit Service 2594b8
unixWarningHandler(const char* module, const char* fmt, va_list ap)
Packit Service 2594b8
{
Packit Service 2594b8
	if (module != NULL)
Packit Service 2594b8
		fprintf(stderr, "%s: ", module);
Packit Service 2594b8
	fprintf(stderr, "Warning, ");
Packit Service 2594b8
	vfprintf(stderr, fmt, ap);
Packit Service 2594b8
	fprintf(stderr, ".\n");
Packit Service 2594b8
}
Packit Service 2594b8
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
Packit Service 2594b8
Packit Service 2594b8
static void
Packit Service 2594b8
unixErrorHandler(const char* module, const char* fmt, va_list ap)
Packit Service 2594b8
{
Packit Service 2594b8
	if (module != NULL)
Packit Service 2594b8
		fprintf(stderr, "%s: ", module);
Packit Service 2594b8
	vfprintf(stderr, fmt, ap);
Packit Service 2594b8
	fprintf(stderr, ".\n");
Packit Service 2594b8
}
Packit Service 2594b8
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;
Packit Service 2594b8
Packit Service 2594b8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit Service 2594b8
Packit Service 2594b8
/*
Packit Service 2594b8
 * Local Variables:
Packit Service 2594b8
 * mode: c
Packit Service 2594b8
 * c-basic-offset: 8
Packit Service 2594b8
 * fill-column: 78
Packit Service 2594b8
 * End:
Packit Service 2594b8
 */