Added routine TIFFDataWidth for detrmining TIFFDataType sizes instead of

working with tiffDataWidth array directly. Should prevent out-of-borders bugs
in case of unknown or broken data types.
EstimateStripByteCounts routine modified, so it won't work when tags with
uknown sizes founded.
This commit is contained in:
Andrey Kiselev 2002-03-15 11:05:56 +00:00
parent ce38c97afb
commit fa0ce83d3f
5 changed files with 61 additions and 36 deletions

View File

@ -1,4 +1,4 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dir.c,v 1.17 2002-02-24 15:40:57 warmerda Exp $ */
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dir.c,v 1.18 2002-03-15 11:05:56 dron Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@ -517,7 +517,7 @@ _TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
td->td_customValues = (TIFFTagValue *)
_TIFFmalloc(sizeof(TIFFTagValue));
tv_size = tiffDataWidth[fip->field_type];
tv_size = TIFFDataWidth(fip->field_type);
tv = td->td_customValues + (td->td_customValueCount - 1);
tv->info = fip;
if( fip->field_passcount )

View File

@ -1,4 +1,4 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dir.h,v 1.4 2002-02-24 16:07:49 warmerda Exp $ */
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dir.h,v 1.5 2002-03-15 11:05:56 dron Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@ -233,7 +233,6 @@ typedef struct {
((v) & (tif)->tif_typemask[type]) << (tif)->tif_typeshift[type] : \
(v) & (tif)->tif_typemask[type]))
extern const int tiffDataWidth[]; /* table of tag datatype widths */
#define BITn(n) (((u_long)1L)<<((n)&0x1f))
#define BITFIELDn(tif, n) ((tif)->tif_dir.td_fieldsset[(n)/32])
@ -247,6 +246,7 @@ extern const int tiffDataWidth[]; /* table of tag datatype widths */
#if defined(__cplusplus)
extern "C" {
#endif
extern int TIFFDataWidth(TIFFDataType); /* table of tag datatype widths */
extern void _TIFFSetupFieldInfo(TIFF*);
extern void _TIFFPrintFieldInfo(TIFF*, FILE*);
extern TIFFDataType _TIFFSampleToTagType(TIFF*);

View File

@ -1,4 +1,4 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirinfo.c,v 1.10 2002-03-10 13:11:35 dron Exp $ */
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirinfo.c,v 1.11 2002-03-15 11:05:56 dron Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@ -340,21 +340,35 @@ _TIFFPrintFieldInfo(TIFF* tif, FILE* fd)
}
}
const int tiffDataWidth[] = {
1, /* nothing */
1, /* TIFF_BYTE */
1, /* TIFF_ASCII */
2, /* TIFF_SHORT */
4, /* TIFF_LONG */
8, /* TIFF_RATIONAL */
1, /* TIFF_SBYTE */
1, /* TIFF_UNDEFINED */
2, /* TIFF_SSHORT */
4, /* TIFF_SLONG */
8, /* TIFF_SRATIONAL */
4, /* TIFF_FLOAT */
8, /* TIFF_DOUBLE */
};
/*
* Return size of TIFFDataType in bytes
*/
int
TIFFDataWidth(TIFFDataType type)
{
switch(type)
{
case 0: /* nothing */
case 1: /* TIFF_BYTE */
case 2: /* TIFF_ASCII */
case 6: /* TIFF_SBYTE */
case 7: /* TIFF_UNDEFINED */
return 1;
case 3: /* TIFF_SHORT */
case 8: /* TIFF_SSHORT */
return 2;
case 4: /* TIFF_LONG */
case 9: /* TIFF_SLONG */
case 11: /* TIFF_FLOAT */
return 4;
case 5: /* TIFF_RATIONAL */
case 10: /* TIFF_SRATIONAL */
case 12: /* TIFF_DOUBLE */
return 8;
default:
return 1; /* will return safe value for unknown sizes */
}
}
/*
* Return nearest TIFFDataType to the sample type of an image.

View File

@ -1,4 +1,4 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirread.c,v 1.7 2002-02-24 15:01:15 warmerda Exp $ */
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirread.c,v 1.8 2002-03-15 11:05:56 dron Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@ -41,7 +41,7 @@ extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
#endif
static void EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
static int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
static void MissingRequired(TIFF*, const char*);
static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
@ -511,7 +511,8 @@ TIFFReadDirectory(TIFF* tif)
TIFFWarning(tif->tif_name,
"TIFF directory is missing required \"%s\" field, calculating from imagelength",
_TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
EstimateStripByteCounts(tif, dir, dircount);
if (EstimateStripByteCounts(tif, dir, dircount) < 0)
goto bad;
#define BYTECOUNTLOOKSBAD \
((td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
(td->td_compression == COMPRESSION_NONE && \
@ -527,7 +528,8 @@ TIFFReadDirectory(TIFF* tif)
TIFFWarning(tif->tif_name,
"Bogus \"%s\" field, ignoring and calculating from imagelength",
_TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
EstimateStripByteCounts(tif, dir, dircount);
if(EstimateStripByteCounts(tif, dir, dircount) < 0)
goto bad;
}
if (dir)
_TIFFfree((char *)dir);
@ -566,7 +568,7 @@ bad:
return (0);
}
static void
static int
EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
{
register TIFFDirEntry *dp;
@ -587,10 +589,18 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
uint16 n;
/* calculate amount of space used by indirect values */
for (dp = dir, n = dircount; n > 0; n--, dp++) {
uint32 cc = dp->tdir_count*tiffDataWidth[dp->tdir_type];
if (cc > sizeof (uint32))
space += cc;
for (dp = dir, n = dircount; n > 0; n--, dp++)
{
uint32 cc;
if(dp->tdir_tag == IGNORE)
{
TIFFError(tif->tif_name,
"Cannot determine StripByteCounts values, because of tags with unknown sizes");
return -1;
}
cc = dp->tdir_count*TIFFDataWidth(dp->tdir_type);
if (cc > sizeof (uint32))
space += cc;
}
space = filesize - space;
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
@ -618,6 +628,7 @@ EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
td->td_rowsperstrip = td->td_imagelength;
return 1;
}
static void
@ -652,7 +663,7 @@ CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
static tsize_t
TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
{
int w = tiffDataWidth[dir->tdir_type];
int w = TIFFDataWidth(dir->tdir_type);
tsize_t cc = dir->tdir_count * w;
if (!isMapped(tif)) {
@ -857,7 +868,7 @@ TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
uint32* l;
l = (uint32*)CheckMalloc(tif,
dir->tdir_count*tiffDataWidth[dir->tdir_type],
dir->tdir_count*TIFFDataWidth(dir->tdir_type),
"to fetch array of rationals");
if (l) {
if (TIFFFetchData(tif, dir, (char *)l)) {
@ -988,7 +999,7 @@ TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
/* TIFF_ASCII */
/* TIFF_UNDEFINED */
TIFFError(tif->tif_name,
"Cannot read TIFF_ANY type %d for field \"%s\"",
"cannot read TIFF_ANY type %d for field \"%s\"",
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
return (0);
}

View File

@ -1,4 +1,4 @@
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirwrite.c,v 1.11 2002-03-10 13:11:35 dron Exp $ */
/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirwrite.c,v 1.12 2002-03-15 11:05:56 dron Exp $ */
/*
* Copyright (c) 1988-1997 Sam Leffler
@ -842,8 +842,8 @@ TIFFWriteAnyArray(TIFF* tif,
char* w = buf;
int i, status = 0;
if (n * tiffDataWidth[type] > sizeof buf)
w = (char*) _TIFFmalloc(n * tiffDataWidth[type]);
if (n * TIFFDataWidth(type) > sizeof buf)
w = (char*) _TIFFmalloc(n * TIFFDataWidth(type));
switch (type) {
case TIFF_BYTE:
{ uint8* bp = (uint8*) w;
@ -991,7 +991,7 @@ TIFFWriteData(TIFF* tif, TIFFDirEntry* dir, char* cp)
}
}
dir->tdir_offset = tif->tif_dataoff;
cc = dir->tdir_count * tiffDataWidth[dir->tdir_type];
cc = dir->tdir_count * TIFFDataWidth(dir->tdir_type);
if (SeekOK(tif, dir->tdir_offset) &&
WriteOK(tif, cp, cc)) {
tif->tif_dataoff += (cc + 1) & ~1;