/*
 * Grow the strip data structures by delta strips.
 */
static int
TIFFGrowStrips(TIFF* tif, uint32 delta, const char* module)
{
	TIFFDirectory *td = &tif->tif_dir;
	uint64* new_stripoffset;
	uint64* new_stripbytecount;

	assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
	new_stripoffset = (uint64*)_TIFFrealloc(td->td_stripoffset,
		(td->td_nstrips + delta) * sizeof (uint64));
	new_stripbytecount = (uint64*)_TIFFrealloc(td->td_stripbytecount,
		(td->td_nstrips + delta) * sizeof (uint64));
	if (new_stripoffset == NULL || new_stripbytecount == NULL) {
		if (new_stripoffset)
			_TIFFfree(new_stripoffset);
		if (new_stripbytecount)
			_TIFFfree(new_stripbytecount);
		td->td_nstrips = 0;
		TIFFErrorExt(tif->tif_clientdata, module, "No space to expand strip arrays");
		return (0);
	}
	td->td_stripoffset = new_stripoffset;
	td->td_stripbytecount = new_stripbytecount;
	_TIFFmemset(td->td_stripoffset + td->td_nstrips,
		    0, delta*sizeof (uint64));
	_TIFFmemset(td->td_stripbytecount + td->td_nstrips,
		    0, delta*sizeof (uint64));
	td->td_nstrips += delta;
        tif->tif_flags |= TIFF_DIRTYDIRECT;

	return (1);
}
Exemple #2
0
/*
 * Grow the strip data structures by delta strips.
 */
static int
TIFFGrowStrips(TIFF* tif, int delta, const char* module) {
    TIFFDirectory*	td = &tif->tif_dir;
    uint32*		new_stripoffset, *new_stripbytecount;

    assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
    new_stripoffset = (uint32*)_TIFFrealloc(td->td_stripoffset,
                                            (td->td_nstrips + delta) * sizeof(uint32));
    new_stripbytecount = (uint32*)_TIFFrealloc(td->td_stripbytecount,
                         (td->td_nstrips + delta) * sizeof(uint32));
    if (new_stripoffset == NULL || new_stripbytecount == NULL) {
        if (new_stripoffset) {
            _TIFFfree(new_stripoffset);
        }
        if (new_stripbytecount) {
            _TIFFfree(new_stripbytecount);
        }
        td->td_nstrips = 0;
        TIFFError(module, "%s: No space to expand strip arrays",
                  tif->tif_name);
        return (0);
    }
    td->td_stripoffset = new_stripoffset;
    td->td_stripbytecount = new_stripbytecount;
    _TIFFmemset(td->td_stripoffset + td->td_nstrips,
                0, delta * sizeof(uint32));
    _TIFFmemset(td->td_stripbytecount + td->td_nstrips,
                0, delta * sizeof(uint32));
    td->td_nstrips += delta;
    return (1);
}
Exemple #3
0
/*
 * Grow the strip data structures by delta strips.  This routine assumes entire
 * offsets / byte counts arrays are in memory.
 */
static int
TIFFGrowStrips(TIFF* tif, int delta, const char* module)
{
	TIFFDirectory	*td = &tif->tif_dir;
	toff_t		*new_stripoffs;
	uint32		*new_stripbcs;

	assert(td->td_planarconfig == PLANARCONFIG_CONTIG);
	/* failure if have already placed offsets array in file */
	if (td->td_stripoffsoff) {
		td->td_nstrips = 0;
		TIFFErrorExt(tif->tif_clientdata, module, 
			"%s: Can't expand strip array", tif->tif_name);
		return (0);
	}
	new_stripoffs = (toff_t*)_TIFFrealloc(td->td_stripoffsbuf,
		(td->td_nstrips + delta) * sizeof (toff_t));
	new_stripbcs = (uint32*)_TIFFrealloc(td->td_stripbcsbuf,
		(td->td_nstrips + delta) * sizeof (uint32));
	if (new_stripoffs == NULL || new_stripbcs == NULL) {
		_TIFFfree(new_stripoffs);
		_TIFFfree(new_stripbcs);
		td->td_nstrips = 0;
		TIFFErrorExt(tif->tif_clientdata, module, 
			"%s: No space to expand strip arrays", tif->tif_name);
		return (0);
	}
	td->td_stripoffsbuf = new_stripoffs;
	td->td_stripbcsbuf = new_stripbcs;
	_TIFFmemset(td->td_stripoffsbuf + td->td_nstrips, 0, delta * sizeof(toff_t));
	_TIFFmemset(td->td_stripbcsbuf + td->td_nstrips, 0, delta * sizeof(uint32));
	td->td_nstrips += delta;
	td->td_stripbufmax = TIFFmax(td->td_stripbufmax, td->td_nstrips);
	return (1);
}
Exemple #4
0
static int
cpTiles(TIFF* in, TIFF* out)
{
	tmsize_t bufsize = TIFFTileSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		ttile_t t, nt = TIFFNumberOfTiles(in);
		uint64 *bytecounts;

		TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);
		for (t = 0; t < nt; t++) {
			if (bytecounts[t] > (uint64) bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);
				if (!buf)
					return (0);
				bufsize = (tmsize_t)bytecounts[t];
			}
			if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||
			    TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
