2019.10.01 18:01 "[Tiff] TIFF_IO_MAX too large for Windows XP", by David C. Partridge

2019.10.02 02:39 "Re: [Tiff] TIFF_IO_MAX too large for Windows XP", by David C. Partridge

Sadly it can't all be done with the pre-processor, you need a runtime call as well

But based on the assumption that the limit is USRHT_MAX in Windows XP. If the real limit is greater you just need to change USRHT_MAX in the following to the appropriate value.

just before the #define TIFF_IO_MAX 2147483647U

add the lines:

#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
BOOL IsWindowsXP()
{
        DWORD version = GetVersion();
        DWORD major = (DWORD)(LOBYTE(LOWORD(version)));
        DWORD minor = (DWORD)(HIBYTE(LOWORD(version)));
        return ((major == 5) && (minor >= 1)); // 5.1 is WIN Xp 5.2 is XP x64
};

#endif

Then:

_tiffReadProc and _tiffWriteProc change to read like:

static tmsize_t
_tiffReadProc(thandle_t fd, void* buf, tmsize_t size)
{
        fd_as_handle_union_t fdh;
        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;
        }
#ifdef __WIN32__
        unsigned long limit = TIFF_IO_MAX;
        if (IsWindowsXP()) limit = USHRT_MAX;
#else
        const unsigned long limit = TIFF_IO_MAX;
#endif
        fdh.h = fd;
        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 > limit)
                        io_size = limit;
                count=read(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
                if (count <= 0)
                        break;
        }
                if (count < 0)
                {
                        fprintf(stderr, "read() failed with errno %d\n", *_errno());
                        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;
        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;
        }
#ifdef __WIN32__
        unsigned long limit = TIFF_IO_MAX;
        if (IsWindowsXP()) limit = USHRT_MAX;
#else
        const unsigned long limit = TIFF_IO_MAX;
#endif

        fdh.h = fd;
        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 > limit)
                        io_size = limit;
                count=write(fdh.fd, buf_offset, (TIFFIOSize_t) io_size);
                if (count <= 0)
                        break;
        }
                if (count < 0)
                {
                        fprintf(stderr, "write() failed with errno %d\n", *_errno());
                        return (tmsize_t)-1;
                }
        return (tmsize_t) bytes_written;
        /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
}

David

-----Original Message-----
From: Tiff [mailto:tiff-bounces@lists.osgeo.org] On Behalf Of Roger Leigh
Sent: 01 October 2019 20:38
To: tiff@lists.osgeo.org
Subje