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
|
/*
* Copyright (c) 2022, Even Rouault <even.rouault at spatialys.com>
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
* TIFF Library
*
* Test IFD loop detection
*/
#include "tif_config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tiffio.h"
/* 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;
}
/* Test loop detection within chained SubIFDs.
* test_ifd_loop_subifd.tif contains seven main-IFDs (0 to 6) and within IFD 1
* there are three SubIFDs (0 to 2). Main IFD 4 loops back to main IFD 2.
* SubIFD 2 loops back to SubIFD 1.
* Within each IFD the tag PageName is filled with a string, indicating the
* IFD. The main IFDs are numbered 0 to 6 and the SubIFDs 200 to 202. */
int test_subifd_loop(void)
{
const char *filename = SOURCE_DIR "/images/test_ifd_loop_subifd.tif";
TIFF *tif;
int i, n;
int ret = 0;
#define NUMBER_OF_SUBIFDs 3
toff_t sub_IFDs_offsets[NUMBER_OF_SUBIFDs] = {
0UL}; /* array for SubIFD tag */
void *ptr;
uint16_t number_of_sub_IFDs = 0;
tif = TIFFOpen(filename, "r");
if (!tif)
{
fprintf(stderr, "Can't open %s\n", filename);
return 1;
}
/* Try to read six further main directories. Fifth read shall fail. */
for (i = 0; i < 6; i++)
{
if (!TIFFReadDirectory(tif))
break;
}
if (i != 4)
{
fprintf(stderr, "(30) Expected fifth TIFFReadDirectory() to fail\n");
ret = 1;
}
if (!is_requested_directory(tif, 4, filename))
{
fprintf(stderr, "(31) Expected fifth main IFD to be loaded\n");
ret = 1;
}
/* Switch to IFD 1 and get SubIFDs.
* Then read through SubIFDs and detect SubIFD loop.
* Finally go back to main-IFD and check if right IFD is loaded.
*/
if (!TIFFSetDirectory(tif, 1))
ret = 1;
/* Check if there are SubIFD subfiles */
if (TIFFGetField(tif, TIFFTAG_SUBIFD, &number_of_sub_IFDs, &ptr) &&
(number_of_sub_IFDs == 3))
{
/* 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 SubIFD directory directly via offset.
* SubIFDs PageName string contains numbers 200 to 202. */
if (!TIFFSetSubDirectory(tif, sub_IFDs_offsets[i]))
ret = 1;
if (!is_requested_directory(tif, 200 + i, filename))
{
fprintf(stderr, "(32) Expected SubIFD %d to be loaded.\n", i);
ret = 1;
}
/* Now test SubIFD loop detection.
* The (i+n).th read in the SubIFD chain shall fail. */
for (n = 0; n < number_of_sub_IFDs; n++)
{
if (!TIFFReadDirectory(tif))
break;
}
if ((i + n) != 2)
{
fprintf(
stderr,
"(33) Expected third SubIFD-TIFFReadDirectory() to fail\n");
ret = 1;
}
}
/* Go back to main-IFD chain and re-read that main-IFD directory */
if (!TIFFSetDirectory(tif, 3))
ret = 1;
if (!is_requested_directory(tif, 3, filename))
{
fprintf(stderr, "(34) Expected fourth main IFD to be loaded\n");
ret = 1;
}
}
else
{
fprintf(stderr, "(35) No or wrong expected SubIFDs within main IFD\n");
ret = 1;
}
TIFFClose(tif);
return ret;
} /*-- test_subifd_loop() --*/
int main()
{
int ret = 0;
{
TIFF *tif =
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_self.tif", "r");
assert(tif);
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(1) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
TIFFClose(tif);
}
{
TIFF *tif =
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_self.tif", "r");
assert(tif);
int n = TIFFNumberOfDirectories(tif);
if (n != 1)
{
fprintf(
stderr,
"(2) Expected TIFFNumberOfDirectories() to return 1. Got %d\n",
n);
ret = 1;
}
TIFFClose(tif);
}
{
TIFF *tif =
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_first.tif", "r");
assert(tif);
if (TIFFReadDirectory(tif) != 1)
{
fprintf(stderr, "(3) Expected TIFFReadDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(4) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 1) != 1)
{
fprintf(stderr, "(5) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(6) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 0) != 1)
{
fprintf(stderr, "(7) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif) != 1)
{
fprintf(stderr, "(8) Expected TIFFReadDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(9) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
TIFFClose(tif);
}
{
TIFF *tif =
TIFFOpen(SOURCE_DIR "/images/test_ifd_loop_to_first.tif", "r");
assert(tif);
int n = TIFFNumberOfDirectories(tif);
if (n != 2)
{
fprintf(
stderr,
"(10) Expected TIFFNumberOfDirectories() to return 2. Got %d\n",
n);
ret = 1;
}
TIFFClose(tif);
}
{
TIFF *tif = TIFFOpen(SOURCE_DIR "/images/test_two_ifds.tif", "r");
assert(tif);
if (TIFFReadDirectory(tif) != 1)
{
fprintf(stderr, "(11) Expected TIFFReadDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(12) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 1) != 1)
{
fprintf(stderr, "(13) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(14) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 0) != 1)
{
fprintf(stderr, "(15) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif) != 1)
{
fprintf(stderr, "(16) Expected TIFFReadDirectory() to succeed\n");
ret = 1;
}
if (TIFFReadDirectory(tif))
{
fprintf(stderr, "(17) Expected TIFFReadDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 0) != 1)
{
fprintf(stderr, "(18) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 1) != 1)
{
fprintf(stderr, "(19) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 2))
{
fprintf(stderr, "(20) Expected TIFFSetDirectory() to fail\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 1) != 1)
{
fprintf(stderr, "(21) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
if (TIFFSetDirectory(tif, 0) != 1)
{
fprintf(stderr, "(22) Expected TIFFSetDirectory() to succeed\n");
ret = 1;
}
TIFFClose(tif);
}
{
TIFF *tif = TIFFOpen(SOURCE_DIR "/images/test_two_ifds.tif", "r");
assert(tif);
int n = TIFFNumberOfDirectories(tif);
if (n != 2)
{
fprintf(
stderr,
"(23) Expected TIFFNumberOfDirectories() to return 2. Got %d\n",
n);
ret = 1;
}
TIFFClose(tif);
}
ret += test_subifd_loop();
return ret;
}
|