roentgen b75cab
/* $Id: tif_read.c,v 1.41 2012-07-06 19:22:58 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 * Scanline-oriented Read Support
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include <stdio.h></stdio.h>
roentgen b75cab
roentgen b75cab
int TIFFFillStrip(TIFF* tif, uint32 strip);
roentgen b75cab
int TIFFFillTile(TIFF* tif, uint32 tile);
roentgen b75cab
static int TIFFStartStrip(TIFF* tif, uint32 strip);
roentgen b75cab
static int TIFFStartTile(TIFF* tif, uint32 tile);
roentgen b75cab
static int TIFFCheckRead(TIFF*, int);
roentgen b75cab
static tmsize_t
roentgen b75cab
TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
roentgen b75cab
roentgen b75cab
#define NOSTRIP ((uint32)(-1))       /* undefined state */
roentgen b75cab
#define NOTILE ((uint32)(-1))         /* undefined state */
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFFillStripPartial";
roentgen b75cab
	register TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
        uint64 unused_data;
roentgen b75cab
        uint64 read_offset;
roentgen b75cab
        tmsize_t cc, to_read;
roentgen b75cab
        /* tmsize_t bytecountm; */
roentgen b75cab
        
roentgen b75cab
        if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
            return 0;
roentgen b75cab
        
roentgen b75cab
        /*
roentgen b75cab
         * Expand raw data buffer, if needed, to hold data
roentgen b75cab
         * strip coming from file (perhaps should set upper
roentgen b75cab
         * bound on the size of a buffer we'll use?).
roentgen b75cab
         */
roentgen b75cab
roentgen b75cab
        /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
roentgen b75cab
        if (read_ahead*2 > tif->tif_rawdatasize) {
roentgen b75cab
                assert( restart );
roentgen b75cab
                
roentgen b75cab
                tif->tif_curstrip = NOSTRIP;
roentgen b75cab
                if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
roentgen b75cab
                        TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                                     "Data buffer too small to hold part of strip %lu",
roentgen b75cab
                                     (unsigned long) strip);
roentgen b75cab
                        return (0);
roentgen b75cab
                }
roentgen b75cab
                if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
roentgen b75cab
                        return (0);
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        if( restart )
roentgen b75cab
        {
roentgen b75cab
                tif->tif_rawdataloaded = 0;
roentgen b75cab
                tif->tif_rawdataoff = 0;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** If we are reading more data, move any unused data to the
roentgen b75cab
        ** start of the buffer.
roentgen b75cab
        */
roentgen b75cab
        if( tif->tif_rawdataloaded > 0 )
roentgen b75cab
                unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
roentgen b75cab
        else
roentgen b75cab
                unused_data = 0;
roentgen b75cab
        
roentgen b75cab
        if( unused_data > 0 )
roentgen b75cab
        {
roentgen b75cab
		assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
roentgen b75cab
                memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** Seek to the point in the file where more data should be read.
roentgen b75cab
        */
roentgen b75cab
        read_offset = td->td_stripoffset[strip]
roentgen b75cab
                + tif->tif_rawdataoff + tif->tif_rawdataloaded;
roentgen b75cab
roentgen b75cab
        if (!SeekOK(tif, read_offset)) {
roentgen b75cab
                TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                             "Seek error at scanline %lu, strip %lu",
roentgen b75cab
                             (unsigned long) tif->tif_row, (unsigned long) strip);
roentgen b75cab
                return 0;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** How much do we want to read?
roentgen b75cab
        */
roentgen b75cab
        to_read = tif->tif_rawdatasize - unused_data;
roentgen b75cab
        if( (uint64) to_read > td->td_stripbytecount[strip] 
roentgen b75cab
            - tif->tif_rawdataoff - tif->tif_rawdataloaded )
roentgen b75cab
        {
roentgen b75cab
                to_read = td->td_stripbytecount[strip]
roentgen b75cab
                        - tif->tif_rawdataoff - tif->tif_rawdataloaded;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
roentgen b75cab
        cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
roentgen b75cab
roentgen b75cab
        if (cc != to_read) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
                TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                             "Read error at scanline %lu; got %I64u bytes, expected %I64u",
roentgen b75cab
                             (unsigned long) tif->tif_row,
roentgen b75cab
                             (unsigned __int64) cc,
roentgen b75cab
                             (unsigned __int64) to_read);
roentgen b75cab
#else
roentgen b75cab
                TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                             "Read error at scanline %lu; got %llu bytes, expected %llu",
roentgen b75cab
                             (unsigned long) tif->tif_row,
roentgen b75cab
                             (unsigned long long) cc,
roentgen b75cab
                             (unsigned long long) to_read);
roentgen b75cab
#endif
roentgen b75cab
                return 0;
roentgen b75cab
        }
roentgen b75cab
        
roentgen b75cab
        tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
