| 1 |
/* |
| 2 |
* Copyright (c) 1992-1997 Sam Leffler |
| 3 |
* Copyright (c) 1992-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "libport.h" |
| 26 |
#include "tif_config.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "tiffio.h" |
| 33 |
|
| 34 |
#ifndef EXIT_SUCCESS |
| 35 |
#define EXIT_SUCCESS 0 |
| 36 |
#endif |
| 37 |
#ifndef EXIT_FAILURE |
| 38 |
#define EXIT_FAILURE 1 |
| 39 |
#endif |
| 40 |
|
| 41 |
#define CopyField(tag, v) \ |
| 42 |
if (TIFFGetField(in, tag, &v)) \ |
| 43 |
TIFFSetField(out, tag, v) |
| 44 |
#define CopyField2(tag, v1, v2) \ |
| 45 |
if (TIFFGetField(in, tag, &v1, &v2)) \ |
| 46 |
TIFFSetField(out, tag, v1, v2) |
| 47 |
#define CopyField3(tag, v1, v2, v3) \ |
| 48 |
if (TIFFGetField(in, tag, &v1, &v2, &v3)) \ |
| 49 |
TIFFSetField(out, tag, v1, v2, v3) |
| 50 |
|
| 51 |
#define PATH_LENGTH 8192 |
| 52 |
|
| 53 |
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024) |
| 54 |
|
| 55 |
/* malloc size limit (in bytes) |
| 56 |
* disabled when set to 0 */ |
| 57 |
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC; |
| 58 |
|
| 59 |
static const char TIFF_SUFFIX[] = ".tif"; |
| 60 |
|
| 61 |
static char fname[PATH_LENGTH]; |
| 62 |
|
| 63 |
static int tiffcp(TIFF *, TIFF *); |
| 64 |
static void newfilename(void); |
| 65 |
static int cpStrips(TIFF *, TIFF *); |
| 66 |
static int cpTiles(TIFF *, TIFF *); |
| 67 |
|
| 68 |
static void usage(int); |
| 69 |
|
| 70 |
/** |
| 71 |
* This custom malloc function enforce a maximum allocation size |
| 72 |
*/ |
| 73 |
static void *limitMalloc(tmsize_t s) |
| 74 |
{ |
| 75 |
/* tmsize_t is signed and _TIFFmalloc() converts s to size_t. Therefore |
| 76 |
* check for negative s. */ |
| 77 |
if (maxMalloc && ((s > maxMalloc) || (s < 0))) |
| 78 |
{ |
| 79 |
fprintf(stderr, |
| 80 |
"MemoryLimitError: allocation of %" TIFF_SSIZE_FORMAT |
| 81 |
" bytes is forbidden. Limit is %" TIFF_SSIZE_FORMAT ".\n", |
| 82 |
s, maxMalloc); |
| 83 |
fprintf(stderr, " use -M option to change limit.\n"); |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
return _TIFFmalloc(s); |
| 87 |
} |
| 88 |
|
| 89 |
static void *limitRealloc(void *buf, tmsize_t s) |
| 90 |
{ |
| 91 |
if (maxMalloc && ((s > maxMalloc) || (s < 0))) |
| 92 |
{ |
| 93 |
fprintf(stderr, |
| 94 |
"MemoryLimitError: re-allocation of %" TIFF_SSIZE_FORMAT |
| 95 |
" bytes is forbidden. Limit is %" TIFF_SSIZE_FORMAT ".\n", |
| 96 |
s, maxMalloc); |
| 97 |
fprintf(stderr, " use -M option to change limit.\n"); |
| 98 |
if (buf != NULL) |
| 99 |
_TIFFfree(buf); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
return _TIFFrealloc(buf, s); |
| 103 |
} |
| 104 |
|
| 105 |
int main(int argc, char *argv[]) |
| 106 |
{ |
| 107 |
TIFF *in, *out; |
| 108 |
#if !HAVE_DECL_OPTARG |
| 109 |
extern char *optarg; |
| 110 |
extern int optind; |
| 111 |
#endif |
| 112 |
int c; |
| 113 |
|
| 114 |
while ((c = getopt(argc, argv, "M:")) != -1) |
| 115 |
{ |
| 116 |
switch (c) |
| 117 |
{ |
| 118 |
case 'M': |
| 119 |
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20; |
| 120 |
if ((maxMalloc == 0) && (optarg[0] != '0')) |
| 121 |
{ |
| 122 |
fprintf(stderr, |
| 123 |
"tiffsplit: Error: Option -M was not followed by a " |
| 124 |
"number but <%s>\n", |
| 125 |
optarg); |
| 126 |
usage(EXIT_FAILURE); |
| 127 |
} |
| 128 |
break; |
| 129 |
case '?': |
| 130 |
usage(EXIT_SUCCESS); |
| 131 |
break; |
| 132 |
default: |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
c = argc - optind; |
| 138 |
if (c < 1 || c > 2) |
| 139 |
usage(EXIT_FAILURE); |
| 140 |
if (c > 1) |
| 141 |
{ |
| 142 |
strncpy(fname, argv[optind + 1], sizeof(fname)); |
| 143 |
fname[sizeof(fname) - 1] = '\0'; |
| 144 |
} |
| 145 |
|
| 146 |
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc(); |
| 147 |
if (opts == NULL) |
| 148 |
{ |
| 149 |
return EXIT_FAILURE; |
| 150 |
} |
| 151 |
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc); |
| 152 |
in = TIFFOpenExt(argv[optind], "r", opts); |
| 153 |
if (in == NULL) |
| 154 |
{ |
| 155 |
fprintf(stderr, "tiffsplit: Error: Could not open %s \n", argv[optind]); |
| 156 |
TIFFOpenOptionsFree(opts); |
| 157 |
usage(EXIT_FAILURE); |
| 158 |
} |
| 159 |
|
| 160 |
do |
| 161 |
{ |
| 162 |
size_t path_len; |
| 163 |
char *path = NULL; |
| 164 |
|
| 165 |
newfilename(); |
| 166 |
|
| 167 |
path_len = strlen(fname) + sizeof(TIFF_SUFFIX); |
| 168 |
path = (char *)limitMalloc(path_len); |
| 169 |
if (!path) |
| 170 |
{ |
| 171 |
fprintf(stderr, |
| 172 |
"tiffsplit: Error: Can't allocate %" TIFF_SIZE_FORMAT |
| 173 |
" bytes for path-variable.\n", |
| 174 |
path_len); |
| 175 |
TIFFClose(in); |
| 176 |
TIFFOpenOptionsFree(opts); |
| 177 |
return (EXIT_FAILURE); |
| 178 |
} |
| 179 |
strncpy(path, fname, path_len); |
| 180 |
path[path_len - 1] = '\0'; |
| 181 |
strncat(path, TIFF_SUFFIX, path_len - strlen(path) - 1); |
| 182 |
out = TIFFOpenExt(path, TIFFIsBigEndian(in) ? "wb" : "wl", opts); |
| 183 |
|
| 184 |
if (out == NULL) |
| 185 |
{ |
| 186 |
TIFFClose(in); |
| 187 |
fprintf(stderr, |
| 188 |
"tiffsplit: Error: Could not open output file %s \n", path); |
| 189 |
_TIFFfree(path); |
| 190 |
TIFFOpenOptionsFree(opts); |
| 191 |
return (EXIT_FAILURE); |
| 192 |
} |
| 193 |
_TIFFfree(path); |
| 194 |
if (!tiffcp(in, out)) |
| 195 |
{ |
| 196 |
TIFFClose(in); |
| 197 |
TIFFClose(out); |
| 198 |
TIFFOpenOptionsFree(opts); |
| 199 |
fprintf(stderr, "tiffsplit: Error: Could not copy data from input " |
| 200 |
"to output.\n"); |
| 201 |
return (EXIT_FAILURE); |
| 202 |
} |
| 203 |
TIFFClose(out); |
| 204 |
} while (TIFFReadDirectory(in)); |
| 205 |
|
| 206 |
TIFFOpenOptionsFree(opts); |
| 207 |
(void)TIFFClose(in); |
| 208 |
|
| 209 |
return (EXIT_SUCCESS); |
| 210 |
} |
| 211 |
|
| 212 |
static void newfilename(void) |
| 213 |
{ |
| 214 |
static int first = 1; |
| 215 |
static long lastTurn; |
| 216 |
static long fnum; |
| 217 |
static short defname; |
| 218 |
static char *fpnt; |
| 219 |
|
| 220 |
if (first) |
| 221 |
{ |
| 222 |
if (fname[0]) |
| 223 |
{ |
| 224 |
fpnt = fname + strlen(fname); |
| 225 |
defname = 0; |
| 226 |
} |
| 227 |
else |
| 228 |
{ |
| 229 |
fname[0] = 'x'; |
| 230 |
fpnt = fname + 1; |
| 231 |
defname = 1; |
| 232 |
} |
| 233 |
first = 0; |
| 234 |
} |
| 235 |
#define MAXFILES 17576 |
| 236 |
if (fnum == MAXFILES) |
| 237 |
{ |
| 238 |
if (!defname || fname[0] == 'z') |
| 239 |
{ |
| 240 |
fprintf(stderr, "tiffsplit: too many files.\n"); |
| 241 |
exit(EXIT_FAILURE); |
| 242 |
} |
| 243 |
fname[0]++; |
| 244 |
fnum = 0; |
| 245 |
} |
| 246 |
if (fnum % 676 == 0) |
| 247 |
{ |
| 248 |
if (fnum != 0) |
| 249 |
{ |
| 250 |
/* |
| 251 |
* advance to next letter every 676 pages |
| 252 |
* condition for 'z'++ will be covered above |
| 253 |
*/ |
| 254 |
fpnt[0]++; |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
/* |
| 259 |
* set to 'a' if we are on the very first file |
| 260 |
*/ |
| 261 |
fpnt[0] = 'a'; |
| 262 |
} |
| 263 |
/* |
| 264 |
* set the value of the last turning point |
| 265 |
*/ |
| 266 |
lastTurn = fnum; |
| 267 |
} |
| 268 |
/* |
| 269 |
* start from 0 every 676 times (provided by lastTurn) |
| 270 |
* this keeps us within a-z boundaries |
| 271 |
*/ |
| 272 |
fpnt[1] = (char)((fnum - lastTurn) / 26) + 'a'; |
| 273 |
/* |
| 274 |
* cycle last letter every file, from a-z, then repeat |
| 275 |
*/ |
| 276 |
fpnt[2] = (char)(fnum % 26) + 'a'; |
| 277 |
fnum++; |
| 278 |
} |
| 279 |
|
| 280 |
static int tiffcp(TIFF *in, TIFF *out) |
| 281 |
{ |
| 282 |
uint16_t bitspersample, samplesperpixel, compression, shortv, *shortav; |
| 283 |
uint32_t w, l; |
| 284 |
float floatv; |
| 285 |
char *stringv; |
| 286 |
uint32_t longv; |
| 287 |
|
| 288 |
CopyField(TIFFTAG_SUBFILETYPE, longv); |
| 289 |
CopyField(TIFFTAG_TILEWIDTH, w); |
| 290 |
CopyField(TIFFTAG_TILELENGTH, l); |
| 291 |
CopyField(TIFFTAG_IMAGEWIDTH, w); |
| 292 |
CopyField(TIFFTAG_IMAGELENGTH, l); |
| 293 |
CopyField(TIFFTAG_BITSPERSAMPLE, bitspersample); |
| 294 |
CopyField(TIFFTAG_SAMPLESPERPIXEL, samplesperpixel); |
| 295 |
CopyField(TIFFTAG_COMPRESSION, compression); |
| 296 |
if (compression == COMPRESSION_JPEG) |
| 297 |
{ |
| 298 |
uint32_t count = 0; |
| 299 |
void *table = NULL; |
| 300 |
if (TIFFGetField(in, TIFFTAG_JPEGTABLES, &count, &table) && count > 0 && |
| 301 |
table) |
| 302 |
{ |
| 303 |
TIFFSetField(out, TIFFTAG_JPEGTABLES, count, table); |
| 304 |
} |
| 305 |
} |
| 306 |
CopyField(TIFFTAG_PHOTOMETRIC, shortv); |
| 307 |
CopyField(TIFFTAG_PREDICTOR, shortv); |
| 308 |
CopyField(TIFFTAG_THRESHHOLDING, shortv); |
| 309 |
CopyField(TIFFTAG_FILLORDER, shortv); |
| 310 |
CopyField(TIFFTAG_ORIENTATION, shortv); |
| 311 |
CopyField(TIFFTAG_MINSAMPLEVALUE, shortv); |
| 312 |
CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv); |
| 313 |
CopyField(TIFFTAG_XRESOLUTION, floatv); |
| 314 |
CopyField(TIFFTAG_YRESOLUTION, floatv); |
| 315 |
CopyField(TIFFTAG_GROUP3OPTIONS, longv); |
| 316 |
CopyField(TIFFTAG_GROUP4OPTIONS, longv); |
| 317 |
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv); |
| 318 |
CopyField(TIFFTAG_PLANARCONFIG, shortv); |
| 319 |
CopyField(TIFFTAG_ROWSPERSTRIP, longv); |
| 320 |
CopyField(TIFFTAG_XPOSITION, floatv); |
| 321 |
CopyField(TIFFTAG_YPOSITION, floatv); |
| 322 |
CopyField(TIFFTAG_IMAGEDEPTH, longv); |
| 323 |
CopyField(TIFFTAG_TILEDEPTH, longv); |
| 324 |
CopyField(TIFFTAG_SAMPLEFORMAT, shortv); |
| 325 |
CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav); |
| 326 |
{ |
| 327 |
uint16_t *red, *green, *blue; |
| 328 |
CopyField3(TIFFTAG_COLORMAP, red, green, blue); |
| 329 |
} |
| 330 |
{ |
| 331 |
uint16_t shortv2; |
| 332 |
CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2); |
| 333 |
} |
| 334 |
CopyField(TIFFTAG_ARTIST, stringv); |
| 335 |
CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv); |
| 336 |
CopyField(TIFFTAG_MAKE, stringv); |
| 337 |
CopyField(TIFFTAG_MODEL, stringv); |
| 338 |
CopyField(TIFFTAG_SOFTWARE, stringv); |
| 339 |
CopyField(TIFFTAG_DATETIME, stringv); |
| 340 |
CopyField(TIFFTAG_HOSTCOMPUTER, stringv); |
| 341 |
CopyField(TIFFTAG_PAGENAME, stringv); |
| 342 |
CopyField(TIFFTAG_DOCUMENTNAME, stringv); |
| 343 |
CopyField(TIFFTAG_BADFAXLINES, longv); |
| 344 |
CopyField(TIFFTAG_CLEANFAXDATA, longv); |
| 345 |
CopyField(TIFFTAG_CONSECUTIVEBADFAXLINES, longv); |
| 346 |
CopyField(TIFFTAG_FAXRECVPARAMS, longv); |
| 347 |
CopyField(TIFFTAG_FAXRECVTIME, longv); |
| 348 |
CopyField(TIFFTAG_FAXSUBADDRESS, stringv); |
| 349 |
CopyField(TIFFTAG_FAXDCS, stringv); |
| 350 |
if (TIFFIsTiled(in)) |
| 351 |
return (cpTiles(in, out)); |
| 352 |
else |
| 353 |
return (cpStrips(in, out)); |
| 354 |
} |
| 355 |
|
| 356 |
static int cpStrips(TIFF *in, TIFF *out) |
| 357 |
{ |
| 358 |
tmsize_t bufsize = TIFFStripSize(in); |
| 359 |
unsigned char *buf = (unsigned char *)limitMalloc(bufsize); |
| 360 |
if (buf) |
| 361 |
{ |
| 362 |
tstrip_t s, ns = TIFFNumberOfStrips(in); |
| 363 |
uint64_t *bytecounts; |
| 364 |
|
| 365 |
if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) |
| 366 |
{ |
| 367 |
fprintf(stderr, "tiffsplit: strip byte counts are missing\n"); |
| 368 |
_TIFFfree(buf); |
| 369 |
return (0); |
| 370 |
} |
| 371 |
for (s = 0; s < ns; s++) |
| 372 |
{ |
| 373 |
if (bytecounts[s] > (uint64_t)bufsize) |
| 374 |
{ |
| 375 |
buf = |
| 376 |
(unsigned char *)limitRealloc(buf, (tmsize_t)bytecounts[s]); |
| 377 |
if (!buf) |
| 378 |
{ |
| 379 |
fprintf(stderr, |
| 380 |
"tiffsplit: Error: Can't re-allocate " |
| 381 |
"%" TIFF_SSIZE_FORMAT " bytes for strip-size.\n", |
| 382 |
(tmsize_t)bytecounts[s]); |
| 383 |
return (0); |
| 384 |
} |
| 385 |
bufsize = (tmsize_t)bytecounts[s]; |
| 386 |
} |
| 387 |
if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 || |
| 388 |
TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) |
| 389 |
{ |
| 390 |
_TIFFfree(buf); |
| 391 |
return (0); |
| 392 |
} |
| 393 |
} |
| 394 |
_TIFFfree(buf); |
| 395 |
return (1); |
| 396 |
} |
| 397 |
else |
| 398 |
{ |
| 399 |
fprintf(stderr, |
| 400 |
"tiffsplit: Error: Can't allocate %" TIFF_SSIZE_FORMAT |
| 401 |
" bytes for strip-size.\n", |
| 402 |
bufsize); |
| 403 |
} |
| 404 |
return (0); |
| 405 |
} |
| 406 |
|
| 407 |
static int cpTiles(TIFF *in, TIFF *out) |
| 408 |
{ |
| 409 |
tmsize_t bufsize = TIFFTileSize(in); |
| 410 |
unsigned char *buf = (unsigned char *)limitMalloc(bufsize); |
| 411 |
|
| 412 |
if (buf) |
| 413 |
{ |
| 414 |
ttile_t t, nt = TIFFNumberOfTiles(in); |
| 415 |
uint64_t *bytecounts; |
| 416 |
|
| 417 |
if (!TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts)) |
| 418 |
{ |
| 419 |
fprintf(stderr, "tiffsplit: tile byte counts are missing\n"); |
| 420 |
_TIFFfree(buf); |
| 421 |
return (0); |
| 422 |
} |
| 423 |
for (t = 0; t < nt; t++) |
| 424 |
{ |
| 425 |
if (bytecounts[t] > (uint64_t)bufsize) |
| 426 |
{ |
| 427 |
buf = |
| 428 |
(unsigned char *)limitRealloc(buf, (tmsize_t)bytecounts[t]); |
| 429 |
if (!buf) |
| 430 |
{ |
| 431 |
fprintf(stderr, |
| 432 |
"tiffsplit: Error: Can't re-allocate " |
| 433 |
"%" TIFF_SSIZE_FORMAT " bytes for tile-size.\n", |
| 434 |
(tmsize_t)bytecounts[t]); |
| 435 |
return (0); |
| 436 |
} |
| 437 |
bufsize = (tmsize_t)bytecounts[t]; |
| 438 |
} |
| 439 |
if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 || |
| 440 |
TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) |
| 441 |
{ |
| 442 |
_TIFFfree(buf); |
| 443 |
return (0); |
| 444 |
} |
| 445 |
} |
| 446 |
_TIFFfree(buf); |
| 447 |
return (1); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
fprintf(stderr, |
| 452 |
"tiffsplit: Error: Can't allocate %" TIFF_SSIZE_FORMAT |
| 453 |
" bytes for tile-size.\n", |
| 454 |
bufsize); |
| 455 |
} |
| 456 |
return (0); |
| 457 |
} |
| 458 |
|
| 459 |
static void usage(int code) |
| 460 |
{ |
| 461 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 462 |
|
| 463 |
fprintf(out, "\n\n%s\n\n", TIFFGetVersion()); |
| 464 |
fprintf(out, "Split a multi-image TIFF into single-image TIFF files\n\n"); |
| 465 |
fprintf(out, "usage: tiffsplit [option] input.tif [prefix]\n"); |
| 466 |
fprintf(out, "where option is:\n"); |
| 467 |
fprintf(out, " -M size set the memory allocation limit in MiB. 0 to " |
| 468 |
"disable limit.\n"); |
| 469 |
exit(code); |
| 470 |
} |