all repos — mgba @ 9de8f084ba55460b02d300c1dd8b8e6c56f691d5

mGBA Game Boy Advance Emulator

src/third-party/libpng/contrib/libtests/pngunknown.c (view raw)

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