| 1 |
/* |
| 2 |
* TIFF Library |
| 3 |
* |
| 4 |
* The purpose of this test suite is to test the correctness of |
| 5 |
* TIFFWriteDirectory() when appending multiple directories to an open file. |
| 6 |
* |
| 7 |
* Currently, there is an optimization where the TIFF data structure stores the |
| 8 |
* offset of the last written directory in order to avoid having to traverse the |
| 9 |
* entire directory list each time a new one is added. The offset is not stored |
| 10 |
* in the file itself, only in the in-memory data structure. This means we still |
| 11 |
* go through the entire list the first time a directory is appended to a |
| 12 |
* newly-opened file, and the shortcut is taken for subsequent directory writes. |
| 13 |
* |
| 14 |
* In order to test the correctness of the optimization, the |
| 15 |
* `test_lastdir_offset` function writes 10 directories to two different files. |
| 16 |
* For the first file we use the optimization, by simply calling |
| 17 |
* TIFFWriteDirectory() repeatedly on an open TIFF handle. For the second file, |
| 18 |
* we avoid the optimization by closing the file after each call to |
| 19 |
* TIFFWriteDirectory(), which means the next directory write will traverse the |
| 20 |
* entire list. |
| 21 |
* |
| 22 |
* Finally, the two files are compared to check that the number of directories |
| 23 |
* written is the same, and that their offsets match. The test is then repeated |
| 24 |
* using BigTIFF files. |
| 25 |
* |
| 26 |
* Furthermore, arbitrary directory loading using TIFFSetDirectory() is checked, |
| 27 |
* especially after the update with "relative" movement possibility. Also |
| 28 |
* TIFFUnlinkDirectory() is tested. |
| 29 |
* |
| 30 |
* Furthermore, tests are performed with big-endian, little-endian and BigTIFF |
| 31 |
* images. |
| 32 |
* |
| 33 |
* Furthermore the correctness of tif_curdir = TIFFCurrentDirectory() when |
| 34 |
* moving though a multi-IFD file and creating new IFDs, overwriting or |
| 35 |
* re-writing IFDs as well for reading and writing SubIFDs is tested. |
| 36 |
* |
| 37 |
*/ |
| 38 |
|
| 39 |
#include "tif_config.h" |
| 40 |
|
| 41 |
#include <stdbool.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#include "tiffio.h" |
| 47 |
|
| 48 |
#ifdef HAVE_UNISTD_H |
| 49 |
#include <unistd.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
/* Global settings for the test image. It is not save to change. */ |
| 53 |
#define SPP 3 /* Samples per pixel */ |
| 54 |
#define N_DIRECTORIES 10 /* Number of directories to write */ |
| 55 |
const uint16_t width = 1; |
| 56 |
const uint16_t length = 1; |
| 57 |
const uint16_t bps = 8; |
| 58 |
const uint16_t photometric = PHOTOMETRIC_RGB; |
| 59 |
const uint16_t rows_per_strip = 1; |
| 60 |
const uint16_t planarconfig = PLANARCONFIG_CONTIG; |
| 61 |
|
| 62 |
char *openModeStrings[] = {"wl", "wb", "w8l", "w8b"}; |
| 63 |
char *openModeText[] = {"non-BigTIFF and LE", "non-BigTIFF and BE", |
| 64 |
"BigTIFF and LE", "BigTIFF and BE"}; |
| 65 |
|
| 66 |
/* Some functions and macros to get more readable test code. */ |
| 67 |
int CheckCurDirNum(TIFF *tif, tdir_t expected_dirnum, int line) |
| 68 |
{ |
| 69 |
|
| 70 |
tdir_t curdir = TIFFCurrentDirectory(tif); |
| 71 |
if (curdir != expected_dirnum) |
| 72 |
{ |
| 73 |
fprintf(stderr, |
| 74 |
"Error: curdir %d is different to expected one %d at line %d\n", |
| 75 |
curdir, expected_dirnum, line); |
| 76 |
return 1; |
| 77 |
} |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
#define TIFFWriteDirectory_M(tif, filename, line) \ |
| 82 |
if (!TIFFWriteDirectory(tif)) \ |
| 83 |
{ \ |
| 84 |
fprintf(stderr, "Can't write directory to %s at line %d\n", filename, \ |
| 85 |
line); \ |
| 86 |
goto failure; \ |
| 87 |
} |
| 88 |
|
| 89 |
#define TIFFCheckpointDirectory_M(tif, dirnum, filename, line) \ |
| 90 |
if (!TIFFCheckpointDirectory(tif)) \ |
| 91 |
{ \ |
| 92 |
fprintf(stderr, "Can't checkpoint directory %d of %s at line %d\n", \ |
| 93 |
dirnum, filename, line); \ |
| 94 |
goto failure; \ |
| 95 |
} |
| 96 |
|
| 97 |
#define TIFFSetDirectory_M(tif, dirnum, filename, line) \ |
| 98 |
if (!TIFFSetDirectory(tif, dirnum)) \ |
| 99 |
{ \ |
| 100 |
fprintf(stderr, "Can't set directory %d of %s at line %d\n", dirnum, \ |
| 101 |
filename, line); \ |
| 102 |
goto failure; \ |
| 103 |
} |
| 104 |
|
| 105 |
#define CHECKCURDIRNUM_M(tif, x, line) \ |
| 106 |
if (CheckCurDirNum(tif, x, line)) \ |
| 107 |
{ \ |
| 108 |
goto failure; \ |
| 109 |
} |
| 110 |
|
| 111 |
/* Writes basic tags to current directory (IFD) as well one pixel to the file. |
| 112 |
* For is_corrupted = TRUE a corrupted IFD (missing image width tag) is |
| 113 |
* generated. */ |
| 114 |
int write_data_to_current_directory(TIFF *tif, int i, bool is_corrupted) |
| 115 |
{ |
| 116 |
unsigned char buf[SPP] = {0, 127, 255}; |
| 117 |
char auxString[128]; |
| 118 |
|
| 119 |
if (!tif) |
| 120 |
{ |
| 121 |
fprintf(stderr, "Invalid TIFF handle.\n"); |
| 122 |
return 1; |
| 123 |
} |
| 124 |
if (!is_corrupted) |
| 125 |
{ |
| 126 |
if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) |
| 127 |
{ |
| 128 |
fprintf(stderr, "Can't set ImageWidth tag.\n"); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
} |
| 132 |
if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) |
| 133 |
{ |
| 134 |
fprintf(stderr, "Can't set ImageLength tag.\n"); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) |
| 138 |
{ |
| 139 |
fprintf(stderr, "Can't set BitsPerSample tag.\n"); |
| 140 |
return 1; |
| 141 |
} |
| 142 |
if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP)) |
| 143 |
{ |
| 144 |
fprintf(stderr, "Can't set SamplesPerPixel tag.\n"); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) |
| 148 |
{ |
| 149 |
fprintf(stderr, "Can't set SamplesPerPixel tag.\n"); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) |
| 153 |
{ |
| 154 |
fprintf(stderr, "Can't set PlanarConfiguration tag.\n"); |
| 155 |
return 1; |
| 156 |
} |
| 157 |
if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) |
| 158 |
{ |
| 159 |
fprintf(stderr, "Can't set PhotometricInterpretation tag.\n"); |
| 160 |
return 1; |
| 161 |
} |
| 162 |
/* Write IFD identification number to ASCII string of PageName tag. */ |
| 163 |
sprintf(auxString, "%d th. IFD", i); |
| 164 |
if (!TIFFSetField(tif, TIFFTAG_PAGENAME, auxString)) |
| 165 |
{ |
| 166 |
fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag.\n"); |
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
/* Write dummy pixel data. */ |
| 171 |
if (TIFFWriteScanline(tif, buf, 0, 0) == -1 && !is_corrupted) |
| 172 |
{ |
| 173 |
fprintf(stderr, "Can't write image data.\n"); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
/* Fills a new IFD and appends it to a file by opening a file and closing it |
| 181 |
* again after writing. */ |
| 182 |
int write_directory_to_closed_file(const char *filename, unsigned int openMode, |
| 183 |
int i) |
| 184 |
{ |
| 185 |
TIFF *tif; |
| 186 |
/* Replace 'w' for write by 'a' for append. */ |
| 187 |
char strAux[8] = {0}; |
| 188 |
strncpy(strAux, openModeStrings[openMode], sizeof(strAux) - 1); |
| 189 |
strAux[0] = 'a'; |
| 190 |
tif = TIFFOpen(filename, strAux); |
| 191 |
if (!tif) |
| 192 |
{ |
| 193 |
fprintf(stderr, "Can't create/open %s\n", filename); |
| 194 |
return 1; |
| 195 |
} |
| 196 |
|
| 197 |
if (write_data_to_current_directory(tif, i, false)) |
| 198 |
{ |
| 199 |
fprintf(stderr, "Can't write data to directory %d of %s.\n", i, |
| 200 |
filename); |
| 201 |
TIFFClose(tif); |
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
if (!TIFFWriteDirectory(tif)) |
| 206 |
{ |
| 207 |
fprintf(stderr, "TIFFWriteDirectory() failed for directory %d of %s.\n", |
| 208 |
i, filename); |
| 209 |
TIFFClose(tif); |
| 210 |
return 1; |
| 211 |
} |
| 212 |
|
| 213 |
TIFFClose(tif); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
/* Opens a file and counts its directories. */ |
| 218 |
int count_directories(const char *filename, int *count) |
| 219 |
{ |
| 220 |
TIFF *tif; |
| 221 |
*count = 0; |
| 222 |
|
| 223 |
tif = TIFFOpen(filename, "r"); |
| 224 |
if (!tif) |
| 225 |
{ |
| 226 |
fprintf(stderr, "Can't read %s\n", filename); |
| 227 |
return 1; |
| 228 |
} |
| 229 |
|
| 230 |
do |
| 231 |
{ |
| 232 |
CHECKCURDIRNUM_M(tif, (tdir_t)(*count), __LINE__); |
| 233 |
(*count)++; |
| 234 |
} while (TIFFReadDirectory(tif)); |
| 235 |
failure: |
| 236 |
TIFFClose(tif); |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
/* Compare 'requested_dir_number' with number written in PageName tag |
| 241 |
* into the IFD to identify that IFD. */ |
| 242 |
int is_requested_directory(TIFF *tif, int requested_dir_number, |
| 243 |
const char *filename) |
| 244 |
{ |
| 245 |
char *ptr = NULL; |
| 246 |
char *auxStr = NULL; |
| 247 |
|
| 248 |
if (!TIFFGetField(tif, TIFFTAG_PAGENAME, &ptr)) |
| 249 |
{ |
| 250 |
fprintf(stderr, "Can't get TIFFTAG_PAGENAME tag.\n"); |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
/* Check for reading errors */ |
| 255 |
if (ptr != NULL) |
| 256 |
auxStr = strchr(ptr, ' '); |
| 257 |
|
| 258 |
if (ptr == NULL || auxStr == NULL || strncmp(auxStr, " th.", 4)) |
| 259 |
{ |
| 260 |
ptr = ptr == NULL ? "(null)" : ptr; |
| 261 |
fprintf(stderr, |
| 262 |
"Error reading IFD directory number from PageName tag: %s\n", |
| 263 |
ptr); |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
/* Retrieve IFD identification number from ASCII string */ |
| 268 |
const int nthIFD = atoi(ptr); |
| 269 |
if (nthIFD == requested_dir_number) |
| 270 |
{ |
| 271 |
return 1; |
| 272 |
} |
| 273 |
fprintf(stderr, "Expected directory %d from %s was not loaded but: %s\n", |
| 274 |
requested_dir_number, filename, ptr); |
| 275 |
|
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
enum DirWalkMode |
| 280 |
{ |
| 281 |
DirWalkMode_ReadDirectory, |
| 282 |
DirWalkMode_SetDirectory, |
| 283 |
DirWalkMode_SetDirectory_Reverse, |
| 284 |
}; |
| 285 |
/* Gets a list of the directory offsets in a file. Assumes the file has at least |
| 286 |
* N_DIRECTORIES directories. |
| 287 |
* There are three methods to test walking through the IFD chain. |
| 288 |
* This tests the optimization of faster SetDirectory(). */ |
| 289 |
int get_dir_offsets(const char *filename, uint64_t *offsets, |
| 290 |
enum DirWalkMode dirWalkMode) |
| 291 |
{ |
| 292 |
TIFF *tif; |
| 293 |
int i; |
| 294 |
|
| 295 |
tif = TIFFOpen(filename, "r"); |
| 296 |
if (!tif) |
| 297 |
{ |
| 298 |
fprintf(stderr, "Can't read %s\n", filename); |
| 299 |
return 1; |
| 300 |
} |
| 301 |
|
| 302 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 303 |
{ |
| 304 |
tdir_t dirn = (dirWalkMode == DirWalkMode_SetDirectory_Reverse) |
| 305 |
? (N_DIRECTORIES - i - 1) |
| 306 |
: i; |
| 307 |
|
| 308 |
if (dirWalkMode != DirWalkMode_ReadDirectory && |
| 309 |
!TIFFSetDirectory(tif, dirn) && dirn < (N_DIRECTORIES - 1)) |
| 310 |
{ |
| 311 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 312 |
TIFFClose(tif); |
| 313 |
return 3; |
| 314 |
} |
| 315 |
|
| 316 |
offsets[dirn] = TIFFCurrentDirOffset(tif); |
| 317 |
|
| 318 |
/* Check, if dirn is requested directory */ |
| 319 |
if (!is_requested_directory(tif, dirn, filename)) |
| 320 |
{ |
| 321 |
TIFFClose(tif); |
| 322 |
return 4; |
| 323 |
} |
| 324 |
|
| 325 |
if (dirWalkMode == DirWalkMode_ReadDirectory && |
| 326 |
!TIFFReadDirectory(tif) && i < (N_DIRECTORIES - 1)) |
| 327 |
{ |
| 328 |
fprintf(stderr, "Can't read %d.th directory from %s\n", i, |
| 329 |
filename); |
| 330 |
TIFFClose(tif); |
| 331 |
return 2; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
TIFFClose(tif); |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
/* Checks that TIFFSetDirectory() work well after update for relative seeking |
| 340 |
* to following directories. |
| 341 |
* |
| 342 |
* There are several issues especially when SubIFDs and custom directories are |
| 343 |
* involved. There are no real directory number for those and TIFFSetDirectory() |
| 344 |
* cannot be used. However, TIFFSetDirectory() is needed to switch back to the |
| 345 |
* main-IFD chain. Furthermore, IFD-loop handling needs to be supported in any |
| 346 |
* cases. |
| 347 |
* Also the case where directly after TIFFWriteDirectory() that directory |
| 348 |
* is re-read using TIFFSetDirectory() is tested. |
| 349 |
*/ |
| 350 |
int test_arbitrary_directrory_loading(unsigned int openMode) |
| 351 |
{ |
| 352 |
char filename[128] = {0}; |
| 353 |
TIFF *tif; |
| 354 |
uint64_t offsets_base[N_DIRECTORIES]; |
| 355 |
int expected_original_dirnumber; |
| 356 |
tdir_t expected_curdir = (tdir_t)(-1); |
| 357 |
|
| 358 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 359 |
{ |
| 360 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 361 |
openMode); |
| 362 |
return 1; |
| 363 |
} |
| 364 |
|
| 365 |
/* Get individual filenames and delete existent ones. */ |
| 366 |
sprintf(filename, "test_arbitrary_directrory_loading_%s.tif", |
| 367 |
openModeStrings[openMode]); |
| 368 |
unlink(filename); |
| 369 |
|
| 370 |
/* Create a file and write N_DIRECTORIES (10) directories to it. */ |
| 371 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 372 |
if (!tif) |
| 373 |
{ |
| 374 |
fprintf(stderr, "Can't create %s\n", filename); |
| 375 |
return 1; |
| 376 |
} |
| 377 |
TIFFSetDirectory(tif, 0); |
| 378 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 379 |
for (int i = 0; i < N_DIRECTORIES; i++) |
| 380 |
{ |
| 381 |
if (write_data_to_current_directory(tif, i, false)) |
| 382 |
{ |
| 383 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 384 |
filename); |
| 385 |
goto failure; |
| 386 |
} |
| 387 |
if (!TIFFWriteDirectory(tif)) |
| 388 |
{ |
| 389 |
fprintf(stderr, "Can't write directory to %s\n", filename); |
| 390 |
goto failure; |
| 391 |
} |
| 392 |
expected_curdir++; |
| 393 |
/* Fails in 4.6.0 */ |
| 394 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 395 |
if (i >= 2 && i <= 4) |
| 396 |
{ |
| 397 |
if (i == 3) |
| 398 |
{ |
| 399 |
/* Invalidate directory - TIFFSetSubDirectory() will fail. |
| 400 |
* The next TIFFSetDirectory(i) will revoke this action.*/ |
| 401 |
if (TIFFSetSubDirectory(tif, 0)) |
| 402 |
{ |
| 403 |
fprintf(stderr, |
| 404 |
"Unexpected return at invalidate directory %d " |
| 405 |
"within %s.\n", |
| 406 |
i, filename); |
| 407 |
goto failure; |
| 408 |
} |
| 409 |
} |
| 410 |
/* Test jump back to just written directory. */ |
| 411 |
if (!TIFFSetDirectory(tif, i)) |
| 412 |
{ |
| 413 |
fprintf(stderr, |
| 414 |
"Can't set directory %d within %s " |
| 415 |
"right after TIFFWriteDirectory().\n", |
| 416 |
i, filename); |
| 417 |
goto failure; |
| 418 |
} |
| 419 |
if (i == 4) |
| 420 |
{ |
| 421 |
/* Invalidate directory - TIFFSetSubDirectory() will fail. |
| 422 |
* This time, tif_curdir keeps set to -1 and counts now again |
| 423 |
* from 0, despite the directory number in the file is equal |
| 424 |
* "i". */ |
| 425 |
if (TIFFSetSubDirectory(tif, 0)) |
| 426 |
{ |
| 427 |
fprintf(stderr, |
| 428 |
"Unexpected return at invalidate directory %d " |
| 429 |
"within %s.\n", |
| 430 |
i, filename); |
| 431 |
goto failure; |
| 432 |
} |
| 433 |
} |
| 434 |
/*Check if correct directory is loaded */ |
| 435 |
if (!is_requested_directory(tif, i, filename)) |
| 436 |
{ |
| 437 |
goto failure; |
| 438 |
} |
| 439 |
/* Reset to next directory to go on with writing. */ |
| 440 |
TIFFCreateDirectory(tif); |
| 441 |
} |
| 442 |
} |
| 443 |
TIFFClose(tif); |
| 444 |
|
| 445 |
/* Reopen prepared testfile */ |
| 446 |
tif = TIFFOpen(filename, "r+"); |
| 447 |
if (!tif) |
| 448 |
{ |
| 449 |
fprintf(stderr, "Can't create %s\n", filename); |
| 450 |
return 1; |
| 451 |
} |
| 452 |
|
| 453 |
/* Get directory offsets */ |
| 454 |
if (get_dir_offsets(filename, offsets_base, DirWalkMode_ReadDirectory)) |
| 455 |
{ |
| 456 |
fprintf(stderr, "Error reading directory offsets from %s.\n", filename); |
| 457 |
goto failure; |
| 458 |
} |
| 459 |
|
| 460 |
/* Set last directory and then one after, which should fail. */ |
| 461 |
if (!TIFFSetDirectory(tif, N_DIRECTORIES - 1)) |
| 462 |
{ |
| 463 |
fprintf(stderr, "Can't set last directory %d within %s\n", |
| 464 |
N_DIRECTORIES - 1, filename); |
| 465 |
goto failure; |
| 466 |
} |
| 467 |
if (TIFFSetDirectory(tif, N_DIRECTORIES + 1)) |
| 468 |
{ |
| 469 |
fprintf(stderr, |
| 470 |
"End of IFD chain not detected. Set non existing directory %d " |
| 471 |
"within %s\n", |
| 472 |
N_DIRECTORIES + 1, filename); |
| 473 |
goto failure; |
| 474 |
} |
| 475 |
CHECKCURDIRNUM_M(tif, N_DIRECTORIES - 1, __LINE__); |
| 476 |
|
| 477 |
/* Test very fast TIFFSetDirectory() using IFD loop directory list. |
| 478 |
* First populate IFD loop directory list and then go through directories in |
| 479 |
* reverse order. Within between read after end of IFDs using |
| 480 |
* TIFFReadDirectory() where IFD loop directory list should be kept. */ |
| 481 |
for (int i = 0; i < N_DIRECTORIES; i++) |
| 482 |
{ |
| 483 |
if (!TIFFSetDirectory(tif, i)) |
| 484 |
{ |
| 485 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 486 |
goto failure; |
| 487 |
} |
| 488 |
CHECKCURDIRNUM_M(tif, i, __LINE__); |
| 489 |
} |
| 490 |
TIFFReadDirectory(tif); |
| 491 |
CHECKCURDIRNUM_M(tif, N_DIRECTORIES - 1, __LINE__); |
| 492 |
for (int i = N_DIRECTORIES - 1; i >= 0; i--) |
| 493 |
{ |
| 494 |
if (!TIFFSetDirectory(tif, i)) |
| 495 |
{ |
| 496 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 497 |
goto failure; |
| 498 |
} |
| 499 |
if (!is_requested_directory(tif, i, filename)) |
| 500 |
{ |
| 501 |
goto failure; |
| 502 |
} |
| 503 |
CHECKCURDIRNUM_M(tif, i, __LINE__); |
| 504 |
} |
| 505 |
|
| 506 |
/* Test not existing directory number */ |
| 507 |
if (TIFFSetDirectory(tif, N_DIRECTORIES)) |
| 508 |
{ |
| 509 |
fprintf(stderr, |
| 510 |
"No expected fail for accessing not existent directory number " |
| 511 |
"%d in file %s\n", |
| 512 |
N_DIRECTORIES, filename); |
| 513 |
goto failure; |
| 514 |
} |
| 515 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 516 |
|
| 517 |
/* Close and Reopen prepared testfile */ |
| 518 |
TIFFClose(tif); |
| 519 |
tif = TIFFOpen(filename, "r+"); |
| 520 |
if (!tif) |
| 521 |
{ |
| 522 |
fprintf(stderr, "Can't create %s\n", filename); |
| 523 |
return 1; |
| 524 |
} |
| 525 |
|
| 526 |
/* Step through directories just using TIFFSetSubDirectory() */ |
| 527 |
for (int i = N_DIRECTORIES - 1; i >= 0; i--) |
| 528 |
{ |
| 529 |
if (!TIFFSetSubDirectory(tif, offsets_base[i])) |
| 530 |
{ |
| 531 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 532 |
goto failure; |
| 533 |
} |
| 534 |
if (!is_requested_directory(tif, i, filename)) |
| 535 |
{ |
| 536 |
goto failure; |
| 537 |
} |
| 538 |
CHECKCURDIRNUM_M(tif, i, __LINE__); |
| 539 |
} |
| 540 |
|
| 541 |
/* More specialized test cases for relative seeking within TIFFSetDirectory. |
| 542 |
* However, with using IFD loop directory list, most of this test cases will |
| 543 |
* never be reached! */ |
| 544 |
if (!TIFFSetDirectory(tif, 2)) |
| 545 |
{ |
| 546 |
fprintf(stderr, "Can't set directory %d within %s\n", 2, filename); |
| 547 |
goto failure; |
| 548 |
} |
| 549 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 550 |
uint64_t off2 = TIFFCurrentDirOffset(tif); |
| 551 |
/* Note that dirnum = 2 is deleted here since TIFFUnlinkDirectory() |
| 552 |
* starts with 1 instead of 0. */ |
| 553 |
if (!TIFFUnlinkDirectory(tif, 3)) |
| 554 |
{ |
| 555 |
fprintf(stderr, "Can't unlink directory %d within %s\n", 3, filename); |
| 556 |
goto failure; |
| 557 |
} |
| 558 |
/* Move to the unlinked IFD. This sets tif_curdir=0 because unlinked IFD |
| 559 |
* offset is not in the IFD loop list. This indicates a SubIFD chain. */ |
| 560 |
if (!TIFFSetSubDirectory(tif, off2)) |
| 561 |
{ |
| 562 |
fprintf(stderr, |
| 563 |
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64 |
| 564 |
") within %s\n", |
| 565 |
off2, off2, filename); |
| 566 |
goto failure; |
| 567 |
} |
| 568 |
/*Check if correct directory is loaded */ |
| 569 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 570 |
expected_original_dirnumber = 2; |
| 571 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 572 |
{ |
| 573 |
goto failure; |
| 574 |
} |
| 575 |
/* This should move back to the main-IFD chain and load the third |
| 576 |
* directory which has IFD number 4, due to the deleted third IFD. */ |
| 577 |
if (!TIFFSetDirectory(tif, 3)) |
| 578 |
{ |
| 579 |
fprintf(stderr, "Can't set new directory %d within %s\n", 3, filename); |
| 580 |
goto failure; |
| 581 |
} |
| 582 |
CHECKCURDIRNUM_M(tif, 3, __LINE__); |
| 583 |
expected_original_dirnumber = 4; |
| 584 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 585 |
{ |
| 586 |
goto failure; |
| 587 |
} |
| 588 |
/* Test backwards jump. */ |
| 589 |
if (!TIFFSetDirectory(tif, 2)) |
| 590 |
{ |
| 591 |
fprintf(stderr, "Can't set new directory %d within %s\n", 2, filename); |
| 592 |
goto failure; |
| 593 |
} |
| 594 |
expected_original_dirnumber = 3; |
| 595 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 596 |
{ |
| 597 |
goto failure; |
| 598 |
} |
| 599 |
|
| 600 |
/* Second UnlinkDirectory -> two IFDs are missing in the main-IFD chain |
| 601 |
* then, original dirnum 2 and 3 */ |
| 602 |
if (!TIFFUnlinkDirectory(tif, 3)) |
| 603 |
{ |
| 604 |
fprintf(stderr, "Can't unlink directory %d within %s\n", 3, filename); |
| 605 |
goto failure; |
| 606 |
} |
| 607 |
if (!TIFFSetDirectory(tif, 2)) |
| 608 |
{ |
| 609 |
fprintf(stderr, |
| 610 |
"Can't set new directory %d after second " |
| 611 |
"TIFFUnlinkDirectory(3) within %s\n", |
| 612 |
2, filename); |
| 613 |
goto failure; |
| 614 |
} |
| 615 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 616 |
/* which should now be the previous dir-3. */ |
| 617 |
expected_original_dirnumber = 4; |
| 618 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 619 |
{ |
| 620 |
goto failure; |
| 621 |
} |
| 622 |
|
| 623 |
/* Check, if third original directory, which is unlinked, could be loaded |
| 624 |
* and the following, still chained one. This is like for a SubIFD. |
| 625 |
* tif_curdir will be set to 0 because deleted IFD is not in main sequence |
| 626 |
* and then incremented to 1 when reading the still chained next IFD using |
| 627 |
* TIFFReadDirectory(). */ |
| 628 |
if (!TIFFSetSubDirectory(tif, offsets_base[2])) |
| 629 |
{ |
| 630 |
fprintf(stderr, |
| 631 |
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64 |
| 632 |
") within %s\n", |
| 633 |
offsets_base[2], offsets_base[2], filename); |
| 634 |
goto failure; |
| 635 |
} |
| 636 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 637 |
if (!TIFFReadDirectory(tif)) |
| 638 |
{ |
| 639 |
fprintf(stderr, |
| 640 |
"Can't read directory after directory at offset 0x%" PRIx64 |
| 641 |
" (%" PRIu64 ") within %s\n", |
| 642 |
offsets_base[2], offsets_base[2], filename); |
| 643 |
goto failure; |
| 644 |
} |
| 645 |
/* Check if correct directory is loaded, which was unlinked the second |
| 646 |
* time. |
| 647 |
*/ |
| 648 |
CHECKCURDIRNUM_M(tif, 1, __LINE__); |
| 649 |
expected_original_dirnumber = 3; |
| 650 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 651 |
{ |
| 652 |
goto failure; |
| 653 |
} |
| 654 |
|
| 655 |
/* Load unlinked directory like a SubIFD and then go back to the |
| 656 |
* main-IFD chain using TIFFSetDirectory(). Also check two loads of the |
| 657 |
* same directory. */ |
| 658 |
if (!TIFFSetSubDirectory(tif, offsets_base[2])) |
| 659 |
{ |
| 660 |
fprintf(stderr, |
| 661 |
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64 |
| 662 |
") within %s\n", |
| 663 |
offsets_base[2], offsets_base[2], filename); |
| 664 |
goto failure; |
| 665 |
} |
| 666 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 667 |
if (!TIFFSetDirectory(tif, 3)) |
| 668 |
{ |
| 669 |
fprintf(stderr, "Can't set new directory %d within %s\n", 3, filename); |
| 670 |
goto failure; |
| 671 |
} |
| 672 |
if (!TIFFSetDirectory(tif, 3)) |
| 673 |
{ |
| 674 |
fprintf(stderr, "Can't set new directory %d a second time within %s\n", |
| 675 |
3, filename); |
| 676 |
goto failure; |
| 677 |
} |
| 678 |
CHECKCURDIRNUM_M(tif, 3, __LINE__); |
| 679 |
/*Check if correct directory is loaded. Because two original IFDs are |
| 680 |
* unlinked / missing, the original dirnumber is now 5. */ |
| 681 |
expected_original_dirnumber = 5; |
| 682 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 683 |
{ |
| 684 |
goto failure; |
| 685 |
} |
| 686 |
|
| 687 |
/* Another sequence for testing. */ |
| 688 |
if (!TIFFSetDirectory(tif, 2)) |
| 689 |
{ |
| 690 |
fprintf(stderr, "Can't set new directory %d a second time within %s\n", |
| 691 |
2, filename); |
| 692 |
goto failure; |
| 693 |
} |
| 694 |
if (!TIFFSetDirectory(tif, 3)) |
| 695 |
{ |
| 696 |
fprintf(stderr, "Can't set new directory %d a second time within %s\n", |
| 697 |
3, filename); |
| 698 |
goto failure; |
| 699 |
} |
| 700 |
if (!TIFFSetSubDirectory(tif, offsets_base[2])) |
| 701 |
{ |
| 702 |
fprintf(stderr, |
| 703 |
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64 |
| 704 |
") within %s\n", |
| 705 |
offsets_base[2], offsets_base[2], filename); |
| 706 |
goto failure; |
| 707 |
} |
| 708 |
expected_original_dirnumber = 2; |
| 709 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 710 |
{ |
| 711 |
goto failure; |
| 712 |
} |
| 713 |
if (!TIFFSetDirectory(tif, 3)) |
| 714 |
{ |
| 715 |
fprintf(stderr, "Can't set new directory %d a second time within %s\n", |
| 716 |
3, filename); |
| 717 |
goto failure; |
| 718 |
} |
| 719 |
/*Check if correct directory is loaded. Because two original IFDs are |
| 720 |
* unlinked / missing, the original dirnumber is now 5. */ |
| 721 |
expected_original_dirnumber = 5; |
| 722 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 723 |
{ |
| 724 |
goto failure; |
| 725 |
} |
| 726 |
|
| 727 |
/* Third UnlinkDirectory -> three IFDs are missing in the main-IFD chain |
| 728 |
* then, original dirnum 0, 2 and 3 |
| 729 |
* Furthermore, this test checks that TIFFUnlinkDirectory() can unlink |
| 730 |
* the first directory dirnum = 0 and a following TIFFSetDirectory(0) |
| 731 |
* does not load the unlinked directory. */ |
| 732 |
CHECKCURDIRNUM_M(tif, 3, __LINE__); |
| 733 |
if (!TIFFUnlinkDirectory(tif, 1)) |
| 734 |
{ |
| 735 |
fprintf(stderr, "Can't unlink directory %d within %s\n", 0, filename); |
| 736 |
goto failure; |
| 737 |
} |
| 738 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 739 |
/* Now three directories are missing (0,2,3) and thus directory 0 is |
| 740 |
* original directory 1 and directory 2 is original directory 5. */ |
| 741 |
if (!TIFFSetDirectory(tif, 0)) |
| 742 |
{ |
| 743 |
fprintf(stderr, |
| 744 |
"Can't set new directory %d after third " |
| 745 |
"TIFFUnlinkDirectory(1) within %s\n", |
| 746 |
0, filename); |
| 747 |
goto failure; |
| 748 |
} |
| 749 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 750 |
expected_original_dirnumber = 1; |
| 751 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 752 |
{ |
| 753 |
goto failure; |
| 754 |
} |
| 755 |
if (!TIFFSetDirectory(tif, 2)) |
| 756 |
{ |
| 757 |
fprintf(stderr, |
| 758 |
"Can't set new directory %d after third " |
| 759 |
"TIFFUnlinkDirectory(1) within %s\n", |
| 760 |
2, filename); |
| 761 |
goto failure; |
| 762 |
} |
| 763 |
expected_original_dirnumber = 5; |
| 764 |
if (!is_requested_directory(tif, expected_original_dirnumber, filename)) |
| 765 |
{ |
| 766 |
goto failure; |
| 767 |
} |
| 768 |
|
| 769 |
/* TIFFUnlinkDirectory(0) is not allowed, because dirnum starts for |
| 770 |
* this function with 1 instead of 0. |
| 771 |
* An error return is expected here. */ |
| 772 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 773 |
fprintf(stderr, "----- Expect error messages about 'TIFFUnlinkDirectory() " |
| 774 |
"first directory starts with number 1 and not 0.' -----\n"); |
| 775 |
if (TIFFUnlinkDirectory(tif, 0)) |
| 776 |
{ |
| 777 |
fprintf(stderr, "TIFFUnlinkDirectory(0) did not return an error.\n"); |
| 778 |
goto failure; |
| 779 |
} |
| 780 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 781 |
|
| 782 |
TIFFClose(tif); |
| 783 |
unlink(filename); |
| 784 |
return 0; |
| 785 |
|
| 786 |
failure: |
| 787 |
if (tif) |
| 788 |
{ |
| 789 |
TIFFClose(tif); |
| 790 |
} |
| 791 |
return 1; |
| 792 |
} /*-- test_arbitrary_directrory_loading() --*/ |
| 793 |
|
| 794 |
/* Tests SubIFD writing and reading |
| 795 |
* |
| 796 |
* |
| 797 |
*/ |
| 798 |
int test_SubIFD_directrory_handling(unsigned int openMode) |
| 799 |
{ |
| 800 |
char filename[128] = {0}; |
| 801 |
|
| 802 |
/* Define the number of sub-IFDs you are going to write */ |
| 803 |
#define NUMBER_OF_SUBIFDs 4 |
| 804 |
uint16_t number_of_sub_IFDs = NUMBER_OF_SUBIFDs; |
| 805 |
toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = { |
| 806 |
0UL}; /* array for SubIFD tag */ |
| 807 |
int blnWriteSubIFD = 0; |
| 808 |
int i; |
| 809 |
int iIFD = 0, iSubIFD = 0; |
| 810 |
TIFF *tif; |
| 811 |
int expected_original_dirnumber; |
| 812 |
|
| 813 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 814 |
{ |
| 815 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 816 |
openMode); |
| 817 |
return 1; |
| 818 |
} |
| 819 |
|
| 820 |
/* Get individual filenames and delete existent ones. */ |
| 821 |
sprintf(filename, "test_SubIFD_directrory_handling_%s.tif", |
| 822 |
openModeStrings[openMode]); |
| 823 |
unlink(filename); |
| 824 |
|
| 825 |
/* Create a file and write N_DIRECTORIES (10) directories to it */ |
| 826 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 827 |
if (!tif) |
| 828 |
{ |
| 829 |
fprintf(stderr, "Can't create %s\n", filename); |
| 830 |
return 1; |
| 831 |
} |
| 832 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 833 |
{ |
| 834 |
if (write_data_to_current_directory( |
| 835 |
tif, blnWriteSubIFD ? 200 + iSubIFD++ : iIFD++, false)) |
| 836 |
{ |
| 837 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 838 |
filename); |
| 839 |
goto failure; |
| 840 |
} |
| 841 |
if (blnWriteSubIFD) |
| 842 |
{ |
| 843 |
/* SUBFILETYPE tag is not mandatory for SubIFD writing, but a |
| 844 |
* good idea to indicate thumbnails */ |
| 845 |
if (!TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE)) |
| 846 |
goto failure; |
| 847 |
} |
| 848 |
|
| 849 |
expected_original_dirnumber = blnWriteSubIFD ? 0 : iIFD - 1; |
| 850 |
|
| 851 |
/* For the second multi-page image, trigger TIFFWriteDirectory() to |
| 852 |
* switch for the next number_of_sub_IFDs calls to add those as SubIFDs |
| 853 |
* e.g. for thumbnails */ |
| 854 |
if (1 == i) |
| 855 |
{ |
| 856 |
blnWriteSubIFD = 1; |
| 857 |
/* Now here is the trick: the next n directories written |
| 858 |
* will be sub-IFDs of the main-IFD (where n is number_of_sub_IFDs |
| 859 |
* specified when you set the TIFFTAG_SUBIFD field. |
| 860 |
* The contents of sub_IFDs_offsets is initially copied to the |
| 861 |
* internal SubIFD offset array and written to the main-IFD. Those |
| 862 |
* SubIFD offsets in the main-IFD are then patched by the following |
| 863 |
* number_of_sub_IFDs TIFFWriteDirectory() calls which write the |
| 864 |
* sub-IFDs to file. |
| 865 |
*/ |
| 866 |
if (!TIFFSetField(tif, TIFFTAG_SUBIFD, number_of_sub_IFDs, |
| 867 |
sub_IFDs_offsets)) |
| 868 |
goto failure; |
| 869 |
} |
| 870 |
|
| 871 |
if (!TIFFWriteDirectory(tif)) |
| 872 |
{ |
| 873 |
fprintf(stderr, "Can't write directory to %s\n", filename); |
| 874 |
goto failure; |
| 875 |
} |
| 876 |
/* Fails in 4.6.0 */ |
| 877 |
CHECKCURDIRNUM_M(tif, expected_original_dirnumber, __LINE__); |
| 878 |
|
| 879 |
if (iSubIFD >= number_of_sub_IFDs) |
| 880 |
blnWriteSubIFD = 0; |
| 881 |
} |
| 882 |
TIFFClose(tif); |
| 883 |
|
| 884 |
/* Reopen prepared testfile */ |
| 885 |
tif = TIFFOpen(filename, "r+"); |
| 886 |
if (!tif) |
| 887 |
{ |
| 888 |
fprintf(stderr, "Can't create %s\n", filename); |
| 889 |
goto failure; |
| 890 |
} |
| 891 |
|
| 892 |
tdir_t numberOfMainIFDs = TIFFNumberOfDirectories(tif); |
| 893 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 894 |
if (numberOfMainIFDs != (tdir_t)N_DIRECTORIES - number_of_sub_IFDs) |
| 895 |
{ |
| 896 |
fprintf(stderr, |
| 897 |
"Unexpected number of directories in %s. Expected %i, " |
| 898 |
"found %i.\n", |
| 899 |
filename, N_DIRECTORIES - number_of_sub_IFDs, numberOfMainIFDs); |
| 900 |
goto failure; |
| 901 |
} |
| 902 |
|
| 903 |
tdir_t currentDirNumber = TIFFCurrentDirectory(tif); |
| 904 |
|
| 905 |
/* The first directory is already read through TIFFOpen() */ |
| 906 |
int blnRead = 0; |
| 907 |
expected_original_dirnumber = 1; |
| 908 |
do |
| 909 |
{ |
| 910 |
/* Check if there are SubIFD subfiles */ |
| 911 |
void *ptr; |
| 912 |
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr)) |
| 913 |
{ |
| 914 |
/* Copy SubIFD array from pointer */ |
| 915 |
memcpy(sub_IFDs_offsets, ptr, |
| 916 |
number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0])); |
| 917 |
|
| 918 |
for (i = 0; i < number_of_sub_IFDs; i++) |
| 919 |
{ |
| 920 |
/* Read first SubIFD directory */ |
| 921 |
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i])) |
| 922 |
goto failure; |
| 923 |
if (!is_requested_directory(tif, 200 + i, filename)) |
| 924 |
{ |
| 925 |
goto failure; |
| 926 |
} |
| 927 |
/* For SubIFDs, curdir is always set to 0 for the first SubIFD |
| 928 |
* read. |
| 929 |
* When SubIFDs are not concatenated, curdir is 0 for all |
| 930 |
* SubIFDs read. |
| 931 |
* When the SubIFDs are concatenated the following |
| 932 |
* ones after the first read one get incremented curdir numbers. |
| 933 |
* The curdir sequence gets updated when a previous one before |
| 934 |
* the firstly read one is read. I.e. curdir behaves in the |
| 935 |
* SubIFD chain like in the main IFD chain to support IFD loop |
| 936 |
* checking. |
| 937 |
*/ |
| 938 |
/* Check if there is a SubIFD chain behind the first one from |
| 939 |
* the array, as specified by Adobe */ |
| 940 |
int n = 0; |
| 941 |
/* subdirnum starts with 1 because first SubIFD is already |
| 942 |
* read with TIFFSetSubDirectory() */ |
| 943 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 944 |
tdir_t subdirnum = 1; |
| 945 |
/* analyse SubIFD chain */ |
| 946 |
while (TIFFReadDirectory(tif)) |
| 947 |
{ |
| 948 |
if (!is_requested_directory(tif, 201 + i + n++, filename)) |
| 949 |
goto failure; |
| 950 |
/* Fails in 4.6.0 when SubIFD chaining is enabled. */ |
| 951 |
CHECKCURDIRNUM_M(tif, subdirnum, __LINE__); |
| 952 |
subdirnum++; |
| 953 |
} |
| 954 |
} |
| 955 |
/*-- Go through SubIFDs in reverse order --*/ |
| 956 |
/* Activate main-IFD to reset SubIFD loop list from above. */ |
| 957 |
TIFFSetDirectory(tif, 0); |
| 958 |
for (i = number_of_sub_IFDs - 1; i >= 0; i--) |
| 959 |
{ |
| 960 |
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i])) |
| 961 |
goto failure; |
| 962 |
if (!is_requested_directory(tif, 200 + i, filename)) |
| 963 |
{ |
| 964 |
goto failure; |
| 965 |
} |
| 966 |
int n = 0; |
| 967 |
tdir_t subdirnum = 1; |
| 968 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 969 |
/* analyse SubIFD chain */ |
| 970 |
while (TIFFReadDirectory(tif)) |
| 971 |
{ |
| 972 |
if (!is_requested_directory(tif, 201 + i + n++, filename)) |
| 973 |
goto failure; |
| 974 |
/* Fails in 4.6.0 when SubIFD chaining is enabled. */ |
| 975 |
CHECKCURDIRNUM_M(tif, subdirnum, __LINE__); |
| 976 |
subdirnum++; |
| 977 |
} |
| 978 |
} |
| 979 |
/* Go back to main-IFD chain and re-read that main-IFD directory */ |
| 980 |
if (!TIFFSetDirectory(tif, currentDirNumber)) |
| 981 |
goto failure; |
| 982 |
} |
| 983 |
/* Read next main-IFD directory (subfile) */ |
| 984 |
blnRead = TIFFReadDirectory(tif); |
| 985 |
currentDirNumber = TIFFCurrentDirectory(tif); |
| 986 |
if (blnRead) |
| 987 |
CHECKCURDIRNUM_M(tif, expected_original_dirnumber, __LINE__); |
| 988 |
if (blnRead && !is_requested_directory( |
| 989 |
tif, expected_original_dirnumber++, filename)) |
| 990 |
goto failure; |
| 991 |
} while (blnRead); |
| 992 |
|
| 993 |
/*--- Now test arbitrary directory loading with SubIFDs ---*/ |
| 994 |
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[1])) |
| 995 |
goto failure; |
| 996 |
if (!is_requested_directory(tif, 201, filename)) |
| 997 |
{ |
| 998 |
goto failure; |
| 999 |
} |
| 1000 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1001 |
|
| 1002 |
/*-- Test TIFFCheckpointDirectory() with SubIFDs -- |
| 1003 |
* However, SubIFDs cannot be re-written to another location because |
| 1004 |
* TIFFRewriteDirectory() does not support SubIFDs. Overwriting works. */ |
| 1005 |
tdir_t expected_curdir = TIFFNumberOfDirectories(tif); |
| 1006 |
TIFFCreateDirectory(tif); |
| 1007 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1008 |
{ |
| 1009 |
fprintf(stderr, |
| 1010 |
"Can't write data to current directory in %s at line %d\n", |
| 1011 |
filename, __LINE__); |
| 1012 |
goto failure; |
| 1013 |
} |
| 1014 |
/* prepare to write next directory as SubIFD */ |
| 1015 |
number_of_sub_IFDs = 1; |
| 1016 |
if (!TIFFSetField(tif, TIFFTAG_SUBIFD, number_of_sub_IFDs, |
| 1017 |
sub_IFDs_offsets)) |
| 1018 |
goto failure; |
| 1019 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1020 |
/* Fails in 4.6.0 */ |
| 1021 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1022 |
/* write SubIFD */ |
| 1023 |
if (write_data_to_current_directory(tif, 700, false)) |
| 1024 |
{ |
| 1025 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1026 |
filename); |
| 1027 |
goto failure; |
| 1028 |
} |
| 1029 |
/* SUBFILETYPE tag is not mandatory for SubIFD writing, but a |
| 1030 |
* good idea to indicate thumbnails */ |
| 1031 |
if (!TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE)) |
| 1032 |
goto failure; |
| 1033 |
|
| 1034 |
/* Checkpoint SubIFD test */ |
| 1035 |
TIFFCheckpointDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1036 |
/* The next four CHECKKURDIRNUM_M fail in 4.6.0 */ |
| 1037 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1038 |
/* Overwriting works but re-writing will not. */ |
| 1039 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1040 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1041 |
/* Write another main-IFD */ |
| 1042 |
expected_curdir++; |
| 1043 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1044 |
{ |
| 1045 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1046 |
filename); |
| 1047 |
goto failure; |
| 1048 |
} |
| 1049 |
TIFFCheckpointDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1050 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1051 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1052 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1053 |
|
| 1054 |
TIFFClose(tif); |
| 1055 |
unlink(filename); |
| 1056 |
return 0; |
| 1057 |
|
| 1058 |
failure: |
| 1059 |
if (tif) |
| 1060 |
{ |
| 1061 |
TIFFClose(tif); |
| 1062 |
} |
| 1063 |
return 1; |
| 1064 |
} /*--- test_SubIFD_directrory_handling() ---*/ |
| 1065 |
|
| 1066 |
/* Test failure in TIFFSetSubDirectory() (see issue #618 and MR !543) |
| 1067 |
* |
| 1068 |
* If int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff) fails due to |
| 1069 |
* TIFFReadDirectory() error, subsequent calls to TIFFSetDirectory() cannot |
| 1070 |
* recover a consistent state. This is reproducible in master, 4.6, 4.5.1. |
| 1071 |
* This issue surfaced around the time when relative seeking |
| 1072 |
* support / IFD hash map optimiazions were introduced. This can be reproduced |
| 1073 |
* by opening an invalid SubIFD directory (e.g. w/o required ImageLength tag). |
| 1074 |
* The issue was fixed by MR !543. |
| 1075 |
*/ |
| 1076 |
int test_SetSubDirectory_failure(unsigned int openMode) |
| 1077 |
{ |
| 1078 |
char filename[128] = {0}; |
| 1079 |
|
| 1080 |
/* Define the number of sub-IFDs you are going to write */ |
| 1081 |
#define NUMBER_OF_SUBIFDs2 1 |
| 1082 |
uint16_t number_of_sub_IFDs = NUMBER_OF_SUBIFDs2; |
| 1083 |
toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs2] = { |
| 1084 |
0UL}; /* array for SubIFD tag */ |
| 1085 |
TIFF *tif; |
| 1086 |
tdir_t expected_curdir = 0; |
| 1087 |
|
| 1088 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 1089 |
{ |
| 1090 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 1091 |
openMode); |
| 1092 |
return 1; |
| 1093 |
} |
| 1094 |
|
| 1095 |
/* Get individual filenames and delete existent ones. */ |
| 1096 |
sprintf(filename, "test_SetSubDirectory_failure_%s.tif", |
| 1097 |
openModeStrings[openMode]); |
| 1098 |
unlink(filename); |
| 1099 |
|
| 1100 |
/* Create a file and write one directory with one corrupted sub-directory to |
| 1101 |
* it */ |
| 1102 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1103 |
if (!tif) |
| 1104 |
{ |
| 1105 |
fprintf(stderr, "Can't create %s\n", filename); |
| 1106 |
return 1; |
| 1107 |
} |
| 1108 |
|
| 1109 |
if (write_data_to_current_directory(tif, 300, false)) |
| 1110 |
{ |
| 1111 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1112 |
filename); |
| 1113 |
goto failure; |
| 1114 |
} |
| 1115 |
/* prepare to write next directory as SubIFD */ |
| 1116 |
if (!TIFFSetField(tif, TIFFTAG_SUBIFD, number_of_sub_IFDs, |
| 1117 |
sub_IFDs_offsets)) |
| 1118 |
goto failure; |
| 1119 |
if (!TIFFWriteDirectory(tif)) |
| 1120 |
{ |
| 1121 |
fprintf(stderr, "Can't write directory to %s\n", filename); |
| 1122 |
goto failure; |
| 1123 |
} |
| 1124 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1125 |
/* write corrupted SubIFD */ |
| 1126 |
fprintf(stderr, "----- Expect some error messages about 'scanline size is " |
| 1127 |
"zero' -----\n"); |
| 1128 |
if (write_data_to_current_directory(tif, 310, true)) |
| 1129 |
{ |
| 1130 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1131 |
filename); |
| 1132 |
goto failure; |
| 1133 |
} |
| 1134 |
/* SUBFILETYPE tag is not mandatory for SubIFD writing, but a |
| 1135 |
* good idea to indicate thumbnails */ |
| 1136 |
if (!TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE)) |
| 1137 |
goto failure; |
| 1138 |
|
| 1139 |
if (!TIFFWriteDirectory(tif)) |
| 1140 |
{ |
| 1141 |
fprintf(stderr, "Can't write directory to %s\n", filename); |
| 1142 |
goto failure; |
| 1143 |
} |
| 1144 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1145 |
TIFFClose(tif); |
| 1146 |
|
| 1147 |
/* Reopen prepared testfile */ |
| 1148 |
tif = TIFFOpen(filename, "r"); |
| 1149 |
if (!tif) |
| 1150 |
{ |
| 1151 |
fprintf(stderr, "Can't open %s\n", filename); |
| 1152 |
goto failure; |
| 1153 |
} |
| 1154 |
/* The first directory is already read through TIFFOpen() */ |
| 1155 |
if (!is_requested_directory(tif, 300, filename)) |
| 1156 |
{ |
| 1157 |
goto failure; |
| 1158 |
} |
| 1159 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1160 |
|
| 1161 |
/* Check if there are SubIFD subfiles */ |
| 1162 |
void *ptr; |
| 1163 |
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr)) |
| 1164 |
{ |
| 1165 |
/* Copy SubIFD array from pointer */ |
| 1166 |
memcpy(sub_IFDs_offsets, ptr, |
| 1167 |
number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0])); |
| 1168 |
|
| 1169 |
/* Read first SubIFD directory */ |
| 1170 |
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[0])) |
| 1171 |
{ |
| 1172 |
/* Attempt to set back to the main directory. |
| 1173 |
* Would fail if absolute seeking is not forced independent of |
| 1174 |
* TIFFReadDirectory success. */ |
| 1175 |
if (!TIFFSetDirectory(tif, 0)) |
| 1176 |
{ |
| 1177 |
fprintf(stderr, |
| 1178 |
"Failed to reset from not valid SubIFD back to " |
| 1179 |
"main directory. %s\n", |
| 1180 |
filename); |
| 1181 |
goto failure; |
| 1182 |
} |
| 1183 |
if (!is_requested_directory(tif, 300, filename)) |
| 1184 |
{ |
| 1185 |
goto failure; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
else |
| 1189 |
{ |
| 1190 |
if (!is_requested_directory(tif, 310, filename)) |
| 1191 |
{ |
| 1192 |
goto failure; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
/* Go back to main-IFD chain and re-read that main-IFD directory */ |
| 1196 |
if (!TIFFSetDirectory(tif, 0)) |
| 1197 |
goto failure; |
| 1198 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1199 |
} |
| 1200 |
|
| 1201 |
TIFFClose(tif); |
| 1202 |
unlink(filename); |
| 1203 |
return 0; |
| 1204 |
|
| 1205 |
failure: |
| 1206 |
if (tif) |
| 1207 |
{ |
| 1208 |
TIFFClose(tif); |
| 1209 |
} |
| 1210 |
return 1; |
| 1211 |
} /*--- test_SetSubDirectory_failure() ---*/ |
| 1212 |
|
| 1213 |
/* Checks that rewriting a directory does not break the directory linked |
| 1214 |
* list. |
| 1215 |
* |
| 1216 |
* This could happen because TIFFRewriteDirectory() relies on the traversal of |
| 1217 |
* the directory linked list in order to move the rewritten directory to the |
| 1218 |
* end of the file. This means the `lastdir_offset` optimization should be |
| 1219 |
* skipped, otherwise the linked list will be broken at the point where it |
| 1220 |
* connected to the rewritten directory, resulting in the loss of the |
| 1221 |
* directories that come after it. |
| 1222 |
* Rewriting the first directory requires an additional test, because it is |
| 1223 |
* treated differently from the directories that have a predecessor in the list. |
| 1224 |
*/ |
| 1225 |
int test_rewrite_lastdir_offset(unsigned int openMode) |
| 1226 |
{ |
| 1227 |
char filename[128] = {0}; |
| 1228 |
int i, count; |
| 1229 |
TIFF *tif; |
| 1230 |
|
| 1231 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 1232 |
{ |
| 1233 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 1234 |
openMode); |
| 1235 |
return 1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/* Get individual filenames and delete existent ones. */ |
| 1239 |
sprintf(filename, "test_directory_rewrite_%s.tif", |
| 1240 |
openModeStrings[openMode]); |
| 1241 |
unlink(filename); |
| 1242 |
|
| 1243 |
/* Create a file and write N_DIRECTORIES (10) directories to it. */ |
| 1244 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1245 |
if (!tif) |
| 1246 |
{ |
| 1247 |
fprintf(stderr, "Can't create %s\n", filename); |
| 1248 |
return 1; |
| 1249 |
} |
| 1250 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 1251 |
{ |
| 1252 |
if (write_data_to_current_directory(tif, i, false)) |
| 1253 |
{ |
| 1254 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1255 |
filename); |
| 1256 |
goto failure; |
| 1257 |
} |
| 1258 |
if (!TIFFWriteDirectory(tif)) |
| 1259 |
{ |
| 1260 |
fprintf(stderr, "Can't write directory to %s\n", filename); |
| 1261 |
goto failure; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
/* Without closing the file, go to the fifth directory |
| 1266 |
* and check, if dirn is requested directory. */ |
| 1267 |
TIFFSetDirectory(tif, 4); |
| 1268 |
if (!is_requested_directory(tif, 4, filename)) |
| 1269 |
{ |
| 1270 |
TIFFClose(tif); |
| 1271 |
return 4; |
| 1272 |
} |
| 1273 |
|
| 1274 |
/* Rewrite the fifth directory by calling TIFFRewriteDirectory |
| 1275 |
* and check, if the offset of IFD loaded by TIFFSetDirectory() is |
| 1276 |
* different. Then, the IFD loop directory list is correctly maintained for |
| 1277 |
* speed up of TIFFSetDirectory() with directly getting the offset that |
| 1278 |
* list. |
| 1279 |
*/ |
| 1280 |
uint64_t off1 = TIFFCurrentDirOffset(tif); |
| 1281 |
if (write_data_to_current_directory(tif, 4, false)) |
| 1282 |
{ |
| 1283 |
fprintf(stderr, "Can't write data to fifth directory in %s\n", |
| 1284 |
filename); |
| 1285 |
goto failure; |
| 1286 |
} |
| 1287 |
if (!TIFFRewriteDirectory(tif)) |
| 1288 |
{ |
| 1289 |
fprintf(stderr, "Can't rewrite fifth directory to %s\n", filename); |
| 1290 |
goto failure; |
| 1291 |
} |
| 1292 |
i = 4; |
| 1293 |
if (!TIFFSetDirectory(tif, i)) |
| 1294 |
{ |
| 1295 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 1296 |
goto failure; |
| 1297 |
} |
| 1298 |
uint64_t off2 = TIFFCurrentDirOffset(tif); |
| 1299 |
if (!is_requested_directory(tif, i, filename)) |
| 1300 |
{ |
| 1301 |
goto failure; |
| 1302 |
} |
| 1303 |
if (off1 == off2) |
| 1304 |
{ |
| 1305 |
fprintf(stderr, |
| 1306 |
"Rewritten directory %d was not correctly accessed by " |
| 1307 |
"TIFFSetDirectory() in file %s\n", |
| 1308 |
i, filename); |
| 1309 |
goto failure; |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* Now, perform the test for the first directory */ |
| 1313 |
TIFFSetDirectory(tif, 0); |
| 1314 |
if (!is_requested_directory(tif, 0, filename)) |
| 1315 |
{ |
| 1316 |
TIFFClose(tif); |
| 1317 |
return 5; |
| 1318 |
} |
| 1319 |
off1 = TIFFCurrentDirOffset(tif); |
| 1320 |
if (write_data_to_current_directory(tif, 0, false)) |
| 1321 |
{ |
| 1322 |
fprintf(stderr, "Can't write data to first directory in %s\n", |
| 1323 |
filename); |
| 1324 |
goto failure; |
| 1325 |
} |
| 1326 |
if (!TIFFRewriteDirectory(tif)) |
| 1327 |
{ |
| 1328 |
fprintf(stderr, "Can't rewrite first directory to %s\n", filename); |
| 1329 |
goto failure; |
| 1330 |
} |
| 1331 |
i = 0; |
| 1332 |
if (!TIFFSetDirectory(tif, i)) |
| 1333 |
{ |
| 1334 |
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename); |
| 1335 |
goto failure; |
| 1336 |
} |
| 1337 |
off2 = TIFFCurrentDirOffset(tif); |
| 1338 |
if (!is_requested_directory(tif, i, filename)) |
| 1339 |
{ |
| 1340 |
goto failure; |
| 1341 |
} |
| 1342 |
if (off1 == off2) |
| 1343 |
{ |
| 1344 |
fprintf(stderr, |
| 1345 |
"Rewritten directory %d was not correctly accessed by " |
| 1346 |
"TIFFSetDirectory() in file %s\n", |
| 1347 |
i, filename); |
| 1348 |
goto failure; |
| 1349 |
} |
| 1350 |
|
| 1351 |
TIFFClose(tif); |
| 1352 |
tif = NULL; |
| 1353 |
|
| 1354 |
/* Check that the file has the expected number of directories*/ |
| 1355 |
if (count_directories(filename, &count)) |
| 1356 |
{ |
| 1357 |
fprintf(stderr, "Error counting directories in file %s.\n", filename); |
| 1358 |
goto failure; |
| 1359 |
} |
| 1360 |
if (count != N_DIRECTORIES) |
| 1361 |
{ |
| 1362 |
fprintf(stderr, |
| 1363 |
"Unexpected number of directories in %s. Expected %i, " |
| 1364 |
"found %i.\n", |
| 1365 |
filename, N_DIRECTORIES, count); |
| 1366 |
goto failure; |
| 1367 |
} |
| 1368 |
unlink(filename); |
| 1369 |
return 0; |
| 1370 |
|
| 1371 |
failure: |
| 1372 |
if (tif) |
| 1373 |
{ |
| 1374 |
TIFFClose(tif); |
| 1375 |
} |
| 1376 |
return 1; |
| 1377 |
} /*-- test_rewrite_lastdir_offset() --*/ |
| 1378 |
|
| 1379 |
/* Compares multi-directory files written with and without the lastdir |
| 1380 |
* optimization */ |
| 1381 |
int test_lastdir_offset(unsigned int openMode) |
| 1382 |
{ |
| 1383 |
char filename_optimized[128] = {0}; |
| 1384 |
char filename_non_optimized[128] = {0}; |
| 1385 |
int i, count_optimized, count_non_optimized; |
| 1386 |
uint64_t offsets_base[N_DIRECTORIES]; |
| 1387 |
uint64_t offsets_comparison[N_DIRECTORIES]; |
| 1388 |
TIFF *tif; |
| 1389 |
|
| 1390 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 1391 |
{ |
| 1392 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 1393 |
openMode); |
| 1394 |
return 1; |
| 1395 |
} |
| 1396 |
|
| 1397 |
/* Get individual filenames and delete existent ones. */ |
| 1398 |
sprintf(filename_optimized, "test_directory_optimized_%s.tif", |
| 1399 |
openModeStrings[openMode]); |
| 1400 |
sprintf(filename_non_optimized, "test_directory_non_optimized_%s.tif", |
| 1401 |
openModeStrings[openMode]); |
| 1402 |
unlink(filename_optimized); |
| 1403 |
unlink(filename_non_optimized); |
| 1404 |
|
| 1405 |
/* First file: open it and add multiple directories. This uses the |
| 1406 |
* lastdir optimization. */ |
| 1407 |
tif = TIFFOpen(filename_optimized, openModeStrings[openMode]); |
| 1408 |
if (!tif) |
| 1409 |
{ |
| 1410 |
fprintf(stderr, "Can't create %s\n", filename_optimized); |
| 1411 |
return 1; |
| 1412 |
} |
| 1413 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 1414 |
{ |
| 1415 |
if (write_data_to_current_directory(tif, i, false)) |
| 1416 |
{ |
| 1417 |
fprintf(stderr, "Can't write data to current directory in %s\n", |
| 1418 |
filename_optimized); |
| 1419 |
goto failure; |
| 1420 |
} |
| 1421 |
if (!TIFFWriteDirectory(tif)) |
| 1422 |
{ |
| 1423 |
fprintf(stderr, "Can't write directory to %s\n", |
| 1424 |
filename_optimized); |
| 1425 |
goto failure; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
TIFFClose(tif); |
| 1429 |
tif = NULL; |
| 1430 |
|
| 1431 |
/* Second file: close it after adding each directory. This avoids the |
| 1432 |
* lastdir optimization. */ |
| 1433 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 1434 |
{ |
| 1435 |
if (write_directory_to_closed_file(filename_non_optimized, openMode, i)) |
| 1436 |
{ |
| 1437 |
fprintf(stderr, "Can't write directory to %s\n", |
| 1438 |
filename_non_optimized); |
| 1439 |
goto failure; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* Check that both files have the expected number of directories */ |
| 1444 |
if (count_directories(filename_optimized, &count_optimized)) |
| 1445 |
{ |
| 1446 |
fprintf(stderr, "Error counting directories in file %s.\n", |
| 1447 |
filename_optimized); |
| 1448 |
goto failure; |
| 1449 |
} |
| 1450 |
if (count_optimized != N_DIRECTORIES) |
| 1451 |
{ |
| 1452 |
fprintf(stderr, |
| 1453 |
"Unexpected number of directories in %s. Expected %i, " |
| 1454 |
"found %i.\n", |
| 1455 |
filename_optimized, N_DIRECTORIES, count_optimized); |
| 1456 |
goto failure; |
| 1457 |
} |
| 1458 |
if (count_directories(filename_non_optimized, &count_non_optimized)) |
| 1459 |
{ |
| 1460 |
fprintf(stderr, "Error counting directories in file %s.\n", |
| 1461 |
filename_non_optimized); |
| 1462 |
goto failure; |
| 1463 |
} |
| 1464 |
if (count_non_optimized != N_DIRECTORIES) |
| 1465 |
{ |
| 1466 |
fprintf(stderr, |
| 1467 |
"Unexpected number of directories in %s. Expected %i, " |
| 1468 |
"found %i.\n", |
| 1469 |
filename_non_optimized, N_DIRECTORIES, count_non_optimized); |
| 1470 |
goto failure; |
| 1471 |
} |
| 1472 |
|
| 1473 |
/* Check that both files have the same directory offsets */ |
| 1474 |
/* In parallel of comparing offsets between optimized base file and |
| 1475 |
* non-optimized file, test also three methods of walking through the |
| 1476 |
* IFD chain within get_dir_offsets(). This tests the optimization of |
| 1477 |
* faster SetDirectory(). */ |
| 1478 |
if (get_dir_offsets(filename_optimized, offsets_base, |
| 1479 |
DirWalkMode_ReadDirectory)) |
| 1480 |
{ |
| 1481 |
fprintf(stderr, "Error reading directory offsets from %s.\n", |
| 1482 |
filename_optimized); |
| 1483 |
goto failure; |
| 1484 |
} |
| 1485 |
for (int file_i = 0; file_i < 2; ++file_i) |
| 1486 |
{ |
| 1487 |
const char *filename = |
| 1488 |
(file_i == 0) ? filename_optimized : filename_non_optimized; |
| 1489 |
|
| 1490 |
for (enum DirWalkMode mode = DirWalkMode_ReadDirectory; |
| 1491 |
mode <= DirWalkMode_SetDirectory_Reverse; ++mode) |
| 1492 |
{ |
| 1493 |
if (get_dir_offsets(filename, offsets_comparison, mode)) |
| 1494 |
{ |
| 1495 |
fprintf(stderr, |
| 1496 |
"Error reading directory offsets from %s in mode %d.\n", |
| 1497 |
filename, mode); |
| 1498 |
goto failure; |
| 1499 |
} |
| 1500 |
for (i = 0; i < N_DIRECTORIES; i++) |
| 1501 |
{ |
| 1502 |
if (offsets_base[i] != offsets_comparison[i]) |
| 1503 |
{ |
| 1504 |
fprintf(stderr, |
| 1505 |
"Unexpected directory offset for directory %i, " |
| 1506 |
"expected " |
| 1507 |
"offset %" PRIu64 " but got %" PRIu64 ".\n", |
| 1508 |
i, offsets_base[i], offsets_comparison[i]); |
| 1509 |
goto failure; |
| 1510 |
} |
| 1511 |
} |
| 1512 |
} |
| 1513 |
} |
| 1514 |
unlink(filename_optimized); |
| 1515 |
unlink(filename_non_optimized); |
| 1516 |
return 0; |
| 1517 |
|
| 1518 |
failure: |
| 1519 |
if (tif) |
| 1520 |
{ |
| 1521 |
TIFFClose(tif); |
| 1522 |
} |
| 1523 |
return 1; |
| 1524 |
} /*-- test_lastdir_offset() --*/ |
| 1525 |
|
| 1526 |
/* Adds an EXIF IFD with some default values to the active IFD. |
| 1527 |
*/ |
| 1528 |
int write_EXIF_data_to_current_directory(TIFF *tif, int i, |
| 1529 |
uint64_t *dir_offset_EXIF) |
| 1530 |
{ |
| 1531 |
char auxString[128]; |
| 1532 |
|
| 1533 |
if (!tif) |
| 1534 |
{ |
| 1535 |
fprintf(stderr, "Invalid TIFF handle.\n"); |
| 1536 |
return 1; |
| 1537 |
} |
| 1538 |
|
| 1539 |
/*-- Now create an EXIF directory. */ |
| 1540 |
if (TIFFCreateEXIFDirectory(tif) != 0) |
| 1541 |
{ |
| 1542 |
fprintf(stderr, "TIFFCreateEXIFDirectory() failed.\n"); |
| 1543 |
return 1; |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (!TIFFSetField(tif, EXIFTAG_EXIFVERSION, "0231")) |
| 1547 |
{ |
| 1548 |
fprintf(stderr, "Can't write EXIFTAG_EXIFVERSION\n"); |
| 1549 |
return 1; |
| 1550 |
} |
| 1551 |
|
| 1552 |
/* Write IFD identification number to ASCII string of SUBSECTIMEORIGINAL |
| 1553 |
* tag. */ |
| 1554 |
sprintf(auxString, "EXIF-%d-EXIF", i); |
| 1555 |
if (!TIFFSetField(tif, EXIFTAG_SUBSECTIMEORIGINAL, auxString)) |
| 1556 |
{ |
| 1557 |
fprintf(stderr, "Can't set EXIFTAG_SUBSECTIMEORIGINAL tag.\n"); |
| 1558 |
return 1; |
| 1559 |
} |
| 1560 |
|
| 1561 |
if (!TIFFWriteCustomDirectory(tif, dir_offset_EXIF)) |
| 1562 |
{ |
| 1563 |
fprintf(stderr, "TIFFWriteCustomDirectory() with EXIF failed.\n"); |
| 1564 |
return 1; |
| 1565 |
} |
| 1566 |
|
| 1567 |
return 0; |
| 1568 |
} /*-- write_EXIF_data_to_current_directory() --*/ |
| 1569 |
|
| 1570 |
/* Test correct incrementing of tif_curdir TIFFCurrentDirectory() |
| 1571 |
* |
| 1572 |
* Up to 4.6.0 there are cases where tif_curdir is set inconsistently or |
| 1573 |
* incorrectly, depending on the previous history. This function shows some of |
| 1574 |
* these cases and tests them after the improvements. |
| 1575 |
*/ |
| 1576 |
int test_current_dirnum_incrementing(int testcase, unsigned int openMode) |
| 1577 |
{ |
| 1578 |
#define N_DIRECTORIES_2 2 |
| 1579 |
char filename[128] = {0}; |
| 1580 |
tdir_t lastdir; |
| 1581 |
tdir_t expected_curdir = (tdir_t)(-1); |
| 1582 |
|
| 1583 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 1584 |
{ |
| 1585 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 1586 |
openMode); |
| 1587 |
return 1; |
| 1588 |
} |
| 1589 |
|
| 1590 |
/* Get individual filenames and delete existent ones. */ |
| 1591 |
sprintf(filename, "test_current_dirnum_incrementing_%s.tif", |
| 1592 |
openModeStrings[openMode]); |
| 1593 |
unlink(filename); |
| 1594 |
|
| 1595 |
/* Create a file and write N_DIRECTORIES_2 directories to it. */ |
| 1596 |
TIFF *tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1597 |
if (!tif) |
| 1598 |
{ |
| 1599 |
fprintf(stderr, "Can't create %s\n", filename); |
| 1600 |
return 1; |
| 1601 |
} |
| 1602 |
|
| 1603 |
/*-- Standard case for writing sequential IFDs. --*/ |
| 1604 |
for (int i = 0; i < N_DIRECTORIES_2; i++) |
| 1605 |
{ |
| 1606 |
if (write_data_to_current_directory(tif, i, false)) |
| 1607 |
{ |
| 1608 |
fprintf(stderr, |
| 1609 |
"Can't write data to current directory in %s at %d\n", |
| 1610 |
filename, __LINE__); |
| 1611 |
goto failure; |
| 1612 |
} |
| 1613 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1614 |
expected_curdir++; |
| 1615 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1616 |
} |
| 1617 |
|
| 1618 |
/*-- Standard case for sequential reading up to the end. --*/ |
| 1619 |
expected_curdir = 0; |
| 1620 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1621 |
while (TIFFReadDirectory(tif)) |
| 1622 |
{ |
| 1623 |
expected_curdir++; |
| 1624 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1625 |
} |
| 1626 |
/* Last read for next_offset 0 shall not increment tif_curdir. */ |
| 1627 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1628 |
|
| 1629 |
/* Now, go back to IFD 0 and overwrite it in the first loop and rewrite it |
| 1630 |
* in the second loop (due to "i" having two digits then). |
| 1631 |
* tif_curdir should stay to that IFD-number. */ |
| 1632 |
expected_curdir = 0; |
| 1633 |
for (unsigned int i = 9; i < 11; i++) |
| 1634 |
{ |
| 1635 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1636 |
if (write_data_to_current_directory(tif, i, false)) |
| 1637 |
{ |
| 1638 |
fprintf(stderr, |
| 1639 |
"Can't write data to current directory in %s at %d\n", |
| 1640 |
filename, __LINE__); |
| 1641 |
goto failure; |
| 1642 |
} |
| 1643 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1644 |
/* Fails in 4.6.0 */ |
| 1645 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1646 |
} |
| 1647 |
|
| 1648 |
/* Now, go back to IFD 1 and overwrite it in the first loop and rewrite it |
| 1649 |
* in the second loop (due to "i" having two digits then). |
| 1650 |
* tif_curdir should stay to that IFD-number. */ |
| 1651 |
expected_curdir = 1; |
| 1652 |
for (unsigned int i = 8; i < 13; i += 4) |
| 1653 |
{ |
| 1654 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1655 |
if (write_data_to_current_directory(tif, i, false)) |
| 1656 |
{ |
| 1657 |
fprintf(stderr, |
| 1658 |
"Can't write data to current directory in %s at %d\n", |
| 1659 |
filename, __LINE__); |
| 1660 |
goto failure; |
| 1661 |
} |
| 1662 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1663 |
/* Fails in 4.6.0 */ |
| 1664 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1665 |
} |
| 1666 |
|
| 1667 |
/* Set IFD 0 and append new IFDs. */ |
| 1668 |
if (testcase == 0) |
| 1669 |
{ |
| 1670 |
expected_curdir = TIFFNumberOfDirectories(tif); |
| 1671 |
} |
| 1672 |
else if (testcase == 1) |
| 1673 |
{ |
| 1674 |
TIFFClose(tif); |
| 1675 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1676 |
expected_curdir = 0; |
| 1677 |
} |
| 1678 |
else if (testcase == 2) |
| 1679 |
{ |
| 1680 |
expected_curdir = TIFFNumberOfDirectories(tif); |
| 1681 |
TIFFClose(tif); |
| 1682 |
/* Replace 'w' for write by 'a' for append. */ |
| 1683 |
char strAux[8] = {0}; |
| 1684 |
strncpy(strAux, openModeStrings[openMode], sizeof(strAux) - 1); |
| 1685 |
strAux[0] = 'a'; |
| 1686 |
tif = TIFFOpen(filename, strAux); |
| 1687 |
} |
| 1688 |
for (unsigned int setdir = 0; setdir < 3; setdir += 2) |
| 1689 |
{ |
| 1690 |
for (unsigned int i = 0; i < 2; i++) |
| 1691 |
{ |
| 1692 |
if (testcase == 0) |
| 1693 |
{ |
| 1694 |
TIFFSetDirectory_M(tif, setdir, filename, __LINE__); |
| 1695 |
TIFFCreateDirectory(tif); |
| 1696 |
} |
| 1697 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1698 |
{ |
| 1699 |
fprintf( |
| 1700 |
stderr, |
| 1701 |
"Can't write data to current directory in %s at line %d\n", |
| 1702 |
filename, __LINE__); |
| 1703 |
goto failure; |
| 1704 |
} |
| 1705 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1706 |
/* Fails in 4.6.0, except for testcase TIFFOpen("w") */ |
| 1707 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1708 |
expected_curdir++; |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
/*-- Several CheckpointDirectory() testing --*/ |
| 1713 |
/* Set state such as if the last directory would have been just written. |
| 1714 |
* - read last IFD from file and then |
| 1715 |
* - create and activate a newly initialized IFD, not written yet. */ |
| 1716 |
if (testcase == 0) |
| 1717 |
{ |
| 1718 |
lastdir = TIFFNumberOfDirectories(tif) - 1; |
| 1719 |
expected_curdir = lastdir + 1; |
| 1720 |
TIFFSetDirectory(tif, lastdir); |
| 1721 |
TIFFCreateDirectory(tif); |
| 1722 |
} |
| 1723 |
else if (testcase == 1) |
| 1724 |
{ |
| 1725 |
TIFFClose(tif); |
| 1726 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1727 |
expected_curdir = 0; |
| 1728 |
} |
| 1729 |
/* Fill the new IFD and checkpoint several times before writing. */ |
| 1730 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1731 |
{ |
| 1732 |
fprintf(stderr, "Can't write data to current directory in %s at %d\n", |
| 1733 |
filename, __LINE__); |
| 1734 |
goto failure; |
| 1735 |
} |
| 1736 |
for (unsigned int i = 0; i < 2; i++) |
| 1737 |
{ |
| 1738 |
TIFFCheckpointDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1739 |
/* Fails in 4.6.0 only in second loop */ |
| 1740 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1741 |
if (i == 1) |
| 1742 |
{ |
| 1743 |
/* In last loop force IFD to be rewritten. */ |
| 1744 |
TIFFSetField(tif, TIFFTAG_PAGENAME, "6.th IFD rewritten"); |
| 1745 |
} |
| 1746 |
TIFFCheckpointDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1747 |
/* Fails in 4.6.0 */ |
| 1748 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1749 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1750 |
/* Fails in 4.6.0 */ |
| 1751 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1752 |
/* Prepare checkpointing of same IFD in next loop after reading it from |
| 1753 |
* file. */ |
| 1754 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1755 |
} |
| 1756 |
|
| 1757 |
/*-- RewriteDirectory() testing --*/ |
| 1758 |
/* RewriteDirectory() forces to write the IFD to another location |
| 1759 |
* and set-up a new, empty IFD. */ |
| 1760 |
/* Set state such as if the last directory would have been just written. |
| 1761 |
* - read last IFD from file and then |
| 1762 |
* - create and activate a newly initialized IFD, not written yet. */ |
| 1763 |
if (testcase == 0 || testcase == 2) |
| 1764 |
{ |
| 1765 |
lastdir = TIFFNumberOfDirectories(tif) - 1; |
| 1766 |
} |
| 1767 |
else |
| 1768 |
{ |
| 1769 |
lastdir = expected_curdir; |
| 1770 |
} |
| 1771 |
TIFFSetDirectory(tif, lastdir); |
| 1772 |
TIFFCreateDirectory(tif); |
| 1773 |
expected_curdir = lastdir + 1; |
| 1774 |
/* Write a new IFD using TIFFRewriteDirectory, read it and rewrite it again. |
| 1775 |
* First with curdir set to the last IFD and then set to the first one. */ |
| 1776 |
for (unsigned int i = 0; i < 2; i++) |
| 1777 |
{ |
| 1778 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1779 |
{ |
| 1780 |
fprintf(stderr, |
| 1781 |
"Can't write data to current directory in %s at %d\n", |
| 1782 |
filename, __LINE__); |
| 1783 |
goto failure; |
| 1784 |
} |
| 1785 |
if (!TIFFRewriteDirectory(tif)) |
| 1786 |
{ |
| 1787 |
fprintf(stderr, "Can't rewrite directory to %s at line %d\n", |
| 1788 |
filename, __LINE__); |
| 1789 |
goto failure; |
| 1790 |
} |
| 1791 |
/* Fails in 4.6.0 only in second loop */ |
| 1792 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1793 |
/* Read IFD and write it to another location. */ |
| 1794 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1795 |
if (!TIFFRewriteDirectory(tif)) |
| 1796 |
{ |
| 1797 |
fprintf(stderr, "Can't rewrite directory to %s at line %d\n", |
| 1798 |
filename, __LINE__); |
| 1799 |
goto failure; |
| 1800 |
} |
| 1801 |
/* Fails in 4.6.0 */ |
| 1802 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1803 |
expected_curdir++; |
| 1804 |
/* Second loop starts with IFD 0 read from file. */ |
| 1805 |
TIFFSetDirectory_M(tif, 0, filename, __LINE__); |
| 1806 |
TIFFCreateDirectory(tif); |
| 1807 |
} |
| 1808 |
|
| 1809 |
/*-- CustomDirectory testing --*/ |
| 1810 |
/* Set state such as if the last directory would have been just written. |
| 1811 |
* - Read last IFD from file and then |
| 1812 |
* - create and activate a newly initialized IFD, not written yet and |
| 1813 |
* attach an EXIF IFD to it with forcing a re-write of the IFD by |
| 1814 |
* adding the EXIF-IFD tag. |
| 1815 |
* - Do that two times: |
| 1816 |
* - first set last IFD and append a new one, |
| 1817 |
* - second set first IFD and append a new one. */ |
| 1818 |
if (testcase == 0 || testcase == 1) |
| 1819 |
{ |
| 1820 |
lastdir = TIFFNumberOfDirectories(tif) - 1; |
| 1821 |
} |
| 1822 |
else |
| 1823 |
{ |
| 1824 |
lastdir = expected_curdir - 1; |
| 1825 |
} |
| 1826 |
TIFFSetDirectory(tif, lastdir); |
| 1827 |
TIFFCreateDirectory(tif); |
| 1828 |
expected_curdir = lastdir + 1; |
| 1829 |
uint64_t offsetEXIFBase[2]; |
| 1830 |
for (unsigned int i = 0; i < 2; i++) |
| 1831 |
{ |
| 1832 |
if (write_data_to_current_directory(tif, expected_curdir, false)) |
| 1833 |
{ |
| 1834 |
fprintf(stderr, |
| 1835 |
"Can't write data to current directory in %s at %d\n", |
| 1836 |
filename, __LINE__); |
| 1837 |
goto failure; |
| 1838 |
} |
| 1839 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1840 |
/* Fails in 4.6.0 only in second loop */ |
| 1841 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1842 |
if (write_EXIF_data_to_current_directory(tif, i, &offsetEXIFBase[i])) |
| 1843 |
{ |
| 1844 |
fprintf(stderr, |
| 1845 |
"Can't write EXIF data to current directory in %s. " |
| 1846 |
"Testline %d\n", |
| 1847 |
filename, __LINE__); |
| 1848 |
goto failure; |
| 1849 |
} |
| 1850 |
/* CustomDirectory invalidates directory number. */ |
| 1851 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 1852 |
/* Go back to current main-IFD and update EXIF-IFD offset. */ |
| 1853 |
TIFFSetDirectory_M(tif, expected_curdir, filename, __LINE__); |
| 1854 |
if (!TIFFSetField(tif, TIFFTAG_EXIFIFD, offsetEXIFBase[i])) |
| 1855 |
{ |
| 1856 |
fprintf(stderr, "Can't set TIFFTAG_EXIFIFD tag. Testline %d\n", |
| 1857 |
__LINE__); |
| 1858 |
goto failure; |
| 1859 |
} |
| 1860 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1861 |
/* Fails in 4.6.0 */ |
| 1862 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1863 |
expected_curdir++; |
| 1864 |
|
| 1865 |
/* Second loop starts with IFD 0 read from file. */ |
| 1866 |
TIFFSetDirectory_M(tif, 0, filename, __LINE__); |
| 1867 |
TIFFCreateDirectory(tif); |
| 1868 |
} |
| 1869 |
|
| 1870 |
/*-- Read EXIF Custom-Directory --*/ |
| 1871 |
for (unsigned int i = 0; i < 2; i++) |
| 1872 |
{ |
| 1873 |
if (!TIFFReadEXIFDirectory(tif, offsetEXIFBase[i])) |
| 1874 |
{ |
| 1875 |
fprintf(stderr, |
| 1876 |
"TIFFReadEXIFDirectory(%" PRIu64 ") failed for %s " |
| 1877 |
"at %d\n", |
| 1878 |
offsetEXIFBase[i], filename, __LINE__); |
| 1879 |
goto failure; |
| 1880 |
} |
| 1881 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1882 |
} |
| 1883 |
|
| 1884 |
/*-- Test for fail of ReadDirectory() and fail of SetDirectory() --*/ |
| 1885 |
/*-- Writing IFD0, IFD1 as valid IFD and IFD2 as corrupted. --*/ |
| 1886 |
uint64_t offsetBase[4]; |
| 1887 |
TIFFClose(tif); |
| 1888 |
tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 1889 |
expected_curdir = 0; |
| 1890 |
fprintf(stderr, "----- Expect some error messages about 'scanline size is " |
| 1891 |
"zero' -----\n"); |
| 1892 |
for (int i = 0; i < 3; i++) |
| 1893 |
{ |
| 1894 |
if (write_data_to_current_directory(tif, i, (i == 2 ? true : false))) |
| 1895 |
{ |
| 1896 |
fprintf(stderr, |
| 1897 |
"Can't write data to current directory in %s at %d\n", |
| 1898 |
filename, __LINE__); |
| 1899 |
goto failure; |
| 1900 |
} |
| 1901 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 1902 |
/* Expect offsetBase{0,0,0} here, because TIFFWriteDirectory() returns |
| 1903 |
* a new and empty IFD after writing. */ |
| 1904 |
offsetBase[i] = TIFFCurrentDirOffset(tif); |
| 1905 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 1906 |
expected_curdir++; |
| 1907 |
} |
| 1908 |
TIFFSetDirectory_M(tif, 0, filename, __LINE__); |
| 1909 |
offsetBase[0] = TIFFCurrentDirOffset(tif); |
| 1910 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1911 |
TIFFSetDirectory_M(tif, 1, filename, __LINE__); |
| 1912 |
offsetBase[1] = TIFFCurrentDirOffset(tif); |
| 1913 |
CHECKCURDIRNUM_M(tif, 1, __LINE__); |
| 1914 |
/* TIFFReadDirectory() shall fail but curdir shall be incremented, |
| 1915 |
* because parts of corrupt IFD2 are read in. */ |
| 1916 |
if (TIFFReadDirectory(tif)) |
| 1917 |
{ |
| 1918 |
fprintf(stderr, |
| 1919 |
"TIFFReadDirectory() for IFD2 was expected to fail but " |
| 1920 |
"succeeded for %s " |
| 1921 |
"at %d\n", |
| 1922 |
filename, __LINE__); |
| 1923 |
goto failure; |
| 1924 |
} |
| 1925 |
offsetBase[2] = TIFFCurrentDirOffset(tif); |
| 1926 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 1927 |
TIFFSetDirectory_M(tif, 0, filename, __LINE__); |
| 1928 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1929 |
/* TIFFSetDirectory() shall fail on IFD2 but curdir shall be incremented, |
| 1930 |
* because parts of corrupt IFD2 are read in. */ |
| 1931 |
if (TIFFSetDirectory(tif, 2)) |
| 1932 |
{ |
| 1933 |
fprintf(stderr, |
| 1934 |
"TIFFSetDirectory(2) was expected to fail but succeeded for %s " |
| 1935 |
"at %d\n", |
| 1936 |
filename, __LINE__); |
| 1937 |
goto failure; |
| 1938 |
} |
| 1939 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 1940 |
/* Also TIFFSetSubDirectory() shall fail for IFD2 and curdir shall be |
| 1941 |
* set correctly to 2. Therefore, set it back to IFD0 first. */ |
| 1942 |
TIFFSetDirectory_M(tif, 0, filename, __LINE__); |
| 1943 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1944 |
if (TIFFSetSubDirectory(tif, offsetBase[2])) |
| 1945 |
{ |
| 1946 |
fprintf(stderr, |
| 1947 |
"TIFFSetSubDirectory(%" PRIu64 |
| 1948 |
") was expected to fail but succeeded for %s " |
| 1949 |
"at %d\n", |
| 1950 |
offsetBase[2], filename, __LINE__); |
| 1951 |
goto failure; |
| 1952 |
} |
| 1953 |
/* Fails in 4.6.0 */ |
| 1954 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 1955 |
|
| 1956 |
/* However, TIFFSetSubDirectory() on a non valid IFD "offset" shall fail, |
| 1957 |
* and tif_curdir shall be set to "unknown" (-1). */ |
| 1958 |
fprintf(stderr, "----- Expect error messages about 'Sanity check AND " |
| 1959 |
"Failed to read directory' -----\n"); |
| 1960 |
if (TIFFSetSubDirectory(tif, 10)) |
| 1961 |
{ |
| 1962 |
fprintf(stderr, |
| 1963 |
"TIFFSetSubDirectory(%" PRIu64 |
| 1964 |
") was expected to fail but succeeded for %s " |
| 1965 |
"at %d\n", |
| 1966 |
offsetBase[1], filename, __LINE__); |
| 1967 |
goto failure; |
| 1968 |
} |
| 1969 |
/* Fails in 4.6.0 */ |
| 1970 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 1971 |
/* Set IFD0 for next test */ |
| 1972 |
TIFFSetSubDirectory(tif, offsetBase[0]); |
| 1973 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 1974 |
|
| 1975 |
/* TIFFSetSubDirectory(0) shall invalidate the active IFD read from file by |
| 1976 |
* setting tif_nextdiroff and tif_diroff=0 */ |
| 1977 |
TIFFSetSubDirectory(tif, 0); |
| 1978 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 1979 |
|
| 1980 |
/*-- Patch offset of IFD2 to not existing IFD3 without entries. |
| 1981 |
* Thus TIFFFetchDirectory() will fail. --*/ |
| 1982 |
#define TIFFReadFile_M(tif, buf, size) \ |
| 1983 |
((*TIFFGetReadProc(tif))(TIFFClientdata(tif), (buf), (size))); |
| 1984 |
#define TIFFWriteFile_M(tif, buf, size) \ |
| 1985 |
((*TIFFGetWriteProc(tif))(TIFFClientdata(tif), (buf), (size))); |
| 1986 |
#define TIFFSeekFile_M(tif, off, whence) \ |
| 1987 |
((*TIFFGetSeekProc(tif))(TIFFClientdata(tif), (off), (whence))); |
| 1988 |
|
| 1989 |
/* Code below does only handle Classic-TIFF without swapping". */ |
| 1990 |
if (!(TIFFIsByteSwapped(tif) || TIFFIsBigTIFF(tif))) |
| 1991 |
{ |
| 1992 |
uint64_t ss = TIFFSeekFile_M(tif, offsetBase[2], 0); |
| 1993 |
uint16_t cnt = 0; |
| 1994 |
uint64_t rr = TIFFReadFile_M(tif, &cnt, 2); |
| 1995 |
ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0); |
| 1996 |
uint32_t wt = (uint32_t)ss; |
| 1997 |
rr = TIFFWriteFile_M(tif, &wt, 4); |
| 1998 |
(void)rr; |
| 1999 |
|
| 2000 |
/* Now there are offsets to four IFDs in the file, where the last one is |
| 2001 |
* not existing and has a non-valid dircount and entries behind EOF. */ |
| 2002 |
fprintf(stderr, "----- Expect error messages about 'Error fetching " |
| 2003 |
"directory link.' -----\n"); |
| 2004 |
/* TIFFNumberOfDirectories() returns 3 */ |
| 2005 |
lastdir = TIFFNumberOfDirectories(tif); |
| 2006 |
TIFFSetDirectory(tif, 0); |
| 2007 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 2008 |
fprintf(stderr, "----- Expect error messages about 'Cannot read TIFF " |
| 2009 |
"directory.' -----\n"); |
| 2010 |
if (TIFFSetDirectory(tif, 3)) |
| 2011 |
{ |
| 2012 |
fprintf(stderr, |
| 2013 |
"TIFFSetDirectory(3) for IFD4 was expected to fail but " |
| 2014 |
"succeeded for %s " |
| 2015 |
"at %d\n", |
| 2016 |
filename, __LINE__); |
| 2017 |
goto failure; |
| 2018 |
} |
| 2019 |
/* Fails in 4.6.0 */ |
| 2020 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 2021 |
offsetBase[3] = TIFFCurrentDirOffset(tif); |
| 2022 |
|
| 2023 |
/* Point IFD3 to a location within the file, where it has now a |
| 2024 |
* non-valid dircount=0. */ |
| 2025 |
ss = TIFFSeekFile_M(tif, offsetBase[2] + cnt * 12 + 2, 0); |
| 2026 |
wt = (uint32_t)(offsetBase[1] + 8); |
| 2027 |
rr = TIFFWriteFile_M(tif, &wt, 4); |
| 2028 |
fprintf(stderr, "----- Expect error messages about 'Error fetching " |
| 2029 |
"directory link.' -----\n"); |
| 2030 |
/* TIFFNumberOfDirectories() returns now 4 */ |
| 2031 |
lastdir = TIFFNumberOfDirectories(tif); |
| 2032 |
TIFFSetDirectory(tif, 0); |
| 2033 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 2034 |
fprintf(stderr, |
| 2035 |
"----- Expect error messages about 'Failed to allocate " |
| 2036 |
"memory for to read TIFF directory.' AND 'Failed to read " |
| 2037 |
"directory ..' -----\n"); |
| 2038 |
if (TIFFSetDirectory(tif, 3)) |
| 2039 |
{ |
| 2040 |
fprintf(stderr, |
| 2041 |
"TIFFSetDirectory(3) for IFD3 was expected to fail but " |
| 2042 |
"succeeded for %s " |
| 2043 |
"at %d\n", |
| 2044 |
filename, __LINE__); |
| 2045 |
goto failure; |
| 2046 |
} |
| 2047 |
/* Fails in 4.6.0 */ |
| 2048 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 2049 |
} |
| 2050 |
|
| 2051 |
unlink(filename); |
| 2052 |
return 0; |
| 2053 |
|
| 2054 |
failure: |
| 2055 |
if (tif) |
| 2056 |
{ |
| 2057 |
TIFFClose(tif); |
| 2058 |
} |
| 2059 |
return 1; |
| 2060 |
} /*-- test_current_dirnum_incrementing() --*/ |
| 2061 |
|
| 2062 |
/* Test correct setting of tif_curdircount. |
| 2063 |
* |
| 2064 |
*/ |
| 2065 |
int test_curdircount_setting(unsigned int openMode) |
| 2066 |
{ |
| 2067 |
#define N_DIRECTORIES_2 2 |
| 2068 |
char filename[128] = {0}; |
| 2069 |
tdir_t expected_curdir = (tdir_t)(-1); |
| 2070 |
|
| 2071 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 2072 |
{ |
| 2073 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 2074 |
openMode); |
| 2075 |
return 1; |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* Get individual filenames and delete existent ones. */ |
| 2079 |
sprintf(filename, "test_dircount_%s.tif", openModeStrings[openMode]); |
| 2080 |
unlink(filename); |
| 2081 |
|
| 2082 |
/* Create a file and write N_DIRECTORIES_2 directories to it. */ |
| 2083 |
TIFF *tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 2084 |
if (!tif) |
| 2085 |
{ |
| 2086 |
fprintf(stderr, "Can't create %s\n", filename); |
| 2087 |
return 1; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/*-- Standard case for writing sequential IFDs. --*/ |
| 2091 |
for (int i = 0; i < N_DIRECTORIES_2; i++) |
| 2092 |
{ |
| 2093 |
if (write_data_to_current_directory(tif, i, false)) |
| 2094 |
{ |
| 2095 |
fprintf(stderr, |
| 2096 |
"Can't write data to current directory in %s at %d\n", |
| 2097 |
filename, __LINE__); |
| 2098 |
goto failure; |
| 2099 |
} |
| 2100 |
TIFFWriteDirectory_M(tif, filename, __LINE__); |
| 2101 |
expected_curdir++; |
| 2102 |
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__); |
| 2103 |
} |
| 2104 |
TIFFClose(tif); |
| 2105 |
|
| 2106 |
/* Simulate case for trap in TIFFLinkDirectory() where tif_lastdiroff is set |
| 2107 |
* but tif_curdircount is not. |
| 2108 |
* https://gitlab.com/libtiff/libtiff/-/merge_requests/634#note_1990250515 |
| 2109 |
*/ |
| 2110 |
tif = TIFFOpen(filename, "r+"); |
| 2111 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 2112 |
TIFFRewriteDirectory(tif); |
| 2113 |
CHECKCURDIRNUM_M(tif, 0, __LINE__); |
| 2114 |
TIFFCreateDirectory(tif); |
| 2115 |
TIFFWriteDirectory(tif); |
| 2116 |
CHECKCURDIRNUM_M(tif, 2, __LINE__); |
| 2117 |
tdir_t numdir = TIFFNumberOfDirectories(tif); |
| 2118 |
TIFFClose(tif); |
| 2119 |
|
| 2120 |
/* Testcase iswrittentofile=0 for SetSubDirectory(0). */ |
| 2121 |
tif = TIFFOpen(filename, "r+"); |
| 2122 |
TIFFSetSubDirectory(tif, 0); |
| 2123 |
/* After TIFFSetSubDirectory(tif, 0) the read IFD is re-written as a |
| 2124 |
* new IFD and tif_curdir and tif_curdircount shall be updated. |
| 2125 |
* The latter was not the case before resetting iswrittentofile=0 at |
| 2126 |
* SetSubDirectory(0). Even an IFD-Loop was then wrongly assumed at the next |
| 2127 |
* TIFFNumberOfDirectories() call. */ |
| 2128 |
TIFFWriteDirectory(tif); |
| 2129 |
CHECKCURDIRNUM_M(tif, numdir, __LINE__); |
| 2130 |
|
| 2131 |
/* Unlink all directories. */ |
| 2132 |
numdir = TIFFNumberOfDirectories(tif); |
| 2133 |
for (int i = numdir; 0 < i; i--) |
| 2134 |
{ |
| 2135 |
TIFFUnlinkDirectory(tif, i); |
| 2136 |
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__); |
| 2137 |
} |
| 2138 |
|
| 2139 |
unlink(filename); |
| 2140 |
return 0; |
| 2141 |
|
| 2142 |
failure: |
| 2143 |
TIFFClose(tif); |
| 2144 |
return 1; |
| 2145 |
} /*-- test_curdircount_setting() --*/ |
| 2146 |
|
| 2147 |
/* Test writing and reading of a custom directory only (solitary) without having |
| 2148 |
* touched a normal directory. |
| 2149 |
* Reproduces issue https://gitlab.com/libtiff/libtiff/-/issues/643. |
| 2150 |
* Error analysis of issue 643: |
| 2151 |
* Opening a file as read with "h" option does not initialize a default |
| 2152 |
* directory and thus, the pointer tif->tif_tagmethods.vsetfield and |
| 2153 |
* tif->tif_tagmethods.vgetfield are not initialized. This is done in |
| 2154 |
* TIFFDefaultDirectory(). In standard read functions, that default directory |
| 2155 |
* is set up but not for reading custom directories. Any call to |
| 2156 |
* TIFFSetField() or TIFFGetField() within TIFFReadCustomDirectory() will then |
| 2157 |
* fail or cause a SegFault. |
| 2158 |
*/ |
| 2159 |
int test_solitary_custom_directory(unsigned int openMode) |
| 2160 |
{ |
| 2161 |
char filename[128] = {0}; |
| 2162 |
|
| 2163 |
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0]))) |
| 2164 |
{ |
| 2165 |
fprintf(stderr, "Index %d for openMode parameter out of range.\n", |
| 2166 |
openMode); |
| 2167 |
return 1; |
| 2168 |
} |
| 2169 |
|
| 2170 |
/* Get individual filenames and delete existent ones. */ |
| 2171 |
sprintf(filename, "test_solitary_custom_directory_%s.tif", |
| 2172 |
openModeStrings[openMode]); |
| 2173 |
unlink(filename); |
| 2174 |
|
| 2175 |
/* Create a dummy file without IFD. */ |
| 2176 |
TIFF *tif = TIFFOpen(filename, openModeStrings[openMode]); |
| 2177 |
if (!tif) |
| 2178 |
{ |
| 2179 |
fprintf(stderr, "Can't create %s\n", filename); |
| 2180 |
return 1; |
| 2181 |
} |
| 2182 |
/* A TIFFSetFiled() here would work. */ |
| 2183 |
TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, "DocName"); |
| 2184 |
TIFFClose(tif); |
| 2185 |
|
| 2186 |
/* Open file without reading a directory using option "h". |
| 2187 |
* Option "h" suppresses to load the directory. Thus reading functions are |
| 2188 |
* hot set. Read TIFF header only, do not load the first image directory. |
| 2189 |
* That could be useful in case of the broken first directory. We can open |
| 2190 |
* the file and proceed to the other directories. |
| 2191 |
*/ |
| 2192 |
tif = TIFFOpen(filename, "r+h"); |
| 2193 |
if (!tif) |
| 2194 |
{ |
| 2195 |
fprintf(stderr, "Can't open %s\n", filename); |
| 2196 |
return 1; |
| 2197 |
} |
| 2198 |
/* A TIFFSetField() here would fail or cause a SegFault, because of the "h" |
| 2199 |
* option before fix of issue 643. */ |
| 2200 |
TIFFSetField(tif, TIFFTAG_DOCUMENTNAME, "DocName"); |
| 2201 |
|
| 2202 |
/* Write a custom directory. */ |
| 2203 |
uint64_t offsetEXIFBase; |
| 2204 |
if (write_EXIF_data_to_current_directory(tif, 0, &offsetEXIFBase)) |
| 2205 |
{ |
| 2206 |
fprintf(stderr, |
| 2207 |
"Can't write solitary EXIF data into %s. " |
| 2208 |
"Testline %d\n", |
| 2209 |
filename, __LINE__); |
| 2210 |
goto failure; |
| 2211 |
} |
| 2212 |
TIFFClose(tif); |
| 2213 |
|
| 2214 |
/* Open and read custom directory without touching another IFD. */ |
| 2215 |
tif = TIFFOpen(filename, "rh"); |
| 2216 |
if (!tif) |
| 2217 |
{ |
| 2218 |
fprintf(stderr, "Can't open %s\n", filename); |
| 2219 |
return 1; |
| 2220 |
} |
| 2221 |
/* This custom directory reading will cause a SegFault due to the "h" |
| 2222 |
* option before fix of issue 643. */ |
| 2223 |
if (!TIFFReadEXIFDirectory(tif, offsetEXIFBase)) |
| 2224 |
{ |
| 2225 |
fprintf(stderr, |
| 2226 |
"Can't read solitary EXIF data from %s. " |
| 2227 |
"Testline %d\n", |
| 2228 |
filename, __LINE__); |
| 2229 |
goto failure; |
| 2230 |
} |
| 2231 |
|
| 2232 |
TIFFClose(tif); |
| 2233 |
unlink(filename); |
| 2234 |
return 0; |
| 2235 |
|
| 2236 |
failure: |
| 2237 |
TIFFClose(tif); |
| 2238 |
return 1; |
| 2239 |
} /*-- test_solitary_custom_directory() --*/ |
| 2240 |
|
| 2241 |
int main() |
| 2242 |
{ |
| 2243 |
|
| 2244 |
unsigned int openModeMax = |
| 2245 |
(sizeof(openModeStrings) / sizeof(openModeStrings[0])); |
| 2246 |
for (unsigned int openMode = 0; openMode < openModeMax; openMode++) |
| 2247 |
{ |
| 2248 |
fprintf(stderr, "\n--- Test for %s open option. ---\n", |
| 2249 |
openModeText[openMode]); |
| 2250 |
if (test_curdircount_setting(openMode)) |
| 2251 |
{ |
| 2252 |
fprintf(stderr, "Failed during %s test_curdircount_setting test.\n", |
| 2253 |
openModeText[openMode]); |
| 2254 |
return 1; |
| 2255 |
} |
| 2256 |
|
| 2257 |
if (test_lastdir_offset(openMode)) |
| 2258 |
{ |
| 2259 |
fprintf(stderr, "Failed during %s WriteDirectory test.\n", |
| 2260 |
openModeText[openMode]); |
| 2261 |
return 1; |
| 2262 |
} |
| 2263 |
|
| 2264 |
if (test_rewrite_lastdir_offset(openMode)) |
| 2265 |
{ |
| 2266 |
fprintf(stderr, "Failed during %s RewriteDirectory test.\n", |
| 2267 |
openModeText[openMode]); |
| 2268 |
return 1; |
| 2269 |
} |
| 2270 |
|
| 2271 |
/* Test arbitrary directory loading */ |
| 2272 |
if (test_arbitrary_directrory_loading(openMode)) |
| 2273 |
{ |
| 2274 |
fprintf(stderr, |
| 2275 |
"Failed during %s ArbitraryDirectoryLoading test.\n", |
| 2276 |
openModeText[openMode]); |
| 2277 |
return 1; |
| 2278 |
} |
| 2279 |
|
| 2280 |
/* Test SubIFD writing and reading */ |
| 2281 |
if (test_SubIFD_directrory_handling(openMode)) |
| 2282 |
{ |
| 2283 |
fprintf(stderr, |
| 2284 |
"Failed during %s SubIFD_directrory_handling test.\n", |
| 2285 |
openModeText[openMode]); |
| 2286 |
return 1; |
| 2287 |
} |
| 2288 |
/* Test for failure in TIFFSetSubDirectory() */ |
| 2289 |
if (test_SetSubDirectory_failure(openMode)) |
| 2290 |
{ |
| 2291 |
fprintf(stderr, "Failed during %s SetSubDirectory_failure test.\n", |
| 2292 |
openModeText[openMode]); |
| 2293 |
return 1; |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* Test for correct "current directory number" incrementing.*/ |
| 2298 |
for (unsigned int openMode = 0; openMode < openModeMax; openMode++) |
| 2299 |
{ |
| 2300 |
for (int i = 0; i <= 2; i++) |
| 2301 |
{ |
| 2302 |
fprintf(stderr, |
| 2303 |
"\n--- Test for test_current_dirnum_incrementing %s " |
| 2304 |
"testcase %d. " |
| 2305 |
"---\n", |
| 2306 |
openModeText[openMode], i); |
| 2307 |
if (test_current_dirnum_incrementing(i, openMode)) |
| 2308 |
{ |
| 2309 |
fprintf(stderr, |
| 2310 |
"Failed during Current Directory Number Incrementing " |
| 2311 |
"test #%d in %s.\n", |
| 2312 |
i, openModeText[openMode]); |
| 2313 |
return 1; |
| 2314 |
} |
| 2315 |
} |
| 2316 |
} |
| 2317 |
|
| 2318 |
/* Test for reading / writing custom directory (e.g. EXIF) without having |
| 2319 |
* touched a normal directory. */ |
| 2320 |
for (unsigned int openMode = 0; openMode < openModeMax; openMode++) |
| 2321 |
{ |
| 2322 |
fprintf(stderr, |
| 2323 |
"\n--- Test for test_solitary_custom_directory %s ---\n", |
| 2324 |
openModeText[openMode]); |
| 2325 |
if (test_solitary_custom_directory(openMode)) |
| 2326 |
{ |
| 2327 |
fprintf(stderr, |
| 2328 |
"Failed during test_solitary_custom_directory in %s.\n", |
| 2329 |
openModeText[openMode]); |
| 2330 |
return 1; |
| 2331 |
} |
| 2332 |
} |
| 2333 |
|
| 2334 |
return 0; |
| 2335 |
} |