fukasawa e60969
fukasawa e60969
/* pngunknown.c - test the read side unknown chunk handling
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.10 [March 6, 2014]
fukasawa e60969
 * Copyright (c) 2014 Glenn Randers-Pehrson
fukasawa e60969
 * Written by John Cunningham Bowler
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * NOTES:
fukasawa e60969
 *   This is a C program that is intended to be linked against libpng.  It
fukasawa e60969
 *   allows the libpng unknown handling code to be tested by interpreting
fukasawa e60969
 *   arguments to save or discard combinations of chunks.  The program is
fukasawa e60969
 *   currently just a minimal validation for the built-in libpng facilities.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <setjmp.h></setjmp.h>
fukasawa e60969
fukasawa e60969
/* Define the following to use this test against your installed libpng, rather
fukasawa e60969
 * than the one being built here:
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_FREESTANDING_TESTS
fukasawa e60969
#  include <png.h></png.h>
fukasawa e60969
#else
fukasawa e60969
#  include "../../png.h"
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Since this program tests the ability to change the unknown chunk handling
fukasawa e60969
 * these must be defined:
fukasawa e60969
 */
fukasawa e60969
#if defined(PNG_SET_UNKNOWN_CHUNKS_SUPPORTED) &&\
fukasawa e60969
   defined(PNG_READ_SUPPORTED)
fukasawa e60969
fukasawa e60969
/* One of these must be defined to allow us to find out what happened.  It is
fukasawa e60969
 * still useful to set unknown chunk handling without either of these in order
fukasawa e60969
 * to cause *known* chunks to be discarded.  This can be a significant
fukasawa e60969
 * efficiency gain, but it can't really be tested here.
fukasawa e60969
 */
fukasawa e60969
#if defined(PNG_READ_USER_CHUNKS_SUPPORTED) ||\
fukasawa e60969
   defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
fukasawa e60969
fukasawa e60969
#if PNG_LIBPNG_VER < 10500
fukasawa e60969
/* This deliberately lacks the PNG_CONST. */
fukasawa e60969
typedef png_byte *png_const_bytep;
fukasawa e60969
fukasawa e60969
/* This is copied from 1.5.1 png.h: */
fukasawa e60969
#define PNG_INTERLACE_ADAM7_PASSES 7
fukasawa e60969
#define PNG_PASS_START_ROW(pass) (((1U&~(pass))<<(3-((pass)>>1)))&7)
fukasawa e60969
#define PNG_PASS_START_COL(pass) (((1U& (pass))<<(3-(((pass)+1)>>1)))&7)
fukasawa e60969
#define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3)
fukasawa e60969
#define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3)
fukasawa e60969
#define PNG_PASS_ROWS(height, pass) (((height)+(((1<
fukasawa e60969
   -1)-PNG_PASS_START_ROW(pass)))>>PNG_PASS_ROW_SHIFT(pass))
fukasawa e60969
#define PNG_PASS_COLS(width, pass) (((width)+(((1<
fukasawa e60969
   -1)-PNG_PASS_START_COL(pass)))>>PNG_PASS_COL_SHIFT(pass))
