2 /* pngunknown.c - test the read side unknown chunk handling
4 * Copyright (c) 2021 Cosmin Truta
5 * Copyright (c) 2015,2017 Glenn Randers-Pehrson
6 * Written by John Cunningham Bowler
8 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h
13 * This is a C program that is intended to be linked against libpng. It
14 * allows the libpng unknown handling code to be tested by interpreting
15 * arguments to save or discard combinations of chunks. The program is
16 * currently just a minimal validation for the built-in libpng facilities.
24 /* Define the following to use this test against your installed libpng, rather
25 * than the one being built here:
27 #ifdef PNG_FREESTANDING_TESTS
30 # include "../../png.h"
33 /* 1.6.1 added support for the configure test harness, which uses 77 to indicate
34 * a skipped test, in earlier versions we need to succeed on a skipped test, so:
36 #if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
43 /* Since this program tests the ability to change the unknown chunk handling
44 * these must be defined:
46 #if defined(PNG_SET_UNKNOWN_CHUNKS_SUPPORTED) &&\
47 defined(PNG_STDIO_SUPPORTED) &&\
48 defined(PNG_READ_SUPPORTED)
50 /* One of these must be defined to allow us to find out what happened. It is
51 * still useful to set unknown chunk handling without either of these in order
52 * to cause *known* chunks to be discarded. This can be a significant
53 * efficiency gain, but it can't really be tested here.
55 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) ||\
56 defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
58 #if PNG_LIBPNG_VER < 10500
59 /* This deliberately lacks the const. */
60 typedef png_byte *png_const_bytep;
62 /* This is copied from 1.5.1 png.h: */
63 #define PNG_INTERLACE_ADAM7_PASSES 7
64 #define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
65 #define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
66 #define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
67 #define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
68 #define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
69 -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
70 #define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
71 -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
72 #define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
73 (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
74 #define PNG_COL_FROM_PASS_COL(xIn, pass) \
75 (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
76 #define PNG_PASS_MASK(pass,off) ( \
77 ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
78 ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
79 #define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
80 ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
81 #define PNG_COL_IN_INTERLACE_PASS(x, pass) \
82 ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
84 /* These are needed too for the default build: */
85 #define PNG_WRITE_16BIT_SUPPORTED
86 #define PNG_READ_16BIT_SUPPORTED
88 /* This comes from pnglibconf.h after 1.5: */
89 #define PNG_FP_1 100000
90 #define PNG_GAMMA_THRESHOLD_FIXED\
91 ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
94 #if PNG_LIBPNG_VER < 10600
95 /* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
96 * compiled against earlier versions.
98 # define png_const_structp png_structp
101 #if PNG_LIBPNG_VER < 10700
102 /* Copied from libpng 1.7.0 png.h */
103 #define PNG_u2(b1, b2) (((unsigned int)(b1) << 8) + (b2))
105 #define PNG_U16(b1, b2) ((png_uint_16)PNG_u2(b1, b2))
106 #define PNG_U32(b1, b2, b3, b4)\
107 (((png_uint_32)PNG_u2(b1, b2) << 16) + PNG_u2(b3, b4))
109 /* Constants for known chunk types.
111 #define png_IDAT PNG_U32( 73, 68, 65, 84)
112 #define png_IEND PNG_U32( 73, 69, 78, 68)
113 #define png_IHDR PNG_U32( 73, 72, 68, 82)
114 #define png_PLTE PNG_U32( 80, 76, 84, 69)
115 #define png_bKGD PNG_U32( 98, 75, 71, 68)
116 #define png_cHRM PNG_U32( 99, 72, 82, 77)
117 #define png_eXIf PNG_U32(101, 88, 73, 102) /* registered July 2017 */
118 #define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */
119 #define png_gAMA PNG_U32(103, 65, 77, 65)
120 #define png_gIFg PNG_U32(103, 73, 70, 103)
121 #define png_gIFt PNG_U32(103, 73, 70, 116) /* deprecated */
122 #define png_gIFx PNG_U32(103, 73, 70, 120)
123 #define png_hIST PNG_U32(104, 73, 83, 84)
124 #define png_iCCP PNG_U32(105, 67, 67, 80)
125 #define png_iTXt PNG_U32(105, 84, 88, 116)
126 #define png_oFFs PNG_U32(111, 70, 70, 115)
127 #define png_pCAL PNG_U32(112, 67, 65, 76)
128 #define png_pHYs PNG_U32(112, 72, 89, 115)
129 #define png_sBIT PNG_U32(115, 66, 73, 84)
130 #define png_sCAL PNG_U32(115, 67, 65, 76)
131 #define png_sPLT PNG_U32(115, 80, 76, 84)
132 #define png_sRGB PNG_U32(115, 82, 71, 66)
133 #define png_sTER PNG_U32(115, 84, 69, 82)
134 #define png_tEXt PNG_U32(116, 69, 88, 116)
135 #define png_tIME PNG_U32(116, 73, 77, 69)
136 #define png_tRNS PNG_U32(116, 82, 78, 83)
137 #define png_zTXt PNG_U32(122, 84, 88, 116)
139 /* Test on flag values as defined in the spec (section 5.4): */
140 #define PNG_CHUNK_ANCILLARY(c) (1 & ((c) >> 29))
141 #define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c))
142 #define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21))
143 #define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13))
144 #define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5))
146 #endif /* PNG_LIBPNG_VER < 10700 */
149 # define this not_the_cpp_this
150 # define new not_the_cpp_new
151 # define voidcast(type, value) static_cast<type>(value)
153 # define voidcast(type, value) (value)
154 #endif /* __cplusplus */
156 /* Unused formal parameter errors are removed using the following macro which is
157 * expected to have no bad effects on performance.
160 # if defined(__GNUC__) || defined(_MSC_VER)
161 # define UNUSED(param) (void)param;
163 # define UNUSED(param)
167 /* Types of chunks not known to libpng */
168 #define png_vpAg PNG_U32(118, 112, 65, 103)
170 /* Chunk information */
171 #define PNG_INFO_tEXt 0x10000000U
172 #define PNG_INFO_iTXt 0x20000000U
173 #define PNG_INFO_zTXt 0x40000000U
175 #define PNG_INFO_sTER 0x01000000U
176 #define PNG_INFO_vpAg 0x02000000U
187 int unknown; /* Chunk not known to libpng */
188 int all; /* Chunk set by the '-1' option */
189 int position; /* position in pngtest.png */
190 int keep; /* unknown handling setting */
192 /* Critical chunks */
193 { "IDAT", PNG_INFO_IDAT, png_IDAT, 0, 0, START, 0 }, /* must be [0] */
194 { "PLTE", PNG_INFO_PLTE, png_PLTE, 0, 0, ABSENT, 0 },
196 /* Non-critical chunks that libpng handles */
197 /* This is a mess but it seems to be the only way to do it - there is no way
198 * to check for a definition outside a #if.
200 { "bKGD", PNG_INFO_bKGD, png_bKGD,
201 # ifdef PNG_READ_bKGD_SUPPORTED
207 { "cHRM", PNG_INFO_cHRM, png_cHRM,
208 # ifdef PNG_READ_cHRM_SUPPORTED
214 { "eXIf", PNG_INFO_eXIf, png_eXIf,
215 # ifdef PNG_READ_eXIf_SUPPORTED
221 { "gAMA", PNG_INFO_gAMA, png_gAMA,
222 # ifdef PNG_READ_gAMA_SUPPORTED
228 { "hIST", PNG_INFO_hIST, png_hIST,
229 # ifdef PNG_READ_hIST_SUPPORTED
235 { "iCCP", PNG_INFO_iCCP, png_iCCP,
236 # ifdef PNG_READ_iCCP_SUPPORTED
242 { "iTXt", PNG_INFO_iTXt, png_iTXt,
243 # ifdef PNG_READ_iTXt_SUPPORTED
249 { "oFFs", PNG_INFO_oFFs, png_oFFs,
250 # ifdef PNG_READ_oFFs_SUPPORTED
256 { "pCAL", PNG_INFO_pCAL, png_pCAL,
257 # ifdef PNG_READ_pCAL_SUPPORTED
263 { "pHYs", PNG_INFO_pHYs, png_pHYs,
264 # ifdef PNG_READ_pHYs_SUPPORTED
270 { "sBIT", PNG_INFO_sBIT, png_sBIT,
271 # ifdef PNG_READ_sBIT_SUPPORTED
277 { "sCAL", PNG_INFO_sCAL, png_sCAL,
278 # ifdef PNG_READ_sCAL_SUPPORTED
284 { "sPLT", PNG_INFO_sPLT, png_sPLT,
285 # ifdef PNG_READ_sPLT_SUPPORTED
291 { "sRGB", PNG_INFO_sRGB, png_sRGB,
292 # ifdef PNG_READ_sRGB_SUPPORTED
298 { "tEXt", PNG_INFO_tEXt, png_tEXt,
299 # ifdef PNG_READ_tEXt_SUPPORTED
305 { "tIME", PNG_INFO_tIME, png_tIME,
306 # ifdef PNG_READ_tIME_SUPPORTED
312 { "tRNS", PNG_INFO_tRNS, png_tRNS,
313 # ifdef PNG_READ_tRNS_SUPPORTED
319 { "zTXt", PNG_INFO_zTXt, png_zTXt,
320 # ifdef PNG_READ_zTXt_SUPPORTED
327 /* No libpng handling */
328 { "sTER", PNG_INFO_sTER, png_sTER, 1, 1, START, 0 },
329 { "vpAg", PNG_INFO_vpAg, png_vpAg, 1, 0, START, 0 },
332 #define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
339 chunk_info[i].keep = 0;
343 find(const char *name)
348 if (memcmp(chunk_info[i].name, name, 4) == 0)
356 findb(const png_byte *name)
361 if (memcmp(chunk_info[i].name, name, 4) == 0)
369 find_by_flag(png_uint_32 flag)
374 if (chunk_info[i].flag == flag)
377 fprintf(stderr, "pngunknown: internal error\n");
382 ancillary(const char *name)
384 return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
387 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
389 ancillaryb(const png_byte *name)
391 return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
395 /* Type of an error_ptr */
398 jmp_buf error_return;
400 png_infop info_ptr, end_ptr;
401 png_uint_32 before_IDAT;
402 png_uint_32 after_IDAT;
405 int keep; /* the default value */
411 static const char init[] = "initialization";
412 static const char cmd[] = "command line";
415 init_display(display *d, const char *program)
417 memset(d, 0, sizeof *d);
419 d->info_ptr = d->end_ptr = NULL;
420 d->error_count = d->warning_count = 0;
421 d->program = program;
427 clean_display(display *d)
429 png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_ptr);
431 /* This must not happen - it might cause an app crash */
432 if (d->png_ptr != NULL || d->info_ptr != NULL || d->end_ptr != NULL)
434 fprintf(stderr, "%s(%s): png_destroy_read_struct error\n", d->file,
440 PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
444 if (d->png_ptr != NULL)
447 /* During initialization and if this is a single command line argument set
448 * exit now - there is only one test, otherwise longjmp to do the next test.
450 if (d->test == init || d->test == cmd)
453 longjmp(d->error_return, 1);
457 display_rc(const display *d, int strict)
459 return d->error_count + (strict ? d->warning_count : 0);
462 /* libpng error and warning callbacks */
463 PNG_FUNCTION(void, (PNGCBAPI error), (png_structp png_ptr, const char *message),
466 display *d = (display*)png_get_error_ptr(png_ptr);
468 fprintf(stderr, "%s(%s): libpng error: %s\n", d->file, d->test, message);
473 warning(png_structp png_ptr, const char *message)
475 display *d = (display*)png_get_error_ptr(png_ptr);
477 fprintf(stderr, "%s(%s): libpng warning: %s\n", d->file, d->test, message);
478 ++(d->warning_count);
482 get_valid(display *d, png_infop info_ptr)
484 png_uint_32 flags = png_get_valid(d->png_ptr, info_ptr, (png_uint_32)~0);
486 /* Map the text chunks back into the flags */
489 png_uint_32 ntext = png_get_text(d->png_ptr, info_ptr, &text, NULL);
491 while (ntext > 0) switch (text[--ntext].compression)
494 flags |= PNG_INFO_tEXt;
497 flags |= PNG_INFO_zTXt;
501 flags |= PNG_INFO_iTXt;
504 fprintf(stderr, "%s(%s): unknown text compression %d\n", d->file,
505 d->test, text[ntext].compression);
513 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
515 read_callback(png_structp pp, png_unknown_chunkp pc)
517 /* This function mimics the behavior of png_set_keep_unknown_chunks by
518 * returning '0' to keep the chunk and '1' to discard it.
520 display *d = voidcast(display*, png_get_user_chunk_ptr(pp));
521 int chunk = findb(pc->name);
524 if (chunk < 0) /* not one in our list, so not a known chunk */
529 keep = chunk_info[chunk].keep;
530 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
532 /* See the comments in png.h - use the default for unknown chunks,
533 * do not keep known chunks.
535 if (chunk_info[chunk].unknown)
539 keep = PNG_HANDLE_CHUNK_NEVER;
546 fprintf(stderr, "%s(%s): %d: unrecognized chunk option\n", d->file,
547 d->test, chunk_info[chunk].keep);
550 case PNG_HANDLE_CHUNK_AS_DEFAULT:
551 case PNG_HANDLE_CHUNK_NEVER:
552 discard = 1; /*handled; discard*/
555 case PNG_HANDLE_CHUNK_IF_SAFE:
556 case PNG_HANDLE_CHUNK_ALWAYS:
557 discard = 0; /*not handled; keep*/
561 /* Also store information about this chunk in the display, the relevant flag
562 * is set if the chunk is to be kept ('not handled'.)
566 if (!discard) /* stupidity to stop a GCC warning */
568 png_uint_32 flag = chunk_info[chunk].flag;
570 if (pc->location & PNG_AFTER_IDAT)
571 d->after_IDAT |= flag;
574 d->before_IDAT |= flag;
578 /* However if there is no support to store unknown chunks don't ask libpng to
579 * do it; there will be an png_error.
581 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
584 return 1; /*handled; discard*/
587 #endif /* READ_USER_CHUNKS_SUPPORTED */
589 #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
591 get_unknown(display *d, png_infop info_ptr, int after_IDAT)
593 /* Create corresponding 'unknown' flags */
594 png_uint_32 flags = 0;
599 png_unknown_chunkp unknown;
600 int num_unknown = png_get_unknown_chunks(d->png_ptr, info_ptr, &unknown);
602 while (--num_unknown >= 0)
604 int chunk = findb(unknown[num_unknown].name);
606 /* Chunks not known to pngunknown must be validated here; since they
607 * must also be unknown to libpng the 'display->keep' behavior should
610 if (chunk < 0) switch (d->keep)
612 default: /* impossible */
613 case PNG_HANDLE_CHUNK_AS_DEFAULT:
614 case PNG_HANDLE_CHUNK_NEVER:
615 fprintf(stderr, "%s(%s): %s: %s: unknown chunk saved\n",
616 d->file, d->test, d->keep ? "discard" : "default",
617 unknown[num_unknown].name);
621 case PNG_HANDLE_CHUNK_IF_SAFE:
622 if (!ancillaryb(unknown[num_unknown].name))
625 "%s(%s): if-safe: %s: unknown critical chunk saved\n",
626 d->file, d->test, unknown[num_unknown].name);
630 /* FALLTHROUGH */ /* (safe) */
631 case PNG_HANDLE_CHUNK_ALWAYS:
636 flags |= chunk_info[chunk].flag;
642 #else /* SAVE_UNKNOWN_CHUNKS */
644 get_unknown(display *d, png_infop info_ptr, int after_IDAT)
645 /* Otherwise this will return the cached values set by any user callback */
650 return d->after_IDAT;
653 return d->before_IDAT;
656 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
657 /* The #defines above should mean this is never reached, it's just here as
658 * a check to ensure the logic is correct.
660 # error No store support and no user chunk support, this will not work
661 # endif /* READ_USER_CHUNKS */
662 #endif /* SAVE_UNKNOWN_CHUNKS */
665 check(FILE *fp, int argc, const char **argv, png_uint_32p flags/*out*/,
666 display *d, int set_callback)
668 int i, npasses, ipass;
671 d->keep = PNG_HANDLE_CHUNK_AS_DEFAULT;
675 /* Some of these errors are permanently fatal and cause an exit here, others
676 * are per-test and cause an error return.
678 d->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, d, error,
680 if (d->png_ptr == NULL)
682 fprintf(stderr, "%s(%s): could not allocate png struct\n", d->file,
684 /* Terminate here, this error is not test specific. */
688 d->info_ptr = png_create_info_struct(d->png_ptr);
689 d->end_ptr = png_create_info_struct(d->png_ptr);
690 if (d->info_ptr == NULL || d->end_ptr == NULL)
692 fprintf(stderr, "%s(%s): could not allocate png info\n", d->file,
698 png_init_io(d->png_ptr, fp);
700 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
701 /* This is only done if requested by the caller; it interferes with the
702 * standard store/save mechanism.
705 png_set_read_user_chunk_fn(d->png_ptr, d, read_callback);
710 /* Handle each argument in turn; multiple settings are possible for the same
711 * chunk and multiple calls will occur (the last one should override all
714 for (i=0; i<argc; ++i)
716 const char *equals = strchr(argv[i], '=');
722 if (strcmp(equals+1, "default") == 0)
723 option = PNG_HANDLE_CHUNK_AS_DEFAULT;
724 else if (strcmp(equals+1, "discard") == 0)
725 option = PNG_HANDLE_CHUNK_NEVER;
726 else if (strcmp(equals+1, "if-safe") == 0)
727 option = PNG_HANDLE_CHUNK_IF_SAFE;
728 else if (strcmp(equals+1, "save") == 0)
729 option = PNG_HANDLE_CHUNK_ALWAYS;
732 fprintf(stderr, "%s(%s): %s: unrecognized chunk option\n", d->file,
737 switch (equals - argv[i])
739 case 4: /* chunk name */
740 chunk = find(argv[i]);
744 /* These #if tests have the effect of skipping the arguments
745 * if SAVE support is unavailable - we can't do a useful test
746 * in this case, so we just check the arguments! This could
747 * be improved in the future by using the read callback.
749 # if PNG_LIBPNG_VER >= 10700 &&\
750 !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
751 if (option < PNG_HANDLE_CHUNK_IF_SAFE)
752 # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
756 memcpy(name, chunk_info[chunk].name, 5);
757 png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
758 chunk_info[chunk].keep = option;
765 case 7: /* default */
766 if (memcmp(argv[i], "default", 7) == 0)
768 # if PNG_LIBPNG_VER >= 10700 &&\
769 !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
770 if (option < PNG_HANDLE_CHUNK_IF_SAFE)
771 # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
772 png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
781 if (memcmp(argv[i], "all", 3) == 0)
783 # if PNG_LIBPNG_VER >= 10700 &&\
784 !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
785 if (option < PNG_HANDLE_CHUNK_IF_SAFE)
786 # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
787 png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
791 for (chunk = 0; chunk < NINFO; ++chunk)
792 if (chunk_info[chunk].all)
793 chunk_info[chunk].keep = option;
799 default: /* some misplaced = */
805 fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
810 png_read_info(d->png_ptr, d->info_ptr);
812 switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
814 case PNG_INTERLACE_NONE:
818 case PNG_INTERLACE_ADAM7:
819 npasses = PNG_INTERLACE_ADAM7_PASSES;
823 /* Hard error because it is not test specific */
824 fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
829 /* Skip the image data, if IDAT is not being handled then don't do this
830 * because it will cause a CRC error.
832 if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
834 png_start_read_image(d->png_ptr);
835 height = png_get_image_height(d->png_ptr, d->info_ptr);
839 png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
841 for (ipass=0; ipass<npasses; ++ipass)
843 png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
849 for (y=0; y<height; ++y)
850 if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
851 png_read_row(d->png_ptr, NULL, NULL);
856 else /* not interlaced */
860 for (y=0; y<height; ++y)
861 png_read_row(d->png_ptr, NULL, NULL);
865 png_read_end(d->png_ptr, d->end_ptr);
867 flags[0] = get_valid(d, d->info_ptr);
868 flags[1] = get_unknown(d, d->info_ptr, 0/*before IDAT*/);
870 /* Only png_read_png sets PNG_INFO_IDAT! */
871 flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
874 flags[2] = get_valid(d, d->end_ptr);
875 flags[3] = get_unknown(d, d->end_ptr, 1/*after IDAT*/);
883 check_error(display *d, png_uint_32 flags, const char *message)
887 png_uint_32 flag = flags & -(png_int_32)flags;
888 int i = find_by_flag(flag);
890 fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
891 chunk_info[i].name, message);
899 check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
900 png_uint_32 unknown, const char *position, int set_callback)
904 png_uint_32 flag = chunks & -(png_int_32)chunks;
905 int i = find_by_flag(flag);
906 int keep = chunk_info[i].keep;
908 const char *errorx = NULL;
910 if (chunk_info[i].unknown)
912 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
914 type = "UNKNOWN (default)";
919 type = "UNKNOWN (specified)";
922 errorx = "chunk processed";
926 case PNG_HANDLE_CHUNK_AS_DEFAULT:
928 errorx = "DEFAULT: unknown chunk saved";
931 case PNG_HANDLE_CHUNK_NEVER:
933 errorx = "DISCARD: unknown chunk saved";
936 case PNG_HANDLE_CHUNK_IF_SAFE:
937 if (ancillary(chunk_info[i].name))
939 if (!(flag & unknown))
940 errorx = "IF-SAFE: unknown ancillary chunk lost";
943 else if (flag & unknown)
944 errorx = "IF-SAFE: unknown critical chunk saved";
947 case PNG_HANDLE_CHUNK_ALWAYS:
948 if (!(flag & unknown))
949 errorx = "SAVE: unknown chunk lost";
953 errorx = "internal error: bad keep";
956 } /* unknown chunk */
958 else /* known chunk */
964 /* chunk was processed, it won't have been saved because that is
965 * caught below when checking for inconsistent processing.
967 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
968 errorx = "!DEFAULT: known chunk processed";
971 else /* not processed */ switch (keep)
973 case PNG_HANDLE_CHUNK_AS_DEFAULT:
974 errorx = "DEFAULT: known chunk not processed";
977 case PNG_HANDLE_CHUNK_NEVER:
979 errorx = "DISCARD: known chunk saved";
982 case PNG_HANDLE_CHUNK_IF_SAFE:
983 if (ancillary(chunk_info[i].name))
985 if (!(flag & unknown))
986 errorx = "IF-SAFE: known ancillary chunk lost";
989 else if (flag & unknown)
990 errorx = "IF-SAFE: known critical chunk saved";
993 case PNG_HANDLE_CHUNK_ALWAYS:
994 if (!(flag & unknown))
995 errorx = "SAVE: known chunk lost";
999 errorx = "internal error: bad keep (2)";
1007 fprintf(stderr, "%s(%s%s): %s %s %s: %s\n", d->file, d->test,
1008 set_callback ? ",callback" : "",
1009 type, chunk_info[i].name, position, errorx);
1017 perform_one_test(FILE *fp, int argc, const char **argv,
1018 png_uint_32 *default_flags, display *d, int set_callback)
1021 png_uint_32 flags[2][4];
1025 memcpy(flags[0], default_flags, sizeof flags[0]);
1027 def = check(fp, argc, argv, flags[1], d, set_callback);
1029 /* If IDAT is being handled as unknown the image read is skipped and all the
1030 * IDATs after the first end up in the end info struct, so in this case add
1031 * IDAT to the list of unknowns. (Do this after 'check' above sets the
1032 * chunk_info 'keep' fields.)
1034 * Note that the flag setting has to be in the 'known' field to avoid
1035 * triggering the consistency check below and the flag must only be set if
1036 * there are multiple IDATs, so if the check above did find an unknown IDAT
1039 if (chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT &&
1040 (flags[1][3] & PNG_INFO_IDAT) != 0)
1041 flags[0][2] |= PNG_INFO_IDAT;
1043 /* Chunks should either be known or unknown, never both and this should apply
1044 * whether the chunk is before or after the IDAT (actually, the app can
1045 * probably change this by swapping the handling after the image, but this
1046 * test does not do that.)
1048 check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
1049 "chunk handled inconsistently in count tests");
1050 check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
1051 "chunk handled inconsistently in option tests");
1053 /* Now find out what happened to each chunk before and after the IDAT and
1054 * determine if the behavior was correct. First some basic sanity checks,
1055 * any known chunk should be known in the original count, any unknown chunk
1056 * should be either known or unknown in the original.
1061 test = flags[1][0] & ~flags[0][0];
1062 check_error(d, test, "new known chunk before IDAT");
1063 test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
1064 check_error(d, test, "new unknown chunk before IDAT");
1065 test = flags[1][2] & ~flags[0][2];
1066 check_error(d, test, "new known chunk after IDAT");
1067 test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
1068 check_error(d, test, "new unknown chunk after IDAT");
1071 /* Now each chunk in the original list should have been handled according to
1072 * the options set for that chunk, regardless of whether libpng knows about
1075 check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
1076 "before IDAT", set_callback);
1077 check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
1078 "after IDAT", set_callback);
1082 perform_one_test_safe(FILE *fp, int argc, const char **argv,
1083 png_uint_32 *default_flags, display *d, const char *test)
1085 if (setjmp(d->error_return) == 0)
1087 d->test = test; /* allow use of d->error_return */
1088 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1089 perform_one_test(fp, argc, argv, default_flags, d, 0);
1091 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1092 perform_one_test(fp, argc, argv, default_flags, d, 1);
1094 d->test = init; /* prevent use of d->error_return */
1098 static const char *standard_tests[] =
1100 "discard", "default=discard", NULL,
1101 "save", "default=save", NULL,
1102 "if-safe", "default=if-safe", NULL,
1103 "vpAg", "vpAg=if-safe", NULL,
1104 "sTER", "sTER=if-safe", NULL,
1105 "IDAT", "default=discard", "IDAT=save", NULL,
1106 "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
1107 "sBIT=save", "sRGB=save", "eXIf=save", NULL,
1111 static PNG_NORETURN void
1112 usage(const char *program, const char *reason)
1114 fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
1115 "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
1116 "testfile.png\n", reason, program);
1121 main(int argc, const char **argv)
1124 png_uint_32 default_flags[4]; /*valid,unknown{before,after}*/
1125 int strict = 0, default_tests = 0;
1126 const char *count_argv = "default=save";
1127 const char *touch_file = NULL;
1130 init_display(&d, argv[0]);
1132 while (++argv, --argc > 0)
1134 if (strcmp(*argv, "--strict") == 0)
1137 else if (strcmp(*argv, "--default") == 0)
1140 else if (strcmp(*argv, "--touch") == 0)
1143 touch_file = *++argv, --argc;
1146 usage(d.program, "--touch: missing file name");
1153 /* A file name is required, but there should be no other arguments if
1154 * --default was specified.
1157 usage(d.program, "missing test file");
1159 /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
1160 * optimization which causes warnings with -Wstrict-overflow!
1162 else if (default_tests)
1164 usage(d.program, "extra arguments");
1166 /* The name of the test file is the last argument; remove it. */
1167 d.file = argv[--argc];
1169 fp = fopen(d.file, "rb");
1176 /* First find all the chunks, known and unknown, in the test file, a failure
1177 * here aborts the whole test.
1179 * If 'save' is supported then the normal saving method should happen,
1180 * otherwise if 'read' is supported then the read callback will do the
1181 * same thing. If both are supported the 'read' callback won't be
1182 * instantiated by default. If 'save' is *not* supported then a user
1183 * callback is required even though we can call png_get_unknown_chunks.
1185 if (check(fp, 1, &count_argv, default_flags, &d,
1186 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1191 ) != PNG_HANDLE_CHUNK_ALWAYS)
1193 fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
1197 /* Now find what the various supplied options cause to change: */
1200 d.test = cmd; /* acts as a flag to say exit, do not longjmp */
1201 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
1202 perform_one_test(fp, argc, argv, default_flags, &d, 0);
1204 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1205 perform_one_test(fp, argc, argv, default_flags, &d, 1);
1212 const char **test = standard_tests;
1214 /* Set the exit_test pointer here so we can continue after a libpng error.
1215 * NOTE: this leaks memory because the png_struct data from the failing
1216 * test is never freed.
1220 const char *this_test = *test++;
1221 const char **next = test;
1222 int count = display_rc(&d, strict), new_count;
1226 while (*next != NULL)
1232 perform_one_test_safe(fp, arg_count, test, default_flags, &d,
1235 new_count = display_rc(&d, strict);
1237 if (new_count == count)
1243 printf("%s: %s %s\n", result, d.program, this_test);
1251 if (display_rc(&d, strict) == 0)
1253 /* Success, touch the success file if appropriate */
1254 if (touch_file != NULL)
1256 FILE *fsuccess = fopen(touch_file, "wt");
1258 if (fsuccess != NULL)
1261 fprintf(fsuccess, "PNG unknown tests succeeded\n");
1263 err = ferror(fsuccess);
1265 if (fclose(fsuccess) || err)
1267 fprintf(stderr, "%s: write failed\n", touch_file);
1274 fprintf(stderr, "%s: open failed\n", touch_file);
1285 #else /* !(READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS) */
1290 " test ignored: no support to find out about unknown chunks\n");
1291 /* So the test is skipped: */
1294 #endif /* READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS */
1296 #else /* !(SET_UNKNOWN_CHUNKS && READ) */
1301 " test ignored: no support to modify unknown chunk handling\n");
1302 /* So the test is skipped: */
1305 #endif /* SET_UNKNOWN_CHUNKS && READ*/