roentgen b75cab
        tif->tif_rawdataloaded = unused_data + to_read;
roentgen b75cab
roentgen b75cab
        tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
                        
roentgen b75cab
        if (!isFillOrder(tif, td->td_fillorder) &&
roentgen b75cab
            (tif->tif_flags & TIFF_NOBITREV) == 0) {
roentgen b75cab
		assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
roentgen b75cab
                TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** When starting a strip from the beginning we need to
roentgen b75cab
        ** restart the decoder.
roentgen b75cab
        */
roentgen b75cab
        if( restart )
roentgen b75cab
                return TIFFStartStrip(tif, strip);
roentgen b75cab
        else
roentgen b75cab
                return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Seek to a random row+sample in a file.
roentgen b75cab
 *
roentgen b75cab
 * Only used by TIFFReadScanline, and is only used on
roentgen b75cab
 * strip organized files.  We do some tricky stuff to try
roentgen b75cab
 * and avoid reading the whole compressed raw data for big
roentgen b75cab
 * strips.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
roentgen b75cab
{
roentgen b75cab
	register TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint32 strip;
roentgen b75cab
        int    whole_strip;
roentgen b75cab
	tmsize_t read_ahead = 0;
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** Establish what strip we are working from.
roentgen b75cab
        */
roentgen b75cab
	if (row >= td->td_imagelength) {	/* out of range */
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
		    "%lu: Row out of range, max %lu",
roentgen b75cab
		    (unsigned long) row,
roentgen b75cab
		    (unsigned long) td->td_imagelength);
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
		if (sample >= td->td_samplesperpixel) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
			    "%lu: Sample out of range, max %lu",
roentgen b75cab
			    (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
roentgen b75cab
	} else
roentgen b75cab
		strip = row / td->td_rowsperstrip;
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
         * Do we want to treat this strip as one whole chunk or
roentgen b75cab
         * read it a few lines at a time?
roentgen b75cab
         */
roentgen b75cab
#if defined(CHUNKY_STRIP_READ_SUPPORT)
roentgen b75cab
        if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
            return 0;
roentgen b75cab
        whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
roentgen b75cab
                || isMapped(tif);
roentgen b75cab
#else
roentgen b75cab
        whole_strip = 1;
roentgen b75cab
#endif
roentgen b75cab
        
roentgen b75cab
        if( !whole_strip )
roentgen b75cab
        {
roentgen b75cab
                read_ahead = tif->tif_scanlinesize * 16 + 5000;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
         * If we haven't loaded this strip, do so now, possibly
roentgen b75cab
         * only reading the first part.
roentgen b75cab
         */
roentgen b75cab
	if (strip != tif->tif_curstrip) {	/* different strip, refill */
roentgen b75cab
                
roentgen b75cab
                if( whole_strip )
roentgen b75cab
                {
roentgen b75cab
                        if (!TIFFFillStrip(tif, strip))
roentgen b75cab
                                return (0);
roentgen b75cab
                }
roentgen b75cab
                else
roentgen b75cab
                {
roentgen b75cab
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
roentgen b75cab
                                return 0;
roentgen b75cab
                }
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        /*
roentgen b75cab
        ** If we already have some data loaded, do we need to read some more?
roentgen b75cab
        */
roentgen b75cab
        else if( !whole_strip )
roentgen b75cab
        {
roentgen b75cab
                if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead 
roentgen b75cab
                    && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
roentgen b75cab
                {
roentgen b75cab
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
roentgen b75cab
                                return 0;
roentgen b75cab
                }
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        if (row < tif->tif_row) {
roentgen b75cab
		/*
roentgen b75cab
		 * Moving backwards within the same strip: backup
roentgen b75cab
		 * to the start and then decode forward (below).
roentgen b75cab
		 *
roentgen b75cab
		 * NB: If you're planning on lots of random access within a
roentgen b75cab
		 * strip, it's better to just read and decode the entire
roentgen b75cab
		 * strip, and then access the decoded data in a random fashion.
roentgen b75cab
		 */
roentgen b75cab
roentgen b75cab
                if( tif->tif_rawdataoff != 0 )
roentgen b75cab
                {
roentgen b75cab
                        if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
roentgen b75cab
                                return 0;
roentgen b75cab
                }
roentgen b75cab
                else
roentgen b75cab
                {
roentgen b75cab
                        if (!TIFFStartStrip(tif, strip))
roentgen b75cab
                                return (0);
roentgen b75cab
                }
roentgen b75cab
	}
roentgen b75cab
        
roentgen b75cab
	if (row != tif->tif_row) {
roentgen b75cab
		/*
roentgen b75cab
		 * Seek forward to the desired row.
roentgen b75cab
		 */
roentgen b75cab
roentgen b75cab
                /* TODO: Will this really work with partial buffers? */
roentgen b75cab
                
roentgen b75cab
		if (!(*tif->tif_seek)(tif, row - tif->tif_row))
roentgen b75cab
			return (0);
roentgen b75cab
		tif->tif_row = row;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
roentgen b75cab
{
roentgen b75cab
	int e;
roentgen b75cab
roentgen b75cab
	if (!TIFFCheckRead(tif, 0))
roentgen b75cab
		return (-1);
roentgen b75cab
	if( (e = TIFFSeek(tif, row, sample)) != 0) {
roentgen b75cab
		/*
roentgen b75cab
		 * Decompress desired row into user buffer.
roentgen b75cab
		 */
roentgen b75cab
		e = (*tif->tif_decoderow)
roentgen b75cab
		    (tif, (uint8*) buf, tif->tif_scanlinesize, sample);  
roentgen b75cab
roentgen b75cab
		/* we are now poised at the beginning of the next row */
roentgen b75cab
		tif->tif_row = row + 1;
roentgen b75cab
roentgen b75cab
		if (e)
roentgen b75cab
			(*tif->tif_postdecode)(tif, (uint8*) buf,
roentgen b75cab
			    tif->tif_scanlinesize);  
roentgen b75cab
	}
roentgen b75cab
	return (e > 0 ? 1 : -1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a strip of data and decompress the specified
roentgen b75cab
 * amount into the user-supplied buffer.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFReadEncodedStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint32 rowsperstrip;
roentgen b75cab
	uint32 stripsperplane;
roentgen b75cab
	uint32 stripinplane;
roentgen b75cab
	uint16 plane;
roentgen b75cab
	uint32 rows;
roentgen b75cab
	tmsize_t stripsize;
roentgen b75cab
	if (!TIFFCheckRead(tif,0))
roentgen b75cab
		return((tmsize_t)(-1));
roentgen b75cab
	if (strip>=td->td_nstrips)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
		    "%lu: Strip out of range, max %lu",(unsigned long)strip,
roentgen b75cab
		    (unsigned long)td->td_nstrips);
roentgen b75cab
		return((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	/*
roentgen b75cab
	 * Calculate the strip size according to the number of
roentgen b75cab
	 * rows in the strip (check for truncated last strip on any
roentgen b75cab
	 * of the separations).
roentgen b75cab
	 */
roentgen b75cab
	rowsperstrip=td->td_rowsperstrip;
roentgen b75cab
	if (rowsperstrip>td->td_imagelength)
roentgen b75cab
		rowsperstrip=td->td_imagelength;
roentgen b75cab
	stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
roentgen b75cab
	stripinplane=(strip%stripsperplane);
roentgen b75cab
	plane=(strip/stripsperplane);
roentgen b75cab
	rows=td->td_imagelength-stripinplane*rowsperstrip;
roentgen b75cab
	if (rows>rowsperstrip)
roentgen b75cab
		rows=rowsperstrip;
roentgen b75cab
	stripsize=TIFFVStripSize(tif,rows);
roentgen b75cab
	if (stripsize==0)
roentgen b75cab
		return((tmsize_t)(-1));
roentgen b75cab
	if ((size!=(tmsize_t)(-1))&&(size
roentgen b75cab
		stripsize=size;
roentgen b75cab
	if (!TIFFFillStrip(tif,strip))
roentgen b75cab
		return((tmsize_t)(-1));
roentgen b75cab
	if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
roentgen b75cab
		return((tmsize_t)(-1));
roentgen b75cab
	(*tif->tif_postdecode)(tif,buf,stripsize);
roentgen b75cab
	return(stripsize);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
roentgen b75cab
    const char* module)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ))
roentgen b75cab
        return ((tmsize_t)(-1));
roentgen b75cab
        
roentgen b75cab
	assert((tif->tif_flags&TIFF_NOREADRAW)==0);
roentgen b75cab
	if (!isMapped(tif)) {
roentgen b75cab
		tmsize_t cc;
roentgen b75cab
roentgen b75cab
		if (!SeekOK(tif, td->td_stripoffset[strip])) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Seek error at scanline %lu, strip %lu",
roentgen b75cab
			    (unsigned long) tif->tif_row, (unsigned long) strip);
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
		cc = TIFFReadFile(tif, buf, size);
roentgen b75cab
		if (cc != size) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		"Read error at scanline %lu; got %I64u bytes, expected %I64u",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned __int64) cc,
roentgen b75cab
				     (unsigned __int64) size);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		"Read error at scanline %lu; got %llu bytes, expected %llu",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long long) cc,
roentgen b75cab
				     (unsigned long long) size);
roentgen b75cab
#endif
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		tmsize_t ma,mb;
roentgen b75cab
		tmsize_t n;
roentgen b75cab
		ma=(tmsize_t)td->td_stripoffset[strip];
roentgen b75cab
		mb=ma+size;
roentgen b75cab
		if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size))
roentgen b75cab
			n=0;
roentgen b75cab
		else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))</ma)||(mb<size)||(mb>
roentgen b75cab
			n=tif->tif_size-ma;
roentgen b75cab
		else
roentgen b75cab
			n=size;
roentgen b75cab
		if (n!=size) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
	"Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) strip,
roentgen b75cab
				     (unsigned __int64) n,
roentgen b75cab
				     (unsigned __int64) size);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
	"Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) strip,
