Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

692 lines
23 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include <malloc.h>
  7. #include "tiffdump.h"
  8. char *curfile;
  9. int swabflag;
  10. int bigendian;
  11. int typeshift[13]; /* data type shift counts */
  12. long typemask[13]; /* data type masks */
  13. //
  14. // prototypes
  15. //
  16. long ReadDirectory();
  17. void dump(int fd);
  18. long ReadDirectory(int fd,long off);
  19. void PrintTag(FILE *fd, unsigned short tag);
  20. void PrintType(FILE *fd, unsigned short type);
  21. void PrintData(FILE *fd,unsigned short type,long count,unsigned char *data);
  22. void TIFFSwabShort(unsigned short *wp);
  23. void TIFFSwabLong(unsigned long *lp);
  24. void TIFFSwabArrayOfShort(unsigned short *wp,int n);
  25. void TIFFSwabArrayOfLong(unsigned long *lp,int n);
  26. int TIFFFetchData(int fd,TIFFDirEntry *dir,char *cp);
  27. void ReadError(char *what);
  28. void Error(char *fmt,...);
  29. void Fatal(char *fmt,...);
  30. int _cdecl main(int argc, char *argv[])
  31. {
  32. int one = 1, fd;
  33. int multiplefiles = (argc > 1);
  34. bigendian = (*(char *)&one == 0);
  35. for (argc--, argv++; argc > 0; argc--, argv++) {
  36. fd = _open(argv[0], O_RDONLY|O_BINARY, 0);
  37. if (fd < 0) {
  38. perror(argv[0]);
  39. exit(-1);
  40. }
  41. if (multiplefiles)
  42. printf("%s:\n", argv[0]);
  43. curfile = *argv;
  44. swabflag = 0;
  45. dump(fd);
  46. _close(fd);
  47. }
  48. return 0;
  49. }
  50. TIFFHeader h;
  51. #define ord(e) ((int)e)
  52. /*
  53. * Initialize shift & mask tables and byte
  54. * swapping state according to the file
  55. * byte order.
  56. */
  57. void InitByteOrder(int magic)
  58. {
  59. typemask[0] = 0;
  60. typemask[ord(TIFF_BYTE)] = 0xff;
  61. typemask[ord(TIFF_SBYTE)] = 0xff;
  62. typemask[ord(TIFF_UNDEFINED)] = 0xff;
  63. typemask[ord(TIFF_SHORT)] = 0xffff;
  64. typemask[ord(TIFF_SSHORT)] = 0xffff;
  65. typemask[ord(TIFF_LONG)] = 0xffffffff;
  66. typemask[ord(TIFF_SLONG)] = 0xffffffff;
  67. typemask[ord(TIFF_RATIONAL)] = 0xffffffff;
  68. typemask[ord(TIFF_SRATIONAL)] = 0xffffffff;
  69. typemask[ord(TIFF_FLOAT)] = 0xffffffff;
  70. typemask[ord(TIFF_DOUBLE)] = 0xffffffff;
  71. typeshift[0] = 0;
  72. typeshift[ord(TIFF_LONG)] = 0;
  73. typeshift[ord(TIFF_SLONG)] = 0;
  74. typeshift[ord(TIFF_RATIONAL)] = 0;
  75. typeshift[ord(TIFF_SRATIONAL)] = 0;
  76. typeshift[ord(TIFF_FLOAT)] = 0;
  77. typeshift[ord(TIFF_DOUBLE)] = 0;
  78. if (magic == TIFF_BIGENDIAN) {
  79. typeshift[ord(TIFF_BYTE)] = 24;
  80. typeshift[ord(TIFF_SBYTE)] = 24;
  81. typeshift[ord(TIFF_SHORT)] = 16;
  82. typeshift[ord(TIFF_SSHORT)] = 16;
  83. swabflag = !bigendian;
  84. } else {
  85. typeshift[ord(TIFF_BYTE)] = 0;
  86. typeshift[ord(TIFF_SBYTE)] = 0;
  87. typeshift[ord(TIFF_SHORT)] = 0;
  88. typeshift[ord(TIFF_SSHORT)] = 0;
  89. swabflag = bigendian;
  90. }
  91. }
  92. void
  93. dump(
  94. int fd
  95. )
  96. {
  97. long off;
  98. int i;
  99. _lseek(fd, 0L, 0);
  100. if (_read(fd, &h, sizeof (h)) != sizeof (h))
  101. ReadError("TIFF header");
  102. /*
  103. * Setup the byte order handling.
  104. */
  105. if (h.tiff_magic != TIFF_BIGENDIAN && h.tiff_magic != TIFF_LITTLEENDIAN)
  106. Fatal("Not a TIFF file, bad magic number %u (0x%x)",
  107. h.tiff_magic, h.tiff_magic);
  108. InitByteOrder(h.tiff_magic);
  109. /*
  110. * Swap header if required.
  111. */
  112. if (swabflag) {
  113. TIFFSwabShort(&h.tiff_version);
  114. TIFFSwabLong(&h.tiff_diroff);
  115. }
  116. /*
  117. * Now check version (if needed, it's been byte-swapped).
  118. * Note that this isn't actually a version number, it's a
  119. * magic number that doesn't change.
  120. */
  121. if (h.tiff_version != TIFF_VERSION)
  122. Fatal("Not a TIFF file, bad version number %u (0x%x)",
  123. h.tiff_version, h.tiff_version);
  124. printf("Magic: 0x%x <%s-endian> Version: 0x%x\n",
  125. h.tiff_magic,
  126. h.tiff_magic == TIFF_BIGENDIAN ? "big" : "little",
  127. h.tiff_version);
  128. i = 0;
  129. off = h.tiff_diroff;
  130. while (off) {
  131. if (i > 0)
  132. putchar('\n');
  133. printf("Directory %d: offset %lu (0x%lx)\n", i++, off, off);
  134. off = ReadDirectory(fd, off);
  135. }
  136. }
  137. static int datawidth[] = {
  138. 0, /* nothing */
  139. 1, /* TIFF_BYTE */
  140. 1, /* TIFF_ASCII */
  141. 2, /* TIFF_SHORT */
  142. 4, /* TIFF_LONG */
  143. 8, /* TIFF_RATIONAL */
  144. 1, /* TIFF_SBYTE */
  145. 1, /* TIFF_UNDEFINED */
  146. 2, /* TIFF_SSHORT */
  147. 4, /* TIFF_SLONG */
  148. 8, /* TIFF_SRATIONAL */
  149. 4, /* TIFF_FLOAT */
  150. 8, /* TIFF_DOUBLE */
  151. };
  152. #define NWIDTHS (sizeof (datawidth) / sizeof (datawidth[0]))
  153. /*
  154. * Read the next TIFF directory from a file
  155. * and convert it to the internal format.
  156. * We read directories sequentially.
  157. */
  158. long
  159. ReadDirectory(
  160. int fd,
  161. long off
  162. )
  163. {
  164. TIFFDirEntry *dp;
  165. int n;
  166. TIFFDirEntry *dir = 0;
  167. unsigned short dircount;
  168. int space;
  169. long nextdiroff = 0;
  170. if (off == 0) /* no more directories */
  171. goto done;
  172. if (_lseek(fd, off, 0) != off) {
  173. Fatal("Seek error accessing TIFF directory");
  174. goto done;
  175. }
  176. if (_read(fd, &dircount, sizeof (short)) != sizeof (short)) {
  177. ReadError("directory count");
  178. goto done;
  179. }
  180. if (swabflag)
  181. TIFFSwabShort(&dircount);
  182. dir = (TIFFDirEntry *)malloc(dircount * sizeof (TIFFDirEntry));
  183. if (dir == NULL) {
  184. Fatal("No space for TIFF directory");
  185. goto done;
  186. }
  187. n = _read(fd, dir, dircount*sizeof (*dp));
  188. if (n != (int)(dircount*sizeof(*dp))) {
  189. n /= sizeof (*dp);
  190. Error(
  191. "Could only read %u of %u entries in directory at offset 0x%x",
  192. n, dircount, off);
  193. dircount = n;
  194. }
  195. if (_read(fd, &nextdiroff, sizeof (long)) != sizeof (long))
  196. nextdiroff = 0;
  197. if (swabflag)
  198. TIFFSwabLong(&nextdiroff);
  199. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  200. if (swabflag) {
  201. TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
  202. TIFFSwabArrayOfLong(&dp->tdir_count, 2);
  203. }
  204. PrintTag(stdout, dp->tdir_tag);
  205. putchar(' ');
  206. PrintType(stdout, dp->tdir_type);
  207. putchar(' ');
  208. printf("%u<", dp->tdir_count);
  209. if (dp->tdir_type >= NWIDTHS) {
  210. printf(">\n");
  211. continue;
  212. }
  213. space = dp->tdir_count * datawidth[dp->tdir_type];
  214. if (space <= 4) {
  215. switch (dp->tdir_type) {
  216. case TIFF_ASCII: {
  217. char data[4];
  218. memcpy(data,&dp->tdir_offset,4);
  219. if (swabflag)
  220. TIFFSwabLong((unsigned long *)data);
  221. PrintData(stdout,
  222. dp->tdir_type, dp->tdir_count, data);
  223. break;
  224. }
  225. case TIFF_BYTE:
  226. case TIFF_SBYTE:
  227. if (h.tiff_magic == TIFF_LITTLEENDIAN) {
  228. switch ((int)dp->tdir_count) {
  229. case 4:
  230. printf("0x%02x",
  231. dp->tdir_offset&0xff);
  232. case 3:
  233. printf("0x%02x",
  234. (dp->tdir_offset>>8)&0xff);
  235. case 2:
  236. printf("0x%02x",
  237. (dp->tdir_offset>>16)&0xff);
  238. case 1:
  239. printf("0x%02x",
  240. dp->tdir_offset>>24);
  241. break;
  242. }
  243. } else {
  244. switch ((int)dp->tdir_count) {
  245. case 4:
  246. printf("0x%02x",
  247. dp->tdir_offset>>24);
  248. case 3:
  249. printf("0x%02x",
  250. (dp->tdir_offset>>16)&0xff);
  251. case 2:
  252. printf("0x%02x",
  253. (dp->tdir_offset>>8)&0xff);
  254. case 1:
  255. printf("0x%02x",
  256. dp->tdir_offset&0xff);
  257. break;
  258. }
  259. }
  260. break;
  261. case TIFF_SHORT:
  262. case TIFF_SSHORT:
  263. if (h.tiff_magic == TIFF_LITTLEENDIAN) {
  264. switch (dp->tdir_count) {
  265. case 2:
  266. printf("%u ",
  267. dp->tdir_offset&0xffff);
  268. case 1:
  269. printf("%u",
  270. //dp->tdir_offset>>16);
  271. (SHORT)dp->tdir_offset);
  272. break;
  273. }
  274. } else {
  275. switch (dp->tdir_count) {
  276. case 2:
  277. printf("%u ",
  278. dp->tdir_offset>>16);
  279. case 1:
  280. printf("%u",
  281. dp->tdir_offset&0xffff);
  282. break;
  283. }
  284. }
  285. break;
  286. case TIFF_LONG:
  287. printf("%lu", dp->tdir_offset);
  288. break;
  289. case TIFF_SLONG:
  290. printf("%ld", dp->tdir_offset);
  291. break;
  292. }
  293. } else {
  294. char *data = (char *)malloc(space);
  295. if (data) {
  296. if (TIFFFetchData(fd, dp, data))
  297. PrintData(stdout, dp->tdir_type,
  298. dp->tdir_count, data);
  299. free(data);
  300. } else
  301. Error("No space for data for tag %u",
  302. dp->tdir_tag);
  303. }
  304. printf(">\n");
  305. }
  306. done:
  307. if (dir)
  308. free((char *)dir);
  309. return (nextdiroff);
  310. }
  311. static struct tagname {
  312. int tag;
  313. char *name;
  314. } tagnames[] = {
  315. { TIFFTAG_SUBFILETYPE, "SubFileType" },
  316. { TIFFTAG_OSUBFILETYPE, "OldSubFileType" },
  317. { TIFFTAG_IMAGEWIDTH, "ImageWidth" },
  318. { TIFFTAG_IMAGELENGTH, "ImageLength" },
  319. { TIFFTAG_BITSPERSAMPLE, "BitsPerSample" },
  320. { TIFFTAG_COMPRESSION, "Compression" },
  321. { TIFFTAG_PHOTOMETRIC, "Photometric" },
  322. { TIFFTAG_THRESHHOLDING, "Threshholding" },
  323. { TIFFTAG_CELLWIDTH, "CellWidth" },
  324. { TIFFTAG_CELLLENGTH, "CellLength" },
  325. { TIFFTAG_FILLORDER, "FillOrder" },
  326. { TIFFTAG_DOCUMENTNAME, "DocumentName" },
  327. { TIFFTAG_IMAGEDESCRIPTION, "ImageDescription" },
  328. { TIFFTAG_MAKE, "Make" },
  329. { TIFFTAG_MODEL, "Model" },
  330. { TIFFTAG_STRIPOFFSETS, "StripOffsets" },
  331. { TIFFTAG_ORIENTATION, "Orientation" },
  332. { TIFFTAG_SAMPLESPERPIXEL, "SamplesPerPixel" },
  333. { TIFFTAG_ROWSPERSTRIP, "RowsPerStrip" },
  334. { TIFFTAG_STRIPBYTECOUNTS, "StripByteCounts" },
  335. { TIFFTAG_MINSAMPLEVALUE, "MinSampleValue" },
  336. { TIFFTAG_MAXSAMPLEVALUE, "MaxSampleValue" },
  337. { TIFFTAG_XRESOLUTION, "XResolution" },
  338. { TIFFTAG_YRESOLUTION, "YResolution" },
  339. { TIFFTAG_PLANARCONFIG, "PlanarConfig" },
  340. { TIFFTAG_PAGENAME, "PageName" },
  341. { TIFFTAG_XPOSITION, "XPosition" },
  342. { TIFFTAG_YPOSITION, "YPosition" },
  343. { TIFFTAG_FREEOFFSETS, "FreeOffsets" },
  344. { TIFFTAG_FREEBYTECOUNTS, "FreeByteCounts" },
  345. { TIFFTAG_GRAYRESPONSEUNIT, "GrayResponseUnit" },
  346. { TIFFTAG_GRAYRESPONSECURVE,"GrayResponseCurve" },
  347. { TIFFTAG_GROUP3OPTIONS, "Group3Options" },
  348. { TIFFTAG_GROUP4OPTIONS, "Group4Options" },
  349. { TIFFTAG_RESOLUTIONUNIT, "ResolutionUnit" },
  350. { TIFFTAG_PAGENUMBER, "PageNumber" },
  351. { TIFFTAG_COLORRESPONSEUNIT,"ColorResponseUnit" },
  352. { TIFFTAG_TRANSFERFUNCTION, "TransferFunction" },
  353. { TIFFTAG_SOFTWARE, "Software" },
  354. { TIFFTAG_DATETIME, "DateTime" },
  355. { TIFFTAG_ARTIST, "Artist" },
  356. { TIFFTAG_HOSTCOMPUTER, "HostComputer" },
  357. { TIFFTAG_PREDICTOR, "Predictor" },
  358. { TIFFTAG_WHITEPOINT, "Whitepoint" },
  359. { TIFFTAG_PRIMARYCHROMATICITIES,"PrimaryChromaticities" },
  360. { TIFFTAG_COLORMAP, "Colormap" },
  361. { TIFFTAG_HALFTONEHINTS, "HalftoneHints" },
  362. { TIFFTAG_TILEWIDTH, "TileWidth" },
  363. { TIFFTAG_TILELENGTH, "TileLength" },
  364. { TIFFTAG_TILEOFFSETS, "TileOffsets" },
  365. { TIFFTAG_TILEBYTECOUNTS, "TileByteCounts" },
  366. { TIFFTAG_BADFAXLINES, "BadFaxLines" },
  367. { TIFFTAG_CLEANFAXDATA, "CleanFaxData" },
  368. { TIFFTAG_CONSECUTIVEBADFAXLINES, "ConsecutiveBadFaxLines" },
  369. { TIFFTAG_INKSET, "InkSet" },
  370. { TIFFTAG_INKNAMES, "InkNames" },
  371. { TIFFTAG_DOTRANGE, "DotRange" },
  372. { TIFFTAG_TARGETPRINTER, "TargetPrinter" },
  373. { TIFFTAG_EXTRASAMPLES, "ExtraSamples" },
  374. { TIFFTAG_SAMPLEFORMAT, "SampleFormat" },
  375. { TIFFTAG_SMINSAMPLEVALUE, "SMinSampleValue" },
  376. { TIFFTAG_SMAXSAMPLEVALUE, "SMaxSampleValue" },
  377. { TIFFTAG_JPEGPROC, "JPEGProcessingMode" },
  378. { TIFFTAG_JPEGIFOFFSET, "JPEGInterchangeFormat" },
  379. { TIFFTAG_JPEGIFBYTECOUNT, "JPEGInterchangeFormatLength" },
  380. { TIFFTAG_JPEGRESTARTINTERVAL,"JPEGRestartInterval" },
  381. { TIFFTAG_JPEGLOSSLESSPREDICTORS,"JPEGLosslessPredictors" },
  382. { TIFFTAG_JPEGPOINTTRANSFORM,"JPEGPointTransform" },
  383. { TIFFTAG_JPEGQTABLES, "JPEGQTables" },
  384. { TIFFTAG_JPEGDCTABLES, "JPEGDCTables" },
  385. { TIFFTAG_JPEGACTABLES, "JPEGACTables" },
  386. { TIFFTAG_YCBCRCOEFFICIENTS,"YCbCrCoefficients" },
  387. { TIFFTAG_YCBCRSUBSAMPLING, "YCbCrSubsampling" },
  388. { TIFFTAG_YCBCRPOSITIONING, "YCbCrPositioning" },
  389. { TIFFTAG_REFERENCEBLACKWHITE, "ReferenceBlackWhite" },
  390. { TIFFTAG_MATTEING, "OBSOLETE Matteing" },
  391. { TIFFTAG_DATATYPE, "OBSOLETE DataType" },
  392. { TIFFTAG_IMAGEDEPTH, "ImageDepth" },
  393. { TIFFTAG_RECIP_NAME, "RecipientName" },
  394. { TIFFTAG_RECIP_NUMBER, "RecipientNumber" },
  395. { TIFFTAG_SENDER_NAME, "SenderName" },
  396. { TIFFTAG_ROUTING, "Routing" },
  397. { TIFFTAG_CALLERID, "CallerID" },
  398. { TIFFTAG_TSID, "TSID" },
  399. { TIFFTAG_CSID, "CSID" },
  400. { TIFFTAG_FAX_TIME, "FaxTime" },
  401. { TIFFTAG_TILEDEPTH, "TileDepth" },
  402. { 32768, "OLD BOGUS Matteing tag" },
  403. };
  404. #define NTAGS (sizeof (tagnames) / sizeof (tagnames[0]))
  405. void
  406. PrintTag(
  407. FILE *fd,
  408. unsigned short tag
  409. )
  410. {
  411. struct tagname *tp;
  412. for (tp = tagnames; tp < &tagnames[NTAGS]; tp++)
  413. if (tp->tag == tag) {
  414. fprintf(fd, "%s --0x%x (%u)", tp->name, tag,tag);
  415. return;
  416. }
  417. fprintf(fd, "%u (0x%x)", tag, tag);
  418. }
  419. void
  420. PrintType(
  421. FILE *fd,
  422. unsigned short type
  423. )
  424. {
  425. static char *typenames[] = {
  426. "0",
  427. "BYTE",
  428. "ASCII",
  429. "SHORT",
  430. "LONG",
  431. "RATIONAL",
  432. "SBYTE",
  433. "UNDEFINED",
  434. "SSHORT",
  435. "SLONG",
  436. "SRATIONAL",
  437. "FLOAT",
  438. "DOUBLE"
  439. };
  440. #define NTYPES (sizeof (typenames) / sizeof (typenames[0]))
  441. if (type < NTYPES)
  442. fprintf(fd, "%s (%u)", typenames[type], type);
  443. else
  444. fprintf(fd, "%u (0x%x)", type, type);
  445. }
  446. #undef NTYPES
  447. void
  448. PrintData(
  449. FILE *fd,
  450. unsigned short type,
  451. long count,
  452. unsigned char *data
  453. )
  454. {
  455. switch (type) {
  456. case TIFF_BYTE:
  457. while (count-- > 0)
  458. fprintf(fd, "%u%s", *data++, count > 0 ? " " : "");
  459. break;
  460. case TIFF_SBYTE:
  461. while (count-- > 0)
  462. fprintf(fd, "%d%s", *(char *)data++, count > 0 ? " " : "");
  463. break;
  464. case TIFF_UNDEFINED:
  465. while (count-- > 0)
  466. fprintf(fd, "0x%02x", *data++);
  467. break;
  468. case TIFF_ASCII:
  469. fprintf(fd, "%.*s", count, data);
  470. break;
  471. case TIFF_SHORT: {
  472. unsigned short *wp = (unsigned short *)data;
  473. while (count-- > 0)
  474. fprintf(fd, "%u%s", *wp++, count > 0 ? " " : "");
  475. break;
  476. }
  477. case TIFF_SSHORT: {
  478. short *wp = (short *)data;
  479. while (count-- > 0)
  480. fprintf(fd, "%d%s", *wp++, count > 0 ? " " : "");
  481. break;
  482. }
  483. case TIFF_LONG: {
  484. unsigned long *lp = (unsigned long *)data;
  485. while (count-- > 0)
  486. fprintf(fd, "%lu%s", *lp++, count > 0 ? " " : "");
  487. break;
  488. }
  489. case TIFF_SLONG: {
  490. long *lp = (long *)data;
  491. while (count-- > 0)
  492. fprintf(fd, "%ld%s", *lp++, count > 0 ? " " : "");
  493. break;
  494. }
  495. case TIFF_RATIONAL: {
  496. unsigned long *lp = (unsigned long *)data;
  497. while (count-- > 0) {
  498. if (lp[1] == 0)
  499. fprintf(fd, "Nan (%lu/%lu)", lp[0], lp[1]);
  500. else
  501. fprintf(fd, "%g",
  502. (double)lp[0] / (double)lp[1]);
  503. if (count > 0)
  504. fprintf(fd, " ");
  505. lp += 2;
  506. }
  507. break;
  508. }
  509. case TIFF_SRATIONAL: {
  510. long *lp = (long *)data;
  511. while (count-- > 0) {
  512. if (lp[1] == 0)
  513. fprintf(fd, "Nan (%ld/%ld)", lp[0], lp[1]);
  514. else
  515. fprintf(fd, "%g",
  516. (double)lp[0] / (double)lp[1]);
  517. if (count > 0)
  518. fprintf(fd, " ");
  519. lp += 2;
  520. }
  521. break;
  522. }
  523. case TIFF_FLOAT: {
  524. float *fp = (float *)data;
  525. while (count-- > 0)
  526. fprintf(fd, "%g%s", *fp++, count > 0 ? " " : "");
  527. break;
  528. }
  529. case TIFF_DOUBLE: {
  530. double *dp = (double *)data;
  531. while (count-- > 0)
  532. fprintf(fd, "%g%s", *dp++, count > 0 ? " " : "");
  533. break;
  534. }
  535. }
  536. }
  537. void
  538. TIFFSwabShort(
  539. unsigned short *wp
  540. )
  541. {
  542. unsigned char *cp = (unsigned char *)wp;
  543. int t;
  544. t = cp[1]; cp[1] = cp[0]; cp[0] = t;
  545. }
  546. void
  547. TIFFSwabLong(
  548. unsigned long *lp
  549. )
  550. {
  551. unsigned char *cp = (unsigned char *)lp;
  552. int t;
  553. t = cp[3]; cp[3] = cp[0]; cp[0] = t;
  554. t = cp[2]; cp[2] = cp[1]; cp[1] = t;
  555. }
  556. void
  557. TIFFSwabArrayOfShort(
  558. unsigned short *wp,
  559. int n
  560. )
  561. {
  562. unsigned char *cp;
  563. int t;
  564. /* XXX unroll loop some */
  565. while (n-- > 0) {
  566. cp = (unsigned char *)wp;
  567. t = cp[1]; cp[1] = cp[0]; cp[0] = t;
  568. wp++;
  569. }
  570. }
  571. void
  572. TIFFSwabArrayOfLong(
  573. unsigned long *lp,
  574. int n
  575. )
  576. {
  577. unsigned char *cp;
  578. int t;
  579. /* XXX unroll loop some */
  580. while (n-- > 0) {
  581. cp = (unsigned char *)lp;
  582. t = cp[3]; cp[3] = cp[0]; cp[0] = t;
  583. t = cp[2]; cp[2] = cp[1]; cp[1] = t;
  584. lp++;
  585. }
  586. }
  587. /*
  588. * Fetch a contiguous directory item.
  589. */
  590. int
  591. TIFFFetchData(
  592. int fd,
  593. TIFFDirEntry *dir,
  594. char *cp
  595. )
  596. {
  597. int cc, w;
  598. w = (dir->tdir_type < NWIDTHS ? datawidth[dir->tdir_type] : 0);
  599. cc = dir->tdir_count * w;
  600. if (_lseek(fd, dir->tdir_offset, 0) == (int)dir->tdir_offset && _read(fd, cp, cc) == cc) {
  601. if (swabflag) {
  602. switch (dir->tdir_type) {
  603. case TIFF_SHORT:
  604. case TIFF_SSHORT:
  605. TIFFSwabArrayOfShort((unsigned short *)cp, dir->tdir_count);
  606. break;
  607. case TIFF_LONG:
  608. case TIFF_SLONG:
  609. TIFFSwabArrayOfLong((unsigned long *)cp, dir->tdir_count);
  610. break;
  611. case TIFF_RATIONAL:
  612. case TIFF_DOUBLE:
  613. TIFFSwabArrayOfLong((unsigned long *)cp, 2*dir->tdir_count);
  614. break;
  615. }
  616. }
  617. return (cc);
  618. }
  619. Error("Error while reading data for tag %u", dir->tdir_tag);
  620. return (0);
  621. }
  622. void
  623. ReadError(
  624. char *what
  625. )
  626. {
  627. Fatal("Error while reading %s", what);
  628. }
  629. void
  630. Error(
  631. char *fmt,
  632. ...
  633. )
  634. {
  635. char buf[1024];
  636. va_list arg_ptr;
  637. va_start(arg_ptr,fmt);
  638. _vsnprintf(buf,sizeof(buf),fmt,arg_ptr);
  639. va_end(arg_ptr);
  640. fprintf(stderr,"%s: ",curfile);
  641. fprintf(stderr,"%s\n",buf);
  642. }
  643. void
  644. Fatal(
  645. char *fmt,
  646. ...
  647. )
  648. {
  649. char buf[1024];
  650. va_list arg_ptr;
  651. va_start(arg_ptr,fmt);
  652. _vsnprintf(buf,sizeof(buf),fmt,arg_ptr);
  653. va_end(arg_ptr);
  654. fprintf(stderr,"%s: ",curfile);
  655. fprintf(stderr,"%s\n",buf);
  656. exit(-1);
  657. }