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

2019.10.10 12:10 "Re: [Tiff] TIFF_IO_MAX too large for Windows XP", by David C. Partridge

Yes, I also see that for x64 the limit was 32MB minus a bit. So for my code I have set a safe limit of 16MB.

I like to propose that this be adopted into the official codebase :)

So the code now looks like (starting at line 58 of tif_unix.c):

#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

#define TIFF_IO_MAX 2147483647U

typedef union fd_as_handle_union
{
        int fd;
        thandle_t h;
} fd_as_handle_union_t;

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 = 16777216UL;

#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 = 16777216UL;

#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: Edward Lam [mailto:edward@sidefx.com]
Sent: 10 October 2019 12:29
To: David C. Partridge
Cc: tiff@lists.osgeo.org

Subject: Re: [Tiff] TIFF_IO_MAX too large for Windows XP

On 10/10/2019 6:28 AM, David C. Partridge wrote:

> I wrote a crude binary sear