Skip to content

Cast TIFFSetField length args to uint32 #4483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libvips/foreign/tiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@

#include <tiffio.h>

/* Aperio TIFFs (svs) use these compression types for jp2k-compressed tiles.
*/
#define JP2K_YCC (33003)
#define JP2K_RGB (33005)

/* Bioformats uses this tag for jp2k compressed tiles.
*/
#define JP2K_LOSSY (33004)

#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/
Expand Down
9 changes: 0 additions & 9 deletions libvips/foreign/tiff2vips.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,6 @@
#include "jpeg.h"
#endif /*HAVE_JPEG*/

/* Aperio TIFFs (svs) use these compression types for jp2k-compressed tiles.
*/
#define JP2K_YCC 33003
#define JP2K_RGB 33005

/* Bioformats uses this tag for jp2k compressed tiles.
*/
#define JP2K_LOSSY 33004

/* Compression types we handle ourselves.
*/
static int rtiff_we_decompress[] = {
Expand Down
49 changes: 27 additions & 22 deletions libvips/foreign/vips2tiff.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* TIFF PARTS:
* Copyright (c) 1988, 1990 by Sam Leffler.
* All rights reserved.
Expand Down Expand Up @@ -285,10 +285,6 @@
*/
#define MAX_ALPHA (64)

/* Bioformats uses this tag for lossy jp2k compressed tiles.
*/
#define JP2K_LOSSY 33004

/* Compression types we handle ourselves.
*/
static int wtiff_we_compress[] = {
Expand Down Expand Up @@ -396,6 +392,16 @@
GMutex lock;
};

/* libvips uses size_t for the length of binary data items, but libtiff wants
* uint32.
*/
static void
set_data64(TIFF *tif, guint32 tag, size_t length, const void *data)
{
if (length <= UINT_MAX)
TIFFSetField(tif, tag, (guint32) length, data);
}

/* Write an ICC Profile from a file into the JPEG stream.
*/
static int
Expand All @@ -410,7 +416,7 @@
size_t length;
const void *data = vips_blob_get(blob, &length);

TIFFSetField(tif, TIFFTAG_ICCPROFILE, length, data);
set_data64(tif, TIFFTAG_ICCPROFILE, length, data);

#ifdef DEBUG
printf("vips2tiff: attached profile \"%s\"\n", profile);
Expand All @@ -432,7 +438,7 @@

if (vips_image_get_blob(im, VIPS_META_ICC_NAME, &data, &length))
return -1;
TIFFSetField(tif, TIFFTAG_ICCPROFILE, length, data);
set_data64(tif, TIFFTAG_ICCPROFILE, length, data);

#ifdef DEBUG
printf("vips2tiff: attached profile from meta\n");
Expand Down Expand Up @@ -576,7 +582,7 @@
if (vips_image_get_blob(wtiff->ready, VIPS_META_XMP_NAME,
&data, &size))
return -1;
TIFFSetField(tif, TIFFTAG_XMLPACKET, size, data);
set_data64(tif, TIFFTAG_XMLPACKET, size, data);

#ifdef DEBUG
printf("vips2tiff: attached XMP from meta\n");
Expand Down Expand Up @@ -608,7 +614,7 @@
else
size /= 4;

TIFFSetField(tif, TIFFTAG_RICHTIFFIPTC, size, data);
set_data64(tif, TIFFTAG_RICHTIFFIPTC, size, data);

#ifdef DEBUG
printf("vips2tiff: attached IPTC from meta\n");
Expand All @@ -623,16 +629,16 @@
const void *data;
size_t size;

if (!vips_image_get_typeof(wtiff->ready, VIPS_META_PHOTOSHOP_NAME))
return 0;
if (vips_image_get_blob(wtiff->ready, VIPS_META_PHOTOSHOP_NAME,
&data, &size))
return -1;
TIFFSetField(tif, TIFFTAG_PHOTOSHOP, size, data);
if (vips_image_get_typeof(wtiff->ready, VIPS_META_PHOTOSHOP_NAME)) {
if (vips_image_get_blob(wtiff->ready, VIPS_META_PHOTOSHOP_NAME,
&data, &size))
return -1;
set_data64(tif, TIFFTAG_PHOTOSHOP, size, data);

#ifdef DEBUG
printf("vips2tiff: attached photoshop data from meta\n");
printf("vips2tiff: attached %zd bytes of photoshop data\n", size);
#endif /*DEBUG*/
}

return 0;
}
Expand Down Expand Up @@ -885,10 +891,10 @@
if (wtiff->compression == COMPRESSION_ZSTD) {
// Set zstd compression level - only accept valid values (1-22)
if (wtiff->level)
TIFFSetField(tif, TIFFTAG_ZSTD_LEVEL, VIPS_CLIP(1, wtiff->level, 22));
if (wtiff->predictor != VIPS_FOREIGN_TIFF_PREDICTOR_NONE)
TIFFSetField(tif,
TIFFTAG_PREDICTOR, wtiff->predictor);
TIFFTAG_ZSTD_LEVEL, VIPS_CLIP(1, wtiff->level, 22));
if (wtiff->predictor != VIPS_FOREIGN_TIFF_PREDICTOR_NONE)
TIFFSetField(tif, TIFFTAG_PREDICTOR, wtiff->predictor);
}
#endif /*HAVE_TIFF_COMPRESSION_WEBP*/

Expand Down Expand Up @@ -960,8 +966,7 @@

int alpha_bands;

TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL,
wtiff->ready->Bands);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, wtiff->ready->Bands);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE,
vips_format_sizeof(wtiff->ready->BandFmt) << 3);

Expand Down Expand Up @@ -1109,7 +1114,7 @@
printf("setting %zd bytes of table data\n", length);
#endif /*DEBUG*/

TIFFSetField(tif, TIFFTAG_JPEGTABLES, length, buffer);
set_data64(tif, TIFFTAG_JPEGTABLES, length, buffer);

g_free(buffer);
}
Expand Down Expand Up @@ -2297,7 +2302,7 @@
*/
#define CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) \
TIFFSetField(out, tag, v)
TIFFSetField(out, tag, v)

static int
wtiff_copy_tiles(Wtiff *wtiff, TIFF *out, TIFF *in)
Expand Down
Loading