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