roentgen b75cab
				     (unsigned long long) n,
roentgen b75cab
				     (unsigned long long) size);
roentgen b75cab
#endif
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
		_TIFFmemcpy(buf, tif->tif_base + ma,
roentgen b75cab
			    size);
roentgen b75cab
	}
roentgen b75cab
	return (size);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a strip of data from the file.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFReadRawStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64 bytecount;
roentgen b75cab
	tmsize_t bytecountm;
roentgen b75cab
roentgen b75cab
	if (!TIFFCheckRead(tif, 0))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	if (strip >= td->td_nstrips) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		     "%lu: Strip out of range, max %lu",
roentgen b75cab
		     (unsigned long) strip,
roentgen b75cab
		     (unsigned long) td->td_nstrips);
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_flags&TIFF_NOREADRAW)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "Compression scheme does not support access to raw uncompressed data");
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	bytecount = td->td_stripbytecount[strip];
roentgen b75cab
	if (bytecount <= 0) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%I64u: Invalid strip byte count, strip %lu",
roentgen b75cab
			     (unsigned __int64) bytecount,
roentgen b75cab
			     (unsigned long) strip);
roentgen b75cab
#else
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%llu: Invalid strip byte count, strip %lu",
roentgen b75cab
			     (unsigned long long) bytecount,
