AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

1998.12.28 21:35 "TIFFOpen error in tif_win32.c", by Larry Reeve

I have run into a problem with the TIFFOpen(tif_win32.c) function in the v3.4beta037 distribution that causes a failure when the incoming mode character is 'a' to append to an existing file. In this case the _TIFFgetMode call returns O_RDWR|O_CREAT and the subsequent switch sets the dwMode variable to CREATE_NEW. The Win32 CreateFile function then fails to open the existing file because the dwCreationDisposition parameter of CREATE_NEW fails the function on an existing file. Setting the dwMode variable to OPEN_ALWAYS fixes this problem and the image can then be appended to the existing image.

I have attached an updated version of the function.

Larry Reeve

/*
 * Open a TIFF file for read/writing.
 */
TIFF*
TIFFOpen(const char* name, const char* mode)
{
        static const char module[] = "TIFFOpen";
        thandle_t fd;
        int m;
        DWORD dwMode;

        m = _TIFFgetMode(mode, module);

        switch(m)
        {
        case O_RDONLY:
                dwMode = OPEN_EXISTING;
                break;
        case O_RDWR:
                dwMode = OPEN_ALWAYS;
                break;
        case O_RDWR|O_CREAT:
                dwMode = OPEN_ALWAYS;//CREATE_NEW;
                break;
        case O_RDWR|O_TRUNC:
                dwMode = CREATE_ALWAYS;
                break;
        case O_RDWR|O_CREAT|O_TRUNC:
                dwMode = CREATE_ALWAYS;
                break;
        default:
                return ((TIFF*)0);
        }
        fd = (thandle_t)CreateFile(name, (m == O_RDONLY) ? GENERIC_READ :
                        (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ, NULL, dwMode,
                        (m == O_RDONLY) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL, NULL);
        if (fd == INVALID_HANDLE_VALUE) {
                TIFFError(module, "%s: Cannot open", name);
                return ((TIFF *)0);
        }
        return (TIFFFdOpen((int)fd, name, mode));
}