Debian Codegraph @

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

raw
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 open options
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 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #include "tiffio.h"
42 #include "tiffiop.h" // for struct TIFF
43
44 #define ERROR_STRING_SIZE 1024
45
46 /* Test TIFFLIB_AT_LEAST() macro */
47 #if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, \
48 TIFFLIB_MICRO_VERSION)
49 #error "TIFFLIB_AT_LEAST broken"
50 #endif
51 #if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, 0)
52 #error "TIFFLIB_AT_LEAST broken"
53 #endif
54 #if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, 0, 0)
55 #error "TIFFLIB_AT_LEAST broken"
56 #endif
57 #if !TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION - 1, 0, 0)
58 #error "TIFFLIB_AT_LEAST broken"
59 #endif
60 #if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION + 1, 0, 0)
61 #error "TIFFLIB_AT_LEAST broken"
62 #endif
63 #if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION + 1, 0)
64 #error "TIFFLIB_AT_LEAST broken"
65 #endif
66 #if TIFFLIB_AT_LEAST(TIFFLIB_MAJOR_VERSION, TIFFLIB_MINOR_VERSION, \
67 TIFFLIB_MICRO_VERSION + 1)
68 #error "TIFFLIB_AT_LEAST broken"
69 #endif
70
71 typedef struct MyErrorHandlerUserDataStruct
72 {
73 char *buffer;
74 size_t buffer_size;
75 TIFF *tif_got_from_callback;
76 char module[64];
77 } MyErrorHandlerUserDataStruct;
78
79 static int myErrorHandler(TIFF *tiff, void *user_data, const char *module,
80 const char *fmt, va_list ap)
81 {
82 MyErrorHandlerUserDataStruct *errorhandler_user_data =
83 (MyErrorHandlerUserDataStruct *)user_data;
84 vsnprintf(errorhandler_user_data->buffer,
85 errorhandler_user_data->buffer_size, fmt, ap);
86 errorhandler_user_data->tif_got_from_callback = tiff;
87 snprintf(errorhandler_user_data->module,
88 sizeof(errorhandler_user_data->module), "%s", module);
89 return 1;
90 }
91
92 static int test_error_handler()
93 {
94 int ret = 0;
95 char error_buffer[ERROR_STRING_SIZE] = {0};
96 char warn_buffer[ERROR_STRING_SIZE] = {0};
97 MyErrorHandlerUserDataStruct errorhandler_user_data = {
98 .buffer = error_buffer, .buffer_size = ERROR_STRING_SIZE};
99 MyErrorHandlerUserDataStruct warnhandler_user_data = {
100 .buffer = warn_buffer, .buffer_size = ERROR_STRING_SIZE};
101
102 TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
103 assert(opts);
104 TIFFOpenOptionsSetErrorHandlerExtR(opts, myErrorHandler,
105 &errorhandler_user_data);
106 TIFFOpenOptionsSetWarningHandlerExtR(opts, myErrorHandler,
107 &warnhandler_user_data);
108 TIFF *tif = TIFFOpenExt("test_error_handler.tif", "w", opts);
109 TIFFOpenOptionsFree(opts);
110
111 if (tif == NULL)
112 {
113 fprintf(stderr, "Cannot create test_error_handler.tif");
114 exit(1);
115 }
116
117 // Simulate an error emitted by libtiff
118 TIFFErrorExtR(tif, "my_error_module", "%s", "some error message");
119 if (strcmp(error_buffer, "some error message") != 0)
120 {
121 fprintf(stderr, "Did not get expected error message\n");
122 ret = 1;
123 }
124 if (strcmp(errorhandler_user_data.module, "my_error_module") != 0)
125 {
126 fprintf(stderr, "Did not get expected error module\n");
127 ret = 1;
128 }
129 if (errorhandler_user_data.tif_got_from_callback != tif)
130 {
131 fprintf(stderr,
132 "errorhandler_user_data.tif_got_from_callback != tif\n");
133 ret = 1;
134 }
135
136 // Simulate a warning emitted by libtiff
137 TIFFWarningExtR(tif, "my_warning_module", "%s", "some warning message");
138 if (strcmp(warn_buffer, "some warning message") != 0)
139 {
140 fprintf(stderr, "Did not get expected warning message\n");
141 ret = 1;
142 }
143 if (strcmp(warnhandler_user_data.module, "my_warning_module") != 0)
144 {
145 fprintf(stderr, "Did not get expected warning module\n");
146 ret = 1;
147 }
148 if (warnhandler_user_data.tif_got_from_callback != tif)
149 {
150 fprintf(stderr, "warnhandler_user_data.tif_got_from_callback != tif\n");
151 ret = 1;
152 }
153
154 TIFFClose(tif);
155 unlink("test_error_handler.tif");
156 return ret;
157 }
158
159 static int test_TIFFOpenOptionsSetMaxSingleMemAlloc(
160 tmsize_t limit, int expected_to_fail_in_open,
161 int expected_to_fail_in_write_directory, bool is_big_tiff)
162 {
163 int ret = 0;
164 TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
165 assert(opts);
166 TIFFOpenOptionsSetMaxSingleMemAlloc(opts, limit);
167 TIFF *tif =
168 TIFFOpenExt("test_error_handler.tif", is_big_tiff ? "w8" : "w", opts);
169 TIFFOpenOptionsFree(opts);
170 if (expected_to_fail_in_open)
171 {
172 if (tif != NULL)
173 {
174 fprintf(
175 stderr,
176 "Expected TIFFOpenExt() to fail due to memory limitation\n");
177 ret = 1;
178 TIFFClose(tif);
179 }
180 }
181 else
182 {
183 if (tif == NULL)
184 {
185 fprintf(stderr, "Expected TIFFOpenExt() to succeed\n");
186 ret = 1;
187 }
188 else
189 {
190 #define VALUE_SAMPLESPERPIXEL 10000
191 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, VALUE_SAMPLESPERPIXEL);
192 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
193 if (TIFFWriteDirectory(tif) == 0)
194 {
195 if (!expected_to_fail_in_write_directory)
196 {
197 fprintf(stderr,
198 "Expected TIFFWriteDirectory() to succeed\n");
199 ret = 1;
200 }
201 }
202 else
203 {
204 if (expected_to_fail_in_write_directory)
205 {
206 fprintf(stderr, "Expected TIFFWriteDirectory() to fail\n");
207 ret = 1;
208 }
209 }
210 TIFFClose(tif);
211 }
212 }
213 unlink("test_error_handler.tif");
214 return ret;
215 }
216
217 static int test_TIFFOpenOptionsSetMaxCumulatedMemAlloc(
218 tmsize_t limit, int expected_to_fail_in_open,
219 int expected_to_fail_in_write_directory)
220 {
221 int ret = 0;
222 TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
223 assert(opts);
224 TIFFOpenOptionsSetMaxCumulatedMemAlloc(opts, limit);
225 TIFF *tif = TIFFOpenExt("test_error_handler.tif", "w", opts);
226 TIFFOpenOptionsFree(opts);
227 if (expected_to_fail_in_open)
228 {
229 if (tif != NULL)
230 {
231 fprintf(
232 stderr,
233 "Expected TIFFOpenExt() to fail due to memory limitation\n");
234 ret = 1;
235 TIFFClose(tif);
236 }
237 }
238 else
239 {
240 if (tif == NULL)
241 {
242 fprintf(stderr, "Expected TIFFOpenExt() to succeed\n");
243 ret = 1;
244 }
245 else
246 {
247 #define VALUE_SAMPLESPERPIXEL 10000
248 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, VALUE_SAMPLESPERPIXEL);
249 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
250 if (TIFFWriteDirectory(tif) == 0)
251 {
252 if (!expected_to_fail_in_write_directory)
253 {
254 fprintf(stderr,
255 "Expected TIFFWriteDirectory() to succeed\n");
256 ret = 1;
257 }
258 }
259 else
260 {
261 if (expected_to_fail_in_write_directory)
262 {
263 fprintf(stderr, "Expected TIFFWriteDirectory() to fail\n");
264 ret = 1;
265 }
266 }
267 TIFFClose(tif);
268 }
269 }
270 unlink("test_error_handler.tif");
271 return ret;
272 }
273
274 int test_header_byte_order(TIFF *tif, char *openModeString)
275 {
276
277 if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
278 tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN)
279 {
280 fprintf(stderr,
281 "Bad magic number %" PRIu16 " (0x%4.4" PRIx16 ") for '%s'\n",
282 tif->tif_header.common.tiff_magic,
283 tif->tif_header.common.tiff_magic, openModeString);
284 return 1;
285 }
286 if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
287 (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
288 {
289 fprintf(stderr,
290 "Bad version number %" PRIu16 " (0x%4.4" PRIx16
291 ") for '%s'. Should be 0x%4.4" PRIx16 ".\n",
292 tif->tif_header.common.tiff_version,
293 tif->tif_header.common.tiff_version, openModeString,
294 TIFF_VERSION_CLASSIC);
295 return 1;
296 }
297 if (TIFFIsBigTIFF(tif))
298 {
299 if (tif->tif_header.big.tiff_offsetsize != 8)
300 {
301 fprintf(stderr,
302 "Bad BigTIFF offset-size %" PRIu16 " (0x%4.4" PRIx16
303 ") for '%s'. Should be 8.\n",
304 tif->tif_header.big.tiff_offsetsize,
305 tif->tif_header.big.tiff_offsetsize, openModeString);
306 return 1;
307 }
308 }
309
310 return 0;
311 }
312
313 int open_file_and_write(const char *filename, char *openModeString,
314 int blnWriteIFD, int blnClose, TIFF **tifOut)
315 {
316 TIFF *tif = TIFFOpen(filename, openModeString);
317 if (tifOut != NULL)
318 *tifOut = tif;
319 if (!tif)
320 {
321 fprintf(stderr, "Can't create or open %s\n", filename);
322 return 0x0100;
323 }
324 int ret = test_header_byte_order(tif, openModeString);
325 if (blnWriteIFD)
326 {
327 unsigned char buf[3] = {253, 127, 255};
328 TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1);
329 TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1);
330 TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
331 TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
332 TIFFWriteScanline(tif, buf, 0, 0);
333 if (!TIFFWriteDirectory(tif))
334 {
335 fprintf(stderr, "TIFFWriteDirectory() failed for %s.\n", filename);
336 TIFFClose(tif);
337 if (tifOut != NULL)
338 *tifOut = NULL;
339 return 0x1000;
340 }
341 if (test_header_byte_order(tif, openModeString))
342 ret += 0x0010;
343 }
344 if (blnClose)
345 {
346 TIFFClose(tif);
347 if (tifOut != NULL)
348 *tifOut = NULL;
349 }
350 return ret;
351 }
352
353 /* Checks that parameters of internal 'tif_header' structure are all swapped to
354 * host byte-order.
355 *
356 * Till LibTIFF 4.6.0 the parameters 'tiff_magic', 'tiff_version' and
357 * 'tiff_offsetsize' are swapped or not, depending how the file was opened: They
358 * were not swapped when opening with "w" or opening an not existent file with
359 * "a". They were swapped when opening an existent file with "r" or "a". This
360 * behaviour shall be harmonized so that the internal parameters are always in
361 * host byte-order.
362 */
363 int test_TIFFheader_swapping(void)
364 {
365
366 /* Provoke swapping and non-swapping on any host. */
367 char *openModeStrings[2][8] = {{"wb", "ab", "w8b", "a8b", "r"},
368 {"wl", "al", "w8l", "a8l", "r"}};
369 const char *filename = "test_TIFFheader_swapping.tif";
370 TIFF *tif;
371 int ret = 0;
372
373 unlink(filename);
374
375 for (unsigned int i = 0; i < 2; i++)
376 {
377 for (unsigned int k = 0; k < 4; k += 2)
378 {
379 /* Classic TIFF an BigTIFF */
380 /* Before fix, 'wb' and 'wb8' failed on little-endian hosts,
381 * and 'wl' and 'wl8' failed on big-endian hosts. */
382 unsigned int kk = k; /* Existing read-only */
383 if (open_file_and_write(filename, openModeStrings[i][kk], FALSE,
384 TRUE, &tif))
385 {
386 fprintf(stderr,
387 "TIFF header byte-order in opening mode '%s' failed.\n",
388 openModeStrings[i][kk]);
389 ret++;
390 }
391 kk++; /* Existing in append */
392 if (open_file_and_write(filename, openModeStrings[i][kk], TRUE,
393 TRUE, &tif))
394 {
395 fprintf(stderr,
396 "TIFF header byte-order in opening mode '%s' failed.\n",
397 openModeStrings[i][kk]);
398 ret++;
399 }
400
401 kk = 4; /* Existing read-only */
402 if (open_file_and_write(filename, openModeStrings[i][kk], FALSE,
403 TRUE, &tif))
404 {
405 fprintf(stderr,
406 "TIFF header byte-order in opening mode '%s' failed.\n",
407 openModeStrings[i][kk]);
408 ret++;
409 }
410 unlink(filename);
411 }
412
413 for (unsigned int k = 1; k < 4; k += 2)
414 {
415 /* Classic and BigTIFF:
416 * Open and write two IFD with non-existing file in append
417 * mode. Test also TIFFUnlinkDirectory(1) for known bug. */
418 /* Before fix, 'wb' and 'wb8' failed on little-endian hosts,
419 * and 'wl' and 'wl8' failed on big-endian hosts. */
420 if (open_file_and_write(filename, openModeStrings[i][k], TRUE, TRUE,
421 &tif))
422 {
423 fprintf(stderr,
424 "TIFF header byte-order in opening mode '%s' failed.\n",
425 openModeStrings[i][k]);
426 ret++;
427 }
428 /* Write second IFD for unlinking of first IFD and keep file open.
429 */
430 if (open_file_and_write(filename, openModeStrings[i][k], TRUE,
431 FALSE, &tif))
432 {
433 fprintf(stderr,
434 "TIFF header byte-order in opening mode '%s' failed.\n",
435 openModeStrings[i][k]);
436 ret++;
437 }
438 if (!TIFFUnlinkDirectory(tif, 1))
439 {
440 fprintf(stderr,
441 "TIFFUnlinkDirectory(1) in opening mode '%s' failed.\n",
442 openModeStrings[i][k]);
443 ret++;
444 }
445 if (test_header_byte_order(tif, openModeStrings[i][k]))
446 {
447 fprintf(
448 stderr,
449 "TIFF header byte-order after TIFFUnlinkDirectory(1) in "
450 "opening mode '%s' failed.\n",
451 openModeStrings[i][k]);
452 ret++;
453 }
454 /* Before fix, TIFFSetDirectory() failed when BigTIFF and swapping
455 * is needed, because TIFFUnlinkDirectory() stores tiff_diroff in
456 * wrong byte-order then. */
457 if (!TIFFSetDirectory(tif, 0))
458 {
459 fprintf(stderr,
460 "TIFFSetDirectory(0) after TIFFUnlinkDirectory(1) in "
461 "opening mode '%s' failed.\n",
462 openModeStrings[i][k]);
463 ret++;
464 }
465 TIFFClose(tif);
466 unlink(filename);
467 }
468 }
469
470 unlink(filename);
471 return ret;
472 }
473
474 int main()
475 {
476 int ret = 0;
477 fprintf(stderr, "=== test_open_options .... ===\n");
478 fprintf(stderr, "---- test_TIFFheader_swapping ---- \n");
479 if (ret += test_TIFFheader_swapping())
480 {
481 fprintf(stderr,
482 "--- Failed during test_TIFFheader_swapping test with %d "
483 "fails. ---\n",
484 ret);
485 }
486
487 ret += test_error_handler();
488 fprintf(stderr, "\n---- test_TIFFOpenOptionsSetMaxSingleMemAlloc "
489 "with non-BigTIFF ---- \n");
490 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(1, TRUE, -1, FALSE);
491 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(
492 sizeof(TIFF) + strlen("test_error_handler.tif") + 1, FALSE, TRUE,
493 FALSE);
494 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(
495 VALUE_SAMPLESPERPIXEL * sizeof(short), FALSE, FALSE, FALSE);
496
497 fprintf(stderr, "\n---- test_TIFFOpenOptionsSetMaxSingleMemAlloc "
498 "with BigTIFF ---- \n");
499 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(1, TRUE, -1, TRUE);
500 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(
501 sizeof(TIFF) + strlen("test_error_handler.tif") + 1, FALSE, TRUE, TRUE);
502 ret += test_TIFFOpenOptionsSetMaxSingleMemAlloc(
503 VALUE_SAMPLESPERPIXEL * sizeof(short), FALSE, FALSE, TRUE);
504
505 fprintf(stderr,
506 "\n---- test_TIFFOpenOptionsSetMaxCumulatedMemAlloc ---- \n");
507 ret += test_TIFFOpenOptionsSetMaxCumulatedMemAlloc(1, TRUE, -1);
508 ret += test_TIFFOpenOptionsSetMaxCumulatedMemAlloc(
509 sizeof(TIFF) + strlen("test_error_handler.tif") + 1, FALSE, TRUE);
510 ret += test_TIFFOpenOptionsSetMaxCumulatedMemAlloc(
511 sizeof(TIFF) + strlen("test_error_handler.tif") + 1 + 30000, FALSE,
512 FALSE);
513 if (ret > 0)
514 fprintf(stderr, "\n=== test_open_options failed with %d fails ===\n",
515 ret);
516 else
517 fprintf(stderr, "\n=== test_open_options finished OK ===\n");
518
519 return ret;
520 }