roentgen b75cab
			     (unsigned long) strip);
roentgen b75cab
#endif
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	bytecountm = (tmsize_t)bytecount;
roentgen b75cab
	if ((uint64)bytecountm!=bytecount) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	if (size != (tmsize_t)(-1) && size < bytecountm)
roentgen b75cab
		bytecountm = size;
roentgen b75cab
	return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read the specified strip and setup for decoding. The data buffer is
roentgen b75cab
 * expanded, as necessary, to hold the strip's data.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFFillStrip(TIFF* tif, uint32 strip)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFFillStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
        return 0;
roentgen b75cab
        
roentgen b75cab
	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
roentgen b75cab
	{
roentgen b75cab
		uint64 bytecount = td->td_stripbytecount[strip];
roentgen b75cab
		if (bytecount <= 0) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				"Invalid strip byte count %I64u, strip %lu",
roentgen b75cab
				     (unsigned __int64) bytecount,
roentgen b75cab
				     (unsigned long) strip);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				"Invalid strip byte count %llu, strip %lu",
roentgen b75cab
				     (unsigned long long) bytecount,
roentgen b75cab
				     (unsigned long) strip);
roentgen b75cab
#endif
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		if (isMapped(tif) &&
roentgen b75cab
		    (isFillOrder(tif, td->td_fillorder)
roentgen b75cab
		    || (tif->tif_flags & TIFF_NOBITREV))) {
roentgen b75cab
			/*
roentgen b75cab
			 * The image is mapped into memory and we either don't
roentgen b75cab
			 * need to flip bits or the compression routine is
roentgen b75cab
			 * going to handle this operation itself.  In this
roentgen b75cab
			 * case, avoid copying the raw data and instead just
roentgen b75cab
			 * reference the data from the memory mapped file
roentgen b75cab
			 * image.  This assumes that the decompression
roentgen b75cab
			 * routines do not modify the contents of the raw data
roentgen b75cab
			 * buffer (if they try to, the application will get a
roentgen b75cab
			 * fault since the file is mapped read-only).
roentgen b75cab
			 */
roentgen b75cab
			if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
roentgen b75cab
				_TIFFfree(tif->tif_rawdata);
roentgen b75cab
				tif->tif_rawdata = NULL;
roentgen b75cab
				tif->tif_rawdatasize = 0;
roentgen b75cab
			}
roentgen b75cab
			tif->tif_flags &= ~TIFF_MYBUFFER;
roentgen b75cab
			/*
roentgen b75cab
			 * We must check for overflow, potentially causing
roentgen b75cab
			 * an OOB read. Instead of simple
roentgen b75cab
			 *
roentgen b75cab
			 *  td->td_stripoffset[strip]+bytecount > tif->tif_size
roentgen b75cab
			 *
roentgen b75cab
			 * comparison (which can overflow) we do the following
roentgen b75cab
			 * two comparisons:
roentgen b75cab
			 */
