Skip to content

Commit

Permalink
Merge pull request OSGeo#7810 from rouault/libtiff_resync
Browse files Browse the repository at this point in the history
Internal libtiff: resync with upstream
  • Loading branch information
rouault authored May 23, 2023
2 parents a992179 + 40e917a commit d339527
Show file tree
Hide file tree
Showing 10 changed files with 982 additions and 1,146 deletions.
6 changes: 3 additions & 3 deletions frmts/gtiff/libtiff/tif_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
{
uint16_t i = 0;
const char *ep = s + slen;
const char *cp = s;

if (slen > 0)
{
const char *ep = s + slen;
const char *cp = s;
do
{
for (; cp < ep && *cp != '\0'; cp++)
Expand Down Expand Up @@ -1621,7 +1621,7 @@ void TIFFFreeDirectory(TIFF *tif)
TIFFDirectory *td = &tif->tif_dir;
int i;

_TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
_TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset));
CleanupField(td_sminsamplevalue);
CleanupField(td_smaxsamplevalue);
CleanupField(td_colormap[0]);
Expand Down
8 changes: 4 additions & 4 deletions frmts/gtiff/libtiff/tif_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ typedef struct
*/
typedef struct
{
#define FIELD_SETLONGS 4
#define FIELDSET_ITEMS 4
/* bit vector of fields that are set */
unsigned long td_fieldsset[FIELD_SETLONGS];
uint32_t td_fieldsset[FIELDSET_ITEMS];

uint32_t td_imagewidth, td_imagelength, td_imagedepth;
uint32_t td_tilewidth, td_tilelength, td_tiledepth;
Expand Down Expand Up @@ -202,9 +202,9 @@ typedef struct
*/
#define FIELD_PSEUDO 0

#define FIELD_LAST (32 * FIELD_SETLONGS - 1)
#define FIELD_LAST (32 * FIELDSET_ITEMS - 1)

#define BITn(n) (((unsigned long)1L) << ((n)&0x1f))
#define BITn(n) (((uint32_t)1L) << ((n)&0x1f))
#define BITFIELDn(tif, n) ((tif)->tif_dir.td_fieldsset[(n) / 32])
#define TIFFFieldSet(tif, field) (BITFIELDn(tif, field) & BITn(field))
#define TIFFSetFieldBit(tif, field) (BITFIELDn(tif, field) |= BITn(field))
Expand Down
971 changes: 387 additions & 584 deletions frmts/gtiff/libtiff/tif_dirinfo.c

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions frmts/gtiff/libtiff/tif_dirread.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
float *value);
static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedDouble(TIFF *tif, TIFFDirEntry *direntry, double *value);
static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
TIFFRational_t *value);

static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckRangeByteSbyte(int8_t value);
Expand Down Expand Up @@ -3469,6 +3472,47 @@ TIFFReadDirEntryCheckedSrational(TIFF *tif, TIFFDirEntry *direntry,
return (TIFFReadDirEntryErrOk);
}

static enum TIFFReadDirEntryErr
TIFFReadDirEntryCheckedRationalDirect(TIFF *tif, TIFFDirEntry *direntry,
TIFFRational_t *value)
{ /*--: SetGetRATIONAL_directly:_CustomTag: Read rational (and signed rationals)
directly --*/
UInt64Aligned_t m;

assert(sizeof(double) == 8);
assert(sizeof(uint64_t) == 8);
assert(sizeof(uint32_t) == 4);

if (direntry->tdir_count != 1)
return (TIFFReadDirEntryErrCount);

if (direntry->tdir_type != TIFF_RATIONAL &&
direntry->tdir_type != TIFF_SRATIONAL)
return (TIFFReadDirEntryErrType);

if (!(tif->tif_flags & TIFF_BIGTIFF))
{
enum TIFFReadDirEntryErr err;
uint32_t offset = direntry->tdir_offset.toff_long;
if (tif->tif_flags & TIFF_SWAB)
TIFFSwabLong(&offset);
err = TIFFReadDirEntryData(tif, offset, 8, m.i);
if (err != TIFFReadDirEntryErrOk)
return (err);
}
else
{
m.l = direntry->tdir_offset.toff_long8;
}

if (tif->tif_flags & TIFF_SWAB)
TIFFSwabArrayOfLong(m.i, 2);

value->uNum = m.i[0];
value->uDenom = m.i[1];
return (TIFFReadDirEntryErrOk);
} /*-- TIFFReadDirEntryCheckedRationalDirect() --*/

