| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-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 |
#ifdef HAVE_UNISTD_H |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "tiffiop.h" |
| 37 |
|
| 38 |
#ifdef HAVE_FCNTL_H |
| 39 |
#include <fcntl.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifdef HAVE_SYS_TYPES_H |
| 43 |
#include <sys/types.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifdef HAVE_IO_H |
| 47 |
#include <io.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "tiffio.h" |
| 51 |
|
| 52 |
#ifndef EXIT_SUCCESS |
| 53 |
#define EXIT_SUCCESS 0 |
| 54 |
#endif |
| 55 |
#ifndef EXIT_FAILURE |
| 56 |
#define EXIT_FAILURE 1 |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef O_BINARY |
| 60 |
#define O_BINARY 0 |
| 61 |
#endif |
| 62 |
|
| 63 |
static union |
| 64 |
{ |
| 65 |
TIFFHeaderClassic classic; |
| 66 |
TIFFHeaderBig big; |
| 67 |
TIFFHeaderCommon common; |
| 68 |
} hdr; |
| 69 |
static char *appname; |
| 70 |
static char *curfile; |
| 71 |
static int swabflag; |
| 72 |
static int bigendian; |
| 73 |
static int bigtiff; |
| 74 |
static uint32_t maxitems = 24; /* maximum indirect data items to print */ |
| 75 |
|
| 76 |
static const char bytefmt[] = "%s%#02" PRIx8; /* BYTE */ |
| 77 |
static const char sbytefmt[] = "%s%" PRId8; /* SBYTE */ |
| 78 |
static const char shortfmtd[] = "%s%" PRIu16; /* SHORT */ |
| 79 |
static const char shortfmth[] = "%s%#" PRIx16; |
| 80 |
static const char sshortfmtd[] = "%s%" PRId16; /* SSHORT */ |
| 81 |
static const char sshortfmth[] = "%s%#" PRIx16; |
| 82 |
static const char longfmtd[] = "%s%" PRIu32; /* LONG */ |
| 83 |
static const char longfmth[] = "%s%#" PRIx32; |
| 84 |
static const char slongfmtd[] = "%s%" PRId32; /* SLONG */ |
| 85 |
static const char slongfmth[] = "%s%#" PRIx32; |
| 86 |
static const char ifdfmt[] = "%s%#04" PRIx32; /* IFD offset */ |
| 87 |
static const char long8fmt[] = "%s%" PRIu64; /* LONG8 */ |
| 88 |
static const char slong8fmt[] = "%s%" PRId64; /* SLONG8 */ |
| 89 |
static const char ifd8fmt[] = "%s%#08" PRIx64; /* IFD offset8 */ |
| 90 |
static const char rationalfmt[] = "%s%g"; /* RATIONAL */ |
| 91 |
static const char srationalfmt[] = "%s%g"; /* SRATIONAL */ |
| 92 |
static const char floatfmt[] = "%s%g"; /* FLOAT */ |
| 93 |
static const char doublefmt[] = "%s%g"; /* DOUBLE */ |
| 94 |
|
| 95 |
unsigned int hex_mode; |
| 96 |
|
| 97 |
static void dump(int, uint64_t); |
| 98 |
|
| 99 |
#if !HAVE_DECL_OPTARG |
| 100 |
extern int optind; |
| 101 |
extern char *optarg; |
| 102 |
#endif |
| 103 |
|
| 104 |
void usage() |
| 105 |
{ |
| 106 |
fprintf(stderr, "\nDisplay directory information from TIFF files\n\n"); |
| 107 |
fprintf(stderr, "usage: %s [-h] [-o offset] [-m maxitems] file.tif ...\n", |
| 108 |
appname); |
| 109 |
exit(EXIT_FAILURE); |
| 110 |
} |
| 111 |
|
| 112 |
int main(int argc, char *argv[]) |
| 113 |
{ |
| 114 |
int one = 1, fd; |
| 115 |
int multiplefiles = (argc > 1); |
| 116 |
int c; |
| 117 |
uint64_t diroff = 0; |
| 118 |
hex_mode = 0; |
| 119 |
bigendian = (*(char *)&one == 0); |
| 120 |
|
| 121 |
appname = argv[0]; |
| 122 |
while ((c = getopt(argc, argv, "m:o:h")) != -1) |
| 123 |
{ |
| 124 |
switch (c) |
| 125 |
{ |
| 126 |
case 'h': /* print values in hex */ |
| 127 |
hex_mode = 1; |
| 128 |
break; |
| 129 |
case 'o': |
| 130 |
diroff = (uint64_t)strtoul(optarg, NULL, 0); |
| 131 |
break; |
| 132 |
case 'm': |
| 133 |
maxitems = strtoul(optarg, NULL, 0); |
| 134 |
break; |
| 135 |
default: |
| 136 |
usage(); |
| 137 |
} |
| 138 |
} |
| 139 |
if (optind >= argc) |
| 140 |
usage(); |
| 141 |
for (; optind < argc; optind++) |
| 142 |
{ |
| 143 |
fd = open(argv[optind], O_RDONLY | O_BINARY, 0); |
| 144 |
if (fd < 0) |
| 145 |
{ |
| 146 |
perror(argv[0]); |
| 147 |
return (EXIT_FAILURE); |
| 148 |
} |
| 149 |
if (multiplefiles) |
| 150 |
printf("%s:\n", argv[optind]); |
| 151 |
curfile = argv[optind]; |
| 152 |
swabflag = 0; |
| 153 |
bigtiff = 0; |
| 154 |
dump(fd, diroff); |
| 155 |
close(fd); |
| 156 |
} |
| 157 |
return (EXIT_SUCCESS); |
| 158 |
} |
| 159 |
|
| 160 |
#define ord(e) ((int)e) |
| 161 |
|
| 162 |
static uint64_t ReadDirectory(int, unsigned, uint64_t); |
| 163 |
static void ReadError(char *); |
| 164 |
static void Error(const char *, ...); |
| 165 |
static void Fatal(const char *, ...); |
| 166 |
|
| 167 |
static void dump(int fd, uint64_t diroff) |
| 168 |
{ |
| 169 |
unsigned i, j; |
| 170 |
uint64_t *visited_diroff = NULL; |
| 171 |
unsigned int count_visited_dir = 0; |
| 172 |
|
| 173 |
_TIFF_lseek_f(fd, (_TIFF_off_t)0, 0); |
| 174 |
if (read(fd, (char *)&hdr, sizeof(TIFFHeaderCommon)) != |
| 175 |
sizeof(TIFFHeaderCommon)) |
| 176 |
ReadError("TIFF header"); |
| 177 |
if (hdr.common.tiff_magic != TIFF_BIGENDIAN && |
| 178 |
hdr.common.tiff_magic != TIFF_LITTLEENDIAN && |
| 179 |
#if HOST_BIGENDIAN |
| 180 |
/* MDI is sensitive to the host byte order, unlike TIFF */ |
| 181 |
MDI_BIGENDIAN != hdr.common.tiff_magic |
| 182 |
#else |
| 183 |
MDI_LITTLEENDIAN != hdr.common.tiff_magic |
| 184 |
#endif |
| 185 |
) |
| 186 |
{ |
| 187 |
Fatal("Not a TIFF or MDI file, bad magic number %u (%#x)", |
| 188 |
hdr.common.tiff_magic, hdr.common.tiff_magic); |
| 189 |
} |
| 190 |
if (hdr.common.tiff_magic == TIFF_BIGENDIAN || |
| 191 |
hdr.common.tiff_magic == MDI_BIGENDIAN) |
| 192 |
swabflag = !bigendian; |
| 193 |
else |
| 194 |
swabflag = bigendian; |
| 195 |
if (swabflag) |
| 196 |
TIFFSwabShort(&hdr.common.tiff_version); |
| 197 |
if (hdr.common.tiff_version == 42) |
| 198 |
{ |
| 199 |
if (read(fd, |
| 200 |
((char *)&hdr.classic) + |
| 201 |
offsetof(TIFFHeaderClassic, tiff_diroff), |
| 202 |
4) != 4) |
| 203 |
ReadError("TIFF header"); |
| 204 |
if (swabflag) |
| 205 |
TIFFSwabLong(&hdr.classic.tiff_diroff); |
| 206 |
printf("Magic: %#x <%s-endian> Version: %#x <%s>\n", |
| 207 |
hdr.classic.tiff_magic, |
| 208 |
hdr.classic.tiff_magic == TIFF_BIGENDIAN ? "big" : "little", 42, |
| 209 |
"ClassicTIFF"); |
| 210 |
if (diroff == 0) |
| 211 |
diroff = hdr.classic.tiff_diroff; |
| 212 |
} |
| 213 |
else if (hdr.common.tiff_version == 43) |
| 214 |
{ |
| 215 |
if (read(fd, |
| 216 |
((char *)&hdr.big) + offsetof(TIFFHeaderBig, tiff_offsetsize), |
| 217 |
12) != 12) |
| 218 |
ReadError("TIFF header"); |
| 219 |
if (swabflag) |
| 220 |
{ |
| 221 |
TIFFSwabShort(&hdr.big.tiff_offsetsize); |
| 222 |
TIFFSwabShort(&hdr.big.tiff_unused); |
| 223 |
TIFFSwabLong8(&hdr.big.tiff_diroff); |
| 224 |
} |
| 225 |
printf("Magic: %#x <%s-endian> Version: %#x <%s>\n", hdr.big.tiff_magic, |
| 226 |
hdr.big.tiff_magic == TIFF_BIGENDIAN ? "big" : "little", 43, |
| 227 |
"BigTIFF"); |
| 228 |
printf("OffsetSize: %#x Unused: %#x\n", hdr.big.tiff_offsetsize, |
| 229 |
hdr.big.tiff_unused); |
| 230 |
if (diroff == 0) |
| 231 |
diroff = hdr.big.tiff_diroff; |
| 232 |
bigtiff = 1; |
| 233 |
} |
| 234 |
else |
| 235 |
Fatal("Not a TIFF file, bad version number %u (%#x)", |
| 236 |
hdr.common.tiff_version, hdr.common.tiff_version); |
| 237 |
for (i = 0; diroff != 0; i++) |
| 238 |
{ |
| 239 |
for (j = 0; j < count_visited_dir; j++) |
| 240 |
{ |
| 241 |
if (visited_diroff[j] == diroff) |
| 242 |
{ |
| 243 |
free(visited_diroff); |
| 244 |
Fatal("Cycle detected in chaining of TIFF directories!"); |
| 245 |
} |
| 246 |
} |
| 247 |
{ |
| 248 |
size_t alloc_size; |
| 249 |
alloc_size = TIFFSafeMultiply(tmsize_t, (count_visited_dir + 1), |
| 250 |
sizeof(uint64_t)); |
| 251 |
if (alloc_size == 0) |
| 252 |
{ |
| 253 |
if (visited_diroff) |
| 254 |
free(visited_diroff); |
| 255 |
visited_diroff = 0; |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
visited_diroff = |
| 260 |
(uint64_t *)realloc(visited_diroff, alloc_size); |
| 261 |
} |
| 262 |
} |
| 263 |
if (!visited_diroff) |
| 264 |
Fatal("Out of memory"); |
| 265 |
visited_diroff[count_visited_dir] = diroff; |
| 266 |
count_visited_dir++; |
| 267 |
|
| 268 |
if (i > 0) |
| 269 |
putchar('\n'); |
| 270 |
diroff = ReadDirectory(fd, i, diroff); |
| 271 |
} |
| 272 |
if (visited_diroff) |
| 273 |
free(visited_diroff); |
| 274 |
} |
| 275 |
|
| 276 |
static const int datawidth[] = { |
| 277 |
0, /* 00 = undefined */ |
| 278 |
1, /* 01 = TIFF_BYTE */ |
| 279 |
1, /* 02 = TIFF_ASCII */ |
| 280 |
2, /* 03 = TIFF_SHORT */ |
| 281 |
4, /* 04 = TIFF_LONG */ |
| 282 |
8, /* 05 = TIFF_RATIONAL */ |
| 283 |
1, /* 06 = TIFF_SBYTE */ |
| 284 |
1, /* 07 = TIFF_UNDEFINED */ |
| 285 |
2, /* 08 = TIFF_SSHORT */ |
| 286 |
4, /* 09 = TIFF_SLONG */ |
| 287 |
8, /* 10 = TIFF_SRATIONAL */ |
| 288 |
4, /* 11 = TIFF_FLOAT */ |
| 289 |
8, /* 12 = TIFF_DOUBLE */ |
| 290 |
4, /* 13 = TIFF_IFD */ |
| 291 |
0, /* 14 = undefined */ |
| 292 |
0, /* 15 = undefined */ |
| 293 |
8, /* 16 = TIFF_LONG8 */ |
| 294 |
8, /* 17 = TIFF_SLONG8 */ |
| 295 |
8, /* 18 = TIFF_IFD8 */ |
| 296 |
}; |
| 297 |
#define NWIDTHS (sizeof(datawidth) / sizeof(datawidth[0])) |
| 298 |
static void PrintTag(FILE *, uint16_t); |
| 299 |
static void PrintType(FILE *, uint16_t); |
| 300 |
static void PrintData(FILE *, uint16_t, uint32_t, unsigned char *); |
| 301 |
|
| 302 |
/* |
| 303 |
* Read the next TIFF directory from a file |
| 304 |
* and convert it to the internal format. |
| 305 |
* We read directories sequentially. |
| 306 |
*/ |
| 307 |
static uint64_t ReadDirectory(int fd, unsigned int ix, uint64_t off) |
| 308 |
{ |
| 309 |
uint16_t dircount; |
| 310 |
uint32_t direntrysize; |
| 311 |
void *dirmem = NULL; |
| 312 |
uint64_t nextdiroff = 0; |
| 313 |
uint32_t n; |
| 314 |
uint8_t *dp; |
| 315 |
|
| 316 |
if (off == 0) /* no more directories */ |
| 317 |
goto done; |
| 318 |
if (_TIFF_lseek_f(fd, (_TIFF_off_t)off, SEEK_SET) != (_TIFF_off_t)off) |
| 319 |
{ |
| 320 |
Fatal("Seek error accessing TIFF directory"); |
| 321 |
goto done; |
| 322 |
} |
| 323 |
if (!bigtiff) |
| 324 |
{ |
| 325 |
if (read(fd, (char *)&dircount, sizeof(uint16_t)) != sizeof(uint16_t)) |
| 326 |
{ |
| 327 |
ReadError("directory count"); |
| 328 |
goto done; |
| 329 |
} |
| 330 |
if (swabflag) |
| 331 |
TIFFSwabShort(&dircount); |
| 332 |
direntrysize = 12; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
uint64_t dircount64 = 0; |
| 337 |
if (read(fd, (char *)&dircount64, sizeof(uint64_t)) != sizeof(uint64_t)) |
| 338 |
{ |
| 339 |
ReadError("directory count"); |
| 340 |
goto done; |
| 341 |
} |
| 342 |
if (swabflag) |
| 343 |
TIFFSwabLong8(&dircount64); |
| 344 |
if (dircount64 > 0xFFFF) |
| 345 |
{ |
| 346 |
Error("Sanity check on directory count failed"); |
| 347 |
goto done; |
| 348 |
} |
| 349 |
dircount = (uint16_t)dircount64; |
| 350 |
direntrysize = 20; |
| 351 |
} |
| 352 |
dirmem = _TIFFmalloc(TIFFSafeMultiply(tmsize_t, dircount, direntrysize)); |
| 353 |
if (dirmem == NULL) |
| 354 |
{ |
| 355 |
Fatal("No space for TIFF directory"); |
| 356 |
goto done; |
| 357 |
} |
| 358 |
n = read(fd, (char *)dirmem, dircount * direntrysize); |
| 359 |
if (n != dircount * direntrysize) |
| 360 |
{ |
| 361 |
n /= direntrysize; |
| 362 |
Error("Could only read %" PRIu32 " of %" PRIu16 |
| 363 |
" entries in directory at offset %" PRIu64, |
| 364 |
n, dircount, off); |
| 365 |
dircount = n; |
| 366 |
nextdiroff = 0; |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
if (!bigtiff) |
| 371 |
{ |
| 372 |
uint32_t nextdiroff32; |
| 373 |
if (read(fd, (char *)&nextdiroff32, sizeof(uint32_t)) != |
| 374 |
sizeof(uint32_t)) |
| 375 |
nextdiroff32 = 0; |
| 376 |
if (swabflag) |
| 377 |
TIFFSwabLong(&nextdiroff32); |
| 378 |
nextdiroff = nextdiroff32; |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
if (read(fd, (char *)&nextdiroff, sizeof(uint64_t)) != |
| 383 |
sizeof(uint64_t)) |
| 384 |
nextdiroff = 0; |
| 385 |
if (swabflag) |
| 386 |
TIFFSwabLong8(&nextdiroff); |
| 387 |
} |
| 388 |
} |
| 389 |
printf("Directory %u: offset %" PRIu64 " (%#" PRIx64 ") next %" PRIu64 |
| 390 |
" (%#" PRIx64 ")\n", |
| 391 |
ix, off, off, nextdiroff, nextdiroff); |
| 392 |
for (dp = (uint8_t *)dirmem, n = dircount; n > 0; n--) |
| 393 |
{ |
| 394 |
uint16_t tag; |
| 395 |
uint16_t type; |
| 396 |
uint16_t typewidth; |
| 397 |
uint64_t count; |
| 398 |
uint64_t datasize; |
| 399 |
int datafits; |
| 400 |
void *datamem; |
| 401 |
uint64_t dataoffset; |
| 402 |
int datatruncated; |
| 403 |
int datasizeoverflow; |
| 404 |
|
| 405 |
tag = *(uint16_t *)dp; |
| 406 |
if (swabflag) |
| 407 |
TIFFSwabShort(&tag); |
| 408 |
dp += sizeof(uint16_t); |
| 409 |
type = *(uint16_t *)dp; |
| 410 |
dp += sizeof(uint16_t); |
| 411 |
if (swabflag) |
| 412 |
TIFFSwabShort(&type); |
| 413 |
PrintTag(stdout, tag); |
| 414 |
putchar(' '); |
| 415 |
PrintType(stdout, type); |
| 416 |
putchar(' '); |
| 417 |
if (!bigtiff) |
| 418 |
{ |
| 419 |
uint32_t count32; |
| 420 |
count32 = *(uint32_t *)dp; |
| 421 |
if (swabflag) |
| 422 |
TIFFSwabLong(&count32); |
| 423 |
dp += sizeof(uint32_t); |
| 424 |
count = count32; |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
memcpy(&count, dp, sizeof(uint64_t)); |
| 429 |
if (swabflag) |
| 430 |
TIFFSwabLong8(&count); |
| 431 |
dp += sizeof(uint64_t); |
| 432 |
} |
| 433 |
printf("%" PRIu64 "<", count); |
| 434 |
if (type >= NWIDTHS) |
| 435 |
typewidth = 0; |
| 436 |
else |
| 437 |
typewidth = datawidth[type]; |
| 438 |
datasize = TIFFSafeMultiply(tmsize_t, count, typewidth); |
| 439 |
datasizeoverflow = (typewidth > 0 && datasize / typewidth != count); |
| 440 |
datafits = 1; |
| 441 |
datamem = dp; |
| 442 |
dataoffset = 0; |
| 443 |
datatruncated = 0; |
| 444 |
if (!bigtiff) |
| 445 |
{ |
| 446 |
if (datasizeoverflow || datasize > 4) |
| 447 |
{ |
| 448 |
uint32_t dataoffset32; |
| 449 |
datafits = 0; |
| 450 |
datamem = NULL; |
| 451 |
dataoffset32 = *(uint32_t *)dp; |
| 452 |
if (swabflag) |
| 453 |
TIFFSwabLong(&dataoffset32); |
| 454 |
dataoffset = dataoffset32; |
| 455 |
} |
| 456 |
dp += sizeof(uint32_t); |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
if (datasizeoverflow || datasize > 8) |
| 461 |
{ |
| 462 |
datafits = 0; |
| 463 |
datamem = NULL; |
| 464 |
memcpy(&dataoffset, dp, sizeof(uint64_t)); |
| 465 |
if (swabflag) |
| 466 |
TIFFSwabLong8(&dataoffset); |
| 467 |
} |
| 468 |
dp += sizeof(uint64_t); |
| 469 |
} |
| 470 |
if (datasizeoverflow || datasize > 0x10000) |
| 471 |
{ |
| 472 |
datatruncated = 1; |
| 473 |
count = 0x10000 / typewidth; |
| 474 |
datasize = TIFFSafeMultiply(tmsize_t, count, typewidth); |
| 475 |
} |
| 476 |
if (count > maxitems) |
| 477 |
{ |
| 478 |
datatruncated = 1; |
| 479 |
count = maxitems; |
| 480 |
datasize = TIFFSafeMultiply(tmsize_t, count, typewidth); |
| 481 |
} |
| 482 |
if (!datafits) |
| 483 |
{ |
| 484 |
datamem = _TIFFmalloc(datasize); |
| 485 |
if (datamem) |
| 486 |
{ |
| 487 |
if (_TIFF_lseek_f(fd, (_TIFF_off_t)dataoffset, 0) != |
| 488 |
(_TIFF_off_t)dataoffset) |
| 489 |
{ |
| 490 |
Error("Seek error accessing tag %u value", tag); |
| 491 |
_TIFFfree(datamem); |
| 492 |
datamem = NULL; |
| 493 |
} |
| 494 |
else if (read(fd, datamem, (size_t)datasize) != |
| 495 |
(tmsize_t)datasize) |
| 496 |
{ |
| 497 |
Error("Read error accessing tag %u value", tag); |
| 498 |
_TIFFfree(datamem); |
| 499 |
datamem = NULL; |
| 500 |
} |
| 501 |
} |
| 502 |
else |
| 503 |
Error("No space for data for tag %u", tag); |
| 504 |
} |
| 505 |
if (datamem) |
| 506 |
{ |
| 507 |
if (swabflag) |
| 508 |
{ |
| 509 |
switch (type) |
| 510 |
{ |
| 511 |
case TIFF_BYTE: |
| 512 |
case TIFF_ASCII: |
| 513 |
case TIFF_SBYTE: |
| 514 |
case TIFF_UNDEFINED: |
| 515 |
break; |
| 516 |
case TIFF_SHORT: |
| 517 |
case TIFF_SSHORT: |
| 518 |
TIFFSwabArrayOfShort((uint16_t *)datamem, |
| 519 |
(tmsize_t)count); |
| 520 |
break; |
| 521 |
case TIFF_LONG: |
| 522 |
case TIFF_SLONG: |
| 523 |
case TIFF_FLOAT: |
| 524 |
case TIFF_IFD: |
| 525 |
TIFFSwabArrayOfLong((uint32_t *)datamem, |
| 526 |
(tmsize_t)count); |
| 527 |
break; |
| 528 |
case TIFF_RATIONAL: |
| 529 |
case TIFF_SRATIONAL: |
| 530 |
TIFFSwabArrayOfLong((uint32_t *)datamem, |
| 531 |
(tmsize_t)count * 2); |
| 532 |
break; |
| 533 |
case TIFF_DOUBLE: |
| 534 |
case TIFF_LONG8: |
| 535 |
case TIFF_SLONG8: |
| 536 |
case TIFF_IFD8: |
| 537 |
TIFFSwabArrayOfLong8((uint64_t *)datamem, |
| 538 |
(tmsize_t)count); |
| 539 |
break; |
| 540 |
} |
| 541 |
} |
| 542 |
PrintData(stdout, type, (uint32_t)count, datamem); |
| 543 |
if (datatruncated) |
| 544 |
printf(" ..."); |
| 545 |
if (!datafits) |
| 546 |
{ |
| 547 |
_TIFFfree(datamem); |
| 548 |
datamem = NULL; |
| 549 |
} |
| 550 |
} |
| 551 |
printf(">\n"); |
| 552 |
} |
| 553 |
done: |
| 554 |
if (dirmem) |
| 555 |
_TIFFfree((char *)dirmem); |
| 556 |
return (nextdiroff); |
| 557 |
} |
| 558 |
|
| 559 |
static const struct tagname |
| 560 |
{ |
| 561 |
uint16_t tag; |
| 562 |
const char *name; |
| 563 |
} tagnames[] = { |
| 564 |
{TIFFTAG_SUBFILETYPE, "SubFileType"}, |
| 565 |
{TIFFTAG_OSUBFILETYPE, "OldSubFileType"}, |
| 566 |
{TIFFTAG_IMAGEWIDTH, "ImageWidth"}, |
| 567 |
{TIFFTAG_IMAGELENGTH, "ImageLength"}, |
| 568 |
{TIFFTAG_BITSPERSAMPLE, "BitsPerSample"}, |
| 569 |
{TIFFTAG_COMPRESSION, "Compression"}, |
| 570 |
{TIFFTAG_PHOTOMETRIC, "Photometric"}, |
| 571 |
{TIFFTAG_THRESHHOLDING, "Threshholding"}, |
| 572 |
{TIFFTAG_CELLWIDTH, "CellWidth"}, |
| 573 |
{TIFFTAG_CELLLENGTH, "CellLength"}, |
| 574 |
{TIFFTAG_FILLORDER, "FillOrder"}, |
| 575 |
{TIFFTAG_DOCUMENTNAME, "DocumentName"}, |
| 576 |
{TIFFTAG_IMAGEDESCRIPTION, "ImageDescription"}, |
| 577 |
{TIFFTAG_MAKE, "Make"}, |
| 578 |
{TIFFTAG_MODEL, "Model"}, |
| 579 |
{TIFFTAG_STRIPOFFSETS, "StripOffsets"}, |
| 580 |
{TIFFTAG_ORIENTATION, "Orientation"}, |
| 581 |
{TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel"}, |
| 582 |
{TIFFTAG_ROWSPERSTRIP, "RowsPerStrip"}, |
| 583 |
{TIFFTAG_STRIPBYTECOUNTS, "StripByteCounts"}, |
| 584 |
{TIFFTAG_MINSAMPLEVALUE, "MinSampleValue"}, |
| 585 |
{TIFFTAG_MAXSAMPLEVALUE, "MaxSampleValue"}, |
| 586 |
{TIFFTAG_XRESOLUTION, "XResolution"}, |
| 587 |
{TIFFTAG_YRESOLUTION, "YResolution"}, |
| 588 |
{TIFFTAG_PLANARCONFIG, "PlanarConfig"}, |
| 589 |
{TIFFTAG_PAGENAME, "PageName"}, |
| 590 |
{TIFFTAG_XPOSITION, "XPosition"}, |
| 591 |
{TIFFTAG_YPOSITION, "YPosition"}, |
| 592 |
{TIFFTAG_FREEOFFSETS, "FreeOffsets"}, |
| 593 |
{TIFFTAG_FREEBYTECOUNTS, "FreeByteCounts"}, |
| 594 |
{TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit"}, |
| 595 |
{TIFFTAG_GRAYRESPONSECURVE, "GrayResponseCurve"}, |
| 596 |
{TIFFTAG_GROUP3OPTIONS, "Group3Options"}, |
| 597 |
{TIFFTAG_GROUP4OPTIONS, "Group4Options"}, |
| 598 |
{TIFFTAG_RESOLUTIONUNIT, "ResolutionUnit"}, |
| 599 |
{TIFFTAG_PAGENUMBER, "PageNumber"}, |
| 600 |
{TIFFTAG_COLORRESPONSEUNIT, "ColorResponseUnit"}, |
| 601 |
{TIFFTAG_TRANSFERFUNCTION, "TransferFunction"}, |
| 602 |
{TIFFTAG_SOFTWARE, "Software"}, |
| 603 |
{TIFFTAG_DATETIME, "DateTime"}, |
| 604 |
{TIFFTAG_ARTIST, "Artist"}, |
| 605 |
{TIFFTAG_HOSTCOMPUTER, "HostComputer"}, |
| 606 |
{TIFFTAG_PREDICTOR, "Predictor"}, |
| 607 |
{TIFFTAG_WHITEPOINT, "Whitepoint"}, |
| 608 |
{TIFFTAG_PRIMARYCHROMATICITIES, "PrimaryChromaticities"}, |
| 609 |
{TIFFTAG_COLORMAP, "Colormap"}, |
| 610 |
{TIFFTAG_HALFTONEHINTS, "HalftoneHints"}, |
| 611 |
{TIFFTAG_TILEWIDTH, "TileWidth"}, |
| 612 |
{TIFFTAG_TILELENGTH, "TileLength"}, |
| 613 |
{TIFFTAG_TILEOFFSETS, "TileOffsets"}, |
| 614 |
{TIFFTAG_TILEBYTECOUNTS, "TileByteCounts"}, |
| 615 |
{TIFFTAG_BADFAXLINES, "BadFaxLines"}, |
| 616 |
{TIFFTAG_CLEANFAXDATA, "CleanFaxData"}, |
| 617 |
{TIFFTAG_CONSECUTIVEBADFAXLINES, "ConsecutiveBadFaxLines"}, |
| 618 |
{TIFFTAG_SUBIFD, "SubIFD"}, |
| 619 |
{TIFFTAG_INKSET, "InkSet"}, |
| 620 |
{TIFFTAG_INKNAMES, "InkNames"}, |
| 621 |
{TIFFTAG_NUMBEROFINKS, "NumberOfInks"}, |
| 622 |
{TIFFTAG_DOTRANGE, "DotRange"}, |
| 623 |
{TIFFTAG_TARGETPRINTER, "TargetPrinter"}, |
| 624 |
{TIFFTAG_EXTRASAMPLES, "ExtraSamples"}, |
| 625 |
{TIFFTAG_SAMPLEFORMAT, "SampleFormat"}, |
| 626 |
{TIFFTAG_SMINSAMPLEVALUE, "SMinSampleValue"}, |
| 627 |
{TIFFTAG_SMAXSAMPLEVALUE, "SMaxSampleValue"}, |
| 628 |
{TIFFTAG_JPEGPROC, "JPEGProcessingMode"}, |
| 629 |
{TIFFTAG_JPEGIFOFFSET, "JPEGInterchangeFormat"}, |
| 630 |
{TIFFTAG_JPEGIFBYTECOUNT, "JPEGInterchangeFormatLength"}, |
| 631 |
{TIFFTAG_JPEGRESTARTINTERVAL, "JPEGRestartInterval"}, |
| 632 |
{TIFFTAG_JPEGLOSSLESSPREDICTORS, "JPEGLosslessPredictors"}, |
| 633 |
{TIFFTAG_JPEGPOINTTRANSFORM, "JPEGPointTransform"}, |
| 634 |
{TIFFTAG_JPEGTABLES, "JPEGTables"}, |
| 635 |
{TIFFTAG_JPEGQTABLES, "JPEGQTables"}, |
| 636 |
{TIFFTAG_JPEGDCTABLES, "JPEGDCTables"}, |
| 637 |
{TIFFTAG_JPEGACTABLES, "JPEGACTables"}, |
| 638 |
{TIFFTAG_YCBCRCOEFFICIENTS, "YCbCrCoefficients"}, |
| 639 |
{TIFFTAG_YCBCRSUBSAMPLING, "YCbCrSubsampling"}, |
| 640 |
{TIFFTAG_YCBCRPOSITIONING, "YCbCrPositioning"}, |
| 641 |
{TIFFTAG_REFERENCEBLACKWHITE, "ReferenceBlackWhite"}, |
| 642 |
{TIFFTAG_REFPTS, "IgReferencePoints (Island Graphics)"}, |
| 643 |
{TIFFTAG_REGIONTACKPOINT, "IgRegionTackPoint (Island Graphics)"}, |
| 644 |
{TIFFTAG_REGIONWARPCORNERS, "IgRegionWarpCorners (Island Graphics)"}, |
| 645 |
{TIFFTAG_REGIONAFFINE, "IgRegionAffine (Island Graphics)"}, |
| 646 |
{TIFFTAG_MATTEING, "OBSOLETE Matteing (Silicon Graphics)"}, |
| 647 |
{TIFFTAG_DATATYPE, "OBSOLETE DataType (Silicon Graphics)"}, |
| 648 |
{TIFFTAG_IMAGEDEPTH, "ImageDepth (Silicon Graphics)"}, |
| 649 |
{TIFFTAG_TILEDEPTH, "TileDepth (Silicon Graphics)"}, |
| 650 |
{32768, "OLD BOGUS Matteing tag"}, |
| 651 |
{TIFFTAG_COPYRIGHT, "Copyright"}, |
| 652 |
{TIFFTAG_ICCPROFILE, "ICC Profile"}, |
| 653 |
{TIFFTAG_JBIGOPTIONS, "JBIG Options"}, |
| 654 |
{TIFFTAG_STONITS, "StoNits"}, |
| 655 |
{TIFFTAG_GDAL_METADATA, "GDALMetadata"}, |
| 656 |
{TIFFTAG_GDAL_NODATA, "GDALNoDataValue"}, |
| 657 |
}; |
| 658 |
#define NTAGS (sizeof(tagnames) / sizeof(tagnames[0])) |
| 659 |
|
| 660 |
static void PrintTag(FILE *fd, uint16_t tag) |
| 661 |
{ |
| 662 |
const struct tagname *tp; |
| 663 |
|
| 664 |
for (tp = tagnames; tp < &tagnames[NTAGS]; tp++) |
| 665 |
if (tp->tag == tag) |
| 666 |
{ |
| 667 |
fprintf(fd, "%s (%u)", tp->name, tag); |
| 668 |
return; |
| 669 |
} |
| 670 |
fprintf(fd, "%u (%#x)", tag, tag); |
| 671 |
} |
| 672 |
|
| 673 |
static void PrintType(FILE *fd, uint16_t type) |
| 674 |
{ |
| 675 |
static const char *typenames[] = { |
| 676 |
"0", "BYTE", "ASCII", "SHORT", "LONG", |
| 677 |
"RATIONAL", "SBYTE", "UNDEFINED", "SSHORT", "SLONG", |
| 678 |
"SRATIONAL", "FLOAT", "DOUBLE", "IFD", "14", |
| 679 |
"15", "LONG8", "SLONG8", "IFD8"}; |
| 680 |
#define NTYPES (sizeof(typenames) / sizeof(typenames[0])) |
| 681 |
|
| 682 |
if (type < NTYPES) |
| 683 |
fprintf(fd, "%s (%u)", typenames[type], type); |
| 684 |
else |
| 685 |
fprintf(fd, "%u (%#x)", type, type); |
| 686 |
} |
| 687 |
#undef NTYPES |
| 688 |
|
| 689 |
#include <ctype.h> |
| 690 |
|
| 691 |
static void PrintASCII(FILE *fd, uint32_t cc, const unsigned char *cp) |
| 692 |
{ |
| 693 |
for (; cc > 0; cc--, cp++) |
| 694 |
{ |
| 695 |
const char *tp; |
| 696 |
|
| 697 |
if (isprint(*cp)) |
| 698 |
{ |
| 699 |
fputc(*cp, fd); |
| 700 |
continue; |
| 701 |
} |
| 702 |
for (tp = "\tt\bb\rr\nn\vv"; *tp; tp++) |
| 703 |
if (*tp++ == *cp) |
| 704 |
break; |
| 705 |
if (*tp) |
| 706 |
fprintf(fd, "\\%c", *tp); |
| 707 |
else if (*cp) |
| 708 |
fprintf(fd, "\\%03o", *cp); |
| 709 |
else |
| 710 |
fprintf(fd, "\\0"); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
static void PrintData(FILE *fd, uint16_t type, uint32_t count, |
| 715 |
unsigned char *data) |
| 716 |
{ |
| 717 |
char *sep = ""; |
| 718 |
|
| 719 |
switch (type) |
| 720 |
{ |
| 721 |
case TIFF_BYTE: |
| 722 |
while (count-- > 0) |
| 723 |
fprintf(fd, bytefmt, sep, *data++), sep = " "; |
| 724 |
break; |
| 725 |
case TIFF_SBYTE: |
| 726 |
while (count-- > 0) |
| 727 |
fprintf(fd, sbytefmt, sep, *(char *)data++), sep = " "; |
| 728 |
break; |
| 729 |
case TIFF_UNDEFINED: |
| 730 |
while (count-- > 0) |
| 731 |
fprintf(fd, bytefmt, sep, *data++), sep = " "; |
| 732 |
break; |
| 733 |
case TIFF_ASCII: |
| 734 |
PrintASCII(fd, count, data); |
| 735 |
break; |
| 736 |
case TIFF_SHORT: |
| 737 |
{ |
| 738 |
uint16_t *wp = (uint16_t *)data; |
| 739 |
while (count-- > 0) |
| 740 |
fprintf(fd, hex_mode ? shortfmth : shortfmtd, sep, *wp++), |
| 741 |
sep = " "; |
| 742 |
break; |
| 743 |
} |
| 744 |
case TIFF_SSHORT: |
| 745 |
{ |
| 746 |
int16_t *wp = (int16_t *)data; |
| 747 |
while (count-- > 0) |
| 748 |
fprintf(fd, hex_mode ? sshortfmth : sshortfmtd, sep, *wp++), |
| 749 |
sep = " "; |
| 750 |
break; |
| 751 |
} |
| 752 |
case TIFF_LONG: |
| 753 |
{ |
| 754 |
uint32_t *lp = (uint32_t *)data; |
| 755 |
while (count-- > 0) |
| 756 |
{ |
| 757 |
fprintf(fd, hex_mode ? longfmth : longfmtd, sep, *lp++); |
| 758 |
sep = " "; |
| 759 |
} |
| 760 |
break; |
| 761 |
} |
| 762 |
case TIFF_SLONG: |
| 763 |
{ |
| 764 |
int32_t *lp = (int32_t *)data; |
| 765 |
while (count-- > 0) |
| 766 |
fprintf(fd, hex_mode ? slongfmth : slongfmtd, sep, *lp++), |
| 767 |
sep = " "; |
| 768 |
break; |
| 769 |
} |
| 770 |
case TIFF_LONG8: |
| 771 |
{ |
| 772 |
uint64_t *llp = (uint64_t *)data; |
| 773 |
while (count-- > 0) |
| 774 |
{ |
| 775 |
uint64_t val; |
| 776 |
memcpy(&val, llp, sizeof(uint64_t)); |
| 777 |
llp++; |
| 778 |
fprintf(fd, long8fmt, sep, val); |
| 779 |
sep = " "; |
| 780 |
} |
| 781 |
break; |
| 782 |
} |
| 783 |
case TIFF_SLONG8: |
| 784 |
{ |
| 785 |
int64_t *llp = (int64_t *)data; |
| 786 |
while (count-- > 0) |
| 787 |
{ |
| 788 |
int64_t val; |
| 789 |
memcpy(&val, llp, sizeof(int64_t)); |
| 790 |
llp++; |
| 791 |
fprintf(fd, slong8fmt, sep, val); |
| 792 |
sep = " "; |
| 793 |
} |
| 794 |
break; |
| 795 |
} |
| 796 |
case TIFF_RATIONAL: |
| 797 |
{ |
| 798 |
uint32_t *lp = (uint32_t *)data; |
| 799 |
while (count-- > 0) |
| 800 |
{ |
| 801 |
if (lp[1] == 0) |
| 802 |
fprintf(fd, "%sNan (%" PRIu32 "/%" PRIu32 ")", sep, lp[0], |
| 803 |
lp[1]); |
| 804 |
else |
| 805 |
fprintf(fd, rationalfmt, sep, |
| 806 |
(double)lp[0] / (double)lp[1]); |
| 807 |
sep = " "; |
| 808 |
lp += 2; |
| 809 |
} |
| 810 |
break; |
| 811 |
} |
| 812 |
case TIFF_SRATIONAL: |
| 813 |
{ |
| 814 |
int32_t *lp = (int32_t *)data; |
| 815 |
while (count-- > 0) |
| 816 |
{ |
| 817 |
if (lp[1] == 0) |
| 818 |
fprintf(fd, "%sNan (%" PRId32 "/%" PRId32 ")", sep, lp[0], |
| 819 |
lp[1]); |
| 820 |
else |
| 821 |
fprintf(fd, srationalfmt, sep, |
| 822 |
(double)lp[0] / (double)lp[1]); |
| 823 |
sep = " "; |
| 824 |
lp += 2; |
| 825 |
} |
| 826 |
break; |
| 827 |
} |
| 828 |
case TIFF_FLOAT: |
| 829 |
{ |
| 830 |
float *fp = (float *)data; |
| 831 |
while (count-- > 0) |
| 832 |
fprintf(fd, floatfmt, sep, *fp++), sep = " "; |
| 833 |
break; |
| 834 |
} |
| 835 |
case TIFF_DOUBLE: |
| 836 |
{ |
| 837 |
double *dp = (double *)data; |
| 838 |
while (count-- > 0) |
| 839 |
fprintf(fd, doublefmt, sep, *dp++), sep = " "; |
| 840 |
break; |
| 841 |
} |
| 842 |
case TIFF_IFD: |
| 843 |
{ |
| 844 |
uint32_t *lp = (uint32_t *)data; |
| 845 |
while (count-- > 0) |
| 846 |
{ |
| 847 |
fprintf(fd, ifdfmt, sep, *lp++); |
| 848 |
sep = " "; |
| 849 |
} |
| 850 |
break; |
| 851 |
} |
| 852 |
case TIFF_IFD8: |
| 853 |
{ |
| 854 |
uint64_t *llp = (uint64_t *)data; |
| 855 |
while (count-- > 0) |
| 856 |
{ |
| 857 |
fprintf(fd, ifd8fmt, sep, *llp++); |
| 858 |
sep = " "; |
| 859 |
sep = " "; |
| 860 |
} |
| 861 |
break; |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
static void ReadError(char *what) { Fatal("Error while reading %s", what); } |
| 867 |
|
| 868 |
#include <stdarg.h> |
| 869 |
|
| 870 |
static void vError(FILE *fd, const char *fmt, va_list ap) |
| 871 |
{ |
| 872 |
fprintf(fd, "%s: ", curfile); |
| 873 |
vfprintf(fd, fmt, ap); |
| 874 |
fprintf(fd, ".\n"); |
| 875 |
} |
| 876 |
|
| 877 |
static void Error(const char *fmt, ...) |
| 878 |
{ |
| 879 |
va_list ap; |
| 880 |
va_start(ap, fmt); |
| 881 |
vError(stderr, fmt, ap); |
| 882 |
va_end(ap); |
| 883 |
} |
| 884 |
|
| 885 |
static void Fatal(const char *fmt, ...) |
| 886 |
{ |
| 887 |
va_list ap; |
| 888 |
va_start(ap, fmt); |
| 889 |
vError(stderr, fmt, ap); |
| 890 |
va_end(ap); |
| 891 |
exit(EXIT_FAILURE); |
| 892 |
} |