2 /* pngunknown.c - test the read side unknown chunk handling
4 * Last changed in libpng 1.6.0 [February 14, 2013]
5 * Copyright (c) 2013 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 #ifdef PNG_READ_SUPPORTED
35 #if PNG_LIBPNG_VER < 10500
36 /* This deliberately lacks the PNG_CONST. */
37 typedef png_byte
*png_const_bytep
;
39 /* This is copied from 1.5.1 png.h: */
40 #define PNG_INTERLACE_ADAM7_PASSES 7
41 #define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
42 #define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
43 #define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
44 #define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
45 #define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
46 -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
47 #define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
48 -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
49 #define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
50 (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
51 #define PNG_COL_FROM_PASS_COL(xIn, pass) \
52 (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
53 #define PNG_PASS_MASK(pass,off) ( \
54 ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
55 ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
56 #define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
57 ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
58 #define PNG_COL_IN_INTERLACE_PASS(x, pass) \
59 ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
61 /* These are needed too for the default build: */
62 #define PNG_WRITE_16BIT_SUPPORTED
63 #define PNG_READ_16BIT_SUPPORTED
65 /* This comes from pnglibconf.h afer 1.5: */
66 #define PNG_FP_1 100000
67 #define PNG_GAMMA_THRESHOLD_FIXED\
68 ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
71 #if PNG_LIBPNG_VER < 10600
72 /* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
73 * compiled against earlier versions.
75 # define png_const_structp png_structp
79 /* Copied from pngpriv.h */
80 #define PNG_32b(b,s) ((png_uint_32)(b) << (s))
81 #define PNG_CHUNK(b1,b2,b3,b4) \
82 (PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0))
84 #define png_IHDR PNG_CHUNK( 73, 72, 68, 82)
85 #define png_IDAT PNG_CHUNK( 73, 68, 65, 84)
86 #define png_IEND PNG_CHUNK( 73, 69, 78, 68)
87 #define png_PLTE PNG_CHUNK( 80, 76, 84, 69)
88 #define png_bKGD PNG_CHUNK( 98, 75, 71, 68)
89 #define png_cHRM PNG_CHUNK( 99, 72, 82, 77)
90 #define png_gAMA PNG_CHUNK(103, 65, 77, 65)
91 #define png_hIST PNG_CHUNK(104, 73, 83, 84)
92 #define png_iCCP PNG_CHUNK(105, 67, 67, 80)
93 #define png_iTXt PNG_CHUNK(105, 84, 88, 116)
94 #define png_oFFs PNG_CHUNK(111, 70, 70, 115)
95 #define png_pCAL PNG_CHUNK(112, 67, 65, 76)
96 #define png_sCAL PNG_CHUNK(115, 67, 65, 76)
97 #define png_pHYs PNG_CHUNK(112, 72, 89, 115)
98 #define png_sBIT PNG_CHUNK(115, 66, 73, 84)
99 #define png_sPLT PNG_CHUNK(115, 80, 76, 84)
100 #define png_sRGB PNG_CHUNK(115, 82, 71, 66)
101 #define png_sTER PNG_CHUNK(115, 84, 69, 82)
102 #define png_tEXt PNG_CHUNK(116, 69, 88, 116)
103 #define png_tIME PNG_CHUNK(116, 73, 77, 69)
104 #define png_tRNS PNG_CHUNK(116, 82, 78, 83)
105 #define png_zTXt PNG_CHUNK(122, 84, 88, 116)
106 #define png_vpAg PNG_CHUNK('v', 'p', 'A', 'g')
108 /* Test on flag values as defined in the spec (section 5.4): */
109 #define PNG_CHUNK_ANCILLARY(c ) (1 & ((c) >> 29))
110 #define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c))
111 #define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21))
112 #define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13))
113 #define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5))
115 /* Chunk information */
116 #define PNG_INFO_tEXt 0x10000000U
117 #define PNG_INFO_iTXt 0x20000000U
118 #define PNG_INFO_zTXt 0x40000000U
120 #define PNG_INFO_sTER 0x01000000U
121 #define PNG_INFO_vpAg 0x02000000U
132 int unknown
; /* Chunk not known to libpng */
133 int all
; /* Chunk set by the '-1' option */
134 int position
; /* position in pngtest.png */
135 int keep
; /* unknown handling setting */
137 /* Critical chunks */
138 { "IDAT", PNG_INFO_IDAT
, png_IDAT
, 0, 0, START
, 0 }, /* must be [0] */
139 { "PLTE", PNG_INFO_PLTE
, png_PLTE
, 0, 0, ABSENT
, 0 },
141 /* Non-critical chunks that libpng handles */
142 /* This is a mess but it seems to be the only way to do it - there is no way to
143 * check for definition outside a #if.
145 { "bKGD", PNG_INFO_bKGD
, png_bKGD
,
146 # ifdef PNG_READ_bKGD_SUPPORTED
152 { "cHRM", PNG_INFO_cHRM
, png_cHRM
,
153 # ifdef PNG_READ_cHRM_SUPPORTED
159 { "gAMA", PNG_INFO_gAMA
, png_gAMA
,
160 # ifdef PNG_READ_gAMA_SUPPORTED
166 { "hIST", PNG_INFO_hIST
, png_hIST
,
167 # ifdef PNG_READ_hIST_SUPPORTED
173 { "iCCP", PNG_INFO_iCCP
, png_iCCP
,
174 # ifdef PNG_READ_iCCP_SUPPORTED
180 { "iTXt", PNG_INFO_iTXt
, png_iTXt
,
181 # ifdef PNG_READ_iTXt_SUPPORTED
187 { "oFFs", PNG_INFO_oFFs
, png_oFFs
,
188 # ifdef PNG_READ_oFFs_SUPPORTED
194 { "pCAL", PNG_INFO_pCAL
, png_pCAL
,
195 # ifdef PNG_READ_pCAL_SUPPORTED
201 { "pHYs", PNG_INFO_pHYs
, png_pHYs
,
202 # ifdef PNG_READ_pHYs_SUPPORTED
208 { "sBIT", PNG_INFO_sBIT
, png_sBIT
,
209 # ifdef PNG_READ_sBIT_SUPPORTED
215 { "sCAL", PNG_INFO_sCAL
, png_sCAL
,
216 # ifdef PNG_READ_sCAL_SUPPORTED
222 { "sPLT", PNG_INFO_sPLT
, png_sPLT
,
223 # ifdef PNG_READ_sPLT_SUPPORTED
229 { "sRGB", PNG_INFO_sRGB
, png_sRGB
,
230 # ifdef PNG_READ_sRGB_SUPPORTED
236 { "tEXt", PNG_INFO_tEXt
, png_tEXt
,
237 # ifdef PNG_READ_tEXt_SUPPORTED
243 { "tIME", PNG_INFO_tIME
, png_tIME
,
244 # ifdef PNG_READ_tIME_SUPPORTED
250 { "tRNS", PNG_INFO_tRNS
, png_tRNS
,
251 # ifdef PNG_READ_tRNS_SUPPORTED
257 { "zTXt", PNG_INFO_zTXt
, png_zTXt
,
258 # ifdef PNG_READ_zTXt_SUPPORTED
265 /* No libpng handling */
266 { "sTER", PNG_INFO_sTER
, png_sTER
, 1, 1, START
, 0 },
267 { "vpAg", PNG_INFO_vpAg
, png_vpAg
, 1, 0, START
, 0 },
270 #define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
277 chunk_info
[i
].keep
= 0;
281 find(const char *name
)
286 if (memcmp(chunk_info
[i
].name
, name
, 4) == 0)
294 findb(const png_byte
*name
)
299 if (memcmp(chunk_info
[i
].name
, name
, 4) == 0)
307 find_by_flag(png_uint_32 flag
)
311 while (--i
>= 0) if (chunk_info
[i
].flag
== flag
) return i
;
313 fprintf(stderr
, "pngunknown: internal error\n");
318 ancillary(const char *name
)
320 return PNG_CHUNK_ANCILLARY(PNG_CHUNK(name
[0], name
[1], name
[2], name
[3]));
324 ancillaryb(const png_byte
*name
)
326 return PNG_CHUNK_ANCILLARY(PNG_CHUNK(name
[0], name
[1], name
[2], name
[3]));
329 /* Type of an error_ptr */
332 jmp_buf error_return
;
334 png_infop info_ptr
, end_ptr
;
342 static const char init
[] = "initialization";
343 static const char cmd
[] = "command line";
346 init_display(display
*d
, const char *program
)
348 memset(d
, 0, sizeof *d
);
350 d
->info_ptr
= d
->end_ptr
= NULL
;
351 d
->error_count
= d
->warning_count
= 0;
352 d
->program
= program
;
358 clean_display(display
*d
)
360 png_destroy_read_struct(&d
->png_ptr
, &d
->info_ptr
, &d
->end_ptr
);
362 /* This must not happen - it might cause an app crash */
363 if (d
->png_ptr
!= NULL
|| d
->info_ptr
!= NULL
|| d
->end_ptr
!= NULL
)
365 fprintf(stderr
, "%s(%s): png_destroy_read_struct error\n", d
->file
,
370 /* Invalidate the test */
374 PNG_FUNCTION(void, display_exit
, (display
*d
), static PNG_NORETURN
)
378 if (d
->png_ptr
!= NULL
)
381 /* During initialization and if this is a single command line argument set
382 * exit now - there is only one test, otherwise longjmp to do the next test.
384 if (d
->test
== init
|| d
->test
== cmd
)
387 longjmp(d
->error_return
, 1);
391 display_rc(const display
*d
, int strict
)
393 return d
->error_count
+ (strict
? d
->warning_count
: 0);
396 /* libpng error and warning callbacks */
397 PNG_FUNCTION(void, error
, (png_structp png_ptr
, const char *message
),
400 display
*d
= (display
*)png_get_error_ptr(png_ptr
);
402 fprintf(stderr
, "%s(%s): libpng error: %s\n", d
->file
, d
->test
, message
);
407 warning(png_structp png_ptr
, const char *message
)
409 display
*d
= (display
*)png_get_error_ptr(png_ptr
);
411 fprintf(stderr
, "%s(%s): libpng warning: %s\n", d
->file
, d
->test
, message
);
412 ++(d
->warning_count
);
416 get_valid(display
*d
, png_infop info_ptr
)
418 png_uint_32 flags
= png_get_valid(d
->png_ptr
, info_ptr
, (png_uint_32
)~0);
420 /* Map the text chunks back into the flags */
423 png_uint_32 ntext
= png_get_text(d
->png_ptr
, info_ptr
, &text
, NULL
);
425 while (ntext
-- > 0) switch (text
[ntext
].compression
)
428 flags
|= PNG_INFO_tEXt
;
431 flags
|= PNG_INFO_zTXt
;
435 flags
|= PNG_INFO_iTXt
;
438 fprintf(stderr
, "%s(%s): unknown text compression %d\n", d
->file
,
439 d
->test
, text
[ntext
].compression
);
448 get_unknown(display
*d
, int def
, png_infop info_ptr
)
450 /* Create corresponding 'unknown' flags */
451 png_uint_32 flags
= 0;
453 png_unknown_chunkp unknown
;
454 int num_unknown
= png_get_unknown_chunks(d
->png_ptr
, info_ptr
, &unknown
);
456 while (--num_unknown
>= 0)
458 int chunk
= findb(unknown
[num_unknown
].name
);
460 /* Chunks not known to pngunknown must be validated here; since they
461 * must also be unknown to libpng the 'def' behavior should have been
464 if (chunk
< 0) switch (def
)
466 default: /* impossible */
467 case PNG_HANDLE_CHUNK_AS_DEFAULT
:
468 case PNG_HANDLE_CHUNK_NEVER
:
469 fprintf(stderr
, "%s(%s): %s: %s: unknown chunk saved\n",
470 d
->file
, d
->test
, def
? "discard" : "default",
471 unknown
[num_unknown
].name
);
475 case PNG_HANDLE_CHUNK_IF_SAFE
:
476 if (!ancillaryb(unknown
[num_unknown
].name
))
479 "%s(%s): if-safe: %s: unknown critical chunk saved\n",
480 d
->file
, d
->test
, unknown
[num_unknown
].name
);
484 /* FALL THROUGH (safe) */
485 case PNG_HANDLE_CHUNK_ALWAYS
:
490 flags
|= chunk_info
[chunk
].flag
;
498 check(FILE *fp
, int argc
, const char **argv
, png_uint_32p flags
/*out*/,
501 int i
, def
= PNG_HANDLE_CHUNK_AS_DEFAULT
, npasses
, ipass
;
504 /* Some of these errors are permanently fatal and cause an exit here, others
505 * are per-test and cause an error return.
507 d
->png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, d
, error
,
509 if (d
->png_ptr
== NULL
)
511 fprintf(stderr
, "%s(%s): could not allocate png struct\n", d
->file
,
513 /* Terminate here, this error is not test specific. */
517 d
->info_ptr
= png_create_info_struct(d
->png_ptr
);
518 d
->end_ptr
= png_create_info_struct(d
->png_ptr
);
519 if (d
->info_ptr
== NULL
|| d
->end_ptr
== NULL
)
521 fprintf(stderr
, "%s(%s): could not allocate png info\n", d
->file
,
527 png_init_io(d
->png_ptr
, fp
);
529 /* Handle each argument in turn; multiple settings are possible for the same
530 * chunk and multiple calls will occur (the last one should override all
533 for (i
=0; i
<argc
; ++i
)
535 const char *equals
= strchr(argv
[i
], '=');
541 if (strcmp(equals
+1, "default") == 0)
542 option
= PNG_HANDLE_CHUNK_AS_DEFAULT
;
543 else if (strcmp(equals
+1, "discard") == 0)
544 option
= PNG_HANDLE_CHUNK_NEVER
;
545 else if (strcmp(equals
+1, "if-safe") == 0)
546 option
= PNG_HANDLE_CHUNK_IF_SAFE
;
547 else if (strcmp(equals
+1, "save") == 0)
548 option
= PNG_HANDLE_CHUNK_ALWAYS
;
551 fprintf(stderr
, "%s(%s): %s: unrecognized chunk option\n", d
->file
,
556 switch (equals
- argv
[i
])
558 case 4: /* chunk name */
559 chunk
= find(argv
[i
]);
563 /* These #if tests have the effect of skipping the arguments
564 * if SAVE support is unavailable - we can't do a useful test
565 * in this case, so we just check the arguments! This could
566 * be improved in the future by using the read callback.
568 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
571 memcpy(name
, chunk_info
[chunk
].name
, 5);
572 png_set_keep_unknown_chunks(d
->png_ptr
, option
, name
, 1);
573 chunk_info
[chunk
].keep
= option
;
580 case 7: /* default */
581 if (memcmp(argv
[i
], "default", 7) == 0)
583 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
584 png_set_keep_unknown_chunks(d
->png_ptr
, option
, NULL
, 0);
593 if (memcmp(argv
[i
], "all", 3) == 0)
595 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
596 png_set_keep_unknown_chunks(d
->png_ptr
, option
, NULL
, -1);
599 for (chunk
= 0; chunk
< NINFO
; ++chunk
)
600 if (chunk_info
[chunk
].all
)
601 chunk_info
[chunk
].keep
= option
;
608 default: /* some misplaced = */
614 fprintf(stderr
, "%s(%s): %s: unrecognized chunk argument\n", d
->file
,
619 png_read_info(d
->png_ptr
, d
->info_ptr
);
621 switch (png_get_interlace_type(d
->png_ptr
, d
->info_ptr
))
623 case PNG_INTERLACE_NONE
:
627 case PNG_INTERLACE_ADAM7
:
628 npasses
= PNG_INTERLACE_ADAM7_PASSES
;
632 /* Hard error because it is not test specific */
633 fprintf(stderr
, "%s(%s): invalid interlace type\n", d
->file
, d
->test
);
638 /* Skip the image data, if IDAT is not being handled then don't do this
639 * because it will cause a CRC error.
641 if (chunk_info
[0/*IDAT*/].keep
== PNG_HANDLE_CHUNK_AS_DEFAULT
)
643 png_start_read_image(d
->png_ptr
);
644 height
= png_get_image_height(d
->png_ptr
, d
->info_ptr
);
648 png_uint_32 width
= png_get_image_width(d
->png_ptr
, d
->info_ptr
);
650 for (ipass
=0; ipass
<npasses
; ++ipass
)
652 png_uint_32 wPass
= PNG_PASS_COLS(width
, ipass
);
658 for (y
=0; y
<height
; ++y
) if (PNG_ROW_IN_INTERLACE_PASS(y
, ipass
))
659 png_read_row(d
->png_ptr
, NULL
, NULL
);
664 else /* not interlaced */
668 for (y
=0; y
<height
; ++y
)
669 png_read_row(d
->png_ptr
, NULL
, NULL
);
673 png_read_end(d
->png_ptr
, d
->end_ptr
);
675 flags
[0] = get_valid(d
, d
->info_ptr
);
676 flags
[1] = get_unknown(d
, def
, d
->info_ptr
);
678 /* Only png_read_png sets PNG_INFO_IDAT! */
679 flags
[chunk_info
[0/*IDAT*/].keep
!= PNG_HANDLE_CHUNK_AS_DEFAULT
] |=
682 flags
[2] = get_valid(d
, d
->end_ptr
);
683 flags
[3] = get_unknown(d
, def
, d
->end_ptr
);
691 check_error(display
*d
, png_uint_32 flags
, const char *message
)
695 png_uint_32 flag
= flags
& -(png_int_32
)flags
;
696 int i
= find_by_flag(flag
);
698 fprintf(stderr
, "%s(%s): chunk %s: %s\n", d
->file
, d
->test
,
699 chunk_info
[i
].name
, message
);
707 check_handling(display
*d
, int def
, png_uint_32 chunks
, png_uint_32 known
,
708 png_uint_32 unknown
, const char *position
)
712 png_uint_32 flag
= chunks
& -(png_int_32
)chunks
;
713 int i
= find_by_flag(flag
);
714 int keep
= chunk_info
[i
].keep
;
716 const char *errorx
= NULL
;
718 if (chunk_info
[i
].unknown
)
720 if (keep
== PNG_HANDLE_CHUNK_AS_DEFAULT
)
722 type
= "UNKNOWN (default)";
727 type
= "UNKNOWN (specified)";
730 errorx
= "chunk processed";
734 case PNG_HANDLE_CHUNK_AS_DEFAULT
:
736 errorx
= "DEFAULT: unknown chunk saved";
739 case PNG_HANDLE_CHUNK_NEVER
:
741 errorx
= "DISCARD: unknown chunk saved";
744 case PNG_HANDLE_CHUNK_IF_SAFE
:
745 if (ancillary(chunk_info
[i
].name
))
747 if (!(flag
& unknown
))
748 errorx
= "IF-SAFE: unknown ancillary chunk lost";
751 else if (flag
& unknown
)
752 errorx
= "IF-SAFE: unknown critical chunk saved";
755 case PNG_HANDLE_CHUNK_ALWAYS
:
756 if (!(flag
& unknown
))
757 errorx
= "SAVE: unknown chunk lost";
761 errorx
= "internal error: bad keep";
764 } /* unknown chunk */
766 else /* known chunk */
772 /* chunk was processed, it won't have been saved because that is
773 * caught below when checking for inconsistent processing.
775 if (keep
!= PNG_HANDLE_CHUNK_AS_DEFAULT
)
776 errorx
= "!DEFAULT: known chunk processed";
779 else /* not processed */ switch (keep
)
781 case PNG_HANDLE_CHUNK_AS_DEFAULT
:
782 errorx
= "DEFAULT: known chunk not processed";
785 case PNG_HANDLE_CHUNK_NEVER
:
787 errorx
= "DISCARD: known chunk saved";
790 case PNG_HANDLE_CHUNK_IF_SAFE
:
791 if (ancillary(chunk_info
[i
].name
))
793 if (!(flag
& unknown
))
794 errorx
= "IF-SAFE: known ancillary chunk lost";
797 else if (flag
& unknown
)
798 errorx
= "IF-SAFE: known critical chunk saved";
801 case PNG_HANDLE_CHUNK_ALWAYS
:
802 if (!(flag
& unknown
))
803 errorx
= "SAVE: known chunk lost";
807 errorx
= "internal error: bad keep (2)";
815 fprintf(stderr
, "%s(%s): %s %s %s: %s\n",
816 d
->file
, d
->test
, type
, chunk_info
[i
].name
, position
, errorx
);
824 perform_one_test(FILE *fp
, int argc
, const char **argv
,
825 png_uint_32
*default_flags
, display
*d
)
828 png_uint_32 flags
[2][4];
832 memcpy(flags
[0], default_flags
, sizeof flags
[0]);
834 def
= check(fp
, argc
, argv
, flags
[1], d
);
836 /* Chunks should either be known or unknown, never both and this should apply
837 * whether the chunk is before or after the IDAT (actually, the app can
838 * probably change this by swapping the handling after the image, but this
839 * test does not do that.)
841 check_error(d
, (flags
[0][0]|flags
[0][2]) & (flags
[0][1]|flags
[0][3]),
842 "chunk handled inconsistently in count tests");
843 check_error(d
, (flags
[1][0]|flags
[1][2]) & (flags
[1][1]|flags
[1][3]),
844 "chunk handled inconsistently in option tests");
846 /* Now find out what happened to each chunk before and after the IDAT and
847 * determine if the behavior was correct. First some basic sanity checks,
848 * any known chunk should be known in the original count, any unknown chunk
849 * should be either known or unknown in the original.
854 test
= flags
[1][0] & ~flags
[0][0];
855 check_error(d
, test
, "new known chunk before IDAT");
856 test
= flags
[1][1] & ~(flags
[0][0] | flags
[0][1]);
857 check_error(d
, test
, "new unknown chunk before IDAT");
858 test
= flags
[1][2] & ~flags
[0][2];
859 check_error(d
, test
, "new known chunk after IDAT");
860 test
= flags
[1][3] & ~(flags
[0][2] | flags
[0][3]);
861 check_error(d
, test
, "new unknown chunk after IDAT");
864 /* Now each chunk in the original list should have been handled according to
865 * the options set for that chunk, regardless of whether libpng knows about
868 check_handling(d
, def
, flags
[0][0] | flags
[0][1], flags
[1][0], flags
[1][1],
870 check_handling(d
, def
, flags
[0][2] | flags
[0][3], flags
[1][2], flags
[1][3],
875 perform_one_test_safe(FILE *fp
, int argc
, const char **argv
,
876 png_uint_32
*default_flags
, display
*d
, const char *test
)
878 if (setjmp(d
->error_return
) == 0)
880 d
->test
= test
; /* allow use of d->error_return */
881 perform_one_test(fp
, argc
, argv
, default_flags
, d
);
882 d
->test
= init
; /* prevent use of d->error_return */
886 static const char *standard_tests
[] =
888 "discard", "default=discard", 0,
889 "save", "default=save", 0,
890 "if-safe", "default=if-safe", 0,
891 "vpAg", "vpAg=if-safe", 0,
892 "sTER", "sTER=if-safe", 0,
893 "IDAT", "default=discard", "IDAT=save", 0,
894 "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
895 "sBIT=save", "sRGB=save", 0,
899 static PNG_NORETURN
void
900 usage(const char *program
, const char *reason
)
902 fprintf(stderr
, "pngunknown: %s: usage:\n %s [--strict] "
903 "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
904 "testfile.png\n", reason
, program
);
909 main(int argc
, const char **argv
)
912 png_uint_32 default_flags
[4/*valid,unknown{before,after}*/];
913 int strict
= 0, default_tests
= 0;
914 const char *count_argv
= "default=save";
915 const char *touch_file
= NULL
;
918 init_display(&d
, argv
[0]);
920 while (++argv
, --argc
> 0)
922 if (strcmp(*argv
, "--strict") == 0)
925 else if (strcmp(*argv
, "--default") == 0)
928 else if (strcmp(*argv
, "--touch") == 0)
931 touch_file
= *++argv
, --argc
;
934 usage(d
.program
, "--touch: missing file name");
941 /* A file name is required, but there should be no other arguments if
942 * --default was specified.
945 usage(d
.program
, "missing test file");
947 /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
948 * optimization which causes warnings with -Wstrict-overflow!
950 else if (default_tests
) if (argc
!= 1)
951 usage(d
.program
, "extra arguments");
953 # ifndef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
954 fprintf(stderr
, "%s: warning: no 'save' support so arguments ignored\n",
958 /* The name of the test file is the last argument; remove it. */
959 d
.file
= argv
[--argc
];
961 fp
= fopen(d
.file
, "rb");
968 /* First find all the chunks, known and unknown, in the test file, a failure
969 * here aborts the whole test.
971 if (check(fp
, 1, &count_argv
, default_flags
, &d
) !=
972 PNG_HANDLE_CHUNK_ALWAYS
)
974 fprintf(stderr
, "%s: %s: internal error\n", d
.program
, d
.file
);
978 /* Now find what the various supplied options cause to change: */
981 d
.test
= cmd
; /* acts as a flag to say exit, do not longjmp */
982 perform_one_test(fp
, argc
, argv
, default_flags
, &d
);
988 const char **test
= standard_tests
;
990 /* Set the exit_test pointer here so we can continue after a libpng error.
991 * NOTE: this leaks memory because the png_struct data from the failing
992 * test is never freed.
996 const char *this_test
= *test
++;
997 const char **next
= test
;
998 int count
= display_rc(&d
, strict
), new_count
;
1002 while (*next
) ++next
, ++arg_count
;
1004 perform_one_test_safe(fp
, arg_count
, test
, default_flags
, &d
,
1007 new_count
= display_rc(&d
, strict
);
1009 if (new_count
== count
)
1015 printf("%s: %s %s\n", result
, d
.program
, this_test
);
1023 if (display_rc(&d
, strict
) == 0)
1025 /* Success, touch the success file if appropriate */
1026 if (touch_file
!= NULL
)
1028 FILE *fsuccess
= fopen(touch_file
, "wt");
1030 if (fsuccess
!= NULL
)
1033 fprintf(fsuccess
, "PNG unknown tests succeeded\n");
1035 err
= ferror(fsuccess
);
1037 if (fclose(fsuccess
) || err
)
1039 fprintf(stderr
, "%s: write failed\n", touch_file
);
1046 fprintf(stderr
, "%s: open failed\n", touch_file
);
1062 " test ignored because libpng was not built with unknown chunk support\n");
1063 /* So the test is skipped: */