t1_generate_luts.c: fix compiler warnings
[openjpeg.git] / thirdparty / libtiff / tif_unix.c
index e96841a47415c255509d2435dbd6c645a8af452c..81e9d6653c2ab69a8f280fa15baa44ecc6747bc3 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_unix.c,v 1.26 2015-06-16 15:33:17 erouault Exp $ */
+/* $Id: tif_unix.c,v 1.27 2015-08-19 02:31:04 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 
 #include "tiffiop.h"
 
+
+#define TIFF_IO_MAX 2147483647U
+
+
 typedef union fd_as_handle_union
 {
        int fd;
@@ -65,42 +69,71 @@ static tmsize_t
 _tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
 {
        fd_as_handle_union_t fdh;
-       size_t size_io = (size_t) size;
-       if ((tmsize_t) size_io != size)
+        const size_t bytes_total = (size_t) size;
+        size_t bytes_read;
+        tmsize_t count = -1;
+       if ((tmsize_t) bytes_total != size)
        {
                errno=EINVAL;
                return (tmsize_t) -1;
        }
        fdh.h = fd;
-       return ((tmsize_t) read(fdh.fd, buf, size_io));
+        for (bytes_read=0; bytes_read < bytes_total; bytes_read+=count)
+        {
+                char *buf_offset = (char *) buf+bytes_read;
+                size_t io_size = bytes_total-bytes_read;
+                if (io_size > TIFF_IO_MAX)
+                        io_size = TIFF_IO_MAX;
+                count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
+                if (count <= 0)
+                        break;
+        }
+        if (count < 0)
+                return (tmsize_t)-1;
+        return (tmsize_t) bytes_read;
 }
 
 static tmsize_t
 _tiffWriteProc(thandle_t fd, void* buf, tmsize_t size)
 {
        fd_as_handle_union_t fdh;
-       size_t size_io = (size_t) size;
-       if ((tmsize_t) size_io != size)
+       const size_t bytes_total = (size_t) size;
+        size_t bytes_written;
+        tmsize_t count = -1;
+       if ((tmsize_t) bytes_total != size)
        {
                errno=EINVAL;
                return (tmsize_t) -1;
        }
        fdh.h = fd;
-       return ((tmsize_t) write(fdh.fd, buf, size_io));
+        for (bytes_written=0; bytes_written < bytes_total; bytes_written+=count)
+        {
+                const char *buf_offset = (char *) buf+bytes_written;
+                size_t io_size = bytes_total-bytes_written;
+                if (io_size > TIFF_IO_MAX)
+                        io_size = TIFF_IO_MAX;
+                count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
+                if (count <= 0)
+                        break;
+        }
+        if (count < 0)
+                return (tmsize_t)-1;
+        return (tmsize_t) bytes_written;
+       /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
 }
 
 static uint64
 _tiffSeekProc(thandle_t fd, uint64 off, int whence)
 {
        fd_as_handle_union_t fdh;
-       off_t off_io = (off_t) off;
+       _TIFF_off_t off_io = (_TIFF_off_t) off;
        if ((uint64) off_io != off)
        {
                errno=EINVAL;
                return (uint64) -1; /* this is really gross */
        }
        fdh.h = fd;
-       return((uint64)lseek(fdh.fd,off_io,whence));
+       return((uint64)_TIFF_lseek_f(fdh.fd,off_io,whence));
 }
 
 static int
@@ -114,10 +147,10 @@ _tiffCloseProc(thandle_t fd)
 static uint64
 _tiffSizeProc(thandle_t fd)
 {
-       struct stat sb;
+       _TIFF_stat_s sb;
        fd_as_handle_union_t fdh;
        fdh.h = fd;
-       if (fstat(fdh.fd,&sb)<0)
+       if (_TIFF_fstat_f(fdh.fd,&sb)<0)
                return(0);
        else
                return((uint64)sb.st_size);
@@ -245,7 +278,7 @@ TIFFOpenW(const wchar_t* name, const char* mode)
 
        fd = _wopen(name, m, 0666);
        if (fd < 0) {
-               TIFFErrorExt(0, module, "%s: Cannot open", name);
+               TIFFErrorExt(0, module, "%ls: Cannot open", name);
                return ((TIFF *)0);
        }