X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8414a40c52191d4c7cfeea74df22d9d64cbec415..0ab8f4023c8bea18c31bca1215bc57d6c9e0fb24:/src/tiff/tools/tiffsplit.c diff --git a/src/tiff/tools/tiffsplit.c b/src/tiff/tools/tiffsplit.c index aaf4bb455f..300dd8b669 100644 --- a/src/tiff/tools/tiffsplit.c +++ b/src/tiff/tools/tiffsplit.c @@ -43,7 +43,11 @@ extern int getopt(int, char**, char*); #define CopyField3(tag, v1, v2, v3) \ if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3) -static char fname[1024+1]; +#define PATH_LENGTH 8192 + +static const char TIFF_SUFFIX[] = ".tif"; + +static char fname[PATH_LENGTH]; static int tiffcp(TIFF*, TIFF*); static void newfilename(void); @@ -60,16 +64,26 @@ main(int argc, char* argv[]) fprintf(stderr, "usage: tiffsplit input.tif [prefix]\n"); return (-3); } - if (argc > 2) - strcpy(fname, argv[2]); + if (argc > 2) { + strncpy(fname, argv[2], sizeof(fname)); + fname[sizeof(fname) - 1] = '\0'; + } in = TIFFOpen(argv[1], "r"); if (in != NULL) { do { - char path[1024+1]; + size_t path_len; + char *path; + newfilename(); - strcpy(path, fname); - strcat(path, ".tif"); + + path_len = strlen(fname) + sizeof(TIFF_SUFFIX); + path = (char *) _TIFFmalloc(path_len); + strncpy(path, fname, path_len); + path[path_len - 1] = '\0'; + strncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1); out = TIFFOpen(path, TIFFIsBigEndian(in)?"wb":"wl"); + _TIFFfree(path); + if (out == NULL) return (-2); if (!tiffcp(in, out)) @@ -158,7 +172,7 @@ tiffcp(TIFF* in, TIFF* out) CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel); CopyField(TIFFTAG_COMPRESSION, compression); if (compression == COMPRESSION_JPEG) { - uint16 count = 0; + uint32 count = 0; void *table = NULL; if (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table) && count > 0 && table) { @@ -183,7 +197,7 @@ tiffcp(TIFF* in, TIFF* out) CopyField(TIFFTAG_YPOSITION, floatv); CopyField(TIFFTAG_IMAGEDEPTH, longv); CopyField(TIFFTAG_TILEDEPTH, longv); - CopyField(TIFFTAG_SAMPLEFORMAT, longv); + CopyField(TIFFTAG_SAMPLEFORMAT, shortv); CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav); { uint16 *red, *green, *blue; CopyField3(TIFFTAG_COLORMAP, red, green, blue); @@ -216,23 +230,26 @@ tiffcp(TIFF* in, TIFF* out) static int cpStrips(TIFF* in, TIFF* out) { - tsize_t bufsize = TIFFStripSize(in); + tmsize_t bufsize = TIFFStripSize(in); unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); if (buf) { tstrip_t s, ns = TIFFNumberOfStrips(in); - uint32 *bytecounts; + uint64 *bytecounts; - TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &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 (bytecounts[s] > (uint64)bufsize) { + buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]); if (!buf) return (0); - bufsize = bytecounts[s]; + bufsize = (tmsize_t)bytecounts[s]; } - if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 || - TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) { + if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 || + TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) { _TIFFfree(buf); return (0); } @@ -246,23 +263,26 @@ cpStrips(TIFF* in, TIFF* out) static int cpTiles(TIFF* in, TIFF* out) { - tsize_t bufsize = TIFFTileSize(in); + tmsize_t bufsize = TIFFTileSize(in); unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); if (buf) { ttile_t t, nt = TIFFNumberOfTiles(in); - uint32 *bytecounts; + uint64 *bytecounts; - TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &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 (bytecounts[t] > (uint64) bufsize) { + buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]); if (!buf) return (0); - bufsize = bytecounts[t]; + bufsize = (tmsize_t)bytecounts[t]; } - if (TIFFReadRawTile(in, t, buf, bytecounts[t]) < 0 || - TIFFWriteRawTile(out, t, buf, bytecounts[t]) < 0) { + if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 || + TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) { _TIFFfree(buf); return (0); } @@ -274,3 +294,10 @@ cpTiles(TIFF* in, TIFF* out) } /* vim: set ts=8 sts=8 sw=8 noet: */ +/* + * Local Variables: + * mode: c + * c-basic-offset: 8 + * fill-column: 78 + * End: + */