roentgen b75cab
			if (bytecount > (uint64)tif->tif_size ||
roentgen b75cab
			    td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
roentgen b75cab
				/*
roentgen b75cab
				 * This error message might seem strange, but
roentgen b75cab
				 * it's what would happen if a read were done
roentgen b75cab
				 * instead.
roentgen b75cab
				 */
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
roentgen b75cab
					"Read error on strip %lu; "
roentgen b75cab
					"got %I64u bytes, expected %I64u",
roentgen b75cab
					(unsigned long) strip,
roentgen b75cab
					(unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
roentgen b75cab
					(unsigned __int64) bytecount);
roentgen b75cab
#else
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
roentgen b75cab
					"Read error on strip %lu; "
roentgen b75cab
					"got %llu bytes, expected %llu",
roentgen b75cab
					(unsigned long) strip,
roentgen b75cab
					(unsigned long long) tif->tif_size - td->td_stripoffset[strip],
roentgen b75cab
					(unsigned long long) bytecount);
roentgen b75cab
#endif
roentgen b75cab
				tif->tif_curstrip = NOSTRIP;
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			tif->tif_rawdatasize = (tmsize_t)bytecount;
roentgen b75cab
			tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
roentgen b75cab
                        tif->tif_rawdataoff = 0;
roentgen b75cab
                        tif->tif_rawdataloaded = (tmsize_t) bytecount;
roentgen b75cab
roentgen b75cab
			/* 
roentgen b75cab
			 * When we have tif_rawdata reference directly into the memory mapped file
roentgen b75cab
			 * we need to be pretty careful about how we use the rawdata.  It is not
roentgen b75cab
			 * a general purpose working buffer as it normally otherwise is.  So we
roentgen b75cab
			 * keep track of this fact to avoid using it improperly.
roentgen b75cab
			 */
roentgen b75cab
			tif->tif_flags |= TIFF_BUFFERMMAP;
roentgen b75cab
		} else {
roentgen b75cab
			/*
roentgen b75cab
			 * Expand raw data buffer, if needed, to hold data
roentgen b75cab
			 * strip coming from file (perhaps should set upper
roentgen b75cab
			 * bound on the size of a buffer we'll use?).
roentgen b75cab
			 */
roentgen b75cab
			tmsize_t bytecountm;
roentgen b75cab
			bytecountm=(tmsize_t)bytecount;
roentgen b75cab
			if ((uint64)bytecountm!=bytecount)
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
roentgen b75cab
				return(0);
roentgen b75cab
			}
roentgen b75cab
			if (bytecountm > tif->tif_rawdatasize) {
roentgen b75cab
				tif->tif_curstrip = NOSTRIP;
roentgen b75cab
				if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					    "Data buffer too small to hold strip %lu",
roentgen b75cab
					    (unsigned long) strip);
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (!TIFFReadBufferSetup(tif, 0, bytecountm))
roentgen b75cab
					return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags&TIFF_BUFFERMMAP) {
roentgen b75cab
				tif->tif_curstrip = NOSTRIP;
roentgen b75cab
				if (!TIFFReadBufferSetup(tif, 0, bytecountm))
roentgen b75cab
					return (0);
roentgen b75cab
			}
roentgen b75cab
			if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
roentgen b75cab
				bytecountm, module) != bytecountm)
roentgen b75cab
				return (0);
roentgen b75cab
roentgen b75cab
                        tif->tif_rawdataoff = 0;
roentgen b75cab
                        tif->tif_rawdataloaded = bytecountm;
roentgen b75cab
                        
roentgen b75cab
			if (!isFillOrder(tif, td->td_fillorder) &&
roentgen b75cab
			    (tif->tif_flags & TIFF_NOBITREV) == 0)
roentgen b75cab
				TIFFReverseBits(tif->tif_rawdata, bytecountm);
roentgen b75cab
                }
roentgen b75cab
	}
roentgen b75cab
	return (TIFFStartStrip(tif, strip));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Tile-oriented Read Support
roentgen b75cab
 * Contributed by Nancy Cam (Silicon Graphics).
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read and decompress a tile of data.  The
roentgen b75cab
 * tile is selected by the (x,y,z,s) coordinates.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
roentgen b75cab
{
roentgen b75cab
	if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	return (TIFFReadEncodedTile(tif,
roentgen b75cab
	    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a tile of data and decompress the specified
roentgen b75cab
 * amount into the user-supplied buffer.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFReadEncodedTile";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	tmsize_t tilesize = tif->tif_tilesize;
roentgen b75cab
roentgen b75cab
	if (!TIFFCheckRead(tif, 1))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	if (tile >= td->td_nstrips) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "%lu: Tile out of range, max %lu",
roentgen b75cab
		    (unsigned long) tile, (unsigned long) td->td_nstrips);
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	if (size == (tmsize_t)(-1))
roentgen b75cab
		size = tilesize;
roentgen b75cab
	else if (size > tilesize)
roentgen b75cab
		size = tilesize;
roentgen b75cab
	if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
roentgen b75cab
	    (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
roentgen b75cab
		(*tif->tif_postdecode)(tif, (uint8*) buf, size);
roentgen b75cab
		return (size);
roentgen b75cab
	} else
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static tmsize_t
roentgen b75cab
TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ))
roentgen b75cab
        return ((tmsize_t)(-1));
