| 1 |
/* |
| 2 |
* Copyright (c) 2022, Even Rouault <even.rouault at spatialys.com> |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 5 |
* its documentation for any purpose is hereby granted without fee, provided |
| 6 |
* that (i) the above copyright notices and this permission notice appear in |
| 7 |
* all copies of the software and related documentation, and (ii) the names of |
| 8 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 9 |
* publicity relating to the software without the specific, prior written |
| 10 |
* permission of Sam Leffler and Silicon Graphics. |
| 11 |
* |
| 12 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 13 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 14 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 15 |
* |
| 16 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 17 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 18 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 19 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 20 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 |
* OF THIS SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* TIFF Library |
| 26 |
* |
| 27 |
* Test IFD loop detection |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "tif_config.h" |
| 31 |
|
| 32 |
#include <assert.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
#include "tiffio.h" |
| 38 |
|
| 39 |
/* Compare 'requested_dir_number' with number written in PageName tag |
| 40 |
* into the IFD to identify that IFD. */ |
| 41 |
int is_requested_directory(TIFF *tif, int requested_dir_number, |
| 42 |
const char *filename) |
| 43 |
{ |
| 44 |
char *ptr = NULL; |
| 45 |
char *auxStr = NULL; |
| 46 |
|
| 47 |
if (!TIFFGetField(tif, TIFFTAG_PAGENAME, &ptr)) |
| 48 |
{ |
| 49 |
fprintf(stderr, "Can't get TIFFTAG_PAGENAME tag.\n"); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
/* Check for reading errors */ |
| 54 |
if (ptr != NULL) |
| 55 |
auxStr = strchr(ptr, ' '); |
| 56 |
|
| 57 |
if (ptr == NULL || auxStr == NULL || strncmp(auxStr, " th.", 4)) |
| 58 |
{ |
| 59 |
ptr = ptr == NULL ? "(null)" : ptr; |
| 60 |
fprintf(stderr, |
| 61 |
"Error reading IFD directory number from PageName tag: %s\n", |
| 62 |
ptr); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
/* Retrieve IFD identification number from ASCII string */ |
| 67 |
const int nthIFD = atoi(ptr); |
| 68 |
if (nthIFD == requested_dir_number) |
| 69 |
{ |
| 70 |
return 1; |
| 71 |
} |
| 72 |
fprintf(stderr, "Expected directory %d from %s was not loaded but: %s\n", |
| 73 |
requested_dir_number, filename, ptr); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
/* Test loop detection within chained SubIFDs. |
| 79 |
* test_ifd_loop_subifd.tif contains seven main-IFDs (0 to 6) and within IFD 1 |
| 80 |
* there are three SubIFDs (0 to 2). Main IFD 4 loops back to main IFD 2. |
| 81 |
* SubIFD 2 loops back to SubIFD 1. |
| 82 |
* Within each IFD the tag PageName is filled with a string, indicating the |
| 83 |
* IFD. The main IFDs are numbered 0 to 6 and the SubIFDs 200 to 202. */ |
| 84 |
int test_subifd_loop(void) |
| 85 |
{ |
| 86 |
const char *filename = SOURCE_DIR "/images/test_ifd_loop_subifd.tif"; |
| 87 |
TIFF *tif; |
| 88 |
int i, n; |
| 89 |
int ret = 0; |
| 90 |
#define NUMBER_OF_SUBIFDs 3 |
| 91 |
toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = { |
| 92 |
0UL}; /* array for SubIFD tag */ |
| 93 |
void *ptr; |
| 94 |
uint16_t number_of_sub_IFDs = 0; |
| 95 |
|
| 96 |
tif = TIFFOpen(filename, "r"); |
| 97 |
if (!tif) |
| 98 |
{ |
| 99 |
fprintf(stderr, "Can't open %s\n", filename); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
/* Try to read six further main directories. Fifth read shall fail. */ |
| 104 |
for (i = 0; i < 6; i++) |
| 105 |
{ |
| 106 |
if (!TIFFReadDirectory(tif)) |
| 107 |
break; |
| 108 |
} |
| 109 |
if (i != 4) |
| 110 |
{ |
| 111 |
fprintf(stderr, "(30) Expected fifth TIFFReadDirectory() to fail\n"); |
| 112 |
ret = 1; |
| 113 |
} |
| 114 |
if (!is_requested_directory(tif, 4, filename)) |
| 115 |
{ |
| 116 |
fprintf(stderr, "(31) Expected fifth main IFD to be loaded\n"); |
| 117 |
ret = 1; |
| 118 |
} |
| 119 |
|
| 120 |
/* Switch to IFD 1 and get SubIFDs. |
| 121 |
* Then read through SubIFDs and detect SubIFD loop. |
| 122 |
* Finally go back to main-IFD and check if right IFD is loaded. |
| 123 |
*/ |
| 124 |
if (!TIFFSetDirectory(tif, 1)) |
| 125 |
ret = 1; |
| 126 |
|
| 127 |
/* Check if there are SubIFD subfiles */ |
| 128 |
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr) && |
| 129 |
(number_of_sub_IFDs == 3)) |
| 130 |
{ |
| 131 |
/* Copy SubIFD array from pointer */ |
| 132 |
memcpy(sub_IFDs_offsets, ptr, |
| 133 |
number_of_sub_IFDs * sizeof(sub_IFDs_offsets[0])); |
| 134 |
|
| 135 |
for (i = 0; i < number_of_sub_IFDs; i++) |
| 136 |
{ |
| 137 |
/* Read SubIFD directory directly via offset. |
| 138 |
* SubIFDs PageName string contains numbers 200 to 202. */ |
| 139 |
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i])) |
| 140 |
ret = 1; |
| 141 |
if (!is_requested_directory(tif, 200 + i, filename)) |
| 142 |
{ |
| 143 |
fprintf(stderr, "(32) Expected SubIFD %d to be loaded.\n", i); |
| 144 |
ret = 1; |
| 145 |
} |
| 146 |
/* Now test SubIFD loop detection. |
| 147 |
* The (i+n).th read in the SubIFD chain shall fail. */ |
| 148 |
for (n = 0; n < number_of_sub_IFDs; n++) |
| 149 |
{ |
| 150 |
if (!TIFFReadDirectory(tif)) |
| 151 |
break; |
| 152 |
} |
| 153 |
if ((i + n) != 2) |
| 154 |
{ |
| 155 |
fprintf( |
| 156 |
stderr, |
| 157 |
"(33) Expected third SubIFD-TIFFReadDirectory() to fail\n"); |
| 158 |
ret = 1; |
| 159 |
} |
| 160 |
} |
| 161 |
/* Go back to main-IFD chain and re-read that main-IFD directory */ |
| 162 |
if (!TIFFSetDirectory(tif, 3)) |
| 163 |
ret = 1; |
| 164 |
if (!is_requested_directory(tif, 3, filename)) |
| 165 |
{ |
| 166 |
fprintf(stderr, "(34) Expected fourth main IFD to be loaded\n"); |
| 167 |
ret = 1; |
| 168 |
} |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
fprintf(stderr, "(35) No or wrong expected SubIFDs within main IFD\n"); |
| 173 |
ret = 1; |
| 174 |
} |
| 175 |
|
| 176 |
TIFFClose(tif); |
| 177 |
return ret; |
| 178 |
} /*-- test_subifd_loop() --*/ |
| 179 |
|
| 180 |
int main() |
| 181 |
{ |
| 182 |
int ret = 0; |
| 183 |
{ |
| 184 |
TIFF *tif = |
| 185 |
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_self.tif", "r"); |
| 186 |
assert(tif); |
| 187 |
if (TIFFReadDirectory(tif)) |
| 188 |
{ |
| 189 |
fprintf(stderr, "(1) Expected TIFFReadDirectory() to fail\n"); |
| 190 |
ret = 1; |
| 191 |
} |
| 192 |
TIFFClose(tif); |
| 193 |
} |
| 194 |
{ |
| 195 |
TIFF *tif = |
| 196 |
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_self.tif", "r"); |
| 197 |
assert(tif); |
| 198 |
int n = TIFFNumberOfDirectories(tif); |
| 199 |
if (n != 1) |
| 200 |
{ |
| 201 |
fprintf( |
| 202 |
stderr, |
| 203 |
"(2) Expected TIFFNumberOfDirectories() to return 1. Got %d\n", |
| 204 |
n); |
| 205 |
ret = 1; |
| 206 |
} |
| 207 |
TIFFClose(tif); |
| 208 |
} |
| 209 |
{ |
| 210 |
TIFF *tif = |
| 211 |
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_first.tif", "r"); |
| 212 |
assert(tif); |
| 213 |
if (TIFFReadDirectory(tif) != 1) |
| 214 |
{ |
| 215 |
fprintf(stderr, "(3) Expected TIFFReadDirectory() to succeed\n"); |
| 216 |
ret = 1; |
| 217 |
} |
| 218 |
if (TIFFReadDirectory(tif)) |
| 219 |
{ |
| 220 |
fprintf(stderr, "(4) Expected TIFFReadDirectory() to fail\n"); |
| 221 |
ret = 1; |
| 222 |
} |
| 223 |
if (TIFFSetDirectory(tif, 1) != 1) |
| 224 |
{ |
| 225 |
fprintf(stderr, "(5) Expected TIFFSetDirectory() to succeed\n"); |
| 226 |
ret = 1; |
| 227 |
} |
| 228 |
if (TIFFReadDirectory(tif)) |
| 229 |
{ |
| 230 |
fprintf(stderr, "(6) Expected TIFFReadDirectory() to fail\n"); |
| 231 |
ret = 1; |
| 232 |
} |
| 233 |
if (TIFFSetDirectory(tif, 0) != 1) |
| 234 |
{ |
| 235 |
fprintf(stderr, "(7) Expected TIFFSetDirectory() to succeed\n"); |
| 236 |
ret = 1; |
| 237 |
} |
| 238 |
if (TIFFReadDirectory(tif) != 1) |
| 239 |
{ |
| 240 |
fprintf(stderr, "(8) Expected TIFFReadDirectory() to succeed\n"); |
| 241 |
ret = 1; |
| 242 |
} |
| 243 |
if (TIFFReadDirectory(tif)) |
| 244 |
{ |
| 245 |
fprintf(stderr, "(9) Expected TIFFReadDirectory() to fail\n"); |
| 246 |
ret = 1; |
| 247 |
} |
| 248 |
TIFFClose(tif); |
| 249 |
} |
| 250 |
{ |
| 251 |
TIFF *tif = |
| 252 |
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_first.tif", "r"); |
| 253 |
assert(tif); |
| 254 |
int n = TIFFNumberOfDirectories(tif); |
| 255 |
if (n != 2) |
| 256 |
{ |
| 257 |
fprintf( |
| 258 |
stderr, |
| 259 |
"(10) Expected TIFFNumberOfDirectories() to return 2. Got %d\n", |
| 260 |
n); |
| 261 |
ret = 1; |
| 262 |
} |
| 263 |
TIFFClose(tif); |
| 264 |
} |
| 265 |
{ |
| 266 |
TIFF *tif = TIFFOpen(SOURCE_DIR "/images/test_two_ifds.tif", "r"); |
| 267 |
assert(tif); |
| 268 |
if (TIFFReadDirectory(tif) != 1) |
| 269 |
{ |
| 270 |
fprintf(stderr, "(11) Expected TIFFReadDirectory() to succeed\n"); |
| 271 |
ret = 1; |
| 272 |
} |
| 273 |
if (TIFFReadDirectory(tif)) |
| 274 |
{ |
| 275 |
fprintf(stderr, "(12) Expected TIFFReadDirectory() to fail\n"); |
| 276 |
ret = 1; |
| 277 |
} |
| 278 |
if (TIFFSetDirectory(tif, 1) != 1) |
| 279 |
{ |
| 280 |
fprintf(stderr, "(13) Expected TIFFSetDirectory() to succeed\n"); |
| 281 |
ret = 1; |
| 282 |
} |
| 283 |
if (TIFFReadDirectory(tif)) |
| 284 |
{ |
| 285 |
fprintf(stderr, "(14) Expected TIFFReadDirectory() to fail\n"); |
| 286 |
ret = 1; |
| 287 |
} |
| 288 |
if (TIFFSetDirectory(tif, 0) != 1) |
| 289 |
{ |
| 290 |
fprintf(stderr, "(15) Expected TIFFSetDirectory() to succeed\n"); |
| 291 |
ret = 1; |
| 292 |
} |
| 293 |
if (TIFFReadDirectory(tif) != 1) |
| 294 |
{ |
| 295 |
fprintf(stderr, "(16) Expected TIFFReadDirectory() to succeed\n"); |
| 296 |
ret = 1; |
| 297 |
} |
| 298 |
if (TIFFReadDirectory(tif)) |
| 299 |
{ |
| 300 |
fprintf(stderr, "(17) Expected TIFFReadDirectory() to fail\n"); |
| 301 |
ret = 1; |
| 302 |
} |
| 303 |
if (TIFFSetDirectory(tif, 0) != 1) |
| 304 |
{ |
| 305 |
fprintf(stderr, "(18) Expected TIFFSetDirectory() to succeed\n"); |
| 306 |
ret = 1; |
| 307 |
} |
| 308 |
if (TIFFSetDirectory(tif, 1) != 1) |
| 309 |
{ |
| 310 |
fprintf(stderr, "(19) Expected TIFFSetDirectory() to succeed\n"); |
| 311 |
ret = 1; |
| 312 |
} |
| 313 |
if (TIFFSetDirectory(tif, 2)) |
| 314 |
{ |
| 315 |
fprintf(stderr, "(20) Expected TIFFSetDirectory() to fail\n"); |
| 316 |
ret = 1; |
| 317 |
} |
| 318 |
if (TIFFSetDirectory(tif, 1) != 1) |
| 319 |
{ |
| 320 |
fprintf(stderr, "(21) Expected TIFFSetDirectory() to succeed\n"); |
| 321 |
ret = 1; |
| 322 |
} |
| 323 |
if (TIFFSetDirectory(tif, 0) != 1) |
| 324 |
{ |
| 325 |
fprintf(stderr, "(22) Expected TIFFSetDirectory() to succeed\n"); |
| 326 |
ret = 1; |
| 327 |
} |
| 328 |
TIFFClose(tif); |
| 329 |
} |
| 330 |
{ |
| 331 |
TIFF *tif = TIFFOpen(SOURCE_DIR "/images/test_two_ifds.tif", "r"); |
| 332 |
assert(tif); |
| 333 |
int n = TIFFNumberOfDirectories(tif); |
| 334 |
if (n != 2) |
| 335 |
{ |
| 336 |
fprintf( |
| 337 |
stderr, |
| 338 |
"(23) Expected TIFFNumberOfDirectories() to return 2. Got %d\n", |
| 339 |
n); |
| 340 |
ret = 1; |
| 341 |
} |
| 342 |
TIFFClose(tif); |
| 343 |
} |
| 344 |
ret += test_subifd_loop(); |
| 345 |
return ret; |
| 346 |
} |