Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/test/test_IFD_enlargement.c

raw
1 /*
2 * TIFF Library
3 *
4 * The purpose of this test file is to test the correctness of
5 * TIFFWriteDirectory() when an already written IFD is extended with additional
6 * tags or the IFD data size is increased. The IFD must then be re-written to a
7 * different location in the file. On the other hand, the IFD should be
8 * overwritten if its size has not grown.
9 */
10
11 #include "tif_config.h"
12
13 #include <assert.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "tiffio.h"
20
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #ifndef TRUE
26 #define TRUE 1
27 #define FALSE 0
28 #endif
29
30 #ifndef TIFFmin
31 #define TIFFmax(A, B) ((A) > (B) ? (A) : (B))
32 #define TIFFmin(A, B) ((A) < (B) ? (A) : (B))
33 #endif
34
35 /* Global settings -not save to change- */
36 #define SPP 3 /* samples per pixel */
37 #define BPS 8 /* bits per sample */
38
39 char *modeStrings[] = {"wl", "wb", "w8l", "w8b"};
40
41 /* Writes some pixel data as scanline or tiles to file.
42 */
43 int write_image_data(TIFF *tif, uint16_t width, uint16_t length, bool tiled,
44 unsigned int pixval, unsigned char *plastlinedata,
45 unsigned int lastlinebytesmax)
46 {
47 size_t bufLen;
48 unsigned char *pbufLine = NULL;
49 unsigned int bpsmod = (1 << BPS);
50 int tlwidth;
51 int tllength;
52 tmsize_t tlsize;
53
54 assert(SPP == 3);
55
56 if (tiled)
57 {
58 int ret = TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tlwidth);
59 ret += TIFFGetField(tif, TIFFTAG_TILELENGTH, &tllength);
60 if (ret < 2)
61 {
62 fprintf(stderr,
63 "Can't get TIFFTAG_TILEWIDTH/_LENGTH tags. Testline %d\n",
64 __LINE__);
65 return 1;
66 }
67 /* For tiled mode get size of a tile in bytes */
68 tlsize = TIFFTileSize(tif);
69 bufLen = (size_t)tlsize;
70 }
71 else
72 {
73 /* For strip mode get size of a row in bytes */
74 bufLen = (((size_t)width * SPP * BPS) + 7) / 8;
75 }
76
77 pbufLine = _TIFFmalloc(bufLen);
78 if (pbufLine == NULL)
79 return 1;
80
81 /* Fill image buffer. */
82 pbufLine[0] = (unsigned char)((pixval * 100) % bpsmod);
83 pbufLine[1] = 127 % bpsmod;
84 pbufLine[2] = 255 % bpsmod;
85 for (size_t x = SPP; x < bufLen; x += SPP)
86 {
87 pbufLine[x] = 10 % bpsmod;
88 pbufLine[x + 1] = (unsigned char)((x * 40) % bpsmod);
89 pbufLine[x + 2] = (unsigned char)((x * 70) % bpsmod);
90 }
91
92 /* Write dummy pixel data. */
93 if (tiled)
94 {
95 uint32_t numtiles = TIFFNumberOfTiles(tif);
96 for (uint32_t i = 0; i < numtiles; i++)
97 {
98 if (TIFFWriteEncodedTile(tif, i, pbufLine, 0) == -1)
99 {
100 fprintf(stderr, "Can't write image data tile. Testline %d\n",
101 __LINE__);
102 return 1;
103 }
104 if ((plastlinedata != NULL) && (i == (numtiles - 1)))
105 {
106 memcpy(plastlinedata, pbufLine,
107 TIFFmin(bufLen, (size_t)lastlinebytesmax));
108 }
109 /* Change something for next row. */
110 pbufLine[0] = (pbufLine[0] + 35) % bpsmod;
111 }
112 }
113 else
114 {
115 for (int i = 0; i < length; i++)
116 {
117 if (TIFFWriteScanline(tif, pbufLine, i, 0) == -1)
118 {
119 fprintf(stderr,
120 "Can't write image data scanline. Testline %d\n",
121 __LINE__);
122 return 1;
123 }
124 if ((plastlinedata != NULL) && (i == (length - 1)))
125 {
126 memcpy(plastlinedata, pbufLine,
127 TIFFmin(bufLen, (size_t)lastlinebytesmax));
128 }
129 /* Change something for next row. */
130 pbufLine[0] = (pbufLine[0] + 30) % bpsmod;
131 }
132 }
133 _TIFFfree(pbufLine);
134 return 0;
135 } /*-- write_image_data() --*/
136
137 /* Fills the active IFD with some default values and writes
138 * an image with given number of lines as strips (scanlines) or tiles to file.
139 */
140 int write_data_to_current_directory(TIFF *tif, uint16_t width, uint16_t length,
141 bool tiled, int ifd_page_num,
142 bool write_data,
143 unsigned char *plastlinedata,
144 unsigned int lastlinebytesmax)
145 {
146 const uint16_t photometric = PHOTOMETRIC_RGB;
147 const uint16_t rows_per_strip = 1;
148 const uint16_t planarconfig = PLANARCONFIG_CONTIG;
149
150 char auxString[128];
151
152 if (!tif)
153 {
154 fprintf(stderr, "Invalid TIFF handle.\n");
155 return 1;
156 }
157 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width))
158 {
159 fprintf(stderr, "Can't set ImageWidth tag.\n");
160 return 1;
161 }
162 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length))
163 {
164 fprintf(stderr, "Can't set ImageLength tag.\n");
165 return 1;
166 }
167 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, BPS))
168 {
169 fprintf(stderr, "Can't set BitsPerSample tag.\n");
170 return 1;
171 }
172 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP))
173 {
174 fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
175 return 1;
176 }
177
178 if (tiled)
179 {
180 /* Tilesizes must be multiple of 16. */
181 int ret = TIFFSetField(tif, TIFFTAG_TILEWIDTH, 16);
182 ret += TIFFSetField(tif, TIFFTAG_TILELENGTH, 16);
183 if (ret < 2)
184 {
185 fprintf(stderr, "Can't set TIFFTAG_TILEWIDTH/_LENGTH tags.\n");
186 return 1;
187 }
188 }
189 else
190 {
191 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip))
192 {
193 fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
194 return 1;
195 }
196 }
197
198 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig))
199 {
200 fprintf(stderr, "Can't set PlanarConfiguration tag.\n");
201 return 1;
202 }
203 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric))
204 {
205 fprintf(stderr, "Can't set PhotometricInterpretation tag.\n");
206 return 1;
207 }
208 /* Write IFD identification number to ASCII string of PageName tag. */
209 sprintf(auxString, "%d th. IFD", ifd_page_num);
210 if (!TIFFSetField(tif, TIFFTAG_PAGENAME, auxString))
211 {
212 fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag.\n");
213 return 1;
214 }
215
216 /* Write dummy pixel data. */
217 if (write_data)
218 {
219 if (write_image_data(tif, width, length, tiled, ifd_page_num,
220 plastlinedata, lastlinebytesmax))
221 {
222 fprintf(stderr, "Can't write image data. Testline %d\n", __LINE__);
223 return 1;
224 }
225 }
226 return 0;
227 } /*-- write_data_to_current_directory() --*/
228
229 /* Adds an EXIF IFD with some default values to the active IFD.
230 */
231 int write_EXIF_data_to_current_directory(TIFF *tif, int i,
232 uint64_t *dir_offset_EXIF)
233 {
234 char auxString[128];
235
236 if (!tif)
237 {
238 fprintf(stderr, "Invalid TIFF handle.\n");
239 return 1;
240 }
241
242 /*-- Now create an EXIF directory. */
243 if (TIFFCreateEXIFDirectory(tif) != 0)
244 {
245 fprintf(stderr, "TIFFCreateEXIFDirectory() failed.\n");
246 return 1;
247 }
248
249 if (!TIFFSetField(tif, EXIFTAG_EXIFVERSION, "0231"))
250 {
251 fprintf(stderr, "Can't write EXIFTAG_EXIFVERSION\n");
252 return 1;
253 }
254
255 /* Write IFD identification number to ASCII string of SUBSECTIMEORIGINAL
256 * tag. */
257 sprintf(auxString, "EXIF-%d-EXIF", i);
258 if (!TIFFSetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, auxString))
259 {
260 fprintf(stderr, "Can't set EXIFTAG_SUBSECTIMEORIGINAL tag.\n");
261 return 1;
262 }
263
264 if (!TIFFWriteCustomDirectory(tif, dir_offset_EXIF))
265 {
266 fprintf(stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n");
267 return 1;
268 }
269
270 return 0;
271 } /*-- write_EXIF_data_to_current_directory() --*/
272
273 /* Compare 'requested_dir_number' with number written in PageName tag
274 * into the IFD to identify that IFD. */
275 int is_requested_directory(TIFF *tif, int requested_dir_number,
276 const char *filename)
277 {
278 char *ptr = NULL;
279 char *auxStr = NULL;
280
281 if (!TIFFGetField(tif, TIFFTAG_PAGENAME, &ptr))
282 {
283 fprintf(stderr, "Can't get TIFFTAG_PAGENAME tag. Testline %d\n",
284 __LINE__);
285 return 0;
286 }
287
288 /* Check for reading errors */
289 if (ptr != NULL)
290 auxStr = strchr(ptr, ' ');
291
292 if (ptr == NULL || auxStr == NULL || strncmp(auxStr, " th.", 4))
293 {
294 ptr = ptr == NULL ? "(null)" : ptr;
295 fprintf(stderr,
296 "Error reading IFD directory number from PageName tag: %s. "
297 "Testline %d\n",
298 ptr, __LINE__);
299 return 0;
300 }
301
302 /* Retrieve IFD identification number from ASCII string */
303 const int nthIFD = atoi(ptr);
304 if (nthIFD == requested_dir_number)
305 {
306 return 1;
307 }
308 fprintf(
309 stderr,
310 "Expected directory %d from %s was not loaded but: %s. Testline %d\n",
311 requested_dir_number, filename, ptr, __LINE__);
312
313 return 0;
314 }
315
316 /* Checks that IFDs on file can be overwritten if they have not grown and
317 * were re-written to an other location if IFD size has been grown.
318 */
319 int test_IFD_enlargement(const char *filename, unsigned int openMode,
320 bool is_strile_array_writing, int numIFDs,
321 uint16_t width, uint16_t length, bool tiled)
322 {
323
324 #define NUMIFDsMAX 4
325 uint64_t auxUint64;
326 TIFFErrorHandler errHandler = NULL;
327 uint64_t offsetBase[NUMIFDsMAX] = {0};
328 uint64_t offsetNew[NUMIFDsMAX];
329 unsigned char bufLine[NUMIFDsMAX + 1][1024];
330
331 assert(numIFDs <= NUMIFDsMAX);
332 assert(openMode < (sizeof(modeStrings) / sizeof(modeStrings[0])));
333
334 /*-- Create a file and write numIFDs IFDs directly to it --*/
335 TIFF *tif = TIFFOpen(filename, modeStrings[openMode]);
336 if (!tif)
337 {
338 fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
339 return 1;
340 }
341
342 for (int i = 0; i < numIFDs; i++)
343 {
344 /* Writing baseline images (IFDs) to file. */
345 if (write_data_to_current_directory(tif, width, length, tiled, i,
346 !is_strile_array_writing,
347 &bufLine[i][0], sizeof(bufLine[i])))
348 {
349 fprintf(
350 stderr,
351 "Can't write data to current directory in %s. Testline %d\n",
352 filename, __LINE__);
353 goto failure;
354 }
355 /* xmldata need to be 9 bytes (17 bytes for BigTIFF, respectively) thus
356 * not to fit into the entry itself and have space to cover two
357 * strip-/tile-offsets when XMLPACKET tag contents is reduced. */
358 char xmldata[20];
359 if (TIFFIsBigTIFF(tif))
360 strcpy(xmldata, "XML-ABCDEABCDEFGH");
361 else
362 strcpy(xmldata, "XML-ABCDE");
363 uint32_t xmlpacketLength = (uint32_t)strlen(xmldata);
364
365 if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata))
366 {
367 fprintf(stderr, "Can't set TIFFTAG_XMLPACKET tag. Testline %d\n",
368 __LINE__);
369 goto failure;
370 }
371 if (is_strile_array_writing)
372 {
373 /* Set flag for deferred strile-array writing. */
374 if (!TIFFDeferStrileArrayWriting(tif))
375 {
376 fprintf(
377 stderr,
378 "TIFFDeferStrileArrayWriting failed for %s. Testline %d\n",
379 filename, __LINE__);
380 goto failure;
381 }
382 /* Set flag, that image writing has started and freeze relevant IFD
383 * tags. */
384 if (!TIFFWriteCheck(tif, tiled, NULL))
385 {
386 fprintf(stderr, "TIFFWriteCheck failed for %s. Testline %d\n",
387 filename, __LINE__);
388 goto failure;
389 }
390 }
391 /* Write IFD tags to file (and setup new empty IFD). */
392 if (!TIFFWriteDirectory(tif))
393 {
394 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
395 filename, __LINE__);
396 goto failure;
397 }
398 } /*-- for (numIFDs) --*/
399
400 /* Retrieve IFD offset values to read IFDs before image data has been
401 * written when is_strile_array_writing is true. */
402 for (int i = numIFDs - 1; i >= 0; i--)
403 {
404 TIFFSetDirectory(tif, i);
405 offsetBase[i] = TIFFCurrentDirOffset(tif);
406 }
407 /* For TIFFDeferStrileArrayWriting write now image data to file.*/
408 if (is_strile_array_writing)
409 {
410 for (int i = 0; i < numIFDs; i++)
411 {
412 /* Reset to written IFD to write strile arrays with dummy content to
413 * file and update StripOffset tag within IFD on file.
414 * First TIFFForceStrileArrayWriting() prepares internal flags and
415 * memory for next writing after switching back to the already
416 * written IFD. */
417 TIFFSetDirectory(tif, i);
418 if (!TIFFForceStrileArrayWriting(tif))
419 {
420 fprintf(
421 stderr,
422 "TIFFForceStrileArrayWriting failed for %s. Testline %d\n",
423 filename, __LINE__);
424 goto failure;
425 }
426 /* Then write image pixels. */
427 if (write_image_data(tif, width, length, tiled, 5 * i,
428 &bufLine[i][0], sizeof(bufLine[i])))
429 {
430 fprintf(stderr, "Can't write image data. Testline %d\n",
431 __LINE__);
432 return 1;
433 }
434 /* Last image data need to be flushed to file. */
435 if (!TIFFFlushData(tif))
436 {
437 fprintf(stderr, "TIFFFlushData failed for %s. Testline %d\n",
438 filename, __LINE__);
439 goto failure;
440 }
441 /* Fill (overwrite) strile array with final offsets after image data
442 * has been written. Would also be called by TIFFClose(). */
443 if (!TIFFForceStrileArrayWriting(tif))
444 {
445 fprintf(
446 stderr,
447 "TIFFForceStrileArrayWriting failed for %s. Testline %d\n",
448 filename, __LINE__);
449 goto failure;
450 }
451 }
452 }
453 /* Retrieve IFD offset values for later checking. Should not be changed. */
454 for (int i = numIFDs - 1; i >= 0; i--)
455 {
456 TIFFSetDirectory(tif, i);
457 auxUint64 = TIFFCurrentDirOffset(tif);
458 if (offsetBase[i] != auxUint64)
459 {
460 fprintf(stderr, "Unexpected change of offset IFD %d. Testline %d\n",
461 i, __LINE__);
462 goto failure;
463 }
464 }
465
466 /* Test over-writing with less bytes.
467 * This leaves some bytes of the larger previous IFD at EOF or before the
468 * next IFD. */
469 TIFFSetDirectory(tif, 0);
470 char xmldata0[] = "XMLb";
471 uint32_t xmlpacketLength = (uint32_t)strlen(xmldata0);
472
473 if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata0))
474 {
475 fprintf(stderr, "Can't set TIFFTAG_XMLPACKET tag. Testline %d\n",
476 __LINE__);
477 goto failure;
478 }
479 if (!TIFFWriteDirectory(tif))
480 {
481 fprintf(stderr, "Can't overwrite directory to %s. Testline %d\n",
482 filename, __LINE__);
483 goto failure;
484 }
485
486 /* Re-read IFD and check its offset, which should not have been changed at
487 * this point, except for strile_array_writing because then the stripoffsets
488 * are now written within IFD space. However, for only one IFD (numIFDs=1)
489 * the strile array data is written straight behind the IFD and thus can be
490 * overwritten.
491 * Furthermore, strile_array size of <= 9 (<=17 for BigTIFF) can also be
492 * written within the IFD space, because 9 (17 for BigTIFF) byte are gained
493 * by downsizing the XMLPACKET. */
494 TIFFSetDirectory(tif, 0);
495 auxUint64 = TIFFCurrentDirOffset(tif);
496 if (!is_strile_array_writing && offsetBase[0] != auxUint64)
497 {
498 fprintf(stderr,
499 "Failure: Directory 0 offset has changed from 0x%" PRIx64
500 " (%" PRIu64 ") to 0x %" PRIx64 " (%" PRIu64
501 "). Testline %d\n ",
502 offsetBase[0], offsetBase[0], auxUint64, auxUint64, __LINE__);
503 goto failure;
504 }
505 else if (is_strile_array_writing && !tiled)
506 {
507 if (numIFDs > 1 && TIFFNumberOfStrips(tif) > 2)
508 {
509 if (offsetBase[0] == auxUint64)
510 {
511 fprintf(stderr,
512 "Failure with strile array writing: Directory 0 offset "
513 "has now "
514 "NOT changed from 0x%" PRIx64 " (%" PRIu64
515 "). Testline %d\n ",
516 offsetBase[0], offsetBase[0], __LINE__);
517 goto failure;
518 }
519 }
520 else if (offsetBase[0] != auxUint64)
521 {
522 fprintf(stderr,
523 "Failure with strile array writing: Directory 0 offset has "
524 "changed from 0x%" PRIx64 " (%" PRIu64 ") to 0x %" PRIx64
525 " (%" PRIu64 "). Testline %d\n ",
526 offsetBase[0], offsetBase[0], auxUint64, auxUint64,
527 __LINE__);
528 goto failure;
529 }
530 }
531 else if (is_strile_array_writing && tiled)
532 {
533 if (numIFDs > 1 && TIFFNumberOfTiles(tif) > 2)
534 {
535 if (offsetBase[0] == auxUint64)
536 {
537 fprintf(stderr,
538 "Failure with tiled strile array writing: Directory 0 "
539 "offset has now NOT changed from 0x%" PRIx64
540 " (%" PRIu64 "). Testline %d\n ",
541 offsetBase[0], offsetBase[0], __LINE__);
542 goto failure;
543 }
544 }
545 else if (offsetBase[0] != auxUint64)
546 {
547 fprintf(stderr,
548 "Failure with tiled strile array writing: Directory 0 "
549 "offset has "
550 "changed from 0x%" PRIx64 " (%" PRIu64 ") to 0x %" PRIx64
551 " (%" PRIu64 "). Testline %d\n ",
552 offsetBase[0], offsetBase[0], auxUint64, auxUint64,
553 __LINE__);
554 goto failure;
555 }
556 }
557
558 /*-- Enlarge IFD 0 with enlarged tag data and write it again. --*/
559 char xmldata1[] = "XML data package";
560 xmlpacketLength = (uint32_t)strlen(xmldata1);
561
562 if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, xmlpacketLength, xmldata1))
563 {
564 fprintf(stderr, "Can't set TIFFTAG_XMLPACKET tag. Testline %d\n",
565 __LINE__);
566 goto failure;
567 }
568 if (!TIFFWriteDirectory(tif))
569 {
570 fprintf(stderr, "Can't re-write directory 0 to %s. Testline %d\n",
571 filename, __LINE__);
572 goto failure;
573 }
574
575 /* Check IFD 0 offset, which should have been changed.
576 * (Because of unused bytes before EOF generated by downsizing of XMLPACKET
577 * before enlarging IFD will be re-written even for numIFDs==1) */
578 if (!TIFFSetDirectory(tif, 0))
579 {
580 fprintf(stderr, "Can't set directory 0 of %s. Testline %d\n", filename,
581 __LINE__);
582 goto failure;
583 }
584 offsetNew[0] = TIFFCurrentDirOffset(tif);
585 if (offsetBase[0] == offsetNew[0])
586 {
587 fprintf(
588 stderr,
589 "Failure: Directory 0 offset has now NOT changed from 0x%" PRIx64
590 " (%" PRIu64 ").Testline %d\n ",
591 offsetNew[0], offsetNew[0], __LINE__);
592 goto failure;
593 }
594
595 if (numIFDs > 1)
596 {
597 /*-- Read next directory IFD=1 and re-write that. --*/
598 if (!TIFFReadDirectory(tif))
599 {
600 fprintf(stderr, "Can't read directory from %s. Testline %d\n",
601 filename, __LINE__);
602 goto failure;
603 }
604 /* Enlarge IFD 1 by adding new tag. */
605 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION,
606 "WGS 84 / UTM zone 1N|WGS 84|"))
607 {
608 fprintf(stderr,
609 "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
610 __LINE__);
611 goto failure;
612 }
613 auxUint64 = TIFFCurrentDirOffset(tif);
614 if (!TIFFWriteDirectory(tif))
615 {
616 fprintf(stderr, "Can't re-write directory 1 to %s. Testline %d\n",
617 filename, __LINE__);
618 goto failure;
619 }
620 TIFFSetDirectory(tif, 1);
621 offsetNew[1] = TIFFCurrentDirOffset(tif);
622 if (auxUint64 == offsetNew[1])
623 {
624 fprintf(stderr,
625 "Failure: Directory 1 offset has now NOT changed from "
626 "0x%" PRIx64 " (%" PRIu64 ").Testline %d\n ",
627 offsetNew[1], offsetNew[1], __LINE__);
628 goto failure;
629 }
630 }
631
632 /* Use TIFFSetSubDirectory() for old IFD 0 and just overwrite it.
633 * Just overwriting invalid directory works. For strile array writing it
634 * shall fail, because now the striles are tried to be written within the
635 * IFD space. However, for only one IFD (numIFDs=1) the strile array data is
636 * written straight behind the IFD and thus can be overwritten.
637 * The same is true for tiled images, however if the number of tiles
638 * is one (TIFFNumberOfTiles==1), the tile offset fits into the tag entry
639 * and thus can also be overwritten.
640 */
641 if (!TIFFSetSubDirectory(tif, offsetBase[0]))
642 {
643 fprintf(stderr,
644 "Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
645 ") within %s. Testline %d\n",
646 offsetBase[0], offsetBase[0], filename, __LINE__);
647 goto failure;
648 }
649 if (!tiled)
650 {
651 if (!is_strile_array_writing || numIFDs <= 1)
652 {
653 if (!TIFFWriteDirectory(
654 tif)) /* just overwriting invalid directory works!!*/
655 {
656 fprintf(stderr,
657 "Can't overwrite directory to %s. Testline %d\n",
658 filename, __LINE__);
659 goto failure;
660 }
661 }
662 else
663 {
664 /* Because XMLPACKET is 9 byte reduced, small strilearrays can be
665 * overwritten. */
666 if (TIFFNumberOfStrips(tif) > 2)
667 /* Suppress error message from 'TIFFRewriteDirectory()' like
668 * 'Error fetching directory link' or 'Sanity check...'. */
669 errHandler = TIFFSetErrorHandler(NULL);
670 if (TIFFWriteDirectory(tif) && TIFFNumberOfStrips(tif) > 2)
671 {
672 fprintf(stderr,
673 "Was expected to fail but succeeded. %s. Testline %d\n",
674 filename, __LINE__);
675 goto failure;
676 }
677 if (TIFFNumberOfStrips(tif) > 2)
678 TIFFSetErrorHandler(errHandler);
679 }
680 }
681 else
682 {
683 if (!is_strile_array_writing || numIFDs <= 1 ||
684 TIFFNumberOfTiles(tif) <= 1)
685 {
686 if (!TIFFWriteDirectory(
687 tif)) /* just overwriting invalid directory works!!*/
688 {
689 fprintf(stderr,
690 "Can't overwrite directory to %s. Testline %d\n",
691 filename, __LINE__);
692 goto failure;
693 }
694 }
695 else
696 {
697 /* Because XMLPACKET is 9 byte reduced, small strilearrays can be
698 * overwritten. */
699 if (TIFFNumberOfTiles(tif) > 2)
700 /* Suppress error message from 'TIFFRewriteDirectory()' like
701 * 'Error fetching directory link' or 'Sanity check...'. */
702 errHandler = TIFFSetErrorHandler(NULL);
703 if (TIFFWriteDirectory(tif) && TIFFNumberOfTiles(tif) > 2)
704 {
705 fprintf(stderr,
706 "Was expected to fail but succeeded. %s. Testline %d\n",
707 filename, __LINE__);
708 goto failure;
709 }
710 if (TIFFNumberOfTiles(tif) > 2)
711 TIFFSetErrorHandler(errHandler);
712 }
713 }
714
715 /* Re-read the already re-written and thus invalidated IFD 0 with
716 * TIFFSetSubDirectory() and try to re-write it.
717 * Re-writing an invalid IFD should fail. */
718 if (!TIFFSetSubDirectory(tif, offsetBase[0]))
719 {
720 fprintf(stderr,
721 "Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
722 ") within %s. Testline %d\n",
723 offsetBase[0], offsetBase[0], filename, __LINE__);
724 goto failure;
725 }
726 if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, (uint32_t)strlen(xmldata1),
727 xmldata1))
728 {
729 fprintf(stderr, "Can't set TIFFTAG_XMLPACKET tag. Testline %d\n",
730 __LINE__);
731 goto failure;
732 }
733 /* Suppress error message from 'TIFFRewriteDirectory()' like
734 * 'Error fetching directory link' or 'Sanity check...'. */
735 errHandler = TIFFSetErrorHandler(NULL);
736 if (TIFFWriteDirectory(tif))
737 {
738 fprintf(stderr, "Was expected to fail but succeeded. %s. Testline %d\n",
739 filename, __LINE__);
740 goto failure;
741 }
742 TIFFSetErrorHandler(errHandler);
743
744 /*-- Now with a valid IFD: Re-read IFD with TIFFSetSubDirectory() and
745 * re-write it. If it is at the end of the file, it will be overwritten.
746 * This is the case for numIFD==1. */
747 if (!TIFFSetSubDirectory(tif, offsetNew[0]))
748 {
749 fprintf(stderr,
750 "Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
751 ") within %s. Testline %d\n",
752 offsetNew[0], offsetNew[0], filename, __LINE__);
753 goto failure;
754 }
755 char xmldata2[] = "XML data package even longer";
756 if (!TIFFSetField(tif, TIFFTAG_XMLPACKET, (uint32_t)strlen(xmldata2),
757 xmldata2))
758 {
759 fprintf(stderr, "Can't set TIFFTAG_XMLPACKET tag.. Testline %d\n",
760 __LINE__);
761 goto failure;
762 }
763 auxUint64 = TIFFCurrentDirOffset(tif);
764 if (!TIFFWriteDirectory(tif))
765 {
766 fprintf(stderr, "Can't re-write directory 0 to %s. Testline %d\n",
767 filename, __LINE__);
768 goto failure;
769 }
770 TIFFSetDirectory(tif, 0);
771 offsetNew[0] = TIFFCurrentDirOffset(tif);
772 if (numIFDs > 1 && auxUint64 == offsetNew[0])
773 {
774 fprintf(stderr,
775 "Failure: Directory %d offset has NOT changed from 0x%" PRIx64
776 " (%" PRIu64 ").Testline %d\n ",
777 0, offsetNew[0], offsetNew[0], __LINE__);
778 goto failure;
779 }
780
781 /*-- Make a little check. --*/
782 /* Reopen prepared testfile */
783 TIFFClose(tif);
784 tif = TIFFOpen(filename, "r+");
785 if (!tif)
786 {
787 fprintf(stderr, "Can't open %s\n", filename);
788 goto failure;
789 }
790 for (int i = 0; i < numIFDs; i++)
791 {
792
793 if (!TIFFSetDirectory(tif, i))
794 {
795 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", i,
796 filename, __LINE__);
797 goto failure;
798 }
799 if (i < 2)
800 {
801 auxUint64 = TIFFCurrentDirOffset(tif);
802 if (auxUint64 != offsetNew[i])
803 {
804 fprintf(
805 stderr,
806 "Failure: Directory 0 offset has changed from 0x%" PRIx64
807 " (%" PRIu64 ") to 0x %" PRIx64 " (%" PRIu64
808 ").Testline %d\n ",
809 offsetNew[i], offsetNew[i], auxUint64, auxUint64, __LINE__);
810 goto failure;
811 }
812 }
813 if (!is_requested_directory(tif, i, filename))
814 {
815 goto failure;
816 }
817 /* Check last image line or last tile. */
818 tmsize_t bytesRead;
819 if (tiled)
820 {
821 uint32_t numtiles = TIFFNumberOfTiles(tif);
822 uint64_t tileSize = TIFFTileSize64(tif);
823 if (tileSize > sizeof(bufLine[NUMIFDsMAX]))
824 {
825 fprintf(
826 stderr,
827 "Failure: Read buffer for tile too small.Testline %d\n ",
828 __LINE__);
829 goto failure;
830 }
831 bytesRead = TIFFReadEncodedTile(
832 tif, numtiles - 1, &bufLine[NUMIFDsMAX][0], sizeof(bufLine[0]));
833 }
834 else
835 {
836 uint64_t scanlineSize = TIFFScanlineSize64(tif);
837 if (scanlineSize > sizeof(bufLine[NUMIFDsMAX]))
838 {
839 fprintf(stderr,
840 "Failure: Read buffer for scanline too small.Testline "
841 "%d\n ",
842 __LINE__);
843 goto failure;
844 }
845 bytesRead =
846 TIFFReadScanline(tif, &bufLine[NUMIFDsMAX][0], length - 1, 0);
847 }
848 if (bytesRead > 0)
849 {
850 for (int k = 0; k < bytesRead; k++)
851 {
852 if (bufLine[NUMIFDsMAX][k] != bufLine[i][k])
853 {
854 fprintf(
855 stderr,
856 "Failure: Read IFD %d last line pixel %d value %d is "
857 "different to written value %d .Testline %d\n ",
858 i, k, bufLine[i][k], bufLine[NUMIFDsMAX][k], __LINE__);
859 goto failure;
860 }
861 }
862 }
863 else
864 {
865 fprintf(stderr,
866 "Failure: Reading IFD %d last line %d .Testline %d\n ", i,
867 length - 1, __LINE__);
868 goto failure;
869 }
870 }
871
872 TIFFClose(tif);
873 unlink(filename);
874 return 0;
875
876 failure:
877 if (tif)
878 {
879 TIFFClose(tif);
880 }
881 return 1;
882 } /*-- test_IFD_enlargement --*/
883
884 /* Width and length for the next test functions can be adapted here. */
885 #define WIDTH 1
886 #define LENGTH 2
887
888 /* Checks that Custom-IFDs like EXIF-IFD in file can be overwritten if they have
889 * not grown and were re-written to an other location if IFD size has been
890 * grown.
891 */
892 int test_EXIF_enlargement(const char *filename, bool is_big_tiff)
893 {
894 char auxString[128];
895 uint64_t offsetEXIFBase[2];
896 uint64_t offsetEXIFNew[2];
897
898 /*-- Create a file and write two EXIF-IFDs directly to it --*/
899 TIFF *tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
900 if (!tif)
901 {
902 fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
903 return 1;
904 }
905
906 /* Write baseline test image. */
907 for (int i = 0; i < 2; i++)
908 {
909 /* Writing baseline images (IFDs) and EXIF-IFDs to file. */
910 if (write_data_to_current_directory(tif, WIDTH, LENGTH, FALSE, i, true,
911 NULL, 0))
912 {
913 fprintf(
914 stderr,
915 "Can't write data to current directory in %s. Testline %d\n",
916 filename, __LINE__);
917 goto failure;
918 }
919 /* Set EXIFIFD tag with dummy value to reserve space in IFD. */
920 if (!TIFFSetField(tif, TIFFTAG_EXIFIFD, (uint64_t)0))
921 {
922 fprintf(stderr, "Can't set TIFFTAG_EXIFIFD tag. Testline %d\n",
923 __LINE__);
924 goto failure;
925 }
926 if (!TIFFWriteDirectory(tif))
927 {
928 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
929 filename, __LINE__);
930 goto failure;
931 }
932
933 if (write_EXIF_data_to_current_directory(tif, i, &offsetEXIFBase[i]))
934 {
935 fprintf(stderr,
936 "Can't write EXIF data to current directory in %s. "
937 "Testline %d\n",
938 filename, __LINE__);
939 goto failure;
940 }
941 /* Go back to current main-IFD and update EXIF-IFD offset. */
942 if (!TIFFSetDirectory(tif, i))
943 {
944 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", i,
945 filename, __LINE__);
946 goto failure;
947 }
948 if (!TIFFSetField(tif, TIFFTAG_EXIFIFD, offsetEXIFBase[i]))
949 {
950 fprintf(stderr, "Can't set TIFFTAG_EXIFIFD tag. Testline %d\n",
951 __LINE__);
952 goto failure;
953 }
954 if (!TIFFWriteDirectory(tif))
955 {
956 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
957 filename, __LINE__);
958 goto failure;
959 }
960 }
961
962 /* Test over-writing the EXIF directory without change or with less bytes.
963 */
964 int retCode;
965 char *pAscii0;
966 char *pAscii1;
967 if (!TIFFReadEXIFDirectory(tif, offsetEXIFBase[0]))
968 {
969 fprintf(stderr,
970 "Can't read EXIF directory at offset 0x%" PRIx64 " (%" PRIu64
971 ") within %s. Testline %d\n",
972 offsetEXIFBase[0], offsetEXIFBase[0], filename, __LINE__);
973 goto failure;
974 }
975
976 retCode = TIFFGetField(tif, EXIFTAG_EXIFVERSION, &pAscii0);
977 retCode += TIFFGetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, &pAscii1);
978 if (retCode != 2)
979 {
980 fprintf(stderr, "Can't read EXIF tags. Testline %d\n", __LINE__);
981 return 1;
982 }
983
984 /* Use shorter string thus EXIF-IFD should be overwritten. */
985 sprintf(auxString, "EXIF-%d-XX", 0);
986 if (!TIFFSetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, auxString))
987 {
988 fprintf(stderr,
989 "Can't set EXIFTAG_SUBSECTIMEORIGINAL tag. Testline %d\n",
990 __LINE__);
991 return 1;
992 }
993 if (!TIFFWriteCustomDirectory(tif, &offsetEXIFNew[0]))
994 {
995 fprintf(stderr,
996 "TIFFWriteCustomDirectory() with EXIF failed. Testline %d\n",
997 __LINE__);
998 return 1;
999 }
1000 /* Check for overwriting. */
1001 if (offsetEXIFBase[0] != offsetEXIFNew[0])
1002 {
1003 fprintf(stderr,
1004 "Failure: EXIF IFD 0 offset has changed from 0x%" PRIx64
1005 " (%" PRIu64 ") to 0x %" PRIx64 " (%" PRIu64 ").Testline %d\n ",
1006 offsetEXIFBase[0], offsetEXIFBase[0], offsetEXIFNew[0],
1007 offsetEXIFNew[0], __LINE__);
1008 goto failure;
1009 }
1010 else
1011 {
1012 /*EXIF has been overwritten. Check that. */
1013 if (!TIFFReadEXIFDirectory(tif, offsetEXIFNew[0]))
1014 {
1015 fprintf(stderr,
1016 "Can't read EXIF directory at offset 0x%" PRIx64
1017 " (%" PRIu64 ") within %s. Testline %d\n",
1018 offsetEXIFBase[0], offsetEXIFBase[0], filename, __LINE__);
1019 goto failure;
1020 }
1021 if (!TIFFGetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, &pAscii1))
1022 {
1023 fprintf(stderr,
1024 "Can't read tag EXIFTAG_SUBSECTIMEORIGINAL of %s. Testline "
1025 "%d\n",
1026 filename, __LINE__);
1027 goto failure;
1028 }
1029 if (strcmp(pAscii1, auxString))
1030 {
1031 fprintf(stderr,
1032 "Read contents of EXIFTAG_SUBSECTIMEORIGINAL %s is not as "
1033 "expected %s of %s. Testline "
1034 "%d\n",
1035 pAscii1, auxString, filename, __LINE__);
1036 goto failure;
1037 }
1038 }
1039
1040 /* Enlarging and re-writing EXIF IFD 1. Since EXIF IFD 1 is at EOF,
1041 * even an enlargement should overwrite it. */
1042 if (!TIFFReadEXIFDirectory(tif, offsetEXIFBase[1]))
1043 {
1044 fprintf(stderr,
1045 "Can't read EXIF directory 1 at offset 0x%" PRIx64 " (%" PRIu64
1046 ") within %s. Testline %d\n",
1047 offsetEXIFBase[1], offsetEXIFBase[1], filename, __LINE__);
1048 goto failure;
1049 }
1050
1051 retCode = TIFFGetField(tif, EXIFTAG_EXIFVERSION, &pAscii0);
1052 retCode += TIFFGetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, &pAscii1);
1053 if (retCode != 2)
1054 {
1055 fprintf(stderr, "Can't read EXIF tags. Testline %d\n", __LINE__);
1056 return 1;
1057 }
1058
1059 /* Enlarge EXIF IFD 1 with additional tag to provoke re-writing. */
1060 if (!TIFFSetField(tif, EXIFTAG_FLASH, 44))
1061 {
1062 fprintf(stderr, "Can't set EXIFTAG_FLASH tag. Testline %d\n", __LINE__);
1063 goto failure;
1064 }
1065 if (!TIFFWriteCustomDirectory(tif, &offsetEXIFNew[1]))
1066 {
1067 fprintf(stderr,
1068 "TIFFWriteCustomDirectory() with EXIF failed. Testline %d\n",
1069 __LINE__);
1070 goto failure;
1071 }
1072 /* Check: EXIF IFD 1 should be overwritten because should be at EOF. */
1073 if (offsetEXIFBase[1] != offsetEXIFNew[1])
1074 {
1075 fprintf(stderr,
1076 "Failure: EXIF IFD 1 offset has changed from 0x%" PRIx64
1077 " (%" PRIu64 ") to 0x %" PRIx64 " (%" PRIu64 ").Testline %d\n ",
1078 offsetEXIFBase[1], offsetEXIFBase[1], offsetEXIFNew[1],
1079 offsetEXIFNew[1], __LINE__);
1080 goto failure;
1081 }
1082
1083 /* Now enlarge and re-write EXIF IFD 0. */
1084 if (!TIFFReadEXIFDirectory(tif, offsetEXIFBase[0]))
1085 {
1086 fprintf(stderr,
1087 "Can't read EXIF directory at offset 0x%" PRIx64 " (%" PRIu64
1088 ") within %s. Testline %d\n",
1089 offsetEXIFBase[0], offsetEXIFBase[0], filename, __LINE__);
1090 goto failure;
1091 }
1092 /* Enlarge EXIF IFD 0 with additional tag to provoke re-writing. */
1093 if (!TIFFSetField(tif, EXIFTAG_SPECTRALSENSITIVITY,
1094 "EXIF-0-enlargement-EXIF"))
1095 {
1096 fprintf(stderr,
1097 "Can't set EXIFTAG_SPECTRALSENSITIVITY tag. Testline %d\n",
1098 __LINE__);
1099 goto failure;
1100 }
1101 if (!TIFFWriteCustomDirectory(tif, &offsetEXIFNew[0]))
1102 {
1103 fprintf(stderr,
1104 "TIFFWriteCustomDirectory() with EXIF failed. Testline %d\n",
1105 __LINE__);
1106 goto failure;
1107 }
1108
1109 /* Check: EXIF-IFD 0 shall now be re-written. */
1110 if (offsetEXIFBase[0] == offsetEXIFNew[0])
1111 {
1112 fprintf(stderr,
1113 "Failure: EXIF IFD 0 offset has NOT changed from 0x%" PRIx64
1114 " (%" PRIu64 "). Testline %d\n ",
1115 offsetEXIFBase[0], offsetEXIFBase[0], __LINE__);
1116 goto failure;
1117 }
1118 else
1119 {
1120 /* EXIF-IFD has been re-written, thus offset in main-IFD needs
1121 * update. */
1122 if (!TIFFSetDirectory(tif, 1))
1123 {
1124 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", 1,
1125 filename, __LINE__);
1126 goto failure;
1127 }
1128 if (!TIFFSetField(tif, TIFFTAG_EXIFIFD, offsetEXIFNew[0]))
1129 {
1130 fprintf(stderr, "Can't set TIFFTAG_EXIFIFD tag. Testline %d\n",
1131 __LINE__);
1132 goto failure;
1133 }
1134 if (!TIFFWriteDirectory(tif))
1135 {
1136 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
1137 filename, __LINE__);
1138 goto failure;
1139 }
1140 }
1141
1142 /*-- Make a little check. --*/
1143 if (!TIFFSetDirectory(tif, 1))
1144 {
1145 fprintf(stderr, "Can't set directory 1 of %s. Testline %d\n", filename,
1146 __LINE__);
1147 goto failure;
1148 }
1149 if (!is_requested_directory(tif, 1, filename))
1150 {
1151 goto failure;
1152 }
1153 if (!TIFFSetDirectory(tif, 0))
1154 {
1155 fprintf(stderr, "Can't set directory 0 of %s. Line %d\n", filename,
1156 __LINE__);
1157 goto failure;
1158 }
1159 if (!is_requested_directory(tif, 0, filename))
1160 {
1161 goto failure;
1162 }
1163
1164 TIFFClose(tif);
1165 unlink(filename);
1166 return 0;
1167
1168 failure:
1169 if (tif)
1170 {
1171 TIFFClose(tif);
1172 }
1173 return 1;
1174 } /*-- test_EXIF_enlargement --*/
1175
1176 /* Checks that SubIFDs on file can be overwritten if they have not grown.
1177 * But SubIFDs cannot be re-written to another location in the file,
1178 * because TIFFWriteDirectory() cannot determine from which main-IFD the SubIFD
1179 * offset was taken for TIFFSetSubDirectory(). This shall result in an error
1180 * return of TIFFWriteDirectory().
1181 */
1182 int test_SubIFD_enlargement(const char *filename, bool is_big_tiff)
1183 {
1184
1185 /* Define the number of sub-IFDs you are going to write */
1186 #define NUMBER_OF_DIRS 2
1187 #define NUMBER_OF_SUBIFDs 3
1188 uint16_t number_of_sub_IFDs = NUMBER_OF_SUBIFDs;
1189 toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = {
1190 0UL}; /* array for SubIFD tag */
1191
1192 int i;
1193 int blnWriteSubIFD = 0;
1194 int iIFD = 0, iSubIFD = 0;
1195 TIFFErrorHandler errHandler = NULL;
1196
1197 uint64_t offsetBase[NUMBER_OF_DIRS];
1198
1199 /*-- Create a file and write NUMBER_OF_DIRS IFDs with
1200 * NUMBER_OF_SUBIFDs SubIFDs to this file. --*/
1201 TIFF *tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
1202 if (!tif)
1203 {
1204 fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
1205 return 1;
1206 }
1207
1208 for (i = 0; i < NUMBER_OF_DIRS + NUMBER_OF_SUBIFDs; i++)
1209 {
1210 if (write_data_to_current_directory(
1211 tif, WIDTH, LENGTH, FALSE,
1212 blnWriteSubIFD ? 200 + iSubIFD++ : iIFD++, true, NULL, 0))
1213 {
1214 fprintf(
1215 stderr,
1216 "Can't write data to current directory in %s. Testline %d\n",
1217 filename, __LINE__);
1218 goto failure;
1219 }
1220 if (blnWriteSubIFD)
1221 {
1222 /* SUBFILETYPE tag is not mandatory for SubIFD writing, but a
1223 * good idea to indicate thumbnails */
1224 if (!TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE))
1225 goto failure;
1226 }
1227
1228 /* For the first multi-page image, trigger TIFFWriteDirectory() to
1229 * switch for the next number_of_sub_IFDs calls to add those as SubIFDs
1230 * e.g. for thumbnails */
1231 if (0 == i)
1232 {
1233 blnWriteSubIFD = 1;
1234 /* Now here is the trick: the next n directories written
1235 * will be sub-IFDs of the main-IFD (where n is number_of_sub_IFDs
1236 * specified when you set the TIFFTAG_SUBIFD field.
1237 * The SubIFD offset array sub_IFDs_offsets is filled automatically
1238 * with the proper offset values by the following number_of_sub_IFDs
1239 * TIFFWriteDirectory() calls and are updated in the related
1240 * main-IFD with the last call.
1241 */
1242 if (!TIFFSetField(tif, TIFFTAG_SUBIFD, number_of_sub_IFDs,
1243 sub_IFDs_offsets))
1244 goto failure;
1245 }
1246 if (!TIFFWriteDirectory(tif))
1247 {
1248 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
1249 filename, __LINE__);
1250 goto failure;
1251 }
1252 if (iSubIFD >= number_of_sub_IFDs)
1253 blnWriteSubIFD = 0;
1254 }
1255 TIFFClose(tif);
1256
1257 /* Reopen prepared testfile */
1258 tif = TIFFOpen(filename, "r+");
1259 if (!tif)
1260 {
1261 fprintf(stderr, "Can't create %s\n", filename);
1262 goto failure;
1263 }
1264
1265 tdir_t numberOfMainIFDs = TIFFNumberOfDirectories(tif);
1266 if (numberOfMainIFDs !=
1267 (tdir_t)(NUMBER_OF_DIRS + NUMBER_OF_SUBIFDs) - number_of_sub_IFDs)
1268 {
1269 fprintf(stderr,
1270 "Unexpected number of directories in %s. Expected %i, "
1271 "found %i.\n",
1272 filename, NUMBER_OF_DIRS - number_of_sub_IFDs,
1273 numberOfMainIFDs);
1274 goto failure;
1275 }
1276
1277 /* Retrieve IFD offset values for later checking. */
1278 TIFFSetDirectory(tif, 1);
1279 offsetBase[1] = TIFFCurrentDirOffset(tif);
1280 TIFFSetDirectory(tif, 0);
1281 offsetBase[0] = TIFFCurrentDirOffset(tif);
1282
1283 /* The first directory is now read. */
1284 /* Check if there are SubIFD subfiles */
1285 void *ptr;
1286 if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr))
1287 {
1288 /* Copy SubIFD array from pointer */
1289 memcpy(sub_IFDs_offsets, ptr,
1290 number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0]));
1291 }
1292 /* Read first SubIFD directory */
1293 if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[0]))
1294 goto failure;
1295 if (!is_requested_directory(tif, 200 + 0, filename))
1296 {
1297 goto failure;
1298 }
1299 /* Just overwrite SubIFD with smaller text. */
1300 if (!TIFFSetField(tif, TIFFTAG_PAGENAME, "99 th. X"))
1301 {
1302 fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag. Testline %d\n",
1303 __LINE__);
1304 goto failure;
1305 }
1306 if (!TIFFWriteDirectory(tif))
1307 {
1308 fprintf(stderr, "Can't write directory to %s. Testline %d\n", filename,
1309 __LINE__);
1310 goto failure;
1311 }
1312
1313 /* Enlarging and re-writing SubIFD 1 shall fail.
1314 * TIFFWriteDirectory() cannot determine from which main-IFD the SubIFD
1315 * offset was taken for TIFFSetSubDirectory().
1316 * Therefore, SubIFD can be overwritten but cannot be re-written to another
1317 * location in the file.
1318 */
1319 if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[1]))
1320 goto failure;
1321 if (!TIFFSetField(tif, TIFFTAG_PAGENAME,
1322 "Long Pagename to trigger re-writing"))
1323 {
1324 fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag. Testline %d\n",
1325 __LINE__);
1326 goto failure;
1327 }
1328 /* Suppress error message from 'TIFFRewriteDirectory()' like
1329 * 'Error fetching directory link' or 'Sanity check...'. */
1330 errHandler = TIFFSetErrorHandler(NULL);
1331 if (TIFFWriteDirectory(tif))
1332 {
1333 fprintf(stderr, "Was expected to fail but succeeded. %s. Testline %d\n",
1334 filename, __LINE__);
1335 goto failure;
1336 }
1337 TIFFSetErrorHandler(errHandler);
1338
1339 /*-- Make a little check. --*/
1340 if (!TIFFSetDirectory(tif, 1))
1341 {
1342 fprintf(stderr, "Can't set directory 1 of %s. Testline %d\n", filename,
1343 __LINE__);
1344 goto failure;
1345 }
1346 if (!is_requested_directory(tif, 1, filename))
1347 {
1348 goto failure;
1349 }
1350 if (offsetBase[1] != TIFFCurrentDirOffset(tif))
1351 {
1352 fprintf(stderr, "IFD 1 offset has moved. %s. Line %d\n", filename,
1353 __LINE__);
1354 goto failure;
1355 }
1356
1357 if (!TIFFSetDirectory(tif, 0))
1358 {
1359 fprintf(stderr, "Can't set directory 0 of %s. Line %d\n", filename,
1360 __LINE__);
1361 goto failure;
1362 }
1363 if (!is_requested_directory(tif, 0, filename))
1364 {
1365 goto failure;
1366 }
1367 if (offsetBase[0] != TIFFCurrentDirOffset(tif))
1368 {
1369 fprintf(stderr, "IFD 0 offset has moved. %s. Line %d\n", filename,
1370 __LINE__);
1371 goto failure;
1372 }
1373
1374 /* Check SubIFDs */
1375 for (i = 0; i < NUMBER_OF_SUBIFDs; i++)
1376 {
1377 if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i]))
1378 goto failure;
1379 int k = (i == 0 ? 99 : 200 + i);
1380 if (!is_requested_directory(tif, k, filename))
1381 {
1382 fprintf(stderr, "Can't read SubIFD %d of %s correctly. Line %d\n",
1383 k, filename, __LINE__);
1384 goto failure;
1385 }
1386 }
1387
1388 TIFFClose(tif);
1389 unlink(filename);
1390 return 0;
1391
1392 failure:
1393 if (tif)
1394 {
1395 TIFFClose(tif);
1396 }
1397 return 1;
1398 } /*-- test_SubIFD_enlargement --*/
1399
1400 /* Checks that sequences that use TIFFCheckpointDirectory() to store the current
1401 * IFD in a file overwrite it if it has not grown, and write it to another
1402 * location if the IFD size has grown.
1403 */
1404 int test_CheckpointDirectory(const char *filename, bool is_big_tiff)
1405 {
1406
1407 uint64_t auxUint64;
1408 uint64_t offsetBase[2];
1409 uint64_t offsetNew[2];
1410 char auxString[128];
1411
1412 /*-- Create a file and write two IFDs directly to it --*/
1413 TIFF *tif = TIFFOpen(filename, is_big_tiff ? "w8" : "w");
1414 if (!tif)
1415 {
1416 fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
1417 return 1;
1418 }
1419
1420 for (int i = 0; i < 2; i++)
1421 {
1422 /* Writing baseline images (IFDs) to file. */
1423 if (write_data_to_current_directory(tif, WIDTH, LENGTH, FALSE, i, true,
1424 NULL, 0))
1425 {
1426 fprintf(
1427 stderr,
1428 "Can't write data to current directory in %s. Testline %d\n",
1429 filename, __LINE__);
1430 goto failure;
1431 }
1432 /* CheckpointDirectory testing */
1433 if (!TIFFCheckpointDirectory(tif))
1434 {
1435 fprintf(stderr,
1436 "Can't checkpoint directory %d of %s. Testline %d\n", i,
1437 filename, __LINE__);
1438 goto failure;
1439 }
1440 offsetBase[i] = offsetNew[i] = TIFFCurrentDirOffset(tif);
1441 if (i == 1)
1442 {
1443 /* Provoke checkpointed IFD to be re-written by TIFFWriteDirectory()
1444 * below. */
1445 sprintf(auxString, "%d th. IMG", i);
1446 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, auxString))
1447 {
1448 fprintf(stderr,
1449 "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
1450 __LINE__);
1451 goto failure;
1452 }
1453 }
1454 if (!TIFFWriteDirectory(tif))
1455 {
1456 fprintf(stderr, "Can't write directory to %s. Testline %d\n",
1457 filename, __LINE__);
1458 goto failure;
1459 }
1460 /* Check IFD offsets:
1461 * First IFD shall be overwritten (same offset) and second IFD shall be
1462 * re-written (different offset). */
1463 if (!TIFFSetDirectory(tif, i))
1464 {
1465 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", i,
1466 filename, __LINE__);
1467 goto failure;
1468 }
1469 auxUint64 = TIFFCurrentDirOffset(tif);
1470 if ((i == 0) && (auxUint64 != offsetBase[i]))
1471 {
1472 fprintf(stderr,
1473 "Checkpointed offset 0x%" PRIx64
1474 " is different to finally written offset 0x%" PRIx64
1475 " Testline %d\n",
1476 auxUint64, offsetBase[i], __LINE__);
1477 goto failure;
1478 }
1479 else if ((i == 1) && (auxUint64 == offsetBase[i]))
1480 {
1481 fprintf(stderr,
1482 "Checkpointed offset 0x%" PRIx64
1483 " is not different to finally written offset 0x%" PRIx64
1484 " Testline %d\n",
1485 auxUint64, offsetBase[i], __LINE__);
1486 goto failure;
1487 }
1488 /* Update offsetNew for later use. */
1489 offsetNew[i] = TIFFCurrentDirOffset(tif);
1490 /* Because of TIFFSetDirectory() prepare now new empty IFD for next
1491 * loop. */
1492 TIFFCreateDirectory(tif);
1493 }
1494 TIFFClose(tif);
1495
1496 /* Reopen prepared testfile */
1497 tif = TIFFOpen(filename, "r+");
1498 if (!tif)
1499 {
1500 fprintf(stderr, "Can't create %s\n", filename);
1501 goto failure;
1502 }
1503
1504 /* First IFD is read. Change something and checkpoint it several times. */
1505 /* Change IFD identification number string of PageName tag. */
1506 sprintf(auxString, "%d th. IFD", 9);
1507 if (!TIFFSetField(tif, TIFFTAG_PAGENAME, auxString))
1508 {
1509 fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag.\n");
1510 return 1;
1511 }
1512 if (!TIFFCheckpointDirectory(tif))
1513 {
1514 fprintf(stderr, "Can't checkpoint directory %d of %s. Testline %d\n", 0,
1515 filename, __LINE__);
1516 goto failure;
1517 }
1518 auxUint64 = TIFFCurrentDirOffset(tif);
1519 if (auxUint64 != offsetBase[0])
1520 {
1521 fprintf(stderr,
1522 "Checkpointed offset 0x%" PRIx64
1523 " is different to finally written offset 0x%" PRIx64
1524 " Testline %d\n",
1525 auxUint64, offsetBase[0], __LINE__);
1526 goto failure;
1527 }
1528 /* Provoke checkpointed IFD to be re-written due to additional tag in IFD 0.
1529 */
1530 sprintf(auxString, "%d th. IMG YYY", 9);
1531 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, auxString))
1532 {
1533 fprintf(stderr, "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
1534 __LINE__);
1535 goto failure;
1536 }
1537 if (!TIFFCheckpointDirectory(tif))
1538 {
1539 fprintf(stderr, "Can't checkpoint directory %d of %s. Testline %d\n", 0,
1540 filename, __LINE__);
1541 goto failure;
1542 }
1543 auxUint64 = TIFFCurrentDirOffset(tif);
1544 if (auxUint64 == offsetBase[0])
1545 {
1546 fprintf(stderr,
1547 "Checkpointed offset 0x%" PRIx64
1548 " is NOT different to finally written offset 0x%" PRIx64
1549 " Testline %d\n",
1550 auxUint64, offsetBase[0], __LINE__);
1551 goto failure;
1552 }
1553 offsetNew[0] = TIFFCurrentDirOffset(tif);
1554
1555 if (!TIFFWriteDirectory(tif))
1556 {
1557 fprintf(stderr, "Can't write directory to %s. Testline %d\n", filename,
1558 __LINE__);
1559 goto failure;
1560 }
1561
1562 /* Close and reopen prepared testfile */
1563 TIFFClose(tif);
1564 tif = TIFFOpen(filename, "r+");
1565 if (!tif)
1566 {
1567 fprintf(stderr, "Can't create %s\n", filename);
1568 goto failure;
1569 }
1570
1571 /*-- Make a little check. --*/
1572 if (!TIFFSetDirectory(tif, 1))
1573 {
1574 fprintf(stderr, "Can't set directory 1 of %s. Testline %d\n", filename,
1575 __LINE__);
1576 goto failure;
1577 }
1578 if (!is_requested_directory(tif, 1, filename))
1579 {
1580 goto failure;
1581 }
1582 if (!TIFFSetDirectory(tif, 0))
1583 {
1584 fprintf(stderr, "Can't set directory 0 of %s. Line %d\n", filename,
1585 __LINE__);
1586 goto failure;
1587 }
1588 if (!is_requested_directory(tif, 9, filename))
1589 {
1590 goto failure;
1591 }
1592
1593 TIFFClose(tif);
1594 unlink(filename);
1595 return 0;
1596
1597 failure:
1598 if (tif)
1599 {
1600 TIFFClose(tif);
1601 }
1602 return 1;
1603 } /*-- test_CheckpointDirectory --*/
1604
1605 /* LibTIFF increments IFD write-pointer always to even addresses, thus leaving 1
1606 * byte gaps in the data space for data outside IFD entries. This routine
1607 * checks correct handling for that.
1608 */
1609 int test_OddDataSizes(const char *filename, unsigned int openMode)
1610 {
1611
1612 uint64_t auxUint64;
1613 uint64_t offsetBase;
1614
1615 assert(openMode < (sizeof(modeStrings) / sizeof(modeStrings[0])));
1616
1617 /*-- Create a file and write IFDs directly to it --*/
1618 TIFF *tif = TIFFOpen(filename, modeStrings[openMode]);
1619 if (!tif)
1620 {
1621 fprintf(stderr, "Can't create %s. Testline %d\n", filename, __LINE__);
1622 return 1;
1623 }
1624
1625 /* Write 15 byte field (14 byte + zero).
1626 * Set it before other tags, otherwise it will be directly at EOF,
1627 * because custom tag data are written in the sequence they are set.
1628 * At EOF no gap will be inserted!*/
1629 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "123456789ABCDE"))
1630 {
1631 fprintf(stderr, "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
1632 __LINE__);
1633 goto failure;
1634 }
1635 /* Writing baseline images (IFDs) to file. */
1636 if (write_data_to_current_directory(tif, WIDTH, LENGTH, FALSE, 0, TRUE,
1637 NULL, 0))
1638 {
1639 fprintf(stderr,
1640 "Can't write data to current directory in %s. Testline %d\n",
1641 filename, __LINE__);
1642 goto failure;
1643 }
1644 if (!TIFFWriteDirectory(tif))
1645 {
1646 fprintf(stderr, "Can't write directory to %s. Testline %d\n", filename,
1647 __LINE__);
1648 goto failure;
1649 }
1650 /* Now write some data so that IFD 0 ends not at EOF. */
1651 if (write_data_to_current_directory(tif, WIDTH, LENGTH, FALSE, 1, TRUE,
1652 NULL, 0))
1653 {
1654 fprintf(stderr,
1655 "Can't write data to current directory in %s. Testline %d\n",
1656 filename, __LINE__);
1657 goto failure;
1658 }
1659
1660 /* Now start odd / even data size testing with IFD 0 ...*/
1661 if (!TIFFSetDirectory(tif, 0))
1662 {
1663 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", 0,
1664 filename, __LINE__);
1665 goto failure;
1666 }
1667 offsetBase = TIFFCurrentDirOffset(tif);
1668
1669 /* Write now 16 byte field (15 byte + zero), which shall be overwritten,
1670 * because 1 byte "gap" could be used. */
1671 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "123456789ABCDEF"))
1672 {
1673 fprintf(stderr, "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
1674 __LINE__);
1675 goto failure;
1676 }
1677 if (!TIFFWriteDirectory(tif))
1678 {
1679 fprintf(stderr, "Can't write directory to %s. Testline %d\n", filename,
1680 __LINE__);
1681 goto failure;
1682 }
1683 /* Check IFD offset: IFD shall be overwritten (same offset). */
1684 if (!TIFFSetDirectory(tif, 0))
1685 {
1686 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", 0,
1687 filename, __LINE__);
1688 goto failure;
1689 }
1690 auxUint64 = TIFFCurrentDirOffset(tif);
1691
1692 if (auxUint64 != offsetBase)
1693 {
1694 fprintf(stderr,
1695 "Odd to even data size: Offset 0x%" PRIx64
1696 " is different to first written offset 0x%" PRIx64
1697 " Testline %d\n",
1698 auxUint64, offsetBase, __LINE__);
1699 goto failure;
1700 }
1701
1702 /* Write now 17 byte field (16 byte + zero), which shall be re-written. */
1703 if (!TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "123456789ABCDEFX"))
1704 {
1705 fprintf(stderr, "Can't set TIFFTAG_IMAGEDESCRIPTION tag. Testline %d\n",
1706 __LINE__);
1707 goto failure;
1708 }
1709 if (!TIFFWriteDirectory(tif))
1710 {
1711 fprintf(stderr, "Can't write directory to %s. Testline %d\n", filename,
1712 __LINE__);
1713 goto failure;
1714 }
1715 /* Check IFD offset: IFD shall be re-written (different offset). */
1716 if (!TIFFSetDirectory(tif, 0))
1717 {
1718 fprintf(stderr, "Can't set directory %d of %s. Testline %d\n", 0,
1719 filename, __LINE__);
1720 goto failure;
1721 }
1722 auxUint64 = TIFFCurrentDirOffset(tif);
1723
1724 if (auxUint64 == offsetBase)
1725 {
1726 fprintf(stderr,
1727 "Odd to even data size: Offset 0x%" PRIx64 " has NOT changed."
1728 " Testline %d\n",
1729 auxUint64, __LINE__);
1730 goto failure;
1731 }
1732
1733 TIFFClose(tif);
1734 unlink(filename);
1735 return 0;
1736
1737 failure:
1738 if (tif)
1739 {
1740 TIFFClose(tif);
1741 }
1742 return 1;
1743 } /*-- test_OddDataSizes --*/
1744
1745 int main()
1746 {
1747
1748 int retval = 0;
1749 int retvalLast = 0;
1750 int ntest = 0;
1751 char filename[128] = {0};
1752
1753 /* clang-format off */
1754 fprintf(stderr,"==== Testing IFD enlargement (overwriting or re-writing of IFD). ====\n");
1755 for (int tiled = 0; tiled <= 1; tiled++)
1756 {
1757 for (int strileWriting = 0; strileWriting <= 1; strileWriting++)
1758 {
1759 for (unsigned int openMode = 0; openMode < 4; openMode++)
1760 {
1761 sprintf(filename, "test_ifd_enlargement_%02d_%s_%s%s.tif", ntest,
1762 modeStrings[openMode], (tiled ? "TL" : "ST"),
1763 (strileWriting ? "_strileW" : ""));
1764 /* clang-format off */
1765 retval += test_IFD_enlargement(filename, openMode, strileWriting, 1, 1, 2, tiled); ntest++;
1766 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1767 retval += test_IFD_enlargement(filename, openMode, strileWriting, 1, 6, 12, tiled); ntest++;
1768 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1769 retval += test_IFD_enlargement(filename, openMode, strileWriting, 2, 1, 2, tiled); ntest++;
1770 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1771 retval += test_IFD_enlargement(filename, openMode, strileWriting, 2, 6, 12, tiled); ntest++;
1772 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1773 retval += test_IFD_enlargement(filename, openMode, strileWriting, 3, 1, 2, tiled); ntest++;
1774 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1775 retval += test_IFD_enlargement(filename, openMode, strileWriting, 3, 6, 12, tiled); ntest++;
1776 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1777 retval += test_IFD_enlargement(filename, openMode, strileWriting, 3, 40, 22, tiled); ntest++;
1778 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1779 retval += test_IFD_enlargement(filename, openMode, strileWriting, 1, 20, 10, tiled); ntest++;
1780 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1781 retval += test_IFD_enlargement(filename, openMode, strileWriting, 2, 20, 10, tiled); ntest++;
1782 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1783 retval += test_IFD_enlargement(filename, openMode, strileWriting, 3, 20, 10, tiled); ntest++;
1784 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED (openMode %s; strileWriting=%d; tiled=%d). <<<<\n\n", ntest, modeStrings[openMode], strileWriting, tiled); retvalLast = retval; }
1785 /* clang-format on */
1786 }
1787 }
1788 }
1789 /* clang-format off */
1790
1791 /* test EXIF IFD, SubIFD and IFD checkpointing */
1792 retval += test_EXIF_enlargement("test_ifd_EXIF_enlargement.tif", FALSE); ntest++;
1793 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1794 retval += test_EXIF_enlargement("test_ifd_EXIF_enlargementBig.tif", TRUE); ntest++;
1795 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1796
1797 retval += test_SubIFD_enlargement("test_SubIFD_enlargement.tif", FALSE); ntest++;
1798 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1799 retval += test_SubIFD_enlargement("test_SubIFD_enlargementBig.tif", TRUE); ntest++;
1800 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1801
1802 retval += test_CheckpointDirectory("test_CheckpointDirectory.tif", FALSE); ntest++;
1803 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1804 retval += test_CheckpointDirectory("test_CheckpointDirectoryBig.tif", TRUE); ntest++;
1805 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1806
1807 retval += test_OddDataSizes("test_OddDataSizes.tif", 0); ntest++;
1808 if (retval != retvalLast) { fprintf(stderr, " >>>> Test %d FAILED. <<<<\n\n", ntest); retvalLast = retval; }
1809
1810 if (!retval)
1811 {
1812 fprintf(stderr, "==== Testing IFD enlargement finished OK. ====\n");
1813 }
1814 else
1815 {
1816 fprintf(stderr, "==== Testing IFD enlargement finished with ERROR. ====\n");
1817 }
1818
1819 return retval;
1820 }