roentgen b75cab
roentgen b75cab
	assert((tif->tif_flags&TIFF_NOREADRAW)==0);
roentgen b75cab
	if (!isMapped(tif)) {
roentgen b75cab
		tmsize_t cc;
roentgen b75cab
roentgen b75cab
		if (!SeekOK(tif, td->td_stripoffset[tile])) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Seek error at row %lu, col %lu, tile %lu",
roentgen b75cab
			    (unsigned long) tif->tif_row,
roentgen b75cab
			    (unsigned long) tif->tif_col,
roentgen b75cab
			    (unsigned long) tile);
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
		cc = TIFFReadFile(tif, buf, size);
roentgen b75cab
		if (cc != size) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
	"Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) tif->tif_col,
roentgen b75cab
				     (unsigned __int64) cc,
roentgen b75cab
				     (unsigned __int64) size);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
	"Read error at row %lu, col %lu; got %llu bytes, expected %llu",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) tif->tif_col,
roentgen b75cab
				     (unsigned long long) cc,
roentgen b75cab
				     (unsigned long long) size);
roentgen b75cab
#endif
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
	} else {
roentgen b75cab
		tmsize_t ma,mb;
roentgen b75cab
		tmsize_t n;
roentgen b75cab
		ma=(tmsize_t)td->td_stripoffset[tile];
roentgen b75cab
		mb=ma+size;
roentgen b75cab
		if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size))
roentgen b75cab
			n=0;
roentgen b75cab
		else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))</ma)||(mb<size)||(mb>
roentgen b75cab
			n=tif->tif_size-ma;
roentgen b75cab
		else
roentgen b75cab
			n=size;
roentgen b75cab
		if (n!=size) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
"Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) tif->tif_col,
roentgen b75cab
				     (unsigned long) tile,
roentgen b75cab
				     (unsigned __int64) n,
roentgen b75cab
				     (unsigned __int64) size);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
"Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
roentgen b75cab
				     (unsigned long) tif->tif_row,
roentgen b75cab
				     (unsigned long) tif->tif_col,
roentgen b75cab
				     (unsigned long) tile,
roentgen b75cab
				     (unsigned long long) n,
roentgen b75cab
				     (unsigned long long) size);
roentgen b75cab
#endif
roentgen b75cab
			return ((tmsize_t)(-1));
roentgen b75cab
		}
roentgen b75cab
		_TIFFmemcpy(buf, tif->tif_base + ma, size);
roentgen b75cab
	}
roentgen b75cab
	return (size);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read a tile of data from the file.
roentgen b75cab
 */
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFReadRawTile";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64 bytecount64;
roentgen b75cab
	tmsize_t bytecountm;
roentgen b75cab
roentgen b75cab
	if (!TIFFCheckRead(tif, 1))
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	if (tile >= td->td_nstrips) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "%lu: Tile out of range, max %lu",
roentgen b75cab
		    (unsigned long) tile, (unsigned long) td->td_nstrips);
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_flags&TIFF_NOREADRAW)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		"Compression scheme does not support access to raw uncompressed data");
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	bytecount64 = td->td_stripbytecount[tile];
roentgen b75cab
	if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
roentgen b75cab
		bytecount64 = (uint64)size;
roentgen b75cab
	bytecountm = (tmsize_t)bytecount64;
roentgen b75cab
	if ((uint64)bytecountm!=bytecount64)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
roentgen b75cab
		return ((tmsize_t)(-1));
roentgen b75cab
	}
roentgen b75cab
	return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Read the specified tile and setup for decoding. The data buffer is
roentgen b75cab
 * expanded, as necessary, to hold the tile's data.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFFillTile(TIFF* tif, uint32 tile)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFFillTile";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
        return 0;
roentgen b75cab
        
roentgen b75cab
	if ((tif->tif_flags&TIFF_NOREADRAW)==0)
roentgen b75cab
	{
roentgen b75cab
		uint64 bytecount = td->td_stripbytecount[tile];
roentgen b75cab
		if (bytecount <= 0) {
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				"%I64u: Invalid tile byte count, tile %lu",
roentgen b75cab
				     (unsigned __int64) bytecount,
roentgen b75cab
				     (unsigned long) tile);
roentgen b75cab
#else
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				"%llu: Invalid tile byte count, tile %lu",
roentgen b75cab
				     (unsigned long long) bytecount,
roentgen b75cab
				     (unsigned long) tile);