fukasawa e60969
#define PNG_ROW_FROM_PASS_ROW(yIn, pass) \
fukasawa e60969
   (((yIn)<
fukasawa e60969
#define PNG_COL_FROM_PASS_COL(xIn, pass) \
fukasawa e60969
   (((xIn)<
fukasawa e60969
#define PNG_PASS_MASK(pass,off) ( \
fukasawa e60969
   ((0x110145AFU>>(((7-(off))-(pass))<<2)) & 0xFU) | \
fukasawa e60969
   ((0x01145AF0U>>(((7-(off))-(pass))<<2)) & 0xF0U))
fukasawa e60969
#define PNG_ROW_IN_INTERLACE_PASS(y, pass) \
fukasawa e60969
   ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1)
fukasawa e60969
#define PNG_COL_IN_INTERLACE_PASS(x, pass) \
fukasawa e60969
   ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1)
fukasawa e60969
fukasawa e60969
/* These are needed too for the default build: */
fukasawa e60969
#define PNG_WRITE_16BIT_SUPPORTED
fukasawa e60969
#define PNG_READ_16BIT_SUPPORTED
fukasawa e60969
fukasawa e60969
/* This comes from pnglibconf.h afer 1.5: */
fukasawa e60969
#define PNG_FP_1 100000
fukasawa e60969
#define PNG_GAMMA_THRESHOLD_FIXED\
fukasawa e60969
   ((png_fixed_point)(PNG_GAMMA_THRESHOLD * PNG_FP_1))
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if PNG_LIBPNG_VER < 10600
fukasawa e60969
   /* 1.6.0 constifies many APIs. The following exists to allow pngvalid to be
fukasawa e60969
    * compiled against earlier versions.
fukasawa e60969
    */
fukasawa e60969
#  define png_const_structp png_structp
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#if PNG_LIBPNG_VER < 10700
fukasawa e60969
   /* Copied from libpng 1.7.0 png.h */
fukasawa e60969
#define PNG_u2(b1, b2) (((unsigned int)(b1) << 8) + (b2))
fukasawa e60969
fukasawa e60969
#define PNG_U16(b1, b2) ((png_uint_16)PNG_u2(b1, b2))
fukasawa e60969
#define PNG_U32(b1, b2, b3, b4)\
fukasawa e60969
   (((png_uint_32)PNG_u2(b1, b2) << 16) + PNG_u2(b3, b4))
fukasawa e60969
fukasawa e60969
/* Constants for known chunk types.
fukasawa e60969
 */
fukasawa e60969
#define png_IDAT PNG_U32( 73,  68,  65,  84)
fukasawa e60969
#define png_IEND PNG_U32( 73,  69,  78,  68)
fukasawa e60969
#define png_IHDR PNG_U32( 73,  72,  68,  82)
fukasawa e60969
#define png_PLTE PNG_U32( 80,  76,  84,  69)
fukasawa e60969
#define png_bKGD PNG_U32( 98,  75,  71,  68)
fukasawa e60969
#define png_cHRM PNG_U32( 99,  72,  82,  77)
fukasawa e60969
#define png_fRAc PNG_U32(102,  82,  65,  99) /* registered, not defined */
fukasawa e60969
#define png_gAMA PNG_U32(103,  65,  77,  65)
fukasawa e60969
#define png_gIFg PNG_U32(103,  73,  70, 103)
fukasawa e60969
#define png_gIFt PNG_U32(103,  73,  70, 116) /* deprecated */
fukasawa e60969
#define png_gIFx PNG_U32(103,  73,  70, 120)
fukasawa e60969
#define png_hIST PNG_U32(104,  73,  83,  84)
fukasawa e60969
#define png_iCCP PNG_U32(105,  67,  67,  80)
fukasawa e60969
#define png_iTXt PNG_U32(105,  84,  88, 116)
fukasawa e60969
#define png_oFFs PNG_U32(111,  70,  70, 115)
fukasawa e60969
#define png_pCAL PNG_U32(112,  67,  65,  76)
fukasawa e60969
#define png_pHYs PNG_U32(112,  72,  89, 115)
fukasawa e60969
#define png_sBIT PNG_U32(115,  66,  73,  84)
fukasawa e60969
#define png_sCAL PNG_U32(115,  67,  65,  76)
fukasawa e60969
#define png_sPLT PNG_U32(115,  80,  76,  84)
fukasawa e60969
#define png_sRGB PNG_U32(115,  82,  71,  66)
fukasawa e60969
#define png_sTER PNG_U32(115,  84,  69,  82)
fukasawa e60969
#define png_tEXt PNG_U32(116,  69,  88, 116)
fukasawa e60969
#define png_tIME PNG_U32(116,  73,  77,  69)
fukasawa e60969
#define png_tRNS PNG_U32(116,  82,  78,  83)
fukasawa e60969
#define png_zTXt PNG_U32(122,  84,  88, 116)
fukasawa e60969
fukasawa e60969
/* Test on flag values as defined in the spec (section 5.4): */
fukasawa e60969
#define PNG_CHUNK_ANCILLARY(c)    (1 & ((c) >> 29))
fukasawa e60969
#define PNG_CHUNK_CRITICAL(c)     (!PNG_CHUNK_ANCILLARY(c))
fukasawa e60969
#define PNG_CHUNK_PRIVATE(c)      (1 & ((c) >> 21))
fukasawa e60969
#define PNG_CHUNK_RESERVED(c)     (1 & ((c) >> 13))
fukasawa e60969
#define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >>  5))
fukasawa e60969
fukasawa e60969
#endif /* PNG_LIBPNG_VER < 10700 */
fukasawa e60969
fukasawa e60969
#ifdef __cplusplus
fukasawa e60969
#  define this not_the_cpp_this
fukasawa e60969
#  define new not_the_cpp_new
fukasawa e60969
#  define voidcast(type, value) static_cast<type>(value)</type>
fukasawa e60969
#else
fukasawa e60969
#  define voidcast(type, value) (value)
fukasawa e60969
#endif /* __cplusplus */
fukasawa e60969
fukasawa e60969
/* Unused formal parameter errors are removed using the following macro which is
fukasawa e60969
 * expected to have no bad effects on performance.
fukasawa e60969
 */
fukasawa e60969
#ifndef UNUSED
fukasawa e60969
#  if defined(__GNUC__) || defined(_MSC_VER)
fukasawa e60969
#     define UNUSED(param) (void)param;
fukasawa e60969
#  else
fukasawa e60969
#     define UNUSED(param)
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Types of chunks not known to libpng */
fukasawa e60969
#define png_vpAg PNG_U32(118, 112, 65, 103)
fukasawa e60969
fukasawa e60969
/* Chunk information */
fukasawa e60969
#define PNG_INFO_tEXt 0x10000000U
fukasawa e60969
#define PNG_INFO_iTXt 0x20000000U
fukasawa e60969
#define PNG_INFO_zTXt 0x40000000U
fukasawa e60969
fukasawa e60969
#define PNG_INFO_sTER 0x01000000U
fukasawa e60969
#define PNG_INFO_vpAg 0x02000000U
fukasawa e60969
fukasawa e60969
#define ABSENT  0
fukasawa e60969
#define START   1
fukasawa e60969
#define END     2
fukasawa e60969
fukasawa e60969
static struct
fukasawa e60969
{
fukasawa e60969
   char        name[5];
fukasawa e60969
   png_uint_32 flag;
fukasawa e60969
   png_uint_32 tag;
fukasawa e60969
   int         unknown;    /* Chunk not known to libpng */
fukasawa e60969
   int         all;        /* Chunk set by the '-1' option */
fukasawa e60969
   int         position;   /* position in pngtest.png */
fukasawa e60969
   int         keep;       /* unknown handling setting */
fukasawa e60969
} chunk_info[] = {
fukasawa e60969
   /* Critical chunks */
fukasawa e60969
   { "IDAT", PNG_INFO_IDAT, png_IDAT, 0, 0,  START, 0 }, /* must be [0] */
fukasawa e60969
   { "PLTE", PNG_INFO_PLTE, png_PLTE, 0, 0, ABSENT, 0 },
fukasawa e60969
fukasawa e60969
   /* Non-critical chunks that libpng handles */
fukasawa e60969
   /* This is a mess but it seems to be the only way to do it - there is no way
fukasawa e60969
    * to check for a definition outside a #if.
fukasawa e60969
    */
fukasawa e60969
   { "bKGD", PNG_INFO_bKGD, png_bKGD,
fukasawa e60969
#     ifdef PNG_READ_bKGD_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "cHRM", PNG_INFO_cHRM, png_cHRM,
fukasawa e60969
#     ifdef PNG_READ_cHRM_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "gAMA", PNG_INFO_gAMA, png_gAMA,
fukasawa e60969
#     ifdef PNG_READ_gAMA_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "hIST", PNG_INFO_hIST, png_hIST,
fukasawa e60969
#     ifdef PNG_READ_hIST_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1, ABSENT, 0 },
fukasawa e60969
   { "iCCP", PNG_INFO_iCCP, png_iCCP,
fukasawa e60969
#     ifdef PNG_READ_iCCP_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1, ABSENT, 0 },
fukasawa e60969
   { "iTXt", PNG_INFO_iTXt, png_iTXt,
fukasawa e60969
#     ifdef PNG_READ_iTXt_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1, ABSENT, 0 },
fukasawa e60969
   { "oFFs", PNG_INFO_oFFs, png_oFFs,
fukasawa e60969
#     ifdef PNG_READ_oFFs_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "pCAL", PNG_INFO_pCAL, png_pCAL,
fukasawa e60969
#     ifdef PNG_READ_pCAL_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "pHYs", PNG_INFO_pHYs, png_pHYs,
fukasawa e60969
#     ifdef PNG_READ_pHYs_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "sBIT", PNG_INFO_sBIT, png_sBIT,
fukasawa e60969
#     ifdef PNG_READ_sBIT_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "sCAL", PNG_INFO_sCAL, png_sCAL,
fukasawa e60969
#     ifdef PNG_READ_sCAL_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "sPLT", PNG_INFO_sPLT, png_sPLT,
fukasawa e60969
#     ifdef PNG_READ_sPLT_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1, ABSENT, 0 },
fukasawa e60969
   { "sRGB", PNG_INFO_sRGB, png_sRGB,
fukasawa e60969
#     ifdef PNG_READ_sRGB_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "tEXt", PNG_INFO_tEXt, png_tEXt,
fukasawa e60969
#     ifdef PNG_READ_tEXt_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "tIME", PNG_INFO_tIME, png_tIME,
fukasawa e60969
#     ifdef PNG_READ_tIME_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,  START, 0 },
fukasawa e60969
   { "tRNS", PNG_INFO_tRNS, png_tRNS,
fukasawa e60969
#     ifdef PNG_READ_tRNS_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      0, ABSENT, 0 },
fukasawa e60969
   { "zTXt", PNG_INFO_zTXt, png_zTXt,
fukasawa e60969
#     ifdef PNG_READ_zTXt_SUPPORTED
fukasawa e60969
         0,
fukasawa e60969
#     else
fukasawa e60969
         1,
fukasawa e60969
#     endif
fukasawa e60969
      1,    END, 0 },
fukasawa e60969
fukasawa e60969
   /* No libpng handling */
fukasawa e60969
   { "sTER", PNG_INFO_sTER, png_sTER, 1, 1,  START, 0 },
fukasawa e60969
   { "vpAg", PNG_INFO_vpAg, png_vpAg, 1, 0,  START, 0 },
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
#define NINFO ((int)((sizeof chunk_info)/(sizeof chunk_info[0])))
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
clear_keep(void)
fukasawa e60969
{
fukasawa e60969
   int i = NINFO;
fukasawa e60969
   while (--i >= 0)
fukasawa e60969
      chunk_info[i].keep = 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
find(const char *name)
fukasawa e60969
{
fukasawa e60969
   int i = NINFO;
fukasawa e60969
   while (--i >= 0)
fukasawa e60969
   {
fukasawa e60969
      if (memcmp(chunk_info[i].name, name, 4) == 0)
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return i;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
findb(const png_byte *name)
fukasawa e60969
{
fukasawa e60969
   int i = NINFO;
fukasawa e60969
   while (--i >= 0)
fukasawa e60969
   {
fukasawa e60969
      if (memcmp(chunk_info[i].name, name, 4) == 0)
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return i;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
find_by_flag(png_uint_32 flag)
fukasawa e60969
{
fukasawa e60969
   int i = NINFO;
fukasawa e60969
fukasawa e60969
   while (--i >= 0) if (chunk_info[i].flag == flag) return i;
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "pngunknown: internal error\n");
fukasawa e60969
   exit(4);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
ancillary(const char *name)
fukasawa e60969
{
fukasawa e60969
   return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
static int
fukasawa e60969
ancillaryb(const png_byte *name)
fukasawa e60969
{
fukasawa e60969
   return PNG_CHUNK_ANCILLARY(PNG_U32(name[0], name[1], name[2], name[3]));
fukasawa e60969
}
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Type of an error_ptr */
fukasawa e60969
typedef struct
fukasawa e60969
{
fukasawa e60969
   jmp_buf     error_return;
fukasawa e60969
   png_structp png_ptr;
fukasawa e60969
   png_infop   info_ptr, end_ptr;
fukasawa e60969
   png_uint_32 before_IDAT;
fukasawa e60969
   png_uint_32 after_IDAT;
fukasawa e60969
   int         error_count;
fukasawa e60969
   int         warning_count;
fukasawa e60969
   int         keep; /* the default value */
fukasawa e60969
   const char *program;
fukasawa e60969
   const char *file;
fukasawa e60969
   const char *test;
fukasawa e60969
} display;
fukasawa e60969
fukasawa e60969
static const char init[] = "initialization";
fukasawa e60969
static const char cmd[] = "command line";
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
init_display(display *d, const char *program)
fukasawa e60969
{
fukasawa e60969
   memset(d, 0, sizeof *d);
fukasawa e60969
   d->png_ptr = NULL;
fukasawa e60969
   d->info_ptr = d->end_ptr = NULL;
fukasawa e60969
   d->error_count = d->warning_count = 0;
fukasawa e60969
   d->program = program;
fukasawa e60969
   d->file = program;
fukasawa e60969
   d->test = init;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
clean_display(display *d)
fukasawa e60969
{
fukasawa e60969
   png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_ptr);
fukasawa e60969
fukasawa e60969
   /* This must not happen - it might cause an app crash */
fukasawa e60969
   if (d->png_ptr != NULL || d->info_ptr != NULL || d->end_ptr != NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s(%s): png_destroy_read_struct error\n", d->file,
fukasawa e60969
         d->test);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
PNG_FUNCTION(void, display_exit, (display *d), static PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
   ++(d->error_count);
fukasawa e60969
fukasawa e60969
   if (d->png_ptr != NULL)
fukasawa e60969
      clean_display(d);
fukasawa e60969
fukasawa e60969
   /* During initialization and if this is a single command line argument set
fukasawa e60969
    * exit now - there is only one test, otherwise longjmp to do the next test.
fukasawa e60969
    */
fukasawa e60969
   if (d->test == init || d->test == cmd)
fukasawa e60969
      exit(1);
fukasawa e60969
fukasawa e60969
   longjmp(d->error_return, 1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
display_rc(const display *d, int strict)
fukasawa e60969
{
fukasawa e60969
   return d->error_count + (strict ? d->warning_count : 0);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* libpng error and warning callbacks */
fukasawa e60969
PNG_FUNCTION(void, (PNGCBAPI error), (png_structp png_ptr, const char *message),
fukasawa e60969
   static PNG_NORETURN)
fukasawa e60969
{
fukasawa e60969
   display *d = (display*)png_get_error_ptr(png_ptr);
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "%s(%s): libpng error: %s\n", d->file, d->test, message);
fukasawa e60969
   display_exit(d);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void PNGCBAPI
fukasawa e60969
warning(png_structp png_ptr, const char *message)
fukasawa e60969
{
fukasawa e60969
   display *d = (display*)png_get_error_ptr(png_ptr);
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "%s(%s): libpng warning: %s\n", d->file, d->test, message);
fukasawa e60969
   ++(d->warning_count);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_uint_32
fukasawa e60969
get_valid(display *d, png_infop info_ptr)
fukasawa e60969
{
fukasawa e60969
   png_uint_32 flags = png_get_valid(d->png_ptr, info_ptr, (png_uint_32)~0);
fukasawa e60969
fukasawa e60969
   /* Map the text chunks back into the flags */
fukasawa e60969
   {
fukasawa e60969
      png_textp text;
fukasawa e60969
      png_uint_32 ntext = png_get_text(d->png_ptr, info_ptr, &text, NULL);
fukasawa e60969
fukasawa e60969
      while (ntext-- > 0) switch (text[ntext].compression)
fukasawa e60969
      {
fukasawa e60969
         case -1:
fukasawa e60969
            flags |= PNG_INFO_tEXt;
fukasawa e60969
            break;
fukasawa e60969
         case 0:
fukasawa e60969
            flags |= PNG_INFO_zTXt;
fukasawa e60969
            break;
fukasawa e60969
         case 1:
fukasawa e60969
         case 2:
fukasawa e60969
            flags |= PNG_INFO_iTXt;
fukasawa e60969
            break;
fukasawa e60969
         default:
fukasawa e60969
            fprintf(stderr, "%s(%s): unknown text compression %d\n", d->file,
fukasawa e60969
               d->test, text[ntext].compression);
fukasawa e60969
            display_exit(d);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return flags;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
static int PNGCBAPI
fukasawa e60969
read_callback(png_structp pp, png_unknown_chunkp pc)
fukasawa e60969
{
fukasawa e60969
   /* This function mimics the behavior of png_set_keep_unknown_chunks by
fukasawa e60969
    * returning '0' to keep the chunk and '1' to discard it.
fukasawa e60969
    */
fukasawa e60969
   display *d = voidcast(display*, png_get_user_chunk_ptr(pp));
fukasawa e60969
   int chunk = findb(pc->name);
fukasawa e60969
   int keep, discard;
fukasawa e60969
fukasawa e60969
   if (chunk < 0) /* not one in our list, so not a known chunk */
fukasawa e60969
      keep = d->keep;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      keep = chunk_info[chunk].keep;
fukasawa e60969
      if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
fukasawa e60969
      {
fukasawa e60969
         /* See the comments in png.h - use the default for unknown chunks,
fukasawa e60969
          * do not keep known chunks.
fukasawa e60969
          */
fukasawa e60969
         if (chunk_info[chunk].unknown)
fukasawa e60969
            keep = d->keep;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            keep = PNG_HANDLE_CHUNK_NEVER;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   switch (keep)
fukasawa e60969
   {
fukasawa e60969
      default:
fukasawa e60969
         fprintf(stderr, "%s(%s): %d: unrecognized chunk option\n", d->file,
fukasawa e60969
            d->test, chunk_info[chunk].keep);
fukasawa e60969
         display_exit(d);
fukasawa e60969
fukasawa e60969
      case PNG_HANDLE_CHUNK_AS_DEFAULT:
fukasawa e60969
      case PNG_HANDLE_CHUNK_NEVER:
fukasawa e60969
         discard = 1/*handled; discard*/;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_HANDLE_CHUNK_IF_SAFE:
fukasawa e60969
      case PNG_HANDLE_CHUNK_ALWAYS:
fukasawa e60969
         discard = 0/*not handled; keep*/;
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Also store information about this chunk in the display, the relevant flag
fukasawa e60969
    * is set if the chunk is to be kept ('not handled'.)
fukasawa e60969
    */
fukasawa e60969
   if (chunk >= 0) if (!discard) /* stupidity to stop a GCC warning */
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 flag = chunk_info[chunk].flag;
fukasawa e60969
fukasawa e60969
      if (pc->location & PNG_AFTER_IDAT)
fukasawa e60969
         d->after_IDAT |= flag;
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         d->before_IDAT |= flag;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* However if there is no support to store unknown chunks don't ask libpng to
fukasawa e60969
    * do it; there will be an png_error.
fukasawa e60969
    */
fukasawa e60969
#  ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
      return discard;
fukasawa e60969
#  else
fukasawa e60969
      return 1; /*handled; discard*/
fukasawa e60969
#  endif
fukasawa e60969
}
fukasawa e60969
#endif /* READ_USER_CHUNKS_SUPPORTED */
fukasawa e60969
fukasawa e60969
#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
static png_uint_32
fukasawa e60969
get_unknown(display *d, png_infop info_ptr, int after_IDAT)
fukasawa e60969
{
fukasawa e60969
   /* Create corresponding 'unknown' flags */
fukasawa e60969
   png_uint_32 flags = 0;
fukasawa e60969
fukasawa e60969
   UNUSED(after_IDAT)
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      png_unknown_chunkp unknown;
fukasawa e60969
      int num_unknown = png_get_unknown_chunks(d->png_ptr, info_ptr, &unknown);
fukasawa e60969
fukasawa e60969
      while (--num_unknown >= 0)
fukasawa e60969
      {
fukasawa e60969
         int chunk = findb(unknown[num_unknown].name);
fukasawa e60969
fukasawa e60969
         /* Chunks not known to pngunknown must be validated here; since they
fukasawa e60969
          * must also be unknown to libpng the 'display->keep' behavior should
fukasawa e60969
          * have been used.
fukasawa e60969
          */
fukasawa e60969
         if (chunk < 0) switch (d->keep)
fukasawa e60969
         {
fukasawa e60969
            default: /* impossible */
fukasawa e60969
            case PNG_HANDLE_CHUNK_AS_DEFAULT:
fukasawa e60969
            case PNG_HANDLE_CHUNK_NEVER:
fukasawa e60969
               fprintf(stderr, "%s(%s): %s: %s: unknown chunk saved\n",
fukasawa e60969
                  d->file, d->test, d->keep ? "discard" : "default",
fukasawa e60969
                  unknown[num_unknown].name);
fukasawa e60969
               ++(d->error_count);
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_IF_SAFE:
fukasawa e60969
               if (!ancillaryb(unknown[num_unknown].name))
fukasawa e60969
               {
fukasawa e60969
                  fprintf(stderr,
fukasawa e60969
                     "%s(%s): if-safe: %s: unknown critical chunk saved\n",
fukasawa e60969
                     d->file, d->test, unknown[num_unknown].name);
fukasawa e60969
                  ++(d->error_count);
fukasawa e60969
                  break;
fukasawa e60969
               }
fukasawa e60969
               /* FALL THROUGH (safe) */
fukasawa e60969
            case PNG_HANDLE_CHUNK_ALWAYS:
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            flags |= chunk_info[chunk].flag;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return flags;
fukasawa e60969
}
fukasawa e60969
#else
fukasawa e60969
static png_uint_32
fukasawa e60969
get_unknown(display *d, png_infop info_ptr, int after_IDAT)
fukasawa e60969
   /* Otherwise this will return the cached values set by any user callback */
fukasawa e60969
{
fukasawa e60969
   UNUSED(info_ptr);
fukasawa e60969
fukasawa e60969
   if (after_IDAT)
fukasawa e60969
      return d->after_IDAT;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      return d->before_IDAT;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#  ifndef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
      /* The #defines above should mean this is never reached, it's just here as
fukasawa e60969
       * a check to ensure the logic is correct.
fukasawa e60969
       */
fukasawa e60969
#     error No store support and no user chunk support, this will not work
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check(FILE *fp, int argc, const char **argv, png_uint_32p flags/*out*/,
fukasawa e60969
   display *d, int set_callback)
fukasawa e60969
{
fukasawa e60969
   int i, npasses, ipass;
fukasawa e60969
   png_uint_32 height;
fukasawa e60969
fukasawa e60969
   d->keep = PNG_HANDLE_CHUNK_AS_DEFAULT;
fukasawa e60969
   d->before_IDAT = 0;
fukasawa e60969
   d->after_IDAT = 0;
fukasawa e60969
fukasawa e60969
   /* Some of these errors are permanently fatal and cause an exit here, others
fukasawa e60969
    * are per-test and cause an error return.
fukasawa e60969
    */
fukasawa e60969
   d->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, d, error,
fukasawa e60969
      warning);
fukasawa e60969
   if (d->png_ptr == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s(%s): could not allocate png struct\n", d->file,
fukasawa e60969
         d->test);
fukasawa e60969
      /* Terminate here, this error is not test specific. */
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   d->info_ptr = png_create_info_struct(d->png_ptr);
fukasawa e60969
   d->end_ptr = png_create_info_struct(d->png_ptr);
fukasawa e60969
   if (d->info_ptr == NULL || d->end_ptr == NULL)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s(%s): could not allocate png info\n", d->file,
fukasawa e60969
         d->test);
fukasawa e60969
      clean_display(d);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_init_io(d->png_ptr, fp);
fukasawa e60969
fukasawa e60969
#  ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
      /* This is only done if requested by the caller; it interferes with the
fukasawa e60969
       * standard store/save mechanism.
fukasawa e60969
       */
fukasawa e60969
      if (set_callback)
fukasawa e60969
         png_set_read_user_chunk_fn(d->png_ptr, d, read_callback);
fukasawa e60969
#  else
fukasawa e60969
      UNUSED(set_callback)
fukasawa e60969
#  endif
fukasawa e60969
fukasawa e60969
   /* Handle each argument in turn; multiple settings are possible for the same
fukasawa e60969
    * chunk and multiple calls will occur (the last one should override all
fukasawa e60969
    * preceding ones).
fukasawa e60969
    */
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      const char *equals = strchr(argv[i], '=');
fukasawa e60969
fukasawa e60969
      if (equals != NULL)
fukasawa e60969
      {
fukasawa e60969
         int chunk, option;
fukasawa e60969
fukasawa e60969
         if (strcmp(equals+1, "default") == 0)
fukasawa e60969
            option = PNG_HANDLE_CHUNK_AS_DEFAULT;
fukasawa e60969
         else if (strcmp(equals+1, "discard") == 0)
fukasawa e60969
            option = PNG_HANDLE_CHUNK_NEVER;
fukasawa e60969
         else if (strcmp(equals+1, "if-safe") == 0)
fukasawa e60969
            option = PNG_HANDLE_CHUNK_IF_SAFE;
fukasawa e60969
         else if (strcmp(equals+1, "save") == 0)
fukasawa e60969
            option = PNG_HANDLE_CHUNK_ALWAYS;
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "%s(%s): %s: unrecognized chunk option\n", d->file,
fukasawa e60969
               d->test, argv[i]);
fukasawa e60969
            display_exit(d);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         switch (equals - argv[i])
fukasawa e60969
         {
fukasawa e60969
            case 4: /* chunk name */
fukasawa e60969
               chunk = find(argv[i]);
fukasawa e60969
fukasawa e60969
               if (chunk >= 0)
fukasawa e60969
               {
fukasawa e60969
                  /* These #if tests have the effect of skipping the arguments
fukasawa e60969
                   * if SAVE support is unavailable - we can't do a useful test
fukasawa e60969
                   * in this case, so we just check the arguments!  This could
fukasawa e60969
                   * be improved in the future by using the read callback.
fukasawa e60969
                   */
fukasawa e60969
#                 if PNG_LIBPNG_VER >= 10700 &&\
fukasawa e60969
                     !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
fukasawa e60969
                     if (option < PNG_HANDLE_CHUNK_IF_SAFE)
fukasawa e60969
#                 endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
fukasawa e60969
                  {
fukasawa e60969
                     png_byte name[5];
fukasawa e60969
fukasawa e60969
                     memcpy(name, chunk_info[chunk].name, 5);
fukasawa e60969
                     png_set_keep_unknown_chunks(d->png_ptr, option, name, 1);
fukasawa e60969
                     chunk_info[chunk].keep = option;
fukasawa e60969
                  }
fukasawa e60969
                  continue;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 7: /* default */
fukasawa e60969
               if (memcmp(argv[i], "default", 7) == 0)
fukasawa e60969
               {
fukasawa e60969
#                 if PNG_LIBPNG_VER >= 10700 &&\
fukasawa e60969
                     !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
fukasawa e60969
                     if (option < PNG_HANDLE_CHUNK_IF_SAFE)
fukasawa e60969
#                 endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
fukasawa e60969
                     png_set_keep_unknown_chunks(d->png_ptr, option, NULL, 0);
fukasawa e60969
fukasawa e60969
                  d->keep = option;
fukasawa e60969
                  continue;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case 3: /* all */
fukasawa e60969
               if (memcmp(argv[i], "all", 3) == 0)
fukasawa e60969
               {
fukasawa e60969
#                 if PNG_LIBPNG_VER >= 10700 &&\
fukasawa e60969
                     !defined(PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED)
fukasawa e60969
                     if (option < PNG_HANDLE_CHUNK_IF_SAFE)
fukasawa e60969
#                 endif /* 1.7+ SAVE_UNKNOWN_CHUNKS */
fukasawa e60969
                     png_set_keep_unknown_chunks(d->png_ptr, option, NULL, -1);
fukasawa e60969
fukasawa e60969
                  d->keep = option;
fukasawa e60969
fukasawa e60969
                  for (chunk = 0; chunk < NINFO; ++chunk)
fukasawa e60969
                     if (chunk_info[chunk].all)
fukasawa e60969
                        chunk_info[chunk].keep = option;
fukasawa e60969
                  continue;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default: /* some misplaced = */
fukasawa e60969
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "%s(%s): %s: unrecognized chunk argument\n", d->file,
fukasawa e60969
         d->test, argv[i]);
fukasawa e60969
      display_exit(d);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_read_info(d->png_ptr, d->info_ptr);
fukasawa e60969
fukasawa e60969
   switch (png_get_interlace_type(d->png_ptr, d->info_ptr))
fukasawa e60969
   {
fukasawa e60969
      case PNG_INTERLACE_NONE:
fukasawa e60969
         npasses = 1;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      case PNG_INTERLACE_ADAM7:
fukasawa e60969
         npasses = PNG_INTERLACE_ADAM7_PASSES;
fukasawa e60969
         break;
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
         /* Hard error because it is not test specific */
fukasawa e60969
         fprintf(stderr, "%s(%s): invalid interlace type\n", d->file, d->test);
fukasawa e60969
         clean_display(d);
fukasawa e60969
         exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Skip the image data, if IDAT is not being handled then don't do this
fukasawa e60969
    * because it will cause a CRC error.
fukasawa e60969
    */
fukasawa e60969
   if (chunk_info[0/*IDAT*/].keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
fukasawa e60969
   {
fukasawa e60969
      png_start_read_image(d->png_ptr);
fukasawa e60969
      height = png_get_image_height(d->png_ptr, d->info_ptr);
fukasawa e60969
fukasawa e60969
      if (npasses > 1)
fukasawa e60969
      {
fukasawa e60969
         png_uint_32 width = png_get_image_width(d->png_ptr, d->info_ptr);
fukasawa e60969
fukasawa e60969
         for (ipass=0; ipass
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 wPass = PNG_PASS_COLS(width, ipass);
fukasawa e60969
fukasawa e60969
            if (wPass > 0)
fukasawa e60969
            {
fukasawa e60969
               png_uint_32 y;
fukasawa e60969
fukasawa e60969
               for (y=0; y
fukasawa e60969
                  png_read_row(d->png_ptr, NULL, NULL);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
      } /* interlaced */
fukasawa e60969
fukasawa e60969
      else /* not interlaced */
fukasawa e60969
      {
fukasawa e60969
         png_uint_32 y;
fukasawa e60969
fukasawa e60969
         for (y=0; y
fukasawa e60969
            png_read_row(d->png_ptr, NULL, NULL);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_read_end(d->png_ptr, d->end_ptr);
fukasawa e60969
fukasawa e60969
   flags[0] = get_valid(d, d->info_ptr);
fukasawa e60969
   flags[1] = get_unknown(d, d->info_ptr, 0/*before IDAT*/);
fukasawa e60969
fukasawa e60969
   /* Only png_read_png sets PNG_INFO_IDAT! */
fukasawa e60969
   flags[chunk_info[0/*IDAT*/].keep != PNG_HANDLE_CHUNK_AS_DEFAULT] |=
fukasawa e60969
      PNG_INFO_IDAT;
fukasawa e60969
fukasawa e60969
   flags[2] = get_valid(d, d->end_ptr);
fukasawa e60969
   flags[3] = get_unknown(d, d->end_ptr, 1/*after IDAT*/);
fukasawa e60969
fukasawa e60969
   clean_display(d);
fukasawa e60969
fukasawa e60969
   return d->keep;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
check_error(display *d, png_uint_32 flags, const char *message)
fukasawa e60969
{
fukasawa e60969
   while (flags)
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 flag = flags & -(png_int_32)flags;
fukasawa e60969
      int i = find_by_flag(flag);
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "%s(%s): chunk %s: %s\n", d->file, d->test,
fukasawa e60969
         chunk_info[i].name, message);
fukasawa e60969
      ++(d->error_count);
fukasawa e60969
fukasawa e60969
      flags &= ~flag;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
check_handling(display *d, int def, png_uint_32 chunks, png_uint_32 known,
fukasawa e60969
   png_uint_32 unknown, const char *position, int set_callback)
fukasawa e60969
{
fukasawa e60969
   while (chunks)
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 flag = chunks & -(png_int_32)chunks;
fukasawa e60969
      int i = find_by_flag(flag);
fukasawa e60969
      int keep = chunk_info[i].keep;
fukasawa e60969
      const char *type;
fukasawa e60969
      const char *errorx = NULL;
fukasawa e60969
fukasawa e60969
      if (chunk_info[i].unknown)
fukasawa e60969
      {
fukasawa e60969
         if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
fukasawa e60969
         {
fukasawa e60969
            type = "UNKNOWN (default)";
fukasawa e60969
            keep = def;
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            type = "UNKNOWN (specified)";
fukasawa e60969
fukasawa e60969
         if (flag & known)
fukasawa e60969
            errorx = "chunk processed";
fukasawa e60969
fukasawa e60969
         else switch (keep)
fukasawa e60969
         {
fukasawa e60969
            case PNG_HANDLE_CHUNK_AS_DEFAULT:
fukasawa e60969
               if (flag & unknown)
fukasawa e60969
                  errorx = "DEFAULT: unknown chunk saved";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_NEVER:
fukasawa e60969
               if (flag & unknown)
fukasawa e60969
                  errorx = "DISCARD: unknown chunk saved";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_IF_SAFE:
fukasawa e60969
               if (ancillary(chunk_info[i].name))
fukasawa e60969
               {
fukasawa e60969
                  if (!(flag & unknown))
fukasawa e60969
                     errorx = "IF-SAFE: unknown ancillary chunk lost";
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else if (flag & unknown)
fukasawa e60969
                  errorx = "IF-SAFE: unknown critical chunk saved";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_ALWAYS:
fukasawa e60969
               if (!(flag & unknown))
fukasawa e60969
                  errorx = "SAVE: unknown chunk lost";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               errorx = "internal error: bad keep";
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
      } /* unknown chunk */
fukasawa e60969
fukasawa e60969
      else /* known chunk */
fukasawa e60969
      {
fukasawa e60969
         type = "KNOWN";
fukasawa e60969
fukasawa e60969
         if (flag & known)
fukasawa e60969
         {
fukasawa e60969
            /* chunk was processed, it won't have been saved because that is
fukasawa e60969
             * caught below when checking for inconsistent processing.
fukasawa e60969
             */
fukasawa e60969
            if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
fukasawa e60969
               errorx = "!DEFAULT: known chunk processed";
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else /* not processed */ switch (keep)
fukasawa e60969
         {
fukasawa e60969
            case PNG_HANDLE_CHUNK_AS_DEFAULT:
fukasawa e60969
               errorx = "DEFAULT: known chunk not processed";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_NEVER:
fukasawa e60969
               if (flag & unknown)
fukasawa e60969
                  errorx = "DISCARD: known chunk saved";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_IF_SAFE:
fukasawa e60969
               if (ancillary(chunk_info[i].name))
fukasawa e60969
               {
fukasawa e60969
                  if (!(flag & unknown))
fukasawa e60969
                     errorx = "IF-SAFE: known ancillary chunk lost";
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               else if (flag & unknown)
fukasawa e60969
                  errorx = "IF-SAFE: known critical chunk saved";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            case PNG_HANDLE_CHUNK_ALWAYS:
fukasawa e60969
               if (!(flag & unknown))
fukasawa e60969
                  errorx = "SAVE: known chunk lost";
fukasawa e60969
               break;
fukasawa e60969
fukasawa e60969
            default:
fukasawa e60969
               errorx = "internal error: bad keep (2)";
fukasawa e60969
               break;
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      if (errorx != NULL)
fukasawa e60969
      {
fukasawa e60969
         ++(d->error_count);
fukasawa e60969
         fprintf(stderr, "%s(%s%s): %s %s %s: %s\n", d->file, d->test,
fukasawa e60969
            set_callback ? ",callback" : "",
fukasawa e60969
            type, chunk_info[i].name, position, errorx);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      chunks &= ~flag;
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
perform_one_test(FILE *fp, int argc, const char **argv,
fukasawa e60969
   png_uint_32 *default_flags, display *d, int set_callback)
fukasawa e60969
{
fukasawa e60969
   int def;
fukasawa e60969
   png_uint_32 flags[2][4];
fukasawa e60969
fukasawa e60969
   rewind(fp);
fukasawa e60969
   clear_keep();
fukasawa e60969
   memcpy(flags[0], default_flags, sizeof flags[0]);
fukasawa e60969
fukasawa e60969
   def = check(fp, argc, argv, flags[1], d, set_callback);
fukasawa e60969
fukasawa e60969
   /* Chunks should either be known or unknown, never both and this should apply
fukasawa e60969
    * whether the chunk is before or after the IDAT (actually, the app can
fukasawa e60969
    * probably change this by swapping the handling after the image, but this
fukasawa e60969
    * test does not do that.)
fukasawa e60969
    */
fukasawa e60969
   check_error(d, (flags[0][0]|flags[0][2]) & (flags[0][1]|flags[0][3]),
fukasawa e60969
      "chunk handled inconsistently in count tests");
fukasawa e60969
   check_error(d, (flags[1][0]|flags[1][2]) & (flags[1][1]|flags[1][3]),
fukasawa e60969
      "chunk handled inconsistently in option tests");
fukasawa e60969
fukasawa e60969
   /* Now find out what happened to each chunk before and after the IDAT and
fukasawa e60969
    * determine if the behavior was correct.  First some basic sanity checks,
fukasawa e60969
    * any known chunk should be known in the original count, any unknown chunk
fukasawa e60969
    * should be either known or unknown in the original.
fukasawa e60969
    */
fukasawa e60969
   {
fukasawa e60969
      png_uint_32 test;
fukasawa e60969
fukasawa e60969
      test = flags[1][0] & ~flags[0][0];
fukasawa e60969
      check_error(d, test, "new known chunk before IDAT");
fukasawa e60969
      test = flags[1][1] & ~(flags[0][0] | flags[0][1]);
fukasawa e60969
      check_error(d, test, "new unknown chunk before IDAT");
fukasawa e60969
      test = flags[1][2] & ~flags[0][2];
fukasawa e60969
      check_error(d, test, "new known chunk after IDAT");
fukasawa e60969
      test = flags[1][3] & ~(flags[0][2] | flags[0][3]);
fukasawa e60969
      check_error(d, test, "new unknown chunk after IDAT");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Now each chunk in the original list should have been handled according to
fukasawa e60969
    * the options set for that chunk, regardless of whether libpng knows about
fukasawa e60969
    * it or not.
fukasawa e60969
    */
fukasawa e60969
   check_handling(d, def, flags[0][0] | flags[0][1], flags[1][0], flags[1][1],
fukasawa e60969
      "before IDAT", set_callback);
fukasawa e60969
   check_handling(d, def, flags[0][2] | flags[0][3], flags[1][2], flags[1][3],
fukasawa e60969
      "after IDAT", set_callback);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
perform_one_test_safe(FILE *fp, int argc, const char **argv,
fukasawa e60969
   png_uint_32 *default_flags, display *d, const char *test)
fukasawa e60969
{
fukasawa e60969
   if (setjmp(d->error_return) == 0)
fukasawa e60969
   {
fukasawa e60969
      d->test = test; /* allow use of d->error_return */
fukasawa e60969
#     ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
         perform_one_test(fp, argc, argv, default_flags, d, 0);
fukasawa e60969
#     endif
fukasawa e60969
#     ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
         perform_one_test(fp, argc, argv, default_flags, d, 1);
fukasawa e60969
#     endif
fukasawa e60969
      d->test = init; /* prevent use of d->error_return */
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static const char *standard_tests[] =
fukasawa e60969
{
fukasawa e60969
 "discard", "default=discard", 0,
fukasawa e60969
 "save", "default=save", 0,
fukasawa e60969
 "if-safe", "default=if-safe", 0,
fukasawa e60969
 "vpAg", "vpAg=if-safe", 0,
fukasawa e60969
 "sTER", "sTER=if-safe", 0,
fukasawa e60969
 "IDAT", "default=discard", "IDAT=save", 0,
fukasawa e60969
 "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
fukasawa e60969
   "sBIT=save", "sRGB=save", 0,
fukasawa e60969
 0/*end*/
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static PNG_NORETURN void
fukasawa e60969
usage(const char *program, const char *reason)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr, "pngunknown: %s: usage:\n %s [--strict] "
fukasawa e60969
      "--default|{(CHNK|default|all)=(default|discard|if-safe|save)} "
fukasawa e60969
      "testfile.png\n", reason, program);
fukasawa e60969
   exit(99);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(int argc, const char **argv)
fukasawa e60969
{
fukasawa e60969
   FILE *fp;
fukasawa e60969
   png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
fukasawa e60969
   int strict = 0, default_tests = 0;
fukasawa e60969
   const char *count_argv = "default=save";
fukasawa e60969
   const char *touch_file = NULL;
fukasawa e60969
   display d;
fukasawa e60969
fukasawa e60969
   init_display(&d, argv[0]);
fukasawa e60969
fukasawa e60969
   while (++argv, --argc > 0)
fukasawa e60969
   {
fukasawa e60969
      if (strcmp(*argv, "--strict") == 0)
fukasawa e60969
         strict = 1;
fukasawa e60969
fukasawa e60969
      else if (strcmp(*argv, "--default") == 0)
fukasawa e60969
         default_tests = 1;
fukasawa e60969
fukasawa e60969
      else if (strcmp(*argv, "--touch") == 0)
fukasawa e60969
      {
fukasawa e60969
         if (argc > 1)
fukasawa e60969
            touch_file = *++argv, --argc;
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            usage(d.program, "--touch: missing file name");
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         break;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* A file name is required, but there should be no other arguments if
fukasawa e60969
    * --default was specified.
fukasawa e60969
    */
fukasawa e60969
   if (argc <= 0)
fukasawa e60969
      usage(d.program, "missing test file");
fukasawa e60969
fukasawa e60969
   /* GCC BUG: if (default_tests && argc != 1) triggers some weird GCC argc
fukasawa e60969
    * optimization which causes warnings with -Wstrict-overflow!
fukasawa e60969
    */
fukasawa e60969
   else if (default_tests) if (argc != 1)
fukasawa e60969
      usage(d.program, "extra arguments");
fukasawa e60969
fukasawa e60969
   /* The name of the test file is the last argument; remove it. */
fukasawa e60969
   d.file = argv[--argc];
fukasawa e60969
fukasawa e60969
   fp = fopen(d.file, "rb");
fukasawa e60969
   if (fp == NULL)
fukasawa e60969
   {
fukasawa e60969
      perror(d.file);
fukasawa e60969
      exit(99);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* First find all the chunks, known and unknown, in the test file, a failure
fukasawa e60969
    * here aborts the whole test.
fukasawa e60969
    *
fukasawa e60969
    * If 'save' is supported then the normal saving method should happen,
fukasawa e60969
    * otherwise if 'read' is supported then the read callback will do the
fukasawa e60969
    * same thing.  If both are supported the 'read' callback won't be
fukasawa e60969
    * instantiated by default.  If 'save' is *not* supported then a user
fukasawa e60969
    * callback is required even though we can call png_get_unknown_chunks.
fukasawa e60969
    */
fukasawa e60969
   if (check(fp, 1, &count_argv, default_flags, &d,
fukasawa e60969
#     ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
         0
fukasawa e60969
#     else
fukasawa e60969
         1
fukasawa e60969
#     endif
fukasawa e60969
      ) != PNG_HANDLE_CHUNK_ALWAYS)
fukasawa e60969
   {
fukasawa e60969
      fprintf(stderr, "%s: %s: internal error\n", d.program, d.file);
fukasawa e60969
      exit(99);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Now find what the various supplied options cause to change: */
fukasawa e60969
   if (!default_tests)
fukasawa e60969
   {
fukasawa e60969
      d.test = cmd; /* acts as a flag to say exit, do not longjmp */
fukasawa e60969
#     ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
fukasawa e60969
         perform_one_test(fp, argc, argv, default_flags, &d, 0);
fukasawa e60969
#     endif
fukasawa e60969
#     ifdef PNG_READ_USER_CHUNKS_SUPPORTED
fukasawa e60969
         perform_one_test(fp, argc, argv, default_flags, &d, 1);
fukasawa e60969
#     endif
fukasawa e60969
      d.test = init;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      const char **test = standard_tests;
fukasawa e60969
fukasawa e60969
      /* Set the exit_test pointer here so we can continue after a libpng error.
fukasawa e60969
       * NOTE: this leaks memory because the png_struct data from the failing
fukasawa e60969
       * test is never freed.
fukasawa e60969
       */
fukasawa e60969
      while (*test)
fukasawa e60969
      {
fukasawa e60969
         const char *this_test = *test++;
fukasawa e60969
         const char **next = test;
fukasawa e60969
         int count = display_rc(&d, strict), new_count;
fukasawa e60969
         const char *result;
fukasawa e60969
         int arg_count = 0;
fukasawa e60969
fukasawa e60969
         while (*next) ++next, ++arg_count;
fukasawa e60969
fukasawa e60969
         perform_one_test_safe(fp, arg_count, test, default_flags, &d,
fukasawa e60969
            this_test);
fukasawa e60969
fukasawa e60969
         new_count = display_rc(&d, strict);
fukasawa e60969
fukasawa e60969
         if (new_count == count)
fukasawa e60969
            result = "PASS";
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            result = "FAIL";
fukasawa e60969
fukasawa e60969
         printf("%s: %s %s\n", result, d.program, this_test);
fukasawa e60969
fukasawa e60969
         test = next+1;
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   fclose(fp);
fukasawa e60969
fukasawa e60969
   if (display_rc(&d, strict) == 0)
fukasawa e60969
   {
fukasawa e60969
      /* Success, touch the success file if appropriate */
fukasawa e60969
      if (touch_file != NULL)
fukasawa e60969
      {
fukasawa e60969
         FILE *fsuccess = fopen(touch_file, "wt");
fukasawa e60969
fukasawa e60969
         if (fsuccess != NULL)
fukasawa e60969
         {
fukasawa e60969
            int err = 0;
fukasawa e60969
            fprintf(fsuccess, "PNG unknown tests succeeded\n");
fukasawa e60969
            fflush(fsuccess);
fukasawa e60969
            err = ferror(fsuccess);
fukasawa e60969
fukasawa e60969
            if (fclose(fsuccess) || err)
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr, "%s: write failed\n", touch_file);
fukasawa e60969
               exit(99);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
         {
fukasawa e60969
            fprintf(stderr, "%s: open failed\n", touch_file);
fukasawa e60969
            exit(99);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
#else /* !(READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS) */
fukasawa e60969
int
fukasawa e60969
main(void)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr,
fukasawa e60969
      " test ignored: no support to find out about unknown chunks\n");
fukasawa e60969
   /* So the test is skipped: */
fukasawa e60969
   return 77;
fukasawa e60969
}
fukasawa e60969
#endif /* READ_USER_CHUNKS || SAVE_UNKNOWN_CHUNKS */
fukasawa e60969
fukasawa e60969
#else /* !(SET_UNKNOWN_CHUNKS && READ) */
fukasawa e60969
int
fukasawa e60969
main(void)
fukasawa e60969
{
fukasawa e60969
   fprintf(stderr,
fukasawa e60969
      " test ignored: no support to modify unknown chunk handling\n");
fukasawa e60969
   /* So the test is skipped: */
fukasawa e60969
   return 77;
fukasawa e60969
}
fukasawa e60969
#endif /* SET_UNKNOWN_CHUNKS && READ*/