Exemple #5
0
static int
cpStrips(TIFF* in, TIFF* out)
{
	tmsize_t bufsize  = TIFFStripSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		tstrip_t s, ns = TIFFNumberOfStrips(in);
		uint64 *bytecounts;

		TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
		for (s = 0; s < ns; s++) {
			if (bytecounts[s] > (uint64)bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]);
				if (!buf)
					return (0);
				bufsize = (tmsize_t)bytecounts[s];
			}
			if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 ||
			    TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
static int
cpTiles(TIFF* in, TIFF* out)
{
	tsize_t bufsize = TIFFTileSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		ttile_t t, nt = TIFFNumberOfTiles(in);
		uint32 *bytecounts;

		if (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) {
			fprintf(stderr, "tiffsplit: tile byte counts are missing\n");
			return (0);
		}
		for (t = 0; t < nt; t++) {
			if (bytecounts[t] > (uint32) bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[t]);
				if (!buf)
					return (0);
				bufsize = bytecounts[t];
			}
			if (TIFFReadRawTile(in, t, buf, bytecounts[t]) < 0 ||
			    TIFFWriteRawTile(out, t, buf, bytecounts[t]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
static int
cpStrips(TIFF* in, TIFF* out)
{
	tsize_t bufsize  = TIFFStripSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		tstrip_t s, ns = TIFFNumberOfStrips(in);
		uint32 *bytecounts;

		if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) {
			fprintf(stderr, "tiffsplit: strip byte counts are missing\n");
			return (0);
		}
		for (s = 0; s < ns; s++) {
			if (bytecounts[s] > (uint32)bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[s]);
				if (!buf)
					return (0);
				bufsize = bytecounts[s];
			}
			if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 ||
			    TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
Exemple #8
0
void
_TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
{
	TIFFFieldInfo** tp;
	int i;

	if (tif->tif_nfields > 0) {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFrealloc(tif->tif_fieldinfo,
			(tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
	} else {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
	}
	tp = &tif->tif_fieldinfo[tif->tif_nfields];
	for (i = 0; i < n; i++)
		tp[i] = (TIFFFieldInfo*) &info[i];	/* XXX */
	/*
	 * NB: the core tags are presumed sorted correctly.
	 */
	if (tif->tif_nfields > 0)
		qsort(tif->tif_fieldinfo, (size_t) (tif->tif_nfields += n),
		    sizeof (TIFFFieldInfo*), tagCompare);
	else
		tif->tif_nfields += n;
}
static int
cpTiles(TIFF* in, TIFF* out)
{
    tsize_t bufsize = TIFFTileSize(in);
    unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

    if (buf) {
	ttile_t t, nt = TIFFNumberOfTiles(in);
	tsize_t *bytecounts;

	TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);
	for (t = 0; t < nt; t++) {
	    if (bytecounts[t] > bufsize) {
		buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[t]);
		if (!buf)
		    goto bad;
		bufsize = bytecounts[t];
	    }
	    if (TIFFReadRawTile(in, t, buf, bytecounts[t]) < 0 ||
		TIFFWriteRawTile(out, t, buf, bytecounts[t]) < 0) {
		_TIFFfree(buf);
		return 0;
	    }
	}
	_TIFFfree(buf);
	return 1;
    }

bad:
    TIFFError(TIFFFileName(in),
		  "Can't allocate space for tile buffer.");
	return (0);
}
Exemple #10
0
void
_TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
{
	TIFFFieldInfo** tp;
	int i;

        tif->tif_foundfield = NULL;

	if (tif->tif_nfields > 0) {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFrealloc(tif->tif_fieldinfo,
			(tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
	} else {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
	}
	assert(tif->tif_fieldinfo != NULL);
	tp = tif->tif_fieldinfo + tif->tif_nfields;
	for (i = 0; i < n; i++)
		*tp++ = (TIFFFieldInfo*) (info + i);	/* XXX */

        /* Sort the field info by tag number */
        qsort(tif->tif_fieldinfo, tif->tif_nfields += n,
	      sizeof (TIFFFieldInfo*), tagCompare);
}
static int
cpStrips(TIFF* in, TIFF* out)
{
    tsize_t bufsize  = TIFFStripSize(in);
    unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

    if (buf) {
	tstrip_t s, ns = TIFFNumberOfStrips(in);
	tsize_t *bytecounts;

	TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
	for (s = 0; s < ns; s++) {
	    if (bytecounts[s] > bufsize) {
		buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[s]);
		if (!buf)
		    goto bad;
		bufsize = bytecounts[s];
	    }
	    if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 ||
		TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) {
		_TIFFfree(buf);
		return 0;
	    }
	}
	_TIFFfree(buf);
	return 1;
    }

bad:
	TIFFError(TIFFFileName(in),
		  "Can't allocate space for strip buffer.");
	return 0;
}
Exemple #12
0
TIFFCodec*
TIFFGetConfiguredCODECs()
{
	int i = 1;
	codec_t *cd;
	const TIFFCodec* c;
	TIFFCodec* codecs = NULL;
	TIFFCodec* new_codecs;

	for (cd = registeredCODECS; cd; cd = cd->next) {
		new_codecs = (TIFFCodec *)
			_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
		if (!new_codecs) {
			_TIFFfree (codecs);
			return NULL;
		}
		codecs = new_codecs;
		_TIFFmemcpy(codecs + i - 1, cd, sizeof(TIFFCodec));
		i++;
	}
	for (c = _TIFFBuiltinCODECS; c->name; c++) {
		if (TIFFIsCODECConfigured(c->scheme)) {
			new_codecs = (TIFFCodec *)
				_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
			if (!new_codecs) {
				_TIFFfree (codecs);
				return NULL;
			}
			codecs = new_codecs;
			_TIFFmemcpy(codecs + i - 1, (const void*)c, sizeof(TIFFCodec));
			i++;
		}
	}

	new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
	if (!new_codecs) {
		_TIFFfree (codecs);
		return NULL;
	}
	codecs = new_codecs;
	_TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));

	return codecs;
}
Exemple #13
0
tdata_t
_TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
		  size_t nmemb, size_t elem_size, const char* what)
{
	tdata_t cp = NULL;
	tsize_t	bytes = nmemb * elem_size;

	/*
	 * XXX: Check for integer overflow.
	 */
	if (nmemb && elem_size && bytes / elem_size == nmemb)
		cp = _TIFFrealloc(buffer, bytes);

	if (cp == NULL)
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
			     "No space %s", what);

	return cp;
}
Exemple #14
0
TDimTiffIFDs readAllTiffIFDs (TIFF *tif) {
  TDimTiffIFDs ifds;
  TDimTiffIFD  newIFD;

  ifds.count = 1;
  ifds.ifds = (TDimTiffIFD *) _TIFFmalloc( sizeof(TDimTiffIFD) );
  
  ifds.ifds[0] = readFirstTiffIFD ( tif );
  newIFD = ifds.ifds[0];

  while (newIFD.next != 0) {
    ifds.count++;
    newIFD = readTiffIFD ( tif, newIFD.next);
    ifds.ifds = (TDimTiffIFD *) _TIFFrealloc(ifds.ifds, sizeof(TDimTiffIFD) * ifds.count );
    if (ifds.ifds == NULL) return ifds;
    ifds.ifds[ifds.count-1] = newIFD;
  }

  return ifds;
}
tdata_t
_TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
		  size_t nmemb, size_t elem_size, const char* what)
{
	tdata_t cp = NULL;
	tsize_t	bytes = nmemb * elem_size;

	/*
	 * XXX: Check for integer overflow.
	 */
	if (nmemb && elem_size && bytes / elem_size == nmemb)
		cp = _TIFFrealloc(buffer, bytes);

	if (cp == NULL)
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
			     "Failed to allocate memory for %s "
			     "(%ld elements of %ld bytes each)",
			     what,(long) nmemb, (long) elem_size);

	return cp;
}
Exemple #16
0
void*
_TIFFCheckRealloc(TIFF* tif, void* buffer,
		  tmsize_t nmemb, tmsize_t elem_size, const char* what)
{
	void* cp = NULL;
	tmsize_t bytes = nmemb * elem_size;

	/*
	 * XXX: Check for integer overflow.
	 */
	if (nmemb && elem_size && !_TIFFIfMultiplicationOverflow(nmemb, elem_size))
		cp = _TIFFrealloc(buffer, bytes);

	if (cp == NULL) {
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
			     "Failed to allocate memory for %s "
			     "(%ld elements of %ld bytes each)",
			     what,(long) nmemb, (long) elem_size);
	}

	return cp;
}
Exemple #17
0
void
_TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], int n)
{
	TIFFFieldInfo** tp;
	int i;

	if (tif->tif_nfields > 0) {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFrealloc(tif->tif_fieldinfo,
			(tif->tif_nfields+n) * sizeof (TIFFFieldInfo*));
	} else {
		tif->tif_fieldinfo = (TIFFFieldInfo**)
		    _TIFFmalloc(n * sizeof (TIFFFieldInfo*));
	}
	assert(tif->tif_fieldinfo != NULL);
	tp = &tif->tif_fieldinfo[tif->tif_nfields];
	for (i = 0; i < n; i++)
		tp[i] = (TIFFFieldInfo*) &info[i];	/* XXX */

        /* Sort the field info by tag number */
        qsort(tif->tif_fieldinfo, (size_t) (tif->tif_nfields += n),
              sizeof (TIFFFieldInfo*), tagCompare);
}
Exemple #18
0
gdata_t _GTIFrealloc(gdata_t ptr, gsize_t size)
{
    return _TIFFrealloc((tdata_t)ptr, (tsize_t) size);
}
Exemple #19
0
/* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'
 * Returns 1 in case of success, 0 otherwise. */