roentgen b75cab
#endif
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		if (isMapped(tif) &&
roentgen b75cab
		    (isFillOrder(tif, td->td_fillorder)
roentgen b75cab
		     || (tif->tif_flags & TIFF_NOBITREV))) {
roentgen b75cab
			/*
roentgen b75cab
			 * The image is mapped into memory and we either don't
roentgen b75cab
			 * need to flip bits or the compression routine is
roentgen b75cab
			 * going to handle this operation itself.  In this
roentgen b75cab
			 * case, avoid copying the raw data and instead just
roentgen b75cab
			 * reference the data from the memory mapped file
roentgen b75cab
			 * image.  This assumes that the decompression
roentgen b75cab
			 * routines do not modify the contents of the raw data
roentgen b75cab
			 * buffer (if they try to, the application will get a
roentgen b75cab
			 * fault since the file is mapped read-only).
roentgen b75cab
			 */
roentgen b75cab
			if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
roentgen b75cab
				_TIFFfree(tif->tif_rawdata);
roentgen b75cab
				tif->tif_rawdata = NULL;
roentgen b75cab
				tif->tif_rawdatasize = 0;
roentgen b75cab
			}
roentgen b75cab
			tif->tif_flags &= ~TIFF_MYBUFFER;
roentgen b75cab
			/*
roentgen b75cab
			 * We must check for overflow, potentially causing
roentgen b75cab
			 * an OOB read. Instead of simple
roentgen b75cab
			 *
roentgen b75cab
			 *  td->td_stripoffset[tile]+bytecount > tif->tif_size
roentgen b75cab
			 *
roentgen b75cab
			 * comparison (which can overflow) we do the following
roentgen b75cab
			 * two comparisons:
roentgen b75cab
			 */
roentgen b75cab
			if (bytecount > (uint64)tif->tif_size ||
roentgen b75cab
			    td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
roentgen b75cab
				tif->tif_curtile = NOTILE;
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			tif->tif_rawdatasize = (tmsize_t)bytecount;
roentgen b75cab
			tif->tif_rawdata =
roentgen b75cab
				tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
roentgen b75cab
                        tif->tif_rawdataoff = 0;
roentgen b75cab
                        tif->tif_rawdataloaded = (tmsize_t) bytecount;
roentgen b75cab
			tif->tif_flags |= TIFF_BUFFERMMAP;
roentgen b75cab
		} else {
roentgen b75cab
			/*
roentgen b75cab
			 * Expand raw data buffer, if needed, to hold data
roentgen b75cab
			 * tile coming from file (perhaps should set upper
roentgen b75cab
			 * bound on the size of a buffer we'll use?).
roentgen b75cab
			 */
roentgen b75cab
			tmsize_t bytecountm;
roentgen b75cab
			bytecountm=(tmsize_t)bytecount;
roentgen b75cab
			if ((uint64)bytecountm!=bytecount)
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
roentgen b75cab
				return(0);
roentgen b75cab
			}
roentgen b75cab
			if (bytecountm > tif->tif_rawdatasize) {
roentgen b75cab
				tif->tif_curtile = NOTILE;
roentgen b75cab
				if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					    "Data buffer too small to hold tile %lu",
roentgen b75cab
					    (unsigned long) tile);
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (!TIFFReadBufferSetup(tif, 0, bytecountm))
roentgen b75cab
					return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags&TIFF_BUFFERMMAP) {
roentgen b75cab
				tif->tif_curtile = NOTILE;
roentgen b75cab
				if (!TIFFReadBufferSetup(tif, 0, bytecountm))
roentgen b75cab
					return (0);
roentgen b75cab
			}
roentgen b75cab
roentgen b75cab
			if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
roentgen b75cab
			    bytecountm, module) != bytecountm)
roentgen b75cab
				return (0);
roentgen b75cab
roentgen b75cab
                        tif->tif_rawdataoff = 0;
roentgen b75cab
                        tif->tif_rawdataloaded = bytecountm;
roentgen b75cab
                        
roentgen b75cab
			if (!isFillOrder(tif, td->td_fillorder) &&
roentgen b75cab
			    (tif->tif_flags & TIFF_NOBITREV) == 0)
roentgen b75cab
				TIFFReverseBits(tif->tif_rawdata,
roentgen b75cab
                                                tif->tif_rawdataloaded);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	return (TIFFStartTile(tif, tile));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Setup the raw data buffer in preparation for
roentgen b75cab
 * reading a strip of raw data.  If the buffer
roentgen b75cab
 * is specified as zero, then a buffer of appropriate
roentgen b75cab
 * size is allocated by the library.  Otherwise,
roentgen b75cab
 * the client must guarantee that the buffer is
roentgen b75cab
 * large enough to hold any individual strip of
roentgen b75cab
 * raw data.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFReadBufferSetup";
roentgen b75cab
roentgen b75cab
	assert((tif->tif_flags&TIFF_NOREADRAW)==0);
roentgen b75cab
	tif->tif_flags &= ~TIFF_BUFFERMMAP;
