1 | /* pngunknown.c - test the read side unknown chunk handling
|
---|
2 | *
|
---|
3 | * Copyright (c) 2021 Cosmin Truta
|
---|
4 | * Copyright (c) 2015,2017 Glenn Randers-Pehrson
|
---|
5 | * Written by John Cunningham Bowler
|
---|
6 | *
|
---|
7 | * This code is released under the libpng license.
|
---|
8 | * For conditions of distribution and use, see the disclaimer
|
---|
9 | * and license in png.h
|
---|
10 | *
|
---|
11 | * NOTES:
|
---|
12 | * This is a C program that is intended to be linked against libpng. It
|
---|
13 | * allows the libpng unknown handling code to be tested by interpreting
|
---|
14 | * arguments to save or discard combinations of chunks. The program is
|
---|
15 | * currently just a minimal validation for the built-in libpng facilities.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <stdlib.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <setjmp.h>
|
---|
22 |
|
---|
23 | /* Define the following to use this test against your installed libpng, rather
|
---|
24 | * than the one being built here:
|
---|
25 | */
|
---|
26 | #ifdef PNG_FREESTANDING_TESTS
|
---|
27 | # include <png.h>
|
---|
28 | #else
|
---|
29 | # include "../../png.h"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /* 1.6.1 added support for the configure test harness, which uses 77 to indicate
|
---|
33 | * a skipped test, in earlier versions we need to succeed on a skipped test, so:
|
---|
34 | */
|
---|
35 | #if PNG_LIBPNG_VER >= 10601 && defined(HAVE_CONFIG_H)
|
---|
36 | # define SKIP 77
|
---|
37 | #else
|
---|
38 | # define SKIP 0
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | /* Since this program tests the ability to change the unknown chunk handling
|
---|
43 | * these must be defined:
|
---|
44 | */
|
---|
45 | #if defined(PNG_SET_UNKNOWN_CHUNKS_SUPPORTED) &&\
|
---|
46 | defined(PNG_STDIO_SUPPORTED) &&\
|
---|
47 | defined(PNG_READ_SUPPORTED)
|
---|
48 |
|
---|
49 | /* One of these must be defined to allow us to find out what happened. It is
|
---|
50 | * still useful to set unknown chunk handling without either of these in order
|
---|
51 | * to cause *known* chunks to be discarded. This can be a significant
|
---|
52 | * efficiency gain, but it can't really be tested here.
|
---|
53 | */
|
---|
54 | #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) ||\
|
---|
55 | defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
56 |
|
---|
57 | #if PNG_LIBPNG_VER < 10500
|
---|
58 | /* This deliberately lacks the const. */
|
---|
59 | typedef png_byte *png_const_bytep;
|
---|
60 |
|
---|
61 | /* This is copied from 1.5.1 png.h: */
|
---|
62 | #define PNG_INTERLACE_ADAM7_PASSES 7
|
---|
63 | #define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
|
---|
64 | #define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
|
---|
65 | #define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
|
---|
66 | #define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
|
---|
67 | #define PNG_PASS_ROWS(height, pass) (((height)+(((1<<PNG_PASS_ROW_SHIFT(pass))\
|
---|
68 | -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
|
---|
69 | #define PNG_PASS_COLS(width, pass) (((width)+(((1<<PNG_PASS_COL_SHIFT(pass))\
|
---|
70 | -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
|
---|
71 | #define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
|
---|
72 | (((yIn)<<PNG_PASS_ROW_SHIFT(pass))+PNG_PASS_START_ROW(pass))
|
---|
73 | #define PNG_COL_FROM_PASS_COL(xIn, pass) \
|
---|
74 | (((xIn)<<PNG_PASS_COL_SHIFT(pass))+PNG_PASS_START_COL(pass))
|
---|
75 | #define PNG_PASS_MASK(pass,off) ( \
|
---|
76 | ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
|
---|
77 | ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
|
---|
78 | #define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
|
---|
79 | ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
|
---|
80 | #define PNG_COL_IN_INTERLACE_PASS(x, pass) \
|
---|
81 | ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
|
---|
82 |
|
---|
83 | /* These are needed too for the default build: */
|
---|
84 | #define PNG_WRITE_16BIT_SUPPORTED
|
---|
85 | #define PNG_READ_16BIT_SUPPORTED
|
---|
86 |
|
---|
87 | /* This comes from pnglibconf.h after 1.5: */
|
---|
88 | #define PNG_FP_1 100000
|
---|
89 | #define PNG_GAMMA_THRESHOLD_FIXED\
|
---|
90 | ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #if PNG_LIBPNG_VER < 10600
|
---|
94 | /* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
|
---|
95 | * compiled against earlier versions.
|
---|
96 | */
|
---|
97 | # define png_const_structp png_structp
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | #if PNG_LIBPNG_VER < 10700
|
---|
101 | /* Copied from libpng 1.7.0 png.h */
|
---|
102 | #define PNG_u2(b1, b2) (((unsigned int)(b1) << 8) + (b2))
|
---|
103 |
|
---|
104 | #define PNG_U16(b1, b2) ((png_uint_16)PNG_u2(b1, b2))
|
---|
105 | #define PNG_U32(b1, b2, b3, b4)\
|
---|
106 | (((png_uint_32)PNG_u2(b1, b2) << 16) + PNG_u2(b3, b4))
|
---|
107 |
|
---|
108 | /* Constants for known chunk types.
|
---|
109 | */
|
---|
110 | #define png_IDAT PNG_U32( 73, 68, 65, 84)
|
---|
111 | #define png_IEND PNG_U32( 73, 69, 78, 68)
|
---|
112 | #define png_IHDR PNG_U32( 73, 72, 68, 82)
|
---|
113 | #define png_PLTE PNG_U32( 80, 76, 84, 69)
|
---|
114 | #define png_bKGD PNG_U32( 98, 75, 71, 68)
|
---|
115 | #define png_cHRM PNG_U32( 99, 72, 82, 77)
|
---|
116 | #define png_eXIf PNG_U32(101, 88, 73, 102) /* registered July 2017 */
|
---|
117 | #define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */
|
---|
118 | #define png_gAMA PNG_U32(103, 65, 77, 65)
|
---|
119 | #define png_gIFg PNG_U32(103, 73, 70, 103)
|
---|
120 | #define png_gIFt PNG_U32(103, 73, 70, 116) /* deprecated */
|
---|
121 | #define png_gIFx PNG_U32(103, 73, 70, 120)
|
---|
122 | #define png_hIST PNG_U32(104, 73, 83, 84)
|
---|
123 | #define png_iCCP PNG_U32(105, 67, 67, 80)
|
---|
124 | #define png_iTXt PNG_U32(105, 84, 88, 116)
|
---|
125 | #define png_oFFs PNG_U32(111, 70, 70, 115)
|
---|
126 | #define png_pCAL PNG_U32(112, 67, 65, 76)
|
---|
127 | #define png_pHYs PNG_U32(112, 72, 89, 115)
|
---|
128 | #define png_sBIT PNG_U32(115, 66, 73, 84)
|
---|
129 | #define png_sCAL PNG_U32(115, 67, 65, 76)
|
---|
130 | #define png_sPLT PNG_U32(115, 80, 76, 84)
|
---|
131 | #define png_sRGB PNG_U32(115, 82, 71, 66)
|
---|
132 | #define png_sTER PNG_U32(115, 84, 69, 82)
|
---|
133 | #define png_tEXt PNG_U32(116, 69, 88, 116)
|
---|
134 | #define png_tIME PNG_U32(116, 73, 77, 69)
|
---|
135 | #define png_tRNS PNG_U32(116, 82, 78, 83)
|
---|
136 | #define png_zTXt PNG_U32(122, 84, 88, 116)
|
---|
137 |
|
---|
138 | /* Test on flag values as defined in the spec (section 5.4): */
|
---|
139 | #define PNG_CHUNK_ANCILLARY(c) (1 & ((c) >> 29))
|
---|
140 | #define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c))
|
---|
141 | #define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21))
|
---|
142 | #define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13))
|
---|
143 | #define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5))
|
---|
144 |
|
---|
145 | #endif /* PNG_LIBPNG_VER < 10700 */
|
---|
146 |
|
---|
147 | #ifdef __cplusplus
|
---|
148 | # define this not_the_cpp_this
|
---|
149 | # define new not_the_cpp_new
|
---|
150 | # define voidcast(type, value) static_cast<type>(value)
|
---|
151 | #else
|
---|
152 | # define voidcast(type, value) (value)
|
---|
153 | #endif /* __cplusplus */
|
---|
154 |
|
---|
155 | /* Unused formal parameter errors are removed using the following macro which is
|
---|
156 | * expected to have no bad effects on performance.
|
---|
157 | */
|
---|
158 | #ifndef UNUSED
|
---|
159 | # if defined(__GNUC__) || defined(_MSC_VER)
|
---|
160 | # define UNUSED(param) (void)param;
|
---|
161 | # else
|
---|
162 | # define UNUSED(param)
|
---|
163 | # endif
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | /* Types of chunks not known to libpng */
|
---|
167 | #define png_vpAg PNG_U32(118, 112, 65, 103)
|
---|
168 |
|
---|
169 | /* Chunk information */
|
---|
170 | #define PNG_INFO_tEXt 0x10000000U
|
---|
171 | #define PNG_INFO_iTXt 0x20000000U
|
---|
172 | #define PNG_INFO_zTXt 0x40000000U
|
---|
173 |
|
---|
174 | #define PNG_INFO_sTER 0x01000000U
|
---|
175 | #define PNG_INFO_vpAg 0x02000000U
|
---|
176 |
|
---|
177 | #define ABSENT 0
|
---|
178 | #define START 1
|
---|
179 | #define END 2
|
---|
180 |
|
---|
181 | static struct
|
---|
182 | {
|
---|
183 | char name[5];
|
---|
184 | png_uint_32 flag;
|
---|
185 | png_uint_32 tag;
|
---|
186 | int unknown; /* Chunk not known to libpng */
|
---|
187 | int all; /* Chunk set by the '-1' option */
|
---|
188 | int position; /* position in pngtest.png */
|
---|
189 | int keep; /* unknown handling setting */
|
---|
190 | } chunk_info[] = {
|
---|
191 | /* Critical chunks */
|
---|
192 | { "IDAT", PNG_INFO_IDAT, png_IDAT, 0, 0, START, 0 }, /* must be [0] */
|
---|
193 | { "PLTE", PNG_INFO_PLTE, png_PLTE, 0, 0, ABSENT, 0 },
|
---|
194 |
|
---|
195 | /* Non-critical chunks that libpng handles */
|
---|
196 | /* This is a mess but it seems to be the only way to do it - there is no way
|
---|
197 | * to check for a definition outside a #if.
|
---|
198 | */
|
---|
199 | { "bKGD", PNG_INFO_bKGD, png_bKGD,
|
---|
200 | # ifdef PNG_READ_bKGD_SUPPORTED
|
---|
201 | 0,
|
---|
202 | # else
|
---|
203 | 1,
|
---|
204 | # endif
|
---|
205 | 1, START, 0 },
|
---|
206 | { "cHRM", PNG_INFO_cHRM, png_cHRM,
|
---|
207 | # ifdef PNG_READ_cHRM_SUPPORTED
|
---|
208 | 0,
|
---|
209 | # else
|
---|
210 | 1,
|
---|
211 | # endif
|
---|
212 | 1, START, 0 },
|
---|
213 | { "eXIf", PNG_INFO_eXIf, png_eXIf,
|
---|
214 | # ifdef PNG_READ_eXIf_SUPPORTED
|
---|
215 | 0,
|
---|
216 | # else
|
---|
217 | 1,
|
---|
218 | # endif
|
---|
219 | 1, END, 0 },
|
---|
220 | { "gAMA", PNG_INFO_gAMA, png_gAMA,
|
---|
221 | # ifdef PNG_READ_gAMA_SUPPORTED
|
---|
222 | 0,
|
---|
223 | # else
|
---|
224 | 1,
|
---|
225 | # endif
|
---|
226 | 1, START, 0 },
|
---|
227 | { "hIST", PNG_INFO_hIST, png_hIST,
|
---|
228 | # ifdef PNG_READ_hIST_SUPPORTED
|
---|
229 | 0,
|
---|
230 | # else
|
---|
231 | 1,
|
---|
232 | # endif
|
---|
233 | 1, ABSENT, 0 },
|
---|
234 | { "iCCP", PNG_INFO_iCCP, png_iCCP,
|
---|
235 | # ifdef PNG_READ_iCCP_SUPPORTED
|
---|
236 | 0,
|
---|
237 | # else
|
---|
238 | 1,
|
---|
239 | # endif
|
---|
240 | 1, ABSENT, 0 },
|
---|
241 | { "iTXt", PNG_INFO_iTXt, png_iTXt,
|
---|
242 | # ifdef PNG_READ_iTXt_SUPPORTED
|
---|
243 | 0,
|
---|
244 | # else
|
---|
245 | 1,
|
---|
246 | # endif
|
---|
247 | 1, ABSENT, 0 },
|
---|
248 | { "oFFs", PNG_INFO_oFFs, png_oFFs,
|
---|
249 | # ifdef PNG_READ_oFFs_SUPPORTED
|
---|
250 | 0,
|
---|
251 | # else
|
---|
252 | 1,
|
---|
253 | # endif
|
---|
254 | 1, START, 0 },
|
---|
255 | { "pCAL", PNG_INFO_pCAL, png_pCAL,
|
---|
256 | # ifdef PNG_READ_pCAL_SUPPORTED
|
---|
257 | 0,
|
---|
258 | # else
|
---|
259 | 1,
|
---|
260 | # endif
|
---|
261 | 1, START, 0 },
|
---|
262 | { "pHYs", PNG_INFO_pHYs, png_pHYs,
|
---|
263 | # ifdef PNG_READ_pHYs_SUPPORTED
|
---|
264 | 0,
|
---|
265 | # else
|
---|
266 | 1,
|
---|
267 | # endif
|
---|
268 | 1, START, 0 },
|
---|
269 | { "sBIT", PNG_INFO_sBIT, png_sBIT,
|
---|
270 | # ifdef PNG_READ_sBIT_SUPPORTED
|
---|
271 | 0,
|
---|
272 | # else
|
---|
273 | 1,
|
---|
274 | # endif
|
---|
275 | 1, START, 0 },
|
---|
276 | { "sCAL", PNG_INFO_sCAL, png_sCAL,
|
---|
277 | # ifdef PNG_READ_sCAL_SUPPORTED
|
---|
278 | 0,
|
---|
279 | # else
|
---|
280 | 1,
|
---|
281 | # endif
|
---|
282 | 1, START, 0 },
|
---|
283 | { "sPLT", PNG_INFO_sPLT, png_sPLT,
|
---|
284 | # ifdef PNG_READ_sPLT_SUPPORTED
|
---|
285 | 0,
|
---|
286 | # else
|
---|
287 | 1,
|
---|
288 | # endif
|
---|
289 | 1, ABSENT, 0 },
|
---|
290 | { "sRGB", PNG_INFO_sRGB, png_sRGB,
|
---|
291 | # ifdef PNG_READ_sRGB_SUPPORTED
|
---|
292 | 0,
|
---|
293 | # else
|
---|
294 | 1,
|
---|
295 | # endif
|
---|
296 | 1, START, 0 },
|
---|
297 | { "tEXt", PNG_INFO_tEXt, png_tEXt,
|
---|
298 | # ifdef PNG_READ_tEXt_SUPPORTED
|
---|
299 | 0,
|
---|
300 | # else
|
---|
301 | 1,
|
---|
302 | # endif
|
---|
303 | 1, START, 0 },
|
---|
304 | { "tIME", PNG_INFO_tIME, png_tIME,
|
---|
305 | # ifdef PNG_READ_tIME_SUPPORTED
|
---|
306 | 0,
|
---|
307 | # else
|
---|
308 | 1,
|
---|
309 | # endif
|
---|
310 | 1, START, 0 },
|
---|
311 | { "tRNS", PNG_INFO_tRNS, png_tRNS,
|
---|
312 | # ifdef PNG_READ_tRNS_SUPPORTED
|
---|
313 | 0,
|
---|
314 | # else
|
---|
315 | 1,
|
---|
316 | # endif
|
---|
317 | 0, ABSENT, 0 },
|
---|
318 | { "zTXt", PNG_INFO_zTXt, png_zTXt,
|
---|
319 | # ifdef PNG_READ_zTXt_SUPPORTED
|
---|
320 | 0,
|
---|
321 | # else
|
---|
322 | 1,
|
---|
323 | # endif
|
---|
324 | 1, END, 0 },
|
---|
325 |
|
---|
326 | /* No libpng handling */
|
---|
327 | { "sTER", PNG_INFO_sTER, png_sTER, 1, 1, START, 0 },
|
---|
328 | { "vpAg", PNG_INFO_vpAg, png_vpAg, 1, 0, START, 0 },
|
---|
329 | };
|
---|
330 |
|
---|
331 | #define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
|
---|
332 |
|
---|
333 | static void
|
---|
334 | clear_keep(void)
|
---|
335 | {
|
---|
336 | int i = NINFO;
|
---|
337 | while (--i >= 0)
|
---|
338 | chunk_info[i].keep = 0;
|
---|
339 | }
|
---|
340 |
|
---|
341 | static int
|
---|
342 | find(const char *name)
|
---|
343 | {
|
---|
344 | int i = NINFO;
|
---|
345 | while (--i >= 0)
|
---|
346 | {
|
---|
347 | if (memcmp(chunk_info[i].name, name, 4) == 0)
|
---|
348 | break;
|
---|
349 | }
|
---|
350 |
|
---|
351 | return i;
|
---|
352 | }
|
---|
353 |
|
---|
354 | static int
|
---|
355 | findb(const png_byte *name)
|
---|
356 | {
|
---|
357 | int i = NINFO;
|
---|
358 | while (--i >= 0)
|
---|
359 | {
|
---|
360 | if (memcmp(chunk_info[i].name, name, 4) == 0)
|
---|
361 | break;
|
---|
362 | }
|
---|
363 |
|
---|
364 | return i;
|
---|
365 | }
|
---|
366 |
|
---|
367 | static int
|
---|
368 | find_by_flag(png_uint_32 flag)
|
---|
369 | {
|
---|
370 | int i = NINFO;
|
---|
371 |
|
---|
372 | while (--i >= 0)
|
---|
373 | if (chunk_info[i].flag == flag)
|
---|
374 | return i;
|
---|
375 |
|
---|
376 | fprintf(stderr, "pngunknown: internal error\n");
|
---|
377 | exit(4);
|
---|
378 | }
|
---|
379 |
|
---|
380 | static int
|
---|
381 | ancillary(const char *name)
|
---|
382 | {
|
---|
383 | return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
|
---|
384 | }
|
---|
385 |
|
---|
386 | #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
387 | static int
|
---|
388 | ancillaryb(const png_byte *name)
|
---|
389 | {
|
---|
390 | return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
|
---|
391 | }
|
---|
392 | #endif
|
---|
393 |
|
---|
394 | /* Type of an error_ptr */
|
---|
395 | typedef struct
|
---|
396 | {
|
---|
397 | jmp_buf error_return;
|
---|
398 | png_structp png_ptr;
|
---|
399 | png_infop info_ptr, end_ptr;
|
---|
400 | png_uint_32 before_IDAT;
|
---|
401 | png_uint_32 after_IDAT;
|
---|
402 | int error_count;
|
---|
403 | int warning_count;
|
---|
404 | int keep; /* the default value */
|
---|
405 | const char *program;
|
---|
406 | const char *file;
|
---|
407 | const char *test;
|
---|
408 | } display;
|
---|
409 |
|
---|
410 | static const char init[] = "initialization";
|
---|
411 | static const char cmd[] = "command line";
|
---|
412 |
|
---|
413 | static void
|
---|
414 | init_display(display *d, const char *program)
|
---|
415 | {
|
---|
416 | memset(d, 0, sizeof *d);
|
---|
417 | d->png_ptr = NULL;
|
---|
418 | d->info_ptr = d->end_ptr = NULL;
|
---|
419 | d->error_count = d->warning_count = 0;
|
---|
420 | d->program = program;
|
---|
421 | d->file = program;
|
---|
422 | d->test = init;
|
---|
423 | }
|
---|
424 |
|
---|
425 | static void
|
---|
426 | clean_display(display *d)
|
---|
427 | {
|
---|
428 | png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_ptr);
|
---|
429 |
|
---|
430 | /* This must not happen - it might cause an app crash */
|
---|
431 | if (d->png_ptr != NULL || d->info_ptr != NULL || d->end_ptr != NULL)
|
---|
432 | {
|
---|
433 | fprintf(stderr, "%s(%s): png_destroy_read_struct error\n", d->file,
|
---|
434 | d->test);
|
---|
435 | exit(1);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
|
---|
440 | {
|
---|
441 | ++(d->error_count);
|
---|
442 |
|
---|
443 | if (d->png_ptr != NULL)
|
---|
444 | clean_display(d);
|
---|
445 |
|
---|
446 | /* During initialization and if this is a single command line argument set
|
---|
447 | * exit now - there is only one test, otherwise longjmp to do the next test.
|
---|
448 | */
|
---|
449 | if (d->test == init || d->test == cmd)
|
---|
450 | exit(1);
|
---|
451 |
|
---|
452 | longjmp(d->error_return, 1);
|
---|
453 | }
|
---|
454 |
|
---|
455 | static int
|
---|
456 | display_rc(const display *d, int strict)
|
---|
457 | {
|
---|
458 | return d->error_count + (strict ? d->warning_count : 0);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /* libpng error and warning callbacks */
|
---|
462 | PNG_FUNCTION(void, (PNGCBAPI error), (png_structp png_ptr, const char *message),
|
---|
463 | static PNG_NORETURN)
|
---|
464 | {
|
---|
465 | display *d = (display*)png_get_error_ptr(png_ptr);
|
---|
466 |
|
---|
467 | fprintf(stderr, "%s(%s): libpng error: %s\n", d->file, d->test, message);
|
---|
468 | display_exit(d);
|
---|
469 | }
|
---|
470 |
|
---|
471 | static void PNGCBAPI
|
---|
472 | warning(png_structp png_ptr, const char *message)
|
---|
473 | {
|
---|
474 | display *d = (display*)png_get_error_ptr(png_ptr);
|
---|
475 |
|
---|
476 | fprintf(stderr, "%s(%s): libpng warning: %s\n", d->file, d->test, message);
|
---|
477 | ++(d->warning_count);
|
---|
478 | }
|
---|
479 |
|
---|
480 | static png_uint_32
|
---|
481 | get_valid(display *d, png_infop info_ptr)
|
---|
482 | {
|
---|
483 | png_uint_32 flags = png_get_valid(d->png_ptr, info_ptr, (png_uint_32)~0);
|
---|
484 |
|
---|
485 | /* Map the text chunks back into the flags */
|
---|
486 | {
|
---|
487 | png_textp text;
|
---|
488 | png_uint_32 ntext = png_get_text(d->png_ptr, info_ptr, &text, NULL);
|
---|
489 |
|
---|
490 | while (ntext > 0) switch (text[--ntext].compression)
|
---|
491 | {
|
---|
492 | case -1:
|
---|
493 | flags |= PNG_INFO_tEXt;
|
---|
494 | break;
|
---|
495 | case 0:
|
---|
496 | flags |= PNG_INFO_zTXt;
|
---|
497 | break;
|
---|
498 | case 1:
|
---|
499 | case 2:
|
---|
500 | flags |= PNG_INFO_iTXt;
|
---|
501 | break;
|
---|
502 | default:
|
---|
503 | fprintf(stderr, "%s(%s): unknown text compression %d\n", d->file,
|
---|
504 | d->test, text[ntext].compression);
|
---|
505 | display_exit(d);
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | return flags;
|
---|
510 | }
|
---|
511 |
|
---|
512 | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
513 | static int PNGCBAPI
|
---|
514 | read_callback(png_structp pp, png_unknown_chunkp pc)
|
---|
515 | {
|
---|
516 | /* This function mimics the behavior of png_set_keep_unknown_chunks by
|
---|
517 | * returning '0' to keep the chunk and '1' to discard it.
|
---|
518 | */
|
---|
519 | display *d = voidcast(display*, png_get_user_chunk_ptr(pp));
|
---|
520 | int chunk = findb(pc->name);
|
---|
521 | int keep, discard;
|
---|
522 |
|
---|
523 | if (chunk < 0) /* not one in our list, so not a known chunk */
|
---|
524 | keep = d->keep;
|
---|
525 |
|
---|
526 | else
|
---|
527 | {
|
---|
528 | keep = chunk_info[chunk].keep;
|
---|
529 | if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
|
---|
530 | {
|
---|
531 | /* See the comments in png.h - use the default for unknown chunks,
|
---|
532 | * do not keep known chunks.
|
---|
533 | */
|
---|
534 | if (chunk_info[chunk].unknown)
|
---|
535 | keep = d->keep;
|
---|
536 |
|
---|
537 | else
|
---|
538 | keep = PNG_HANDLE_CHUNK_NEVER;
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | switch (keep)
|
---|
543 | {
|
---|
544 | default:
|
---|
545 | fprintf(stderr, "%s(%s): %d: unrecognized chunk option\n", d->file,
|
---|
546 | d->test, chunk_info[chunk].keep);
|
---|
547 | display_exit(d);
|
---|
548 |
|
---|
549 | case PNG_HANDLE_CHUNK_AS_DEFAULT:
|
---|
550 | case PNG_HANDLE_CHUNK_NEVER:
|
---|
551 | discard = 1; /*handled; discard*/
|
---|
552 | break;
|
---|
553 |
|
---|
554 | case PNG_HANDLE_CHUNK_IF_SAFE:
|
---|
555 | case PNG_HANDLE_CHUNK_ALWAYS:
|
---|
556 | discard = 0; /*not handled; keep*/
|
---|
557 | break;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /* Also store information about this chunk in the display, the relevant flag
|
---|
561 | * is set if the chunk is to be kept ('not handled'.)
|
---|
562 | */
|
---|
563 | if (chunk >= 0)
|
---|
564 | {
|
---|
565 | if (!discard) /* stupidity to stop a GCC warning */
|
---|
566 | {
|
---|
567 | png_uint_32 flag = chunk_info[chunk].flag;
|
---|
568 |
|
---|
569 | if (pc->location & PNG_AFTER_IDAT)
|
---|
570 | d->after_IDAT |= flag;
|
---|
571 |
|
---|
572 | else
|
---|
573 | d->before_IDAT |= flag;
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | /* However if there is no support to store unknown chunks don't ask libpng to
|
---|
578 | * do it; there will be an png_error.
|
---|
579 | */
|
---|
580 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
581 | return discard;
|
---|
582 | # else
|
---|
583 | return 1; /*handled; discard*/
|
---|
584 | # endif
|
---|
585 | }
|
---|
586 | #endif /* READ_USER_CHUNKS_SUPPORTED */
|
---|
587 |
|
---|
588 | #ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
589 | static png_uint_32
|
---|
590 | get_unknown(display *d, png_infop info_ptr, int after_IDAT)
|
---|
591 | {
|
---|
592 | /* Create corresponding 'unknown' flags */
|
---|
593 | png_uint_32 flags = 0;
|
---|
594 |
|
---|
595 | UNUSED(after_IDAT)
|
---|
596 |
|
---|
597 | {
|
---|
598 | png_unknown_chunkp unknown;
|
---|
599 | int num_unknown = png_get_unknown_chunks(d->png_ptr, info_ptr, &unknown);
|
---|
600 |
|
---|
601 | while (--num_unknown >= 0)
|
---|
602 | {
|
---|
603 | int chunk = findb(unknown[num_unknown].name);
|
---|
604 |
|
---|
605 | /* Chunks not known to pngunknown must be validated here; since they
|
---|
606 | * must also be unknown to libpng the 'display->keep' behavior should
|
---|
607 | * have been used.
|
---|
608 | */
|
---|
609 | if (chunk < 0) switch (d->keep)
|
---|
610 | {
|
---|
611 | default: /* impossible */
|
---|
612 | case PNG_HANDLE_CHUNK_AS_DEFAULT:
|
---|
613 | case PNG_HANDLE_CHUNK_NEVER:
|
---|
614 | fprintf(stderr, "%s(%s): %s: %s: unknown chunk saved\n",
|
---|
615 | d->file, d->test, d->keep ? "discard" : "default",
|
---|
616 | unknown[num_unknown].name);
|
---|
617 | ++(d->error_count);
|
---|
618 | break;
|
---|
619 |
|
---|
620 | case PNG_HANDLE_CHUNK_IF_SAFE:
|
---|
621 | if (!ancillaryb(unknown[num_unknown].name))
|
---|
622 | {
|
---|
623 | fprintf(stderr,
|
---|
624 | "%s(%s): if-safe: %s: unknown critical chunk saved\n",
|
---|
625 | d->file, d->test, unknown[num_unknown].name);
|
---|
626 | ++(d->error_count);
|
---|
627 | break;
|
---|
628 | }
|
---|
629 | /* FALLTHROUGH */ /* (safe) */
|
---|
630 | case PNG_HANDLE_CHUNK_ALWAYS:
|
---|
631 | break;
|
---|
632 | }
|
---|
633 |
|
---|
634 | else
|
---|
635 | flags |= chunk_info[chunk].flag;
|
---|
636 | }
|
---|
637 | }
|
---|
638 |
|
---|
639 | return flags;
|
---|
640 | }
|
---|
641 | #else /* SAVE_UNKNOWN_CHUNKS */
|
---|
642 | static png_uint_32
|
---|
643 | get_unknown(display *d, png_infop info_ptr, int after_IDAT)
|
---|
644 | /* Otherwise this will return the cached values set by any user callback */
|
---|
645 | {
|
---|
646 | UNUSED(info_ptr);
|
---|
647 |
|
---|
648 | if (after_IDAT)
|
---|
649 | return d->after_IDAT;
|
---|
650 |
|
---|
651 | else
|
---|
652 | return d->before_IDAT;
|
---|
653 | }
|
---|
654 |
|
---|
655 | # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
656 | /* The #defines above should mean this is never reached, it's just here as
|
---|
657 | * a check to ensure the logic is correct.
|
---|
658 | */
|
---|
659 | # error No store support and no user chunk support, this will not work
|
---|
660 | # endif /* READ_USER_CHUNKS */
|
---|
661 | #endif /* SAVE_UNKNOWN_CHUNKS */
|
---|
662 |
|
---|
663 | static int
|
---|
664 | check(FILE *fp, int argc, const char **argv, png_uint_32p flags/*out*/,
|
---|
665 | display *d, int set_callback)
|
---|
666 | {
|
---|
667 | int i, npasses, ipass;
|
---|
668 | png_uint_32 height;
|
---|
669 |
|
---|
670 | d->keep = PNG_HANDLE_CHUNK_AS_DEFAULT;
|
---|
671 | d->before_IDAT = 0;
|
---|
672 | d->after_IDAT = 0;
|
---|
673 |
|
---|
674 | /* Some of these errors are permanently fatal and cause an exit here, others
|
---|
675 | * are per-test and cause an error return.
|
---|
676 | */
|
---|
677 | d->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, d, error,
|
---|
678 | warning);
|
---|
679 | if (d->png_ptr == NULL)
|
---|
680 | {
|
---|
681 | fprintf(stderr, "%s(%s): could not allocate png struct\n", d->file,
|
---|
682 | d->test);
|
---|
683 | /* Terminate here, this error is not test specific. */
|
---|
684 | exit(1);
|
---|
685 | }
|
---|
686 |
|
---|
687 | d->info_ptr = png_create_info_struct(d->png_ptr);
|
---|
688 | d->end_ptr = png_create_info_struct(d->png_ptr);
|
---|
689 | if (d->info_ptr == NULL || d->end_ptr == NULL)
|
---|
690 | {
|
---|
691 | fprintf(stderr, "%s(%s): could not allocate png info\n", d->file,
|
---|
692 | d->test);
|
---|
693 | clean_display(d);
|
---|
694 | exit(1);
|
---|
695 | }
|
---|
696 |
|
---|
697 | png_init_io(d->png_ptr, fp);
|
---|
698 |
|
---|
699 | # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
700 | /* This is only done if requested by the caller; it interferes with the
|
---|
701 | * standard store/save mechanism.
|
---|
702 | */
|
---|
703 | if (set_callback)
|
---|
704 | png_set_read_user_chunk_fn(d->png_ptr, d, read_callback);
|
---|
705 | # else
|
---|
706 | UNUSED(set_callback)
|
---|
707 | # endif
|
---|
708 |
|
---|
709 | /* Handle each argument in turn; multiple settings are possible for the same
|
---|
710 | * chunk and multiple calls will occur (the last one should override all
|
---|
711 | * preceding ones).
|
---|
712 | */
|
---|
713 | for (i=0; i<argc; ++i)
|
---|
714 | {
|
---|
715 | const char *equals = strchr(argv[i], '=');
|
---|
716 |
|
---|
717 | if (equals != NULL)
|
---|
718 | {
|
---|
719 | int chunk, option;
|
---|
720 |
|
---|
721 | if (strcmp(equals+1, "default") == 0)
|
---|
722 | option = PNG_HANDLE_CHUNK_AS_DEFAULT;
|
---|
723 | else if (strcmp(equals+1, "discard") == 0)
|
---|
724 | option = PNG_HANDLE_CHUNK_NEVER;
|
---|
725 | else if (strcmp(equals+1, "if-safe") == 0)
|
---|
726 | option = PNG_HANDLE_CHUNK_IF_SAFE;
|
---|
727 | else if (strcmp(equals+1, "save") == 0)
|
---|
728 | option = PNG_HANDLE_CHUNK_ALWAYS;
|
---|
729 | else
|
---|
730 | {
|
---|
731 | fprintf(stderr, "%s(%s): %s: unrecognized chunk option\n", d->file,
|
---|
732 | d->test, argv[i]);
|
---|
733 | display_exit(d);
|
---|
734 | }
|
---|
735 |
|
---|
736 | switch (equals - argv[i])
|
---|
737 | {
|
---|
738 | case 4: /* chunk name */
|
---|
739 | chunk = find(argv[i]);
|
---|
740 |
|
---|
741 | if (chunk >= 0)
|
---|
742 | {
|
---|
743 | /* These #if tests have the effect of skipping the arguments
|
---|
744 | * if SAVE support is unavailable - we can't do a useful test
|
---|
745 | * in this case, so we just check the arguments! This could
|
---|
746 | * be improved in the future by using the read callback.
|
---|
747 | */
|
---|
748 | # if PNG_LIBPNG_VER >= 10700 &&\
|
---|
749 | !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
750 | if (option < PNG_HANDLE_CHUNK_IF_SAFE)
|
---|
751 | # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
|
---|
752 | {
|
---|
753 | png_byte name[5];
|
---|
754 |
|
---|
755 | memcpy(name, chunk_info[chunk].name, 5);
|
---|
756 | png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
|
---|
757 | chunk_info[chunk].keep = option;
|
---|
758 | }
|
---|
759 | continue;
|
---|
760 | }
|
---|
761 |
|
---|
762 | break;
|
---|
763 |
|
---|
764 | case 7: /* default */
|
---|
765 | if (memcmp(argv[i], "default", 7) == 0)
|
---|
766 | {
|
---|
767 | # if PNG_LIBPNG_VER >= 10700 &&\
|
---|
768 | !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
769 | if (option < PNG_HANDLE_CHUNK_IF_SAFE)
|
---|
770 | # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
|
---|
771 | png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
|
---|
772 |
|
---|
773 | d->keep = option;
|
---|
774 | continue;
|
---|
775 | }
|
---|
776 |
|
---|
777 | break;
|
---|
778 |
|
---|
779 | case 3: /* all */
|
---|
780 | if (memcmp(argv[i], "all", 3) == 0)
|
---|
781 | {
|
---|
782 | # if PNG_LIBPNG_VER >= 10700 &&\
|
---|
783 | !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
|
---|
784 | if (option < PNG_HANDLE_CHUNK_IF_SAFE)
|
---|
785 | # endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
|
---|
786 | png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
|
---|
787 |
|
---|
788 | d->keep = option;
|
---|
789 |
|
---|
790 | for (chunk = 0; chunk < NINFO; ++chunk)
|
---|
791 | if (chunk_info[chunk].all)
|
---|
792 | chunk_info[chunk].keep = option;
|
---|
793 | continue;
|
---|
794 | }
|
---|
795 |
|
---|
796 | break;
|
---|
797 |
|
---|
798 | default: /* some misplaced = */
|
---|
799 |
|
---|
800 | break;
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
|
---|
805 | d->test, argv[i]);
|
---|
806 | display_exit(d);
|
---|
807 | }
|
---|
808 |
|
---|
809 | png_read_info(d->png_ptr, d->info_ptr);
|
---|
810 |
|
---|
811 | switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
|
---|
812 | {
|
---|
813 | case PNG_INTERLACE_NONE:
|
---|
814 | npasses = 1;
|
---|
815 | break;
|
---|
816 |
|
---|
817 | case PNG_INTERLACE_ADAM7:
|
---|
818 | npasses = PNG_INTERLACE_ADAM7_PASSES;
|
---|
819 | break;
|
---|
820 |
|
---|
821 | default:
|
---|
822 | /* Hard error because it is not test specific */
|
---|
823 | fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
|
---|
824 | clean_display(d);
|
---|
825 | exit(1);
|
---|
826 | }
|
---|
827 |
|
---|
828 | /* Skip the image data, if IDAT is not being handled then don't do this
|
---|
829 | * because it will cause a CRC error.
|
---|
830 | */
|
---|
831 | if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
|
---|
832 | {
|
---|
833 | png_start_read_image(d->png_ptr);
|
---|
834 | height = png_get_image_height(d->png_ptr, d->info_ptr);
|
---|
835 |
|
---|
836 | if (npasses > 1)
|
---|
837 | {
|
---|
838 | png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
|
---|
839 |
|
---|
840 | for (ipass=0; ipass<npasses; ++ipass)
|
---|
841 | {
|
---|
842 | png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
|
---|
843 |
|
---|
844 | if (wPass > 0)
|
---|
845 | {
|
---|
846 | png_uint_32 y;
|
---|
847 |
|
---|
848 | for (y=0; y<height; ++y)
|
---|
849 | if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
|
---|
850 | png_read_row(d->png_ptr, NULL, NULL);
|
---|
851 | }
|
---|
852 | }
|
---|
853 | } /* interlaced */
|
---|
854 |
|
---|
855 | else /* not interlaced */
|
---|
856 | {
|
---|
857 | png_uint_32 y;
|
---|
858 |
|
---|
859 | for (y=0; y<height; ++y)
|
---|
860 | png_read_row(d->png_ptr, NULL, NULL);
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | png_read_end(d->png_ptr, d->end_ptr);
|
---|
865 |
|
---|
866 | flags[0] = get_valid(d, d->info_ptr);
|
---|
867 | flags[1] = get_unknown(d, d->info_ptr, 0/*before IDAT*/);
|
---|
868 |
|
---|
869 | /* Only png_read_png sets PNG_INFO_IDAT! */
|
---|
870 | flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
|
---|
871 | PNG_INFO_IDAT;
|
---|
872 |
|
---|
873 | flags[2] = get_valid(d, d->end_ptr);
|
---|
874 | flags[3] = get_unknown(d, d->end_ptr, 1/*after IDAT*/);
|
---|
875 |
|
---|
876 | clean_display(d);
|
---|
877 |
|
---|
878 | return d->keep;
|
---|
879 | }
|
---|
880 |
|
---|
881 | static void
|
---|
882 | check_error(display *d, png_uint_32 flags, const char *message)
|
---|
883 | {
|
---|
884 | while (flags)
|
---|
885 | {
|
---|
886 | png_uint_32 flag = flags & -(png_int_32)flags;
|
---|
887 | int i = find_by_flag(flag);
|
---|
888 |
|
---|
889 | fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
|
---|
890 | chunk_info[i].name, message);
|
---|
891 | ++(d->error_count);
|
---|
892 |
|
---|
893 | flags &= ~flag;
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | static void
|
---|
898 | check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
|
---|
899 | png_uint_32 unknown, const char *position, int set_callback)
|
---|
900 | {
|
---|
901 | while (chunks)
|
---|
902 | {
|
---|
903 | png_uint_32 flag = chunks & -(png_int_32)chunks;
|
---|
904 | int i = find_by_flag(flag);
|
---|
905 | int keep = chunk_info[i].keep;
|
---|
906 | const char *type;
|
---|
907 | const char *errorx = NULL;
|
---|
908 |
|
---|
909 | if (chunk_info[i].unknown)
|
---|
910 | {
|
---|
911 | if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
|
---|
912 | {
|
---|
913 | type = "UNKNOWN (default)";
|
---|
914 | keep = def;
|
---|
915 | }
|
---|
916 |
|
---|
917 | else
|
---|
918 | type = "UNKNOWN (specified)";
|
---|
919 |
|
---|
920 | if (flag & known)
|
---|
921 | errorx = "chunk processed";
|
---|
922 |
|
---|
923 | else switch (keep)
|
---|
924 | {
|
---|
925 | case PNG_HANDLE_CHUNK_AS_DEFAULT:
|
---|
926 | if (flag & unknown)
|
---|
927 | errorx = "DEFAULT: unknown chunk saved";
|
---|
928 | break;
|
---|
929 |
|
---|
930 | case PNG_HANDLE_CHUNK_NEVER:
|
---|
931 | if (flag & unknown)
|
---|
932 | errorx = "DISCARD: unknown chunk saved";
|
---|
933 | break;
|
---|
934 |
|
---|
935 | case PNG_HANDLE_CHUNK_IF_SAFE:
|
---|
936 | if (ancillary(chunk_info[i].name))
|
---|
937 | {
|
---|
938 | if (!(flag & unknown))
|
---|
939 | errorx = "IF-SAFE: unknown ancillary chunk lost";
|
---|
940 | }
|
---|
941 |
|
---|
942 | else if (flag & unknown)
|
---|
943 | errorx = "IF-SAFE: unknown critical chunk saved";
|
---|
944 | break;
|
---|
945 |
|
---|
946 | case PNG_HANDLE_CHUNK_ALWAYS:
|
---|
947 | if (!(flag & unknown))
|
---|
948 | errorx = "SAVE: unknown chunk lost";
|
---|
949 | break;
|
---|
950 |
|
---|
951 | default:
|
---|
952 | errorx = "internal error: bad keep";
|
---|
953 | break;
|
---|
954 | }
|
---|
955 | } /* unknown chunk */
|
---|
956 |
|
---|
957 | else /* known chunk */
|
---|
958 | {
|
---|
959 | type = "KNOWN";
|
---|
960 |
|
---|
961 | if (flag & known)
|
---|
962 | {
|
---|
963 | /* chunk was processed, it won't have been saved because that is
|
---|
964 | * caught below when checking for inconsistent processing.
|
---|
965 | */
|
---|
966 | if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
|
---|
967 | errorx = "!DEFAULT: known chunk processed";
|
---|
968 | }
|
---|
969 |
|
---|
970 | else /* not processed */ switch (keep)
|
---|
971 | {
|
---|
972 | case PNG_HANDLE_CHUNK_AS_DEFAULT:
|
---|
973 | errorx = "DEFAULT: known chunk not processed";
|
---|
974 | break;
|
---|
975 |
|
---|
976 | case PNG_HANDLE_CHUNK_NEVER:
|
---|
977 | if (flag & unknown)
|
---|
978 | errorx = "DISCARD: known chunk saved";
|
---|
979 | break;
|
---|
980 |
|
---|
981 | case PNG_HANDLE_CHUNK_IF_SAFE:
|
---|
982 | if (ancillary(chunk_info[i].name))
|
---|
983 | {
|
---|
984 | if (!(flag & unknown))
|
---|
985 | errorx = "IF-SAFE: known ancillary chunk lost";
|
---|
986 | }
|
---|
987 |
|
---|
988 | else if (flag & unknown)
|
---|
989 | errorx = "IF-SAFE: known critical chunk saved";
|
---|
990 | break;
|
---|
991 |
|
---|
992 | case PNG_HANDLE_CHUNK_ALWAYS:
|
---|
993 | if (!(flag & unknown))
|
---|
994 | errorx = "SAVE: known chunk lost";
|
---|
995 | break;
|
---|
996 |
|
---|
997 | default:
|
---|
998 | errorx = "internal error: bad keep (2)";
|
---|
999 | break;
|
---|
1000 | }
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | if (errorx != NULL)
|
---|
1004 | {
|
---|
1005 | ++(d->error_count);
|
---|
1006 | fprintf(stderr, "%s(%s%s): %s %s %s: %s\n", d->file, d->test,
|
---|
1007 | set_callback ? ",callback" : "",
|
---|
1008 | type, chunk_info[i].name, position, errorx);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | chunks &= ~flag;
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | static void
|
---|
1016 | perform_one_test(FILE *fp, int argc, const char **argv,
|
---|
1017 | png_uint_32 *default_flags, display *d, int set_callback)
|
---|
1018 | {
|
---|
1019 | int def;
|
---|
1020 | png_uint_32 flags[2][4];
|
---|
1021 |
|
---|
1022 | rewind(fp);
|
---|
1023 | clear_keep();
|
---|
1024 | memcpy(flags[0], default_flags, sizeof flags[0]);
|
---|
1025 |
|
---|
1026 | def = check(fp, argc, argv, flags[1], d, set_callback);
|
---|
1027 |
|
---|
1028 | /* If IDAT is being handled as unknown the image read is skipped and all the
|
---|
1029 | * IDATs after the first end up in the end info struct, so in this case add
|
---|
1030 | * IDAT to the list of unknowns. (Do this after 'check' above sets the
|
---|
1031 | * chunk_info 'keep' fields.)
|
---|
1032 | *
|
---|
1033 | * Note that the flag setting has to be in the 'known' field to avoid
|
---|
1034 | * triggering the consistency check below and the flag must only be set if
|
---|
1035 | * there are multiple IDATs, so if the check above did find an unknown IDAT
|
---|
1036 | * after IDAT.
|
---|
1037 | */
|
---|
1038 | if (chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT &&
|
---|
1039 | (flags[1][3] & PNG_INFO_IDAT) != 0)
|
---|
1040 | flags[0][2] |= PNG_INFO_IDAT;
|
---|
1041 |
|
---|
1042 | /* Chunks should either be known or unknown, never both and this should apply
|
---|
1043 | * whether the chunk is before or after the IDAT (actually, the app can
|
---|
1044 | * probably change this by swapping the handling after the image, but this
|
---|
1045 | * test does not do that.)
|
---|
1046 | */
|
---|
1047 | check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
|
---|
1048 | "chunk handled inconsistently in count tests");
|
---|
1049 | check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
|
---|
1050 | "chunk handled inconsistently in option tests");
|
---|
1051 |
|
---|
1052 | /* Now find out what happened to each chunk before and after the IDAT and
|
---|
1053 | * determine if the behavior was correct. First some basic sanity checks,
|
---|
1054 | * any known chunk should be known in the original count, any unknown chunk
|
---|
1055 | * should be either known or unknown in the original.
|
---|
1056 | */
|
---|
1057 | {
|
---|
1058 | png_uint_32 test;
|
---|
1059 |
|
---|
1060 | test = flags[1][0] & ~flags[0][0];
|
---|
1061 | check_error(d, test, "new known chunk before IDAT");
|
---|
1062 | test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
|
---|
1063 | check_error(d, test, "new unknown chunk before IDAT");
|
---|
1064 | test = flags[1][2] & ~flags[0][2];
|
---|
1065 | check_error(d, test, "new known chunk after IDAT");
|
---|
1066 | test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
|
---|
1067 | check_error(d, test, "new unknown chunk after IDAT");
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /* Now each chunk in the original list should have been handled according to
|
---|
1071 | * the options set for that chunk, regardless of whether libpng knows about
|
---|
1072 | * it or not.
|
---|
1073 | */
|
---|
1074 | check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
|
---|
1075 | "before IDAT", set_callback);
|
---|
1076 | check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
|
---|
1077 | "after IDAT", set_callback);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | static void
|
---|
1081 | perform_one_test_safe(FILE *fp, int argc, const char **argv,
|
---|
1082 | png_uint_32 *default_flags, display *d, const char *test)
|
---|
1083 | {
|
---|
1084 | if (setjmp(d->error_return) == 0)
|
---|
1085 | {
|
---|
1086 | d->test = test; /* allow use of d->error_return */
|
---|
1087 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
1088 | perform_one_test(fp, argc, argv, default_flags, d, 0);
|
---|
1089 | # endif
|
---|
1090 | # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
1091 | perform_one_test(fp, argc, argv, default_flags, d, 1);
|
---|
1092 | # endif
|
---|
1093 | d->test = init; /* prevent use of d->error_return */
|
---|
1094 | }
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | static const char *standard_tests[] =
|
---|
1098 | {
|
---|
1099 | "discard", "default=discard", NULL,
|
---|
1100 | "save", "default=save", NULL,
|
---|
1101 | "if-safe", "default=if-safe", NULL,
|
---|
1102 | "vpAg", "vpAg=if-safe", NULL,
|
---|
1103 | "sTER", "sTER=if-safe", NULL,
|
---|
1104 | "IDAT", "default=discard", "IDAT=save", NULL,
|
---|
1105 | "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
|
---|
1106 | "sBIT=save", "sRGB=save", "eXIf=save", NULL,
|
---|
1107 | NULL /*end*/
|
---|
1108 | };
|
---|
1109 |
|
---|
1110 | static PNG_NORETURN void
|
---|
1111 | usage(const char *program, const char *reason)
|
---|
1112 | {
|
---|
1113 | fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
|
---|
1114 | "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
|
---|
1115 | "testfile.png\n", reason, program);
|
---|
1116 | exit(99);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | int
|
---|
1120 | main(int argc, const char **argv)
|
---|
1121 | {
|
---|
1122 | FILE *fp;
|
---|
1123 | png_uint_32 default_flags[4]; /*valid,unknown{before,after}*/
|
---|
1124 | int strict = 0, default_tests = 0;
|
---|
1125 | const char *count_argv = "default=save";
|
---|
1126 | const char *touch_file = NULL;
|
---|
1127 | display d;
|
---|
1128 |
|
---|
1129 | init_display(&d, argv[0]);
|
---|
1130 |
|
---|
1131 | while (++argv, --argc > 0)
|
---|
1132 | {
|
---|
1133 | if (strcmp(*argv, "--strict") == 0)
|
---|
1134 | strict = 1;
|
---|
1135 |
|
---|
1136 | else if (strcmp(*argv, "--default") == 0)
|
---|
1137 | default_tests = 1;
|
---|
1138 |
|
---|
1139 | else if (strcmp(*argv, "--touch") == 0)
|
---|
1140 | {
|
---|
1141 | if (argc > 1)
|
---|
1142 | touch_file = *++argv, --argc;
|
---|
1143 |
|
---|
1144 | else
|
---|
1145 | usage(d.program, "--touch: missing file name");
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | else
|
---|
1149 | break;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | /* A file name is required, but there should be no other arguments if
|
---|
1153 | * --default was specified.
|
---|
1154 | */
|
---|
1155 | if (argc <= 0)
|
---|
1156 | usage(d.program, "missing test file");
|
---|
1157 |
|
---|
1158 | /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
|
---|
1159 | * optimization which causes warnings with -Wstrict-overflow!
|
---|
1160 | */
|
---|
1161 | else if (default_tests)
|
---|
1162 | if (argc != 1)
|
---|
1163 | usage(d.program, "extra arguments");
|
---|
1164 |
|
---|
1165 | /* The name of the test file is the last argument; remove it. */
|
---|
1166 | d.file = argv[--argc];
|
---|
1167 |
|
---|
1168 | fp = fopen(d.file, "rb");
|
---|
1169 | if (fp == NULL)
|
---|
1170 | {
|
---|
1171 | perror(d.file);
|
---|
1172 | exit(99);
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /* First find all the chunks, known and unknown, in the test file, a failure
|
---|
1176 | * here aborts the whole test.
|
---|
1177 | *
|
---|
1178 | * If 'save' is supported then the normal saving method should happen,
|
---|
1179 | * otherwise if 'read' is supported then the read callback will do the
|
---|
1180 | * same thing. If both are supported the 'read' callback won't be
|
---|
1181 | * instantiated by default. If 'save' is *not* supported then a user
|
---|
1182 | * callback is required even though we can call png_get_unknown_chunks.
|
---|
1183 | */
|
---|
1184 | if (check(fp, 1, &count_argv, default_flags, &d,
|
---|
1185 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
1186 | 0
|
---|
1187 | # else
|
---|
1188 | 1
|
---|
1189 | # endif
|
---|
1190 | ) != PNG_HANDLE_CHUNK_ALWAYS)
|
---|
1191 | {
|
---|
1192 | fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
|
---|
1193 | exit(99);
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | /* Now find what the various supplied options cause to change: */
|
---|
1197 | if (!default_tests)
|
---|
1198 | {
|
---|
1199 | d.test = cmd; /* acts as a flag to say exit, do not longjmp */
|
---|
1200 | # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
|
---|
1201 | perform_one_test(fp, argc, argv, default_flags, &d, 0);
|
---|
1202 | # endif
|
---|
1203 | # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
|
---|
1204 | perform_one_test(fp, argc, argv, default_flags, &d, 1);
|
---|
1205 | # endif
|
---|
1206 | d.test = init;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | else
|
---|
1210 | {
|
---|
1211 | const char **test = standard_tests;
|
---|
1212 |
|
---|
1213 | /* Set the exit_test pointer here so we can continue after a libpng error.
|
---|
1214 | * NOTE: this leaks memory because the png_struct data from the failing
|
---|
1215 | * test is never freed.
|
---|
1216 | */
|
---|
1217 | while (*test)
|
---|
1218 | {
|
---|
1219 | const char *this_test = *test++;
|
---|
1220 | const char **next = test;
|
---|
1221 | int count = display_rc(&d, strict), new_count;
|
---|
1222 | const char *result;
|
---|
1223 | int arg_count = 0;
|
---|
1224 |
|
---|
1225 | while (*next != NULL)
|
---|
1226 | {
|
---|
1227 | ++next;
|
---|
1228 | ++arg_count;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | perform_one_test_safe(fp, arg_count, test, default_flags, &d,
|
---|
1232 | this_test);
|
---|
1233 |
|
---|
1234 | new_count = display_rc(&d, strict);
|
---|
1235 |
|
---|
1236 | if (new_count == count)
|
---|
1237 | result = "PASS";
|
---|
1238 |
|
---|
1239 | else
|
---|
1240 | result = "FAIL";
|
---|
1241 |
|
---|
1242 | printf("%s: %s %s\n", result, d.program, this_test);
|
---|
1243 |
|
---|
1244 | test = next+1;
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | fclose(fp);
|
---|
1249 |
|
---|
1250 | if (display_rc(&d, strict) == 0)
|
---|
1251 | {
|
---|
1252 | /* Success, touch the success file if appropriate */
|
---|
1253 | if (touch_file != NULL)
|
---|
1254 | {
|
---|
1255 | FILE *fsuccess = fopen(touch_file, "wt");
|
---|
1256 |
|
---|
1257 | if (fsuccess != NULL)
|
---|
1258 | {
|
---|
1259 | int err = 0;
|
---|
1260 | fprintf(fsuccess, "PNG unknown tests succeeded\n");
|
---|
1261 | fflush(fsuccess);
|
---|
1262 | err = ferror(fsuccess);
|
---|
1263 |
|
---|
1264 | if (fclose(fsuccess) || err)
|
---|
1265 | {
|
---|
1266 | fprintf(stderr, "%s: write failed\n", touch_file);
|
---|
1267 | exit(99);
|
---|
1268 | }
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | else
|
---|
1272 | {
|
---|
1273 | fprintf(stderr, "%s: open failed\n", touch_file);
|
---|
1274 | exit(99);
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | return 0;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | return 1;
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | #else /* !(READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS) */
|
---|
1285 | int
|
---|
1286 | main(void)
|
---|
1287 | {
|
---|
1288 | fprintf(stderr,
|
---|
1289 | " test ignored: no support to find out about unknown chunks\n");
|
---|
1290 | /* So the test is skipped: */
|
---|
1291 | return SKIP;
|
---|
1292 | }
|
---|
1293 | #endif /* READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS */
|
---|
1294 |
|
---|
1295 | #else /* !(SET_UNKNOWN_CHUNKS && READ) */
|
---|
1296 | int
|
---|
1297 | main(void)
|
---|
1298 | {
|
---|
1299 | fprintf(stderr,
|
---|
1300 | " test ignored: no support to modify unknown chunk handling\n");
|
---|
1301 | /* So the test is skipped: */
|
---|
1302 | return SKIP;
|
---|
1303 | }
|
---|
1304 | #endif /* SET_UNKNOWN_CHUNKS && READ*/
|
---|