static void TIFFReadDirEntryCheckedFloat(TIFF *tif, TIFFDirEntry *direntry,
float *value)
{
Expand Down Expand Up @@ -4526,6 +4570,49 @@ int TIFFReadDirectory(TIFF *tif)
}
break;
/* END REV 4.0 COMPATIBILITY */
case TIFFTAG_EP_BATTERYLEVEL:
/* TIFFTAG_EP_BATTERYLEVEL can be RATIONAL or ASCII.
* LibTiff defines it as ASCII and converts RATIONAL to an
* ASCII string. */
switch (dp->tdir_type)
{
case TIFF_RATIONAL:
{
/* Read rational and convert to ASCII*/
enum TIFFReadDirEntryErr err;
TIFFRational_t rValue;
err = TIFFReadDirEntryCheckedRationalDirect(
tif, dp, &rValue);
if (err != TIFFReadDirEntryErrOk)
{
fip = TIFFFieldWithTag(tif, dp->tdir_tag);
TIFFReadDirEntryOutputErr(
tif, err, module,
fip ? fip->field_name : "unknown tagname",
1);
}
else
{
char szAux[32];
snprintf(szAux, sizeof(szAux) - 1, "%d/%d",
rValue.uNum, rValue.uDenom);
TIFFSetField(tif, dp->tdir_tag, szAux);
}
}
break;
case TIFF_ASCII:
(void)TIFFFetchNormalTag(tif, dp, TRUE);
break;
default:
fip = TIFFFieldWithTag(tif, dp->tdir_tag);
TIFFWarningExtR(tif, module,
"Invalid data type for tag %s. "
"ASCII or RATIONAL expected",
fip ? fip->field_name
: "unknown tagname");
break;
}
break;
default:
(void)TIFFFetchNormalTag(tif, dp, TRUE);
break;
Expand Down
2 changes: 1 addition & 1 deletion frmts/gtiff/libtiff/tif_lerc.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ static int LERCPreDecode(TIFF *tif, uint16_t s)
{
const unsigned nb_pixels = sp->segment_width * sp->segment_height;
unsigned i;
#if HOST_BIGENDIAN
#if WORDS_BIGENDIAN
const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0};
#else
const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f};
Expand Down
5 changes: 5 additions & 0 deletions frmts/gtiff/libtiff/tif_lzw.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)

if (sp->read_error)
{
TIFFErrorExtR(tif, module,
"LZWDecode: Scanline %" PRIu32 " cannot be read due to "
"previous error",
tif->tif_row);
return 0;
}

Expand Down Expand Up @@ -742,6 +746,7 @@ static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s)
return (1);

no_eoi:
sp->read_error = 1;
TIFFErrorExtR(tif, module,
"LZWDecode: Strip %" PRIu32 " not terminated with EOI code",
tif->tif_curstrip);
Expand Down
3 changes: 3 additions & 0 deletions frmts/gtiff/libtiff/tif_ojpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,9 @@ static int OJPEGWriteHeaderInfo(TIFF *tif)
{
assert(sp->subsampling_convert_ycbcrbuf == 0);
assert(sp->subsampling_convert_ycbcrimage == 0);
/* Check for division by zero. */
if (sp->subsampling_hor == 0 || sp->subsampling_ver == 0)
return (0);
sp->subsampling_convert_ylinelen =
((sp->strile_width + sp->subsampling_hor * 8 - 1) /
(sp->subsampling_hor * 8) * sp->subsampling_hor * 8);
Expand Down
6 changes: 5 additions & 1 deletion frmts/gtiff/libtiff/tif_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,12 @@ TIFF *TIFFClientOpenExt(const char *name, const char *mode,
(tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
break;
case 'H':
TIFFWarningExtR(tif, name,
"H(ost) mode is deprecated. Since "
"libtiff 4.5.1, it is an alias of 'B' / "
"FILLORDER_MSB2LSB.");
tif->tif_flags =
(tif->tif_flags & ~TIFF_FILLORDER) | HOST_FILLORDER;
(tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
break;
case 'M':
if (m == O_RDONLY)
Expand Down
Loading

0 comments on commit d339527

Please sign in to comment.