roentgen b75cab
roentgen b75cab
	if (tif->tif_rawdata) {
roentgen b75cab
		if (tif->tif_flags & TIFF_MYBUFFER)
roentgen b75cab
			_TIFFfree(tif->tif_rawdata);
roentgen b75cab
		tif->tif_rawdata = NULL;
roentgen b75cab
		tif->tif_rawdatasize = 0;
roentgen b75cab
	}
roentgen b75cab
	if (bp) {
roentgen b75cab
		tif->tif_rawdatasize = size;
roentgen b75cab
		tif->tif_rawdata = (uint8*) bp;
roentgen b75cab
		tif->tif_flags &= ~TIFF_MYBUFFER;
roentgen b75cab
	} else {
roentgen b75cab
		tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
roentgen b75cab
		if (tif->tif_rawdatasize==0)
roentgen b75cab
			tif->tif_rawdatasize=(tmsize_t)(-1);
roentgen b75cab
		tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize);
roentgen b75cab
		tif->tif_flags |= TIFF_MYBUFFER;
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_rawdata == NULL) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
		    "No space for data buffer at scanline %lu",
roentgen b75cab
		    (unsigned long) tif->tif_row);
roentgen b75cab
		tif->tif_rawdatasize = 0;
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Set state to appear as if a
roentgen b75cab
 * strip has just been read in.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFStartStrip(TIFF* tif, uint32 strip)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
        return 0;
roentgen b75cab
roentgen b75cab
	if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
roentgen b75cab
		if (!(*tif->tif_setupdecode)(tif))
roentgen b75cab
			return (0);
roentgen b75cab
		tif->tif_flags |= TIFF_CODERSETUP;
roentgen b75cab
	}
roentgen b75cab
	tif->tif_curstrip = strip;
roentgen b75cab
	tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
roentgen b75cab
        tif->tif_flags &= ~TIFF_BUF4WRITE;
roentgen b75cab
roentgen b75cab
	if (tif->tif_flags&TIFF_NOREADRAW)
roentgen b75cab
	{
roentgen b75cab
		tif->tif_rawcp = NULL;
roentgen b75cab
		tif->tif_rawcc = 0;  
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
		tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
roentgen b75cab
	}
roentgen b75cab
	return ((*tif->tif_predecode)(tif,
roentgen b75cab
			(uint16)(strip / td->td_stripsperimage)));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Set state to appear as if a
roentgen b75cab
 * tile has just been read in.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFStartTile(TIFF* tif, uint32 tile)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
    if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
roentgen b75cab
        return 0;
roentgen b75cab
roentgen b75cab
	if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
roentgen b75cab
		if (!(*tif->tif_setupdecode)(tif))
roentgen b75cab
			return (0);
roentgen b75cab
		tif->tif_flags |= TIFF_CODERSETUP;
roentgen b75cab
	}
roentgen b75cab
	tif->tif_curtile = tile;
roentgen b75cab
	tif->tif_row =
roentgen b75cab
	    (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth)) *
roentgen b75cab
		td->td_tilelength;
roentgen b75cab
	tif->tif_col =
roentgen b75cab
	    (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength)) *
roentgen b75cab
		td->td_tilewidth;
roentgen b75cab
        tif->tif_flags &= ~TIFF_BUF4WRITE;
roentgen b75cab
	if (tif->tif_flags&TIFF_NOREADRAW)
roentgen b75cab
	{
roentgen b75cab
		tif->tif_rawcp = NULL;
roentgen b75cab
		tif->tif_rawcc = 0;
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		tif->tif_rawcp = tif->tif_rawdata;
roentgen b75cab
		tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
roentgen b75cab
	}
roentgen b75cab
	return ((*tif->tif_predecode)(tif,
roentgen b75cab
			(uint16)(tile/td->td_stripsperimage)));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFCheckRead(TIFF* tif, int tiles)
roentgen b75cab
{
roentgen b75cab
	if (tif->tif_mode == O_WRONLY) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	if (tiles ^ isTiled(tif)) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
roentgen b75cab
		    "Can not read tiles from a stripped image" :
roentgen b75cab
		    "Can not read scanlines from a tiled image");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
    (void) tif; (void) buf; (void) cc;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
    (void) tif;
roentgen b75cab
    assert((cc & 1) == 0);
roentgen b75cab
    TIFFSwabArrayOfShort((uint16*) buf, cc/2);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
    (void) tif;
roentgen b75cab
    assert((cc % 3) == 0);
roentgen b75cab
    TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
    (void) tif;
roentgen b75cab
    assert((cc & 3) == 0);
roentgen b75cab
    TIFFSwabArrayOfLong((uint32*) buf, cc/4);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
_TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
    (void) tif;
roentgen b75cab
    assert((cc & 7) == 0);
roentgen b75cab
    TIFFSwabArrayOfDouble((double*) buf, cc/8);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */