-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest_directory.c
2380 lines (2210 loc) · 81 KB
/
test_directory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TIFF Library
*
* The purpose of this test suite is to test the correctness of
* TIFFWriteDirectory() when appending multiple directories to an open file.
*
* Currently, there is an optimization where the TIFF data structure stores the
* offset of the last written directory in order to avoid having to traverse the
* entire directory list each time a new one is added. The offset is not stored
* in the file itself, only in the in-memory data structure. This means we still
* go through the entire list the first time a directory is appended to a
* newly-opened file, and the shortcut is taken for subsequent directory writes.
*
* In order to test the correctness of the optimization, the
* `test_lastdir_offset` function writes 10 directories to two different files.
* For the first file we use the optimization, by simply calling
* TIFFWriteDirectory() repeatedly on an open TIFF handle. For the second file,
* we avoid the optimization by closing the file after each call to
* TIFFWriteDirectory(), which means the next directory write will traverse the
* entire list.
*
* Finally, the two files are compared to check that the number of directories
* written is the same, and that their offsets match. The test is then repeated
* using BigTIFF files.
*
* Furthermore, arbitrary directory loading using TIFFSetDirectory() is checked,
* especially after the update with "relative" movement possibility. Also
* TIFFUnlinkDirectory() is tested.
*
* Furthermore, tests are performed with big-endian, little-endian and BigTIFF
* images.
*
* Furthermore the correctness of tif_curdir = TIFFCurrentDirectory() when
* moving though a multi-IFD file and creating new IFDs, overwriting or
* re-writing IFDs as well for reading and writing SubIFDs is tested.
*
*/
#include "tif_config.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tiffio.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* Global settings for the test image. It is not save to change. */
#define SPP 3 /* Samples per pixel */
#define N_DIRECTORIES 10 /* Number of directories to write */
const uint16_t width = 1;
const uint16_t length = 1;
const uint16_t bps = 8;
const uint16_t photometric = PHOTOMETRIC_RGB;
const uint16_t rows_per_strip = 1;
const uint16_t planarconfig = PLANARCONFIG_CONTIG;
char *openModeStrings[] = {"wl", "wb", "w8l", "w8b"};
char *openModeText[] = {"non-BigTIFF and LE", "non-BigTIFF and BE",
"BigTIFF and LE", "BigTIFF and BE"};
/* Some functions and macros to get more readable test code. */
int CheckCurDirNum(TIFF *tif, tdir_t expected_dirnum, int line)
{
tdir_t curdir = TIFFCurrentDirectory(tif);
if (curdir != expected_dirnum)
{
fprintf(stderr,
"Error: curdir %d is different to expected one %d at line %d\n",
curdir, expected_dirnum, line);
return 1;
}
return 0;
}
#define TIFFWriteDirectory_M(tif, filename, line) \
if (!TIFFWriteDirectory(tif)) \
{ \
fprintf(stderr, "Can't write directory to %s at line %d\n", filename, \
line); \
goto failure; \
}
#define TIFFCheckpointDirectory_M(tif, dirnum, filename, line) \
if (!TIFFCheckpointDirectory(tif)) \
{ \
fprintf(stderr, "Can't checkpoint directory %d of %s at line %d\n", \
dirnum, filename, line); \
goto failure; \
}
#define TIFFSetDirectory_M(tif, dirnum, filename, line) \
if (!TIFFSetDirectory(tif, dirnum)) \
{ \
fprintf(stderr, "Can't set directory %d of %s at line %d\n", dirnum, \
filename, line); \
goto failure; \
}
#define CHECKCURDIRNUM_M(tif, x, line) \
if (CheckCurDirNum(tif, x, line)) \
{ \
goto failure; \
}
/* Writes basic tags to current directory (IFD) as well one pixel to the file.
* For is_corrupted = TRUE a corrupted IFD (missing image width tag) is
* generated. */
int write_data_to_current_directory(TIFF *tif, int i, bool is_corrupted)
{
unsigned char buf[SPP] = {0, 127, 255};
char auxString[128];
if (!tif)
{
fprintf(stderr, "Invalid TIFF handle.\n");
return 1;
}
if (!is_corrupted)
{
if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width))
{
fprintf(stderr, "Can't set ImageWidth tag.\n");
return 1;
}
}
if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length))
{
fprintf(stderr, "Can't set ImageLength tag.\n");
return 1;
}
if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps))
{
fprintf(stderr, "Can't set BitsPerSample tag.\n");
return 1;
}
if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP))
{
fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
return 1;
}
if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip))
{
fprintf(stderr, "Can't set SamplesPerPixel tag.\n");
return 1;
}
if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig))
{
fprintf(stderr, "Can't set PlanarConfiguration tag.\n");
return 1;
}
if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric))
{
fprintf(stderr, "Can't set PhotometricInterpretation tag.\n");
return 1;
}
/* Write IFD identification number to ASCII string of PageName tag. */
sprintf(auxString, "%d th. IFD", i);
if (!TIFFSetField(tif, TIFFTAG_PAGENAME, auxString))
{
fprintf(stderr, "Can't set TIFFTAG_PAGENAME tag.\n");
return 1;
}
/* Write dummy pixel data. */
if (TIFFWriteScanline(tif, buf, 0, 0) == -1 && !is_corrupted)
{
fprintf(stderr, "Can't write image data.\n");
return 1;
}
return 0;
}
/* Fills a new IFD and appends it to a file by opening a file and closing it
* again after writing. */
int write_directory_to_closed_file(const char *filename, unsigned int openMode,
int i)
{
TIFF *tif;
/* Replace 'w' for write by 'a' for append. */
char strAux[8] = {0};
strncpy(strAux, openModeStrings[openMode], sizeof(strAux) - 1);
strAux[0] = 'a';
tif = TIFFOpen(filename, strAux);
if (!tif)
{
fprintf(stderr, "Can't create/open %s\n", filename);
return 1;
}
if (write_data_to_current_directory(tif, i, false))
{
fprintf(stderr, "Can't write data to directory %d of %s.\n", i,
filename);
TIFFClose(tif);
return 1;
}
if (!TIFFWriteDirectory(tif))
{
fprintf(stderr, "TIFFWriteDirectory() failed for directory %d of %s.\n",
i, filename);
TIFFClose(tif);
return 1;
}
TIFFClose(tif);
return 0;
}
/* Opens a file and counts its directories. */
int count_directories(const char *filename, int *count)
{
TIFF *tif;
*count = 0;
tif = TIFFOpen(filename, "r");
if (!tif)
{
fprintf(stderr, "Can't read %s\n", filename);
return 1;
}
do
{
CHECKCURDIRNUM_M(tif, (tdir_t)(*count), __LINE__);
(*count)++;
} while (TIFFReadDirectory(tif));
failure:
TIFFClose(tif);
return 0;
}
/* Compare 'requested_dir_number' with number written in PageName tag
* into the IFD to identify that IFD. */
int is_requested_directory(TIFF *tif, int requested_dir_number,
const char *filename)
{
char *ptr = NULL;
char *auxStr = NULL;
if (!TIFFGetField(tif, TIFFTAG_PAGENAME, &ptr))
{
fprintf(stderr, "Can't get TIFFTAG_PAGENAME tag.\n");
return 0;
}
/* Check for reading errors */
if (ptr != NULL)
auxStr = strchr(ptr, ' ');
if (ptr == NULL || auxStr == NULL || strncmp(auxStr, " th.", 4))
{
ptr = ptr == NULL ? "(null)" : ptr;
fprintf(stderr,
"Error reading IFD directory number from PageName tag: %s\n",
ptr);
return 0;
}
/* Retrieve IFD identification number from ASCII string */
const int nthIFD = atoi(ptr);
if (nthIFD == requested_dir_number)
{
return 1;
}
fprintf(stderr, "Expected directory %d from %s was not loaded but: %s\n",
requested_dir_number, filename, ptr);
return 0;
}
enum DirWalkMode
{
DirWalkMode_ReadDirectory,
DirWalkMode_SetDirectory,
DirWalkMode_SetDirectory_Reverse,
};
/* Gets a list of the directory offsets in a file. Assumes the file has at least
* N_DIRECTORIES directories.
* There are three methods to test walking through the IFD chain.
* This tests the optimization of faster SetDirectory(). */
int get_dir_offsets(const char *filename, uint64_t *offsets,
enum DirWalkMode dirWalkMode)
{
TIFF *tif;
int i;
tif = TIFFOpen(filename, "r");
if (!tif)
{
fprintf(stderr, "Can't read %s\n", filename);
return 1;
}
for (i = 0; i < N_DIRECTORIES; i++)
{
tdir_t dirn = (dirWalkMode == DirWalkMode_SetDirectory_Reverse)
? (N_DIRECTORIES - i - 1)
: i;
if (dirWalkMode != DirWalkMode_ReadDirectory &&
!TIFFSetDirectory(tif, dirn) && dirn < (N_DIRECTORIES - 1))
{
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename);
TIFFClose(tif);
return 3;
}
offsets[dirn] = TIFFCurrentDirOffset(tif);
/* Check, if dirn is requested directory */
if (!is_requested_directory(tif, dirn, filename))
{
TIFFClose(tif);
return 4;
}
if (dirWalkMode == DirWalkMode_ReadDirectory &&
!TIFFReadDirectory(tif) && i < (N_DIRECTORIES - 1))
{
fprintf(stderr, "Can't read %d.th directory from %s\n", i,
filename);
TIFFClose(tif);
return 2;
}
}
TIFFClose(tif);
return 0;
}
/* Checks that TIFFSetDirectory() work well after update for relative seeking
* to following directories.
*
* There are several issues especially when SubIFDs and custom directories are
* involved. There are no real directory number for those and TIFFSetDirectory()
* cannot be used. However, TIFFSetDirectory() is needed to switch back to the
* main-IFD chain. Furthermore, IFD-loop handling needs to be supported in any
* cases.
* Also the case where directly after TIFFWriteDirectory() that directory
* is re-read using TIFFSetDirectory() is tested.
*/
int test_arbitrary_directrory_loading(unsigned int openMode)
{
char filename[128] = {0};
TIFF *tif;
uint64_t offsets_base[N_DIRECTORIES];
int expected_original_dirnumber;
tdir_t expected_curdir = (tdir_t)(-1);
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0])))
{
fprintf(stderr, "Index %d for openMode parameter out of range.\n",
openMode);
return 1;
}
/* Get individual filenames and delete existent ones. */
sprintf(filename, "test_arbitrary_directrory_loading_%s.tif",
openModeStrings[openMode]);
unlink(filename);
/* Create a file and write N_DIRECTORIES (10) directories to it. */
tif = TIFFOpen(filename, openModeStrings[openMode]);
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
return 1;
}
TIFFSetDirectory(tif, 0);
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__);
for (int i = 0; i < N_DIRECTORIES; i++)
{
if (write_data_to_current_directory(tif, i, false))
{
fprintf(stderr, "Can't write data to current directory in %s\n",
filename);
goto failure;
}
if (!TIFFWriteDirectory(tif))
{
fprintf(stderr, "Can't write directory to %s\n", filename);
goto failure;
}
expected_curdir++;
/* Fails in 4.6.0 */
CHECKCURDIRNUM_M(tif, expected_curdir, __LINE__);
if (i >= 2 && i <= 4)
{
if (i == 3)
{
/* Invalidate directory - TIFFSetSubDirectory() will fail.
* The next TIFFSetDirectory(i) will revoke this action.*/
if (TIFFSetSubDirectory(tif, 0))
{
fprintf(stderr,
"Unexpected return at invalidate directory %d "
"within %s.\n",
i, filename);
goto failure;
}
}
/* Test jump back to just written directory. */
if (!TIFFSetDirectory(tif, i))
{
fprintf(stderr,
"Can't set directory %d within %s "
"right after TIFFWriteDirectory().\n",
i, filename);
goto failure;
}
if (i == 4)
{
/* Invalidate directory - TIFFSetSubDirectory() will fail.
* This time, tif_curdir keeps set to -1 and counts now again
* from 0, despite the directory number in the file is equal
* "i". */
if (TIFFSetSubDirectory(tif, 0))
{
fprintf(stderr,
"Unexpected return at invalidate directory %d "
"within %s.\n",
i, filename);
goto failure;
}
}
/*Check if correct directory is loaded */
if (!is_requested_directory(tif, i, filename))
{
goto failure;
}
/* Reset to next directory to go on with writing. */
TIFFCreateDirectory(tif);
}
}
TIFFClose(tif);
/* Reopen prepared testfile */
tif = TIFFOpen(filename, "r+");
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
return 1;
}
/* Get directory offsets */
if (get_dir_offsets(filename, offsets_base, DirWalkMode_ReadDirectory))
{
fprintf(stderr, "Error reading directory offsets from %s.\n", filename);
goto failure;
}
/* Set last directory and then one after, which should fail. */
if (!TIFFSetDirectory(tif, N_DIRECTORIES - 1))
{
fprintf(stderr, "Can't set last directory %d within %s\n",
N_DIRECTORIES - 1, filename);
goto failure;
}
if (TIFFSetDirectory(tif, N_DIRECTORIES + 1))
{
fprintf(stderr,
"End of IFD chain not detected. Set non existing directory %d "
"within %s\n",
N_DIRECTORIES + 1, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, N_DIRECTORIES - 1, __LINE__);
/* Test very fast TIFFSetDirectory() using IFD loop directory list.
* First populate IFD loop directory list and then go through directories in
* reverse order. Within between read after end of IFDs using
* TIFFReadDirectory() where IFD loop directory list should be kept. */
for (int i = 0; i < N_DIRECTORIES; i++)
{
if (!TIFFSetDirectory(tif, i))
{
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, i, __LINE__);
}
TIFFReadDirectory(tif);
CHECKCURDIRNUM_M(tif, N_DIRECTORIES - 1, __LINE__);
for (int i = N_DIRECTORIES - 1; i >= 0; i--)
{
if (!TIFFSetDirectory(tif, i))
{
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename);
goto failure;
}
if (!is_requested_directory(tif, i, filename))
{
goto failure;
}
CHECKCURDIRNUM_M(tif, i, __LINE__);
}
/* Test not existing directory number */
if (TIFFSetDirectory(tif, N_DIRECTORIES))
{
fprintf(stderr,
"No expected fail for accessing not existent directory number "
"%d in file %s\n",
N_DIRECTORIES, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 0, __LINE__);
/* Close and Reopen prepared testfile */
TIFFClose(tif);
tif = TIFFOpen(filename, "r+");
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
return 1;
}
/* Step through directories just using TIFFSetSubDirectory() */
for (int i = N_DIRECTORIES - 1; i >= 0; i--)
{
if (!TIFFSetSubDirectory(tif, offsets_base[i]))
{
fprintf(stderr, "Can't set %d.th directory from %s\n", i, filename);
goto failure;
}
if (!is_requested_directory(tif, i, filename))
{
goto failure;
}
CHECKCURDIRNUM_M(tif, i, __LINE__);
}
/* More specialized test cases for relative seeking within TIFFSetDirectory.
* However, with using IFD loop directory list, most of this test cases will
* never be reached! */
if (!TIFFSetDirectory(tif, 2))
{
fprintf(stderr, "Can't set directory %d within %s\n", 2, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 2, __LINE__);
uint64_t off2 = TIFFCurrentDirOffset(tif);
/* Note that dirnum = 2 is deleted here since TIFFUnlinkDirectory()
* starts with 1 instead of 0. */
if (!TIFFUnlinkDirectory(tif, 3))
{
fprintf(stderr, "Can't unlink directory %d within %s\n", 3, filename);
goto failure;
}
/* Move to the unlinked IFD. This sets tif_curdir=0 because unlinked IFD
* offset is not in the IFD loop list. This indicates a SubIFD chain. */
if (!TIFFSetSubDirectory(tif, off2))
{
fprintf(stderr,
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
") within %s\n",
off2, off2, filename);
goto failure;
}
/*Check if correct directory is loaded */
CHECKCURDIRNUM_M(tif, 0, __LINE__);
expected_original_dirnumber = 2;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* This should move back to the main-IFD chain and load the third
* directory which has IFD number 4, due to the deleted third IFD. */
if (!TIFFSetDirectory(tif, 3))
{
fprintf(stderr, "Can't set new directory %d within %s\n", 3, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 3, __LINE__);
expected_original_dirnumber = 4;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Test backwards jump. */
if (!TIFFSetDirectory(tif, 2))
{
fprintf(stderr, "Can't set new directory %d within %s\n", 2, filename);
goto failure;
}
expected_original_dirnumber = 3;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Second UnlinkDirectory -> two IFDs are missing in the main-IFD chain
* then, original dirnum 2 and 3 */
if (!TIFFUnlinkDirectory(tif, 3))
{
fprintf(stderr, "Can't unlink directory %d within %s\n", 3, filename);
goto failure;
}
if (!TIFFSetDirectory(tif, 2))
{
fprintf(stderr,
"Can't set new directory %d after second "
"TIFFUnlinkDirectory(3) within %s\n",
2, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 2, __LINE__);
/* which should now be the previous dir-3. */
expected_original_dirnumber = 4;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Check, if third original directory, which is unlinked, could be loaded
* and the following, still chained one. This is like for a SubIFD.
* tif_curdir will be set to 0 because deleted IFD is not in main sequence
* and then incremented to 1 when reading the still chained next IFD using
* TIFFReadDirectory(). */
if (!TIFFSetSubDirectory(tif, offsets_base[2]))
{
fprintf(stderr,
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
") within %s\n",
offsets_base[2], offsets_base[2], filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 0, __LINE__);
if (!TIFFReadDirectory(tif))
{
fprintf(stderr,
"Can't read directory after directory at offset 0x%" PRIx64
" (%" PRIu64 ") within %s\n",
offsets_base[2], offsets_base[2], filename);
goto failure;
}
/* Check if correct directory is loaded, which was unlinked the second
* time.
*/
CHECKCURDIRNUM_M(tif, 1, __LINE__);
expected_original_dirnumber = 3;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Load unlinked directory like a SubIFD and then go back to the
* main-IFD chain using TIFFSetDirectory(). Also check two loads of the
* same directory. */
if (!TIFFSetSubDirectory(tif, offsets_base[2]))
{
fprintf(stderr,
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
") within %s\n",
offsets_base[2], offsets_base[2], filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 0, __LINE__);
if (!TIFFSetDirectory(tif, 3))
{
fprintf(stderr, "Can't set new directory %d within %s\n", 3, filename);
goto failure;
}
if (!TIFFSetDirectory(tif, 3))
{
fprintf(stderr, "Can't set new directory %d a second time within %s\n",
3, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 3, __LINE__);
/*Check if correct directory is loaded. Because two original IFDs are
* unlinked / missing, the original dirnumber is now 5. */
expected_original_dirnumber = 5;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Another sequence for testing. */
if (!TIFFSetDirectory(tif, 2))
{
fprintf(stderr, "Can't set new directory %d a second time within %s\n",
2, filename);
goto failure;
}
if (!TIFFSetDirectory(tif, 3))
{
fprintf(stderr, "Can't set new directory %d a second time within %s\n",
3, filename);
goto failure;
}
if (!TIFFSetSubDirectory(tif, offsets_base[2]))
{
fprintf(stderr,
"Can't set sub-directory at offset 0x%" PRIx64 " (%" PRIu64
") within %s\n",
offsets_base[2], offsets_base[2], filename);
goto failure;
}
expected_original_dirnumber = 2;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
if (!TIFFSetDirectory(tif, 3))
{
fprintf(stderr, "Can't set new directory %d a second time within %s\n",
3, filename);
goto failure;
}
/*Check if correct directory is loaded. Because two original IFDs are
* unlinked / missing, the original dirnumber is now 5. */
expected_original_dirnumber = 5;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* Third UnlinkDirectory -> three IFDs are missing in the main-IFD chain
* then, original dirnum 0, 2 and 3
* Furthermore, this test checks that TIFFUnlinkDirectory() can unlink
* the first directory dirnum = 0 and a following TIFFSetDirectory(0)
* does not load the unlinked directory. */
CHECKCURDIRNUM_M(tif, 3, __LINE__);
if (!TIFFUnlinkDirectory(tif, 1))
{
fprintf(stderr, "Can't unlink directory %d within %s\n", 0, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, (tdir_t)(-1), __LINE__);
/* Now three directories are missing (0,2,3) and thus directory 0 is
* original directory 1 and directory 2 is original directory 5. */
if (!TIFFSetDirectory(tif, 0))
{
fprintf(stderr,
"Can't set new directory %d after third "
"TIFFUnlinkDirectory(1) within %s\n",
0, filename);
goto failure;
}
CHECKCURDIRNUM_M(tif, 0, __LINE__);
expected_original_dirnumber = 1;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
if (!TIFFSetDirectory(tif, 2))
{
fprintf(stderr,
"Can't set new directory %d after third "
"TIFFUnlinkDirectory(1) within %s\n",
2, filename);
goto failure;
}
expected_original_dirnumber = 5;
if (!is_requested_directory(tif, expected_original_dirnumber, filename))
{
goto failure;
}
/* TIFFUnlinkDirectory(0) is not allowed, because dirnum starts for
* this function with 1 instead of 0.
* An error return is expected here. */
CHECKCURDIRNUM_M(tif, 2, __LINE__);
fprintf(stderr, "----- Expect error messages about 'TIFFUnlinkDirectory() "
"first directory starts with number 1 and not 0.' -----\n");
if (TIFFUnlinkDirectory(tif, 0))
{
fprintf(stderr, "TIFFUnlinkDirectory(0) did not return an error.\n");
goto failure;
}
CHECKCURDIRNUM_M(tif, 2, __LINE__);
TIFFClose(tif);
unlink(filename);
return 0;
failure:
if (tif)
{
TIFFClose(tif);
}
return 1;
} /*-- test_arbitrary_directrory_loading() --*/
/* Tests SubIFD writing and reading
*
*
*/
int test_SubIFD_directrory_handling(unsigned int openMode)
{
char filename[128] = {0};
/* Define the number of sub-IFDs you are going to write */
#define NUMBER_OF_SUBIFDs 4
uint16_t number_of_sub_IFDs = NUMBER_OF_SUBIFDs;
toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = {
0UL}; /* array for SubIFD tag */
int blnWriteSubIFD = 0;
int i;
int iIFD = 0, iSubIFD = 0;
TIFF *tif;
int expected_original_dirnumber;
if (openMode >= (sizeof(openModeStrings) / sizeof(openModeStrings[0])))
{
fprintf(stderr, "Index %d for openMode parameter out of range.\n",
openMode);
return 1;
}
/* Get individual filenames and delete existent ones. */
sprintf(filename, "test_SubIFD_directrory_handling_%s.tif",
openModeStrings[openMode]);
unlink(filename);
/* Create a file and write N_DIRECTORIES (10) directories to it */
tif = TIFFOpen(filename, openModeStrings[openMode]);
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
return 1;
}
for (i = 0; i < N_DIRECTORIES; i++)
{
if (write_data_to_current_directory(
tif, blnWriteSubIFD ? 200 + iSubIFD++ : iIFD++, false))
{
fprintf(stderr, "Can't write data to current directory in %s\n",
filename);
goto failure;
}
if (blnWriteSubIFD)
{
/* SUBFILETYPE tag is not mandatory for SubIFD writing, but a
* good idea to indicate thumbnails */
if (!TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE))
goto failure;
}
expected_original_dirnumber = blnWriteSubIFD ? 0 : iIFD - 1;
/* For the second multi-page image, trigger TIFFWriteDirectory() to
* switch for the next number_of_sub_IFDs calls to add those as SubIFDs
* e.g. for thumbnails */
if (1 == i)
{
blnWriteSubIFD = 1;
/* Now here is the trick: the next n directories written
* will be sub-IFDs of the main-IFD (where n is number_of_sub_IFDs
* specified when you set the TIFFTAG_SUBIFD field.
* The contents of sub_IFDs_offsets is initially copied to the
* internal SubIFD offset array and written to the main-IFD. Those
* SubIFD offsets in the main-IFD are then patched by the following
* number_of_sub_IFDs TIFFWriteDirectory() calls which write the
* sub-IFDs to file.
*/
if (!TIFFSetField(tif, TIFFTAG_SUBIFD, number_of_sub_IFDs,
sub_IFDs_offsets))
goto failure;
}
if (!TIFFWriteDirectory(tif))
{
fprintf(stderr, "Can't write directory to %s\n", filename);
goto failure;
}
/* Fails in 4.6.0 */
CHECKCURDIRNUM_M(tif, expected_original_dirnumber, __LINE__);
if (iSubIFD >= number_of_sub_IFDs)
blnWriteSubIFD = 0;
}
TIFFClose(tif);
/* Reopen prepared testfile */
tif = TIFFOpen(filename, "r+");
if (!tif)
{
fprintf(stderr, "Can't create %s\n", filename);
goto failure;
}
tdir_t numberOfMainIFDs = TIFFNumberOfDirectories(tif);
CHECKCURDIRNUM_M(tif, 0, __LINE__);
if (numberOfMainIFDs != (tdir_t)N_DIRECTORIES - number_of_sub_IFDs)
{
fprintf(stderr,
"Unexpected number of directories in %s. Expected %i, "
"found %i.\n",
filename, N_DIRECTORIES - number_of_sub_IFDs, numberOfMainIFDs);
goto failure;
}
tdir_t currentDirNumber = TIFFCurrentDirectory(tif);
/* The first directory is already read through TIFFOpen() */
int blnRead = 0;
expected_original_dirnumber = 1;
do
{
/* Check if there are SubIFD subfiles */
void *ptr;
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr))
{
/* Copy SubIFD array from pointer */
memcpy(sub_IFDs_offsets, ptr,
number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0]));
for (i = 0; i < number_of_sub_IFDs; i++)
{
/* Read first SubIFD directory */
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i]))
goto failure;
if (!is_requested_directory(tif, 200 + i, filename))
{
goto failure;
}
/* For SubIFDs, curdir is always set to 0 for the first SubIFD
* read.
* When SubIFDs are not concatenated, curdir is 0 for all
* SubIFDs read.
* When the SubIFDs are concatenated the following
* ones after the first read one get incremented curdir numbers.
* The curdir sequence gets updated when a previous one before
* the firstly read one is read. I.e. curdir behaves in the
* SubIFD chain like in the main IFD chain to support IFD loop
* checking.
*/
/* Check if there is a SubIFD chain behind the first one from
* the array, as specified by Adobe */
int n = 0;
/* subdirnum starts with 1 because first SubIFD is already
* read with TIFFSetSubDirectory() */
CHECKCURDIRNUM_M(tif, 0, __LINE__);
tdir_t subdirnum = 1;
/* analyse SubIFD chain */
while (TIFFReadDirectory(tif))
{
if (!is_requested_directory(tif, 201 + i + n++, filename))
goto failure;
/* Fails in 4.6.0 when SubIFD chaining is enabled. */
CHECKCURDIRNUM_M(tif, subdirnum, __LINE__);
subdirnum++;
}
}
/*-- Go through SubIFDs in reverse order --*/
/* Activate main-IFD to reset SubIFD loop list from above. */
TIFFSetDirectory(tif, 0);
for (i = number_of_sub_IFDs - 1; i >= 0; i--)
{
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i]))
goto failure;
if (!is_requested_directory(tif, 200 + i, filename))
{
goto failure;
}
int n = 0;
tdir_t subdirnum = 1;
CHECKCURDIRNUM_M(tif, 0, __LINE__);
/* analyse SubIFD chain */
while (TIFFReadDirectory(tif))
{
if (!is_requested_directory(tif, 201 + i + n++, filename))
goto failure;
/* Fails in 4.6.0 when SubIFD chaining is enabled. */
CHECKCURDIRNUM_M(tif, subdirnum, __LINE__);
subdirnum++;
}
}
/* Go back to main-IFD chain and re-read that main-IFD directory */
if (!TIFFSetDirectory(tif, currentDirNumber))
goto failure;
}
/* Read next main-IFD directory (subfile) */
blnRead = TIFFReadDirectory(tif);
currentDirNumber = TIFFCurrentDirectory(tif);
if (blnRead)
CHECKCURDIRNUM_M(tif, expected_original_dirnumber, __LINE__);
if (blnRead && !is_requested_directory(
tif, expected_original_dirnumber++, filename))
goto failure;
} while (blnRead);
/*--- Now test arbitrary directory loading with SubIFDs ---*/
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[1]))
goto failure;
if (!is_requested_directory(tif, 201, filename))
{
goto failure;
}
CHECKCURDIRNUM_M(tif, 0, __LINE__);