static int TIFFReadAndRealloc( TIFF* tif, tmsize_t size,
                               tmsize_t rawdata_offset,
                               int is_strip, uint32 strip_or_tile,
                               const char* module )
{
#if SIZEOF_SIZE_T == 8
        tmsize_t threshold = INITIAL_THRESHOLD;
#endif
        tmsize_t already_read = 0;

        /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
        /* so as to avoid allocating too much memory in case the file is too */
        /* short. We could ask for the file size, but this might be */
        /* expensive with some I/O layers (think of reading a gzipped file) */
        /* Restrict to 64 bit processes, so as to avoid reallocs() */
        /* on 32 bit processes where virtual memory is scarce.  */
        while( already_read < size )
        {
            tmsize_t bytes_read;
            tmsize_t to_read = size - already_read;
#if SIZEOF_SIZE_T == 8
            if( to_read >= threshold && threshold < MAX_THRESHOLD &&
                already_read + to_read + rawdata_offset > tif->tif_rawdatasize )
            {
                to_read = threshold;
                threshold *= THRESHOLD_MULTIPLIER;
            }
#endif
            if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize) {
                uint8* new_rawdata;
                assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
                tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
                        (uint64)already_read + to_read + rawdata_offset, 1024);
                if (tif->tif_rawdatasize==0) {
                    TIFFErrorExt(tif->tif_clientdata, module,
                                "Invalid buffer size");
                    return 0;
                }
                new_rawdata = (uint8*) _TIFFrealloc(
                                tif->tif_rawdata, tif->tif_rawdatasize);
                if( new_rawdata == 0 )
                {
                    TIFFErrorExt(tif->tif_clientdata, module,
                        "No space for data buffer at scanline %lu",
                        (unsigned long) tif->tif_row);
                    _TIFFfree(tif->tif_rawdata);
                    tif->tif_rawdata = 0;
                    tif->tif_rawdatasize = 0;
                    return 0;
                }
                tif->tif_rawdata = new_rawdata;
            }

            bytes_read = TIFFReadFile(tif,
                tif->tif_rawdata + rawdata_offset + already_read, to_read);
            already_read += bytes_read;
            if (bytes_read != to_read) {
                memset( tif->tif_rawdata + rawdata_offset + already_read, 0,
                        tif->tif_rawdatasize - rawdata_offset - already_read );
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
                if( is_strip )
                {
                    TIFFErrorExt(tif->tif_clientdata, module,
                        "Read error at scanline %lu; got %I64u bytes, "
                        "expected %I64u",
                                        (unsigned long) tif->tif_row,
                                        (unsigned __int64) already_read,
                                        (unsigned __int64) size);
                }
                else
                {
                    TIFFErrorExt(tif->tif_clientdata, module,
                        "Read error at row %lu, col %lu, tile %lu; "
                        "got %I64u bytes, expected %I64u",
                                        (unsigned long) tif->tif_row,
                                        (unsigned long) tif->tif_col,
                                        (unsigned long) strip_or_tile,
                                        (unsigned __int64) already_read,
                                        (unsigned __int64) size);
                }
#else
                if( is_strip )
                {
                    TIFFErrorExt(tif->tif_clientdata, module,
                        "Read error at scanline %lu; got %llu bytes, "
                        "expected %llu",
                                        (unsigned long) tif->tif_row,
                                        (unsigned long long) already_read,
                                        (unsigned long long) size);
                }
                else
                {
                    TIFFErrorExt(tif->tif_clientdata, module,
                        "Read error at row %lu, col %lu, tile %lu; "
                        "got %llu bytes, expected %llu",
                                        (unsigned long) tif->tif_row,
                                        (unsigned long) tif->tif_col,
                                        (unsigned long) strip_or_tile,
                                        (unsigned long long) already_read,
                                        (unsigned long long) size);
                }
#endif
                return 0;
            }
        }
        return 1;
}