CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libpng17/pngtest.c
Views: 1401
1/* pngtest.c - a simple test program to test libpng2*3* Last changed in libpng 1.6.26 [(PENDING RELEASE)]4* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12* This program reads in a PNG image, writes it out again, and then13* compares the two files. If the files are identical, this shows that14* the basic chunk handling, filtering, and (de)compression code is working15* properly. It does not currently test all of the transforms, although16* it probably should.17*18* The program will report "FAIL" in certain legitimate cases:19* 1) when the compression level or filter selection method is changed.20* 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.21* 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks22* exist in the input file.23* 4) others not listed here...24* In these cases, it is best to check with another tool such as "pngcheck"25* to see what the differences between the two files are.26*27* If a filename is given on the command-line, then this file is used28* for the input, rather than the default "pngtest.png". This allows29* testing a wide variety of files easily. You can also test a number30* of files at once by typing "pngtest -m file1.png file2.png ..."31*/3233#define _POSIX_SOURCE 13435#include <stdio.h>36#include <stdlib.h>37#include <string.h>3839/* Defined so I can write to a file on gui/windowing platforms */40/* #define STDERR stderr */41#define STDERR stdout /* For DOS */4243#include "png.h"4445/* Known chunks that exist in pngtest.png must be supported or pngtest will fail46* simply as a result of re-ordering them. This may be fixed in 1.747*48* pngtest allocates a single row buffer for each row and overwrites it,49* therefore if the write side doesn't support the writing of interlaced images50* nothing can be done for an interlaced image (and the code below will fail51* horribly trying to write extra data after writing garbage).52*/53#if defined PNG_READ_SUPPORTED && /* else nothing can be done */\54defined PNG_READ_bKGD_SUPPORTED &&\55defined PNG_READ_cHRM_SUPPORTED &&\56defined PNG_READ_gAMA_SUPPORTED &&\57defined PNG_READ_oFFs_SUPPORTED &&\58defined PNG_READ_pCAL_SUPPORTED &&\59defined PNG_READ_pHYs_SUPPORTED &&\60defined PNG_READ_sBIT_SUPPORTED &&\61defined PNG_READ_sCAL_SUPPORTED &&\62defined PNG_READ_sRGB_SUPPORTED &&\63defined PNG_READ_sPLT_SUPPORTED &&\64defined PNG_READ_tEXt_SUPPORTED &&\65defined PNG_READ_tIME_SUPPORTED &&\66defined PNG_READ_zTXt_SUPPORTED &&\67(defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)6869#ifdef PNG_ZLIB_HEADER70# include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */71#else72# include "zlib.h"73#endif7475/* Copied from pngpriv.h but only used in error messages below. */76#ifndef PNG_ZBUF_SIZE77# define PNG_ZBUF_SIZE 819278#endif79#define FCLOSE(file) fclose(file)8081#ifndef PNG_STDIO_SUPPORTED82typedef FILE * png_FILE_p;83#endif8485/* Makes pngtest verbose so we can find problems. */86#ifndef PNG_DEBUG87# define PNG_DEBUG 088#endif8990#if PNG_DEBUG > 191# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))92# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))93# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))94#else95# define pngtest_debug(m) ((void)0)96# define pngtest_debug1(m,p1) ((void)0)97# define pngtest_debug2(m,p1,p2) ((void)0)98#endif99100#if !PNG_DEBUG101# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */102#endif103104#ifndef PNG_UNUSED105# define PNG_UNUSED(param) (void)param;106#endif107108/* Turn on CPU timing109#define PNGTEST_TIMING110*/111112#ifndef PNG_FLOATING_POINT_SUPPORTED113#undef PNGTEST_TIMING114#endif115116#ifdef PNGTEST_TIMING117static float t_start, t_stop, t_decode, t_encode, t_misc;118#include <time.h>119#endif120121#ifdef PNG_TIME_RFC1123_SUPPORTED122#define PNG_tIME_STRING_LENGTH 29123static int tIME_chunk_present = 0;124static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";125126#if PNG_LIBPNG_VER < 10619127#define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)128129static int130tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)131{132png_const_charp str = png_convert_to_rfc1123(png_ptr, t);133134if (str == NULL)135return 0;136137strcpy(ts, str);138return 1;139}140#endif /* older libpng */141#endif142143static int verbose = 0;144static int strict = 0;145static int relaxed = 0;146static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */147static int error_count = 0; /* count calls to png_error */148static int warning_count = 0; /* count calls to png_warning */149150/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */151#ifndef png_jmpbuf152# define png_jmpbuf(png_ptr) png_ptr->jmpbuf153#endif154155/* Defines for unknown chunk handling if required. */156#ifndef PNG_HANDLE_CHUNK_ALWAYS157# define PNG_HANDLE_CHUNK_ALWAYS 3158#endif159#ifndef PNG_HANDLE_CHUNK_IF_SAFE160# define PNG_HANDLE_CHUNK_IF_SAFE 2161#endif162163/* Utility to save typing/errors, the argument must be a name */164#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))165166/* Example of using row callbacks to make a simple progress meter */167static int status_pass = 1;168static int status_dots_requested = 0;169static int status_dots = 1;170171static void PNGCBAPI172read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)173{174if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)175return;176177if (status_pass != pass)178{179fprintf(stdout, "\n Pass %d: ", pass);180status_pass = pass;181status_dots = 31;182}183184status_dots--;185186if (status_dots == 0)187{188fprintf(stdout, "\n ");189status_dots=30;190}191192fprintf(stdout, "r");193}194195#ifdef PNG_WRITE_SUPPORTED196static void PNGCBAPI197write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)198{199if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)200return;201202fprintf(stdout, "w");203}204#endif205206207#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED208/* Example of using a user transform callback (doesn't do anything at present).209*/210static void PNGCBAPI211read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)212{213PNG_UNUSED(png_ptr)214PNG_UNUSED(row_info)215PNG_UNUSED(data)216}217#endif218219#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED220/* Example of using user transform callback (we don't transform anything,221* but merely count the zero samples)222*/223224static png_uint_32 zero_samples;225226static void PNGCBAPI227count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)228{229png_bytep dp = data;230if (png_ptr == NULL)231return;232233/* Contents of row_info:234* png_uint_32 width width of row235* png_uint_32 rowbytes number of bytes in row236* png_byte color_type color type of pixels237* png_byte bit_depth bit depth of samples238* png_byte channels number of channels (1-4)239* png_byte pixel_depth bits per pixel (depth*channels)240*/241242/* Counts the number of zero samples (or zero pixels if color_type is 3 */243244if (row_info->color_type == 0 || row_info->color_type == 3)245{246int pos = 0;247png_uint_32 n, nstop;248249for (n = 0, nstop=row_info->width; n<nstop; n++)250{251if (row_info->bit_depth == 1)252{253if (((*dp << pos++ ) & 0x80) == 0)254zero_samples++;255256if (pos == 8)257{258pos = 0;259dp++;260}261}262263if (row_info->bit_depth == 2)264{265if (((*dp << (pos+=2)) & 0xc0) == 0)266zero_samples++;267268if (pos == 8)269{270pos = 0;271dp++;272}273}274275if (row_info->bit_depth == 4)276{277if (((*dp << (pos+=4)) & 0xf0) == 0)278zero_samples++;279280if (pos == 8)281{282pos = 0;283dp++;284}285}286287if (row_info->bit_depth == 8)288if (*dp++ == 0)289zero_samples++;290291if (row_info->bit_depth == 16)292{293if ((*dp | *(dp+1)) == 0)294zero_samples++;295dp+=2;296}297}298}299else /* Other color types */300{301png_uint_32 n, nstop;302int channel;303int color_channels = row_info->channels;304if (row_info->color_type > 3)305color_channels--;306307for (n = 0, nstop=row_info->width; n<nstop; n++)308{309for (channel = 0; channel < color_channels; channel++)310{311if (row_info->bit_depth == 8)312if (*dp++ == 0)313zero_samples++;314315if (row_info->bit_depth == 16)316{317if ((*dp | *(dp+1)) == 0)318zero_samples++;319320dp+=2;321}322}323if (row_info->color_type > 3)324{325dp++;326if (row_info->bit_depth == 16)327dp++;328}329}330}331}332#endif /* WRITE_USER_TRANSFORM */333334#ifndef PNG_STDIO_SUPPORTED335/* START of code to validate stdio-free compilation */336/* These copies of the default read/write functions come from pngrio.c and337* pngwio.c. They allow "don't include stdio" testing of the library.338* This is the function that does the actual reading of data. If you are339* not reading from a standard C stream, you should create a replacement340* read_data function and use it at run time with png_set_read_fn(), rather341* than changing the library.342*/343344#ifdef PNG_IO_STATE_SUPPORTED345void346pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,347png_uint_32 io_op);348void349pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,350png_uint_32 io_op)351{352png_uint_32 io_state = png_get_io_state(png_ptr);353int err = 0;354355/* Check if the current operation (reading / writing) is as expected. */356if ((io_state & PNG_IO_MASK_OP) != io_op)357png_error(png_ptr, "Incorrect operation in I/O state");358359/* Check if the buffer size specific to the current location360* (file signature / header / data / crc) is as expected.361*/362switch (io_state & PNG_IO_MASK_LOC)363{364case PNG_IO_SIGNATURE:365if (data_length > 8)366err = 1;367break;368case PNG_IO_CHUNK_HDR:369if (data_length != 8)370err = 1;371break;372case PNG_IO_CHUNK_DATA:373break; /* no restrictions here */374case PNG_IO_CHUNK_CRC:375if (data_length != 4)376err = 1;377break;378default:379err = 1; /* uninitialized */380}381if (err != 0)382png_error(png_ptr, "Bad I/O state or buffer size");383}384#endif385386static void PNGCBAPI387pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)388{389png_size_t check = 0;390png_voidp io_ptr;391392/* fread() returns 0 on error, so it is OK to store this in a png_size_t393* instead of an int, which is what fread() actually returns.394*/395io_ptr = png_get_io_ptr(png_ptr);396if (io_ptr != NULL)397{398check = fread(data, 1, length, (png_FILE_p)io_ptr);399}400401if (check != length)402{403png_error(png_ptr, "Read Error");404}405406#ifdef PNG_IO_STATE_SUPPORTED407pngtest_check_io_state(png_ptr, length, PNG_IO_READING);408#endif409}410411#ifdef PNG_WRITE_FLUSH_SUPPORTED412static void PNGCBAPI413pngtest_flush(png_structp png_ptr)414{415/* Do nothing; fflush() is said to be just a waste of energy. */416PNG_UNUSED(png_ptr) /* Stifle compiler warning */417}418#endif419420/* This is the function that does the actual writing of data. If you are421* not writing to a standard C stream, you should create a replacement422* write_data function and use it at run time with png_set_write_fn(), rather423* than changing the library.424*/425static void PNGCBAPI426pngtest_write_data(png_structp png_ptr, png_bytep data, png_size_t length)427{428png_size_t check;429430check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));431432if (check != length)433{434png_error(png_ptr, "Write Error");435}436437#ifdef PNG_IO_STATE_SUPPORTED438pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);439#endif440}441#endif /* !STDIO */442443/* This function is called when there is a warning, but the library thinks444* it can continue anyway. Replacement functions don't have to do anything445* here if you don't want to. In the default configuration, png_ptr is446* not used, but it is passed in case it may be useful.447*/448typedef struct449{450PNG_CONST char *file_name;451} pngtest_error_parameters;452453static void PNGCBAPI454pngtest_warning(png_structp png_ptr, png_const_charp message)455{456PNG_CONST char *name = "UNKNOWN (ERROR!)";457pngtest_error_parameters *test =458(pngtest_error_parameters*)png_get_error_ptr(png_ptr);459460++warning_count;461462if (test != NULL && test->file_name != NULL)463name = test->file_name;464465fprintf(STDERR, "%s: libpng warning: %s\n", name, message);466}467468/* This is the default error handling function. Note that replacements for469* this function MUST NOT RETURN, or the program will likely crash. This470* function is used by default, or if the program supplies NULL for the471* error function pointer in png_set_error_fn().472*/473static void PNGCBAPI474pngtest_error(png_structp png_ptr, png_const_charp message)475{476++error_count;477478pngtest_warning(png_ptr, message);479/* We can return because png_error calls the default handler, which is480* actually OK in this case.481*/482}483484/* END of code to validate stdio-free compilation */485486/* START of code to validate memory allocation and deallocation */487#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG488489/* Allocate memory. For reasonable files, size should never exceed490* 64K. However, zlib may allocate more than 64K if you don't tell491* it not to. See zconf.h and png.h for more information. zlib does492* need to allocate exactly 64K, so whatever you call here must493* have the ability to do that.494*495* This piece of code can be compiled to validate max 64K allocations496* by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.497*/498typedef struct memory_information499{500png_alloc_size_t size;501png_voidp pointer;502struct memory_information *next;503} memory_information;504typedef memory_information *memory_infop;505506static memory_infop pinformation = NULL;507static int current_allocation = 0;508static int maximum_allocation = 0;509static int total_allocation = 0;510static int num_allocations = 0;511512png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,513png_alloc_size_t size));514void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));515516png_voidp517PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)518{519520/* png_malloc has already tested for NULL; png_create_struct calls521* png_debug_malloc directly, with png_ptr == NULL which is OK522*/523524if (size == 0)525return (NULL);526527/* This calls the library allocator twice, once to get the requested528buffer and once to get a new free list entry. */529{530/* Disable malloc_fn and free_fn */531memory_infop pinfo;532png_set_mem_fn(png_ptr, NULL, NULL, NULL);533pinfo = (memory_infop)png_malloc(png_ptr,534(sizeof *pinfo));535pinfo->size = size;536current_allocation += size;537total_allocation += size;538num_allocations ++;539540if (current_allocation > maximum_allocation)541maximum_allocation = current_allocation;542543pinfo->pointer = png_malloc(png_ptr, size);544/* Restore malloc_fn and free_fn */545546png_set_mem_fn(png_ptr,547NULL, png_debug_malloc, png_debug_free);548549if (size != 0 && pinfo->pointer == NULL)550{551current_allocation -= size;552total_allocation -= size;553png_error(png_ptr,554"out of memory in pngtest->png_debug_malloc");555}556557pinfo->next = pinformation;558pinformation = pinfo;559/* Make sure the caller isn't assuming zeroed memory. */560memset(pinfo->pointer, 0xdd, pinfo->size);561562if (verbose != 0)563printf("png_malloc %lu bytes at %p\n", (unsigned long)size,564pinfo->pointer);565566return (png_voidp)(pinfo->pointer);567}568}569570/* Free a pointer. It is removed from the list at the same time. */571void PNGCBAPI572png_debug_free(png_structp png_ptr, png_voidp ptr)573{574if (png_ptr == NULL)575fprintf(STDERR, "NULL pointer to png_debug_free.\n");576577if (ptr == 0)578{579#if 0 /* This happens all the time. */580fprintf(STDERR, "WARNING: freeing NULL pointer\n");581#endif582return;583}584585/* Unlink the element from the list. */586if (pinformation != NULL)587{588memory_infop *ppinfo = &pinformation;589590for (;;)591{592memory_infop pinfo = *ppinfo;593594if (pinfo->pointer == ptr)595{596*ppinfo = pinfo->next;597current_allocation -= pinfo->size;598if (current_allocation < 0)599fprintf(STDERR, "Duplicate free of memory\n");600/* We must free the list element too, but first kill601the memory that is to be freed. */602memset(ptr, 0x55, pinfo->size);603free(pinfo);604pinfo = NULL;605break;606}607608if (pinfo->next == NULL)609{610fprintf(STDERR, "Pointer %p not found\n", ptr);611break;612}613614ppinfo = &pinfo->next;615}616}617618/* Finally free the data. */619if (verbose != 0)620printf("Freeing %p\n", ptr);621622if (ptr != NULL)623free(ptr);624ptr = NULL;625}626#endif /* USER_MEM && DEBUG */627/* END of code to test memory allocation/deallocation */628629630#ifdef PNG_READ_USER_CHUNKS_SUPPORTED631/* Demonstration of user chunk support of the sTER and vpAg chunks */632633/* (sTER is a public chunk not yet known by libpng. vpAg is a private634chunk used in ImageMagick to store "virtual page" size). */635636static struct user_chunk_data637{638png_const_infop info_ptr;639png_uint_32 vpAg_width, vpAg_height;640png_byte vpAg_units;641png_byte sTER_mode;642int location[2];643}644user_chunk_data;645646/* Used for location and order; zero means nothing. */647#define have_sTER 0x01648#define have_vpAg 0x02649#define before_PLTE 0x10650#define before_IDAT 0x20651#define after_IDAT 0x40652653static void654init_callback_info(png_const_infop info_ptr)655{656MEMZERO(user_chunk_data);657user_chunk_data.info_ptr = info_ptr;658}659660static int661set_location(png_structp png_ptr, struct user_chunk_data *data, int what)662{663int location;664665if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)666return 0; /* already have one of these */667668/* Find where we are (the code below zeroes info_ptr to indicate that the669* chunks before the first IDAT have been read.)670*/671if (data->info_ptr == NULL) /* after IDAT */672location = what | after_IDAT;673674else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)675location = what | before_IDAT;676677else678location = what | before_PLTE;679680if (data->location[0] == 0)681data->location[0] = location;682683else684data->location[1] = location;685686return 1; /* handled */687}688689static int PNGCBAPI690read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)691{692struct user_chunk_data *my_user_chunk_data =693(struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);694695if (my_user_chunk_data == NULL)696png_error(png_ptr, "lost user chunk pointer");697698/* Return one of the following:699* return (-n); chunk had an error700* return (0); did not recognize701* return (n); success702*703* The unknown chunk structure contains the chunk data:704* png_byte name[5];705* png_byte *data;706* png_size_t size;707*708* Note that libpng has already taken care of the CRC handling.709*/710711if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */712chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */713{714/* Found sTER chunk */715if (chunk->size != 1)716return (-1); /* Error return */717718if (chunk->data[0] != 0 && chunk->data[0] != 1)719return (-1); /* Invalid mode */720721if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)722{723my_user_chunk_data->sTER_mode=chunk->data[0];724return (1);725}726727else728return (0); /* duplicate sTER - give it to libpng */729}730731if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */732chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */733return (0); /* Did not recognize */734735/* Found ImageMagick vpAg chunk */736737if (chunk->size != 9)738return (-1); /* Error return */739740if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)741return (0); /* duplicate vpAg */742743my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);744my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);745my_user_chunk_data->vpAg_units = chunk->data[8];746747return (1);748}749750#ifdef PNG_WRITE_SUPPORTED751static void752write_sTER_chunk(png_structp write_ptr)753{754png_byte sTER[5] = {115, 84, 69, 82, '\0'};755756if (verbose != 0)757fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);758759png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);760}761762static void763write_vpAg_chunk(png_structp write_ptr)764{765png_byte vpAg[5] = {118, 112, 65, 103, '\0'};766767png_byte vpag_chunk_data[9];768769if (verbose != 0)770fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",771(unsigned long)user_chunk_data.vpAg_width,772(unsigned long)user_chunk_data.vpAg_height,773user_chunk_data.vpAg_units);774775png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);776png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);777vpag_chunk_data[8] = user_chunk_data.vpAg_units;778png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);779}780781static void782write_chunks(png_structp write_ptr, int location)783{784int i;785786/* Notice that this preserves the original chunk order, however chunks787* intercepted by the callback will be written *after* chunks passed to788* libpng. This will actually reverse a pair of sTER chunks or a pair of789* vpAg chunks, resulting in an error later. This is not worth worrying790* about - the chunks should not be duplicated!791*/792for (i=0; i<2; ++i)793{794if (user_chunk_data.location[i] == (location | have_sTER))795write_sTER_chunk(write_ptr);796797else if (user_chunk_data.location[i] == (location | have_vpAg))798write_vpAg_chunk(write_ptr);799}800}801#endif /* WRITE */802#else /* !READ_USER_CHUNKS */803# define write_chunks(pp,loc) ((void)0)804#endif805/* END of code to demonstrate user chunk support */806807/* START of code to check that libpng has the required text support; this only808* checks for the write support because if read support is missing the chunk809* will simply not be reported back to pngtest.810*/811#ifdef PNG_TEXT_SUPPORTED812static void813pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,814int num_text)815{816while (num_text > 0)817{818switch (text_ptr[--num_text].compression)819{820case PNG_TEXT_COMPRESSION_NONE:821break;822823case PNG_TEXT_COMPRESSION_zTXt:824# ifndef PNG_WRITE_zTXt_SUPPORTED825++unsupported_chunks;826/* In libpng 1.7 this now does an app-error, so stop it: */827text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;828# endif829break;830831case PNG_ITXT_COMPRESSION_NONE:832case PNG_ITXT_COMPRESSION_zTXt:833# ifndef PNG_WRITE_iTXt_SUPPORTED834++unsupported_chunks;835text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;836# endif837break;838839default:840/* This is an error */841png_error(png_ptr, "invalid text chunk compression field");842break;843}844}845}846#endif847/* END of code to check that libpng has the required text support */848849/* Test one file */850static int851test_one_file(PNG_CONST char *inname, PNG_CONST char *outname)852{853static png_FILE_p fpin;854static png_FILE_p fpout; /* "static" prevents setjmp corruption */855pngtest_error_parameters error_parameters;856png_structp read_ptr;857png_infop read_info_ptr, end_info_ptr;858#ifdef PNG_WRITE_SUPPORTED859png_structp write_ptr;860png_infop write_info_ptr;861png_infop write_end_info_ptr;862#ifdef PNG_WRITE_FILTER_SUPPORTED863int interlace_preserved = 1;864#endif /* WRITE_FILTER */865#else /* !WRITE */866png_structp write_ptr = NULL;867png_infop write_info_ptr = NULL;868png_infop write_end_info_ptr = NULL;869#endif /* !WRITE */870png_bytep row_buf;871png_uint_32 y;872png_uint_32 width, height;873volatile int num_passes;874int pass;875int bit_depth, color_type;876877row_buf = NULL;878error_parameters.file_name = inname;879880if ((fpin = fopen(inname, "rb")) == NULL)881{882fprintf(STDERR, "Could not find input file %s\n", inname);883return (1);884}885886if ((fpout = fopen(outname, "wb")) == NULL)887{888fprintf(STDERR, "Could not open output file %s\n", outname);889FCLOSE(fpin);890return (1);891}892893pngtest_debug("Allocating read and write structures");894#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG895read_ptr =896png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,897NULL, NULL, NULL, png_debug_malloc, png_debug_free);898#else899read_ptr =900png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);901#endif902png_set_error_fn(read_ptr, &error_parameters, pngtest_error,903pngtest_warning);904905#ifdef PNG_WRITE_SUPPORTED906#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG907write_ptr =908png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,909NULL, NULL, NULL, png_debug_malloc, png_debug_free);910#else911write_ptr =912png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);913#endif914png_set_error_fn(write_ptr, &error_parameters, pngtest_error,915pngtest_warning);916#endif917pngtest_debug("Allocating read_info, write_info and end_info structures");918read_info_ptr = png_create_info_struct(read_ptr);919end_info_ptr = png_create_info_struct(read_ptr);920#ifdef PNG_WRITE_SUPPORTED921write_info_ptr = png_create_info_struct(write_ptr);922write_end_info_ptr = png_create_info_struct(write_ptr);923#endif924925#ifdef PNG_READ_USER_CHUNKS_SUPPORTED926init_callback_info(read_info_ptr);927png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,928read_user_chunk_callback);929#endif930931#ifdef PNG_SETJMP_SUPPORTED932pngtest_debug("Setting jmpbuf for read struct");933if (setjmp(png_jmpbuf(read_ptr)))934{935fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);936png_free(read_ptr, row_buf);937row_buf = NULL;938png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);939#ifdef PNG_WRITE_SUPPORTED940png_destroy_info_struct(write_ptr, &write_end_info_ptr);941png_destroy_write_struct(&write_ptr, &write_info_ptr);942#endif943FCLOSE(fpin);944FCLOSE(fpout);945return (1);946}947948#ifdef PNG_WRITE_SUPPORTED949pngtest_debug("Setting jmpbuf for write struct");950951if (setjmp(png_jmpbuf(write_ptr)))952{953fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);954png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);955png_destroy_info_struct(write_ptr, &write_end_info_ptr);956#ifdef PNG_WRITE_SUPPORTED957png_destroy_write_struct(&write_ptr, &write_info_ptr);958#endif959FCLOSE(fpin);960FCLOSE(fpout);961return (1);962}963#endif964#endif965966#ifdef PNG_BENIGN_ERRORS_SUPPORTED967if (strict != 0)968{969/* Treat png_benign_error() as errors on read */970png_set_benign_errors(read_ptr, 0);971972# ifdef PNG_WRITE_SUPPORTED973/* Treat them as errors on write */974png_set_benign_errors(write_ptr, 0);975# endif976977/* if strict is not set, then app warnings and errors are treated as978* warnings in release builds, but not in unstable builds; this can be979* changed with '--relaxed'.980*/981}982983else if (relaxed != 0)984{985/* Allow application (pngtest) errors and warnings to pass */986png_set_benign_errors(read_ptr, 1);987988/* Turn off CRC and ADLER32 checking while reading */989png_set_crc_action(read_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);990991# ifdef PNG_WRITE_SUPPORTED992png_set_benign_errors(write_ptr, 1);993# endif994995}996#endif /* BENIGN_ERRORS */997998pngtest_debug("Initializing input and output streams");999#ifdef PNG_STDIO_SUPPORTED1000png_init_io(read_ptr, fpin);1001# ifdef PNG_WRITE_SUPPORTED1002png_init_io(write_ptr, fpout);1003# endif1004#else1005png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);1006# ifdef PNG_WRITE_SUPPORTED1007png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,1008# ifdef PNG_WRITE_FLUSH_SUPPORTED1009pngtest_flush);1010# else1011NULL);1012# endif1013# endif1014#endif10151016if (status_dots_requested == 1)1017{1018#ifdef PNG_WRITE_SUPPORTED1019png_set_write_status_fn(write_ptr, write_row_callback);1020#endif1021png_set_read_status_fn(read_ptr, read_row_callback);1022}10231024else1025{1026#ifdef PNG_WRITE_SUPPORTED1027png_set_write_status_fn(write_ptr, NULL);1028#endif1029png_set_read_status_fn(read_ptr, NULL);1030}10311032#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED1033png_set_read_user_transform_fn(read_ptr, read_user_callback);1034#endif1035#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED1036zero_samples = 0;1037png_set_write_user_transform_fn(write_ptr, count_zero_samples);1038#endif10391040#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED1041/* Preserve all the unknown chunks, if possible. If this is disabled then,1042* even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use1043* libpng to *save* the unknown chunks on read (because we can't switch the1044* save option on!)1045*1046* Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all1047* unknown chunks and write will write them all.1048*/1049#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED1050png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,1051NULL, 0);1052#endif1053#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED1054png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,1055NULL, 0);1056#endif1057#endif10581059pngtest_debug("Reading info struct");1060png_read_info(read_ptr, read_info_ptr);10611062#ifdef PNG_READ_USER_CHUNKS_SUPPORTED1063/* This is a bit of a hack; there is no obvious way in the callback function1064* to determine that the chunks before the first IDAT have been read, so1065* remove the info_ptr (which is only used to determine position relative to1066* PLTE) here to indicate that we are after the IDAT.1067*/1068user_chunk_data.info_ptr = NULL;1069#endif10701071pngtest_debug("Transferring info struct");1072{1073int interlace_type, compression_type, filter_type;10741075if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,1076&color_type, &interlace_type, &compression_type, &filter_type) != 0)1077{1078png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,1079color_type, interlace_type, compression_type, filter_type);1080/* num_passes may not be available below if interlace support is not1081* provided by libpng for both read and write.1082*/1083switch (interlace_type)1084{1085case PNG_INTERLACE_NONE:1086num_passes = 1;1087break;10881089case PNG_INTERLACE_ADAM7:1090num_passes = 7;1091break;10921093default:1094png_error(read_ptr, "invalid interlace type");1095/*NOT REACHED*/1096}1097}10981099else1100png_error(read_ptr, "png_get_IHDR failed");1101}1102#ifdef PNG_FIXED_POINT_SUPPORTED1103#ifdef PNG_cHRM_SUPPORTED1104{1105png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,1106blue_y;11071108if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,1109&red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)1110{1111png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,1112red_y, green_x, green_y, blue_x, blue_y);1113}1114}1115#endif1116#ifdef PNG_gAMA_SUPPORTED1117{1118png_fixed_point gamma;11191120if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)1121png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);1122}1123#endif1124#else /* Use floating point versions */1125#ifdef PNG_FLOATING_POINT_SUPPORTED1126#ifdef PNG_cHRM_SUPPORTED1127{1128double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,1129blue_y;11301131if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,1132&red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)1133{1134png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,1135red_y, green_x, green_y, blue_x, blue_y);1136}1137}1138#endif1139#ifdef PNG_gAMA_SUPPORTED1140{1141double gamma;11421143if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)1144png_set_gAMA(write_ptr, write_info_ptr, gamma);1145}1146#endif1147#endif /* Floating point */1148#endif /* Fixed point */1149#ifdef PNG_iCCP_SUPPORTED1150{1151png_charp name;1152png_bytep profile;1153png_uint_32 proflen;1154int compression_type;11551156if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,1157&profile, &proflen) != 0)1158{1159png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,1160profile, proflen);1161}1162}1163#endif1164#ifdef PNG_sRGB_SUPPORTED1165{1166int intent;11671168if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)1169png_set_sRGB(write_ptr, write_info_ptr, intent);1170}1171#endif1172{1173png_colorp palette;1174int num_palette;11751176if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)1177png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);1178}1179#ifdef PNG_bKGD_SUPPORTED1180{1181png_color_16p background;11821183if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)1184{1185png_set_bKGD(write_ptr, write_info_ptr, background);1186}1187}1188#endif1189#ifdef PNG_hIST_SUPPORTED1190{1191png_uint_16p hist;11921193if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)1194png_set_hIST(write_ptr, write_info_ptr, hist);1195}1196#endif1197#ifdef PNG_oFFs_SUPPORTED1198{1199png_int_32 offset_x, offset_y;1200int unit_type;12011202if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,1203&unit_type) != 0)1204{1205png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);1206}1207}1208#endif1209#ifdef PNG_pCAL_SUPPORTED1210{1211png_charp purpose, units;1212png_charpp params;1213png_int_32 X0, X1;1214int type, nparams;12151216if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,1217&nparams, &units, ¶ms) != 0)1218{1219png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,1220nparams, units, params);1221}1222}1223#endif1224#ifdef PNG_pHYs_SUPPORTED1225{1226png_uint_32 res_x, res_y;1227int unit_type;12281229if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,1230&unit_type) != 0)1231png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);1232}1233#endif1234#ifdef PNG_sBIT_SUPPORTED1235{1236png_color_8p sig_bit;12371238if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)1239png_set_sBIT(write_ptr, write_info_ptr, sig_bit);1240}1241#endif1242#ifdef PNG_sCAL_SUPPORTED1243#if defined(PNG_FLOATING_POINT_SUPPORTED) && \1244defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)1245{1246int unit;1247double scal_width, scal_height;12481249if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,1250&scal_height) != 0)1251{1252png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);1253}1254}1255#else1256#ifdef PNG_FIXED_POINT_SUPPORTED1257{1258int unit;1259png_charp scal_width, scal_height;12601261if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,1262&scal_height) != 0)1263{1264png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,1265scal_height);1266}1267}1268#endif1269#endif1270#endif12711272#ifdef PNG_sPLT_SUPPORTED1273{1274png_sPLT_tp entries;12751276int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);1277if (num_entries)1278{1279png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);1280}1281}1282#endif12831284#ifdef PNG_TEXT_SUPPORTED1285{1286png_textp text_ptr;1287int num_text;12881289if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)1290{1291pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);12921293pngtest_check_text_support(read_ptr, text_ptr, num_text);12941295if (verbose != 0)1296{1297int i;12981299printf("\n");1300for (i=0; i<num_text; i++)1301{1302printf(" Text compression[%d]=%d\n",1303i, text_ptr[i].compression);1304}1305}13061307png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);1308}1309}1310#endif1311#ifdef PNG_tIME_SUPPORTED1312{1313png_timep mod_time;13141315if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)1316{1317png_set_tIME(write_ptr, write_info_ptr, mod_time);1318#ifdef PNG_TIME_RFC1123_SUPPORTED1319if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)1320tIME_string[(sizeof tIME_string) - 1] = '\0';13211322else1323{1324strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));1325tIME_string[(sizeof tIME_string) - 1] = '\0';1326}13271328tIME_chunk_present++;1329#endif /* TIME_RFC1123 */1330}1331}1332#endif1333#ifdef PNG_tRNS_SUPPORTED1334{1335png_bytep trans_alpha;1336int num_trans;1337png_color_16p trans_color;13381339if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,1340&trans_color) != 0)1341{1342int sample_max = (1 << bit_depth);1343/* libpng doesn't reject a tRNS chunk with out-of-range samples */1344if (!((color_type == PNG_COLOR_TYPE_GRAY &&1345(int)trans_color->gray > sample_max) ||1346(color_type == PNG_COLOR_TYPE_RGB &&1347((int)trans_color->red > sample_max ||1348(int)trans_color->green > sample_max ||1349(int)trans_color->blue > sample_max))))1350png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,1351trans_color);1352}1353}1354#endif1355#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED1356{1357png_unknown_chunkp unknowns;1358int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,1359&unknowns);13601361if (num_unknowns != 0)1362{1363png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,1364num_unknowns);1365#if PNG_LIBPNG_VER < 106001366/* Copy the locations from the read_info_ptr. The automatically1367* generated locations in write_end_info_ptr are wrong prior to 1.6.01368* because they are reset from the write pointer (removed in 1.6.0).1369*/1370{1371int i;1372for (i = 0; i < num_unknowns; i++)1373png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,1374unknowns[i].location);1375}1376#endif1377}1378}1379#endif13801381#ifdef PNG_WRITE_SUPPORTED1382pngtest_debug("Writing info struct");13831384/* Write the info in two steps so that if we write the 'unknown' chunks here1385* they go to the correct place.1386*/1387png_write_info_before_PLTE(write_ptr, write_info_ptr);13881389write_chunks(write_ptr, before_PLTE); /* before PLTE */13901391png_write_info(write_ptr, write_info_ptr);13921393write_chunks(write_ptr, before_IDAT); /* after PLTE */13941395#ifdef PNG_COMPRESSION_COMPAT1396/* Test the 'compatibility' setting here, if it is available. */1397png_set_compression(write_ptr, PNG_COMPRESSION_COMPAT);1398#endif1399#endif14001401#ifdef SINGLE_ROWBUF_ALLOC1402pngtest_debug("Allocating row buffer...");1403row_buf = (png_bytep)png_malloc(read_ptr,1404png_get_rowbytes(read_ptr, read_info_ptr));14051406pngtest_debug1("\t0x%08lx", (unsigned long)row_buf);1407#endif /* SINGLE_ROWBUF_ALLOC */1408pngtest_debug("Writing row data");14091410#if defined(PNG_READ_INTERLACING_SUPPORTED) &&\1411defined(PNG_WRITE_INTERLACING_SUPPORTED)1412/* Both must be defined for libpng to be able to handle the interlace,1413* otherwise it gets handled below by simply reading and writing the passes1414* directly.1415*/1416if (png_set_interlace_handling(read_ptr) != num_passes)1417png_error(write_ptr,1418"png_set_interlace_handling(read): wrong pass count ");1419if (png_set_interlace_handling(write_ptr) != num_passes)1420png_error(write_ptr,1421"png_set_interlace_handling(write): wrong pass count ");1422#else /* png_set_interlace_handling not called on either read or write */1423# define calc_pass_height1424#endif /* not using libpng interlace handling */14251426#ifdef PNGTEST_TIMING1427t_stop = (float)clock();1428t_misc += (t_stop - t_start);1429t_start = t_stop;1430#endif1431for (pass = 0; pass < num_passes; pass++)1432{1433# ifdef calc_pass_height1434png_uint_32 pass_height;14351436if (num_passes == 7) /* interlaced */1437{1438if (PNG_PASS_COLS(width, pass) > 0)1439pass_height = PNG_PASS_ROWS(height, pass);14401441else1442pass_height = 0;1443}14441445else /* not interlaced */1446pass_height = height;1447# else1448# define pass_height height1449# endif14501451pngtest_debug1("Writing row data for pass %d", pass);1452for (y = 0; y < pass_height; y++)1453{1454#ifndef SINGLE_ROWBUF_ALLOC1455pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);14561457row_buf = (png_bytep)png_malloc(read_ptr,1458png_get_rowbytes(read_ptr, read_info_ptr));14591460pngtest_debug2("\t0x%08lx (%lu bytes)", (unsigned long)row_buf,1461(unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));14621463#endif /* !SINGLE_ROWBUF_ALLOC */1464png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);14651466#ifdef PNG_WRITE_SUPPORTED1467#ifdef PNGTEST_TIMING1468t_stop = (float)clock();1469t_decode += (t_stop - t_start);1470t_start = t_stop;1471#endif1472png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);1473#ifdef PNGTEST_TIMING1474t_stop = (float)clock();1475t_encode += (t_stop - t_start);1476t_start = t_stop;1477#endif1478#endif /* WRITE */14791480#ifndef SINGLE_ROWBUF_ALLOC1481pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);1482png_free(read_ptr, row_buf);1483row_buf = NULL;1484#endif /* !SINGLE_ROWBUF_ALLOC */1485}1486}14871488#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED1489# ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED1490png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);1491# endif1492# ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED1493png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);1494# endif1495#endif14961497pngtest_debug("Reading and writing end_info data");14981499png_read_end(read_ptr, end_info_ptr);1500#ifdef PNG_TEXT_SUPPORTED1501{1502png_textp text_ptr;1503int num_text;15041505if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)1506{1507pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);15081509pngtest_check_text_support(read_ptr, text_ptr, num_text);15101511if (verbose != 0)1512{1513int i;15141515printf("\n");1516for (i=0; i<num_text; i++)1517{1518printf(" Text compression[%d]=%d\n",1519i, text_ptr[i].compression);1520}1521}15221523png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);1524}1525}1526#endif1527#ifdef PNG_tIME_SUPPORTED1528{1529png_timep mod_time;15301531if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)1532{1533png_set_tIME(write_ptr, write_end_info_ptr, mod_time);1534#ifdef PNG_TIME_RFC1123_SUPPORTED1535if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)1536tIME_string[(sizeof tIME_string) - 1] = '\0';15371538else1539{1540strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);1541tIME_string[(sizeof tIME_string)-1] = '\0';1542}15431544tIME_chunk_present++;1545#endif /* TIME_RFC1123 */1546}1547}1548#endif1549#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED1550{1551png_unknown_chunkp unknowns;1552int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,1553&unknowns);15541555if (num_unknowns != 0)1556{1557png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,1558num_unknowns);1559#if PNG_LIBPNG_VER < 106001560/* Copy the locations from the read_info_ptr. The automatically1561* generated locations in write_end_info_ptr are wrong prior to 1.6.01562* because they are reset from the write pointer (removed in 1.6.0).1563*/1564{1565int i;1566for (i = 0; i < num_unknowns; i++)1567png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,1568unknowns[i].location);1569}1570#endif1571}1572}1573#endif15741575#ifdef PNG_WRITE_SUPPORTED1576#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED1577/* Normally one would use Z_DEFAULT_STRATEGY for text compression.1578* This is here just to make pngtest replicate the results from libpng1579* versions prior to 1.5.4, and to test this new API.1580*/1581png_set_text_compression_strategy(write_ptr, Z_FILTERED);1582#endif15831584/* When the unknown vpAg/sTER chunks are written by pngtest the only way to1585* do it is to write them *before* calling png_write_end. When unknown1586* chunks are written by libpng, however, they are written just before IEND.1587* There seems to be no way round this, however vpAg/sTER are not expected1588* after IDAT.1589*/1590write_chunks(write_ptr, after_IDAT);15911592png_write_end(write_ptr, write_end_info_ptr);1593#endif15941595#ifdef PNG_EASY_ACCESS_SUPPORTED1596if (verbose != 0)1597{1598png_uint_32 iwidth, iheight;1599iwidth = png_get_image_width(write_ptr, write_info_ptr);1600iheight = png_get_image_height(write_ptr, write_info_ptr);1601fprintf(STDERR, "\n Image width = %lu, height = %lu\n",1602(unsigned long)iwidth, (unsigned long)iheight);1603}1604#endif16051606pngtest_debug("Destroying data structs");1607#ifdef SINGLE_ROWBUF_ALLOC1608pngtest_debug("destroying row_buf for read_ptr");1609png_free(read_ptr, row_buf);1610row_buf = NULL;1611#endif /* SINGLE_ROWBUF_ALLOC */1612pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");1613png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);1614#ifdef PNG_WRITE_SUPPORTED1615pngtest_debug("destroying write_end_info_ptr");1616png_destroy_info_struct(write_ptr, &write_end_info_ptr);1617pngtest_debug("destroying write_ptr, write_info_ptr");1618png_destroy_write_struct(&write_ptr, &write_info_ptr);1619#endif1620pngtest_debug("Destruction complete.");16211622FCLOSE(fpin);1623FCLOSE(fpout);16241625/* Summarize any warnings or errors and in 'strict' mode fail the test.1626* Unsupported chunks can result in warnings, in that case ignore the strict1627* setting, otherwise fail the test on warnings as well as errors.1628*/1629if (error_count > 0)1630{1631/* We don't really expect to get here because of the setjmp handling1632* above, but this is safe.1633*/1634fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",1635inname, error_count, warning_count);16361637if (strict != 0)1638return (1);1639}16401641# ifdef PNG_WRITE_SUPPORTED1642/* If there is no write support nothing was written! */1643else if (unsupported_chunks > 0)1644{1645fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",1646inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");1647}1648# endif16491650else if (warning_count > 0)1651{1652fprintf(STDERR, "\n %s: %d libpng warnings found",1653inname, warning_count);16541655if (strict != 0)1656return (1);1657}16581659pngtest_debug("Opening files for comparison");1660if ((fpin = fopen(inname, "rb")) == NULL)1661{1662fprintf(STDERR, "Could not find file %s\n", inname);1663return (1);1664}16651666if ((fpout = fopen(outname, "rb")) == NULL)1667{1668fprintf(STDERR, "Could not find file %s\n", outname);1669FCLOSE(fpin);1670return (1);1671}16721673#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\1674defined (PNG_WRITE_FILTER_SUPPORTED)1675if (interlace_preserved != 0) /* else the files will be changed */1676{1677for (;;)1678{1679static int wrote_question = 0;1680png_size_t num_in, num_out;1681char inbuf[256], outbuf[256];16821683num_in = fread(inbuf, 1, sizeof inbuf, fpin);1684num_out = fread(outbuf, 1, sizeof outbuf, fpout);16851686if (num_in != num_out)1687{1688fprintf(STDERR, "\nFiles %s and %s are of a different size\n",1689inname, outname);16901691if (wrote_question == 0 && unsupported_chunks == 0)1692{1693fprintf(STDERR,1694" Was %s written with the same maximum IDAT"1695" chunk size (%d bytes),",1696inname, PNG_ZBUF_SIZE);1697fprintf(STDERR,1698"\n filtering heuristic (libpng default), compression");1699fprintf(STDERR,1700" level (zlib default),\n and zlib version (%s)?\n\n",1701ZLIB_VERSION);1702wrote_question = 1;1703}17041705FCLOSE(fpin);1706FCLOSE(fpout);17071708if (strict != 0 && unsupported_chunks == 0)1709return (1);17101711else1712return (0);1713}17141715if (num_in == 0)1716break;17171718if (memcmp(inbuf, outbuf, num_in))1719{1720fprintf(STDERR, "\nFiles %s and %s are different\n", inname,1721outname);17221723if (wrote_question == 0 && unsupported_chunks == 0)1724{1725fprintf(STDERR,1726" Was %s written with the same maximum"1727" IDAT chunk size (%d bytes),",1728inname, PNG_ZBUF_SIZE);1729fprintf(STDERR,1730"\n filtering heuristic (libpng default), compression");1731fprintf(STDERR,1732" level (zlib default),\n and zlib version (%s)?\n\n",1733ZLIB_VERSION);1734wrote_question = 1;1735}17361737FCLOSE(fpin);1738FCLOSE(fpout);17391740/* NOTE: the unsupported_chunks escape is permitted here because1741* unsupported text chunk compression will result in the compression1742* mode being changed (to NONE) yet, in the test case, the result1743* can be exactly the same size!1744*/1745if (strict != 0 && unsupported_chunks == 0)1746return (1);17471748else1749return (0);1750}1751}1752}1753#endif /* WRITE && WRITE_FILTER */17541755FCLOSE(fpin);1756FCLOSE(fpout);17571758return (0);1759}17601761/* Input and output filenames */1762#ifdef RISCOS1763static PNG_CONST char *inname = "pngtest/png";1764static PNG_CONST char *outname = "pngout/png";1765#else1766static PNG_CONST char *inname = "pngtest.png";1767static PNG_CONST char *outname = "pngout.png";1768#endif17691770int1771main(int argc, char *argv[])1772{1773int multiple = 0;1774int ierror = 0;17751776png_structp dummy_ptr;17771778fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);1779fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);1780fprintf(STDERR, "%s", png_get_copyright(NULL));1781/* Show the version of libpng used in building the library */1782fprintf(STDERR, " library (%lu):%s",1783(unsigned long)png_access_version_number(),1784png_get_header_version(NULL));17851786/* Show the version of libpng used in building the application */1787fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,1788PNG_HEADER_VERSION_STRING);17891790/* Do some consistency checking on the memory allocation settings, I'm1791* not sure this matters, but it is nice to know, the first of these1792* tests should be impossible because of the way the macros are set1793* in pngconf.h1794*/1795#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)1796fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");1797#endif1798/* I think the following can happen. */1799#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)1800fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");1801#endif18021803if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))1804{1805fprintf(STDERR,1806"Warning: versions are different between png.h and png.c\n");1807fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);1808fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);1809++ierror;1810}18111812if (argc > 1)1813{1814if (strcmp(argv[1], "-m") == 0)1815{1816multiple = 1;1817status_dots_requested = 0;1818}18191820else if (strcmp(argv[1], "-mv") == 0 ||1821strcmp(argv[1], "-vm") == 0 )1822{1823multiple = 1;1824verbose = 1;1825status_dots_requested = 1;1826}18271828else if (strcmp(argv[1], "-v") == 0)1829{1830verbose = 1;1831status_dots_requested = 1;1832inname = argv[2];1833}18341835else if (strcmp(argv[1], "--strict") == 0)1836{1837status_dots_requested = 0;1838verbose = 1;1839inname = argv[2];1840strict++;1841relaxed = 0;1842}18431844else if (strcmp(argv[1], "--relaxed") == 0)1845{1846status_dots_requested = 0;1847verbose = 1;1848inname = argv[2];1849strict = 0;1850relaxed++;1851}18521853else1854{1855inname = argv[1];1856status_dots_requested = 0;1857}1858}18591860if (multiple == 0 && argc == 3 + verbose)1861outname = argv[2 + verbose];18621863if ((multiple == 0 && argc > 3 + verbose) ||1864(multiple != 0 && argc < 2))1865{1866fprintf(STDERR,1867"usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",1868argv[0], argv[0]);1869fprintf(STDERR,1870" reads/writes one PNG file (without -m) or multiple files (-m)\n");1871fprintf(STDERR,1872" with -m %s is used as a temporary file\n", outname);1873exit(1);1874}18751876if (multiple != 0)1877{1878int i;1879#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG1880int allocation_now = current_allocation;1881#endif1882for (i=2; i<argc; ++i)1883{1884int kerror;1885fprintf(STDERR, "\n Testing %s:", argv[i]);1886#if PNG_DEBUG > 01887fprintf(STDERR, "\n");1888#endif1889kerror = test_one_file(argv[i], outname);1890if (kerror == 0)1891{1892#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED1893fprintf(STDERR, "\n PASS (%lu zero samples)\n",1894(unsigned long)zero_samples);1895#else1896fprintf(STDERR, " PASS\n");1897#endif1898#ifdef PNG_TIME_RFC1123_SUPPORTED1899if (tIME_chunk_present != 0)1900fprintf(STDERR, " tIME = %s\n", tIME_string);19011902tIME_chunk_present = 0;1903#endif /* TIME_RFC1123 */1904}19051906else1907{1908fprintf(STDERR, " FAIL\n");1909ierror += kerror;1910}1911#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG1912if (allocation_now != current_allocation)1913fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",1914current_allocation - allocation_now);19151916if (current_allocation != 0)1917{1918memory_infop pinfo = pinformation;19191920fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",1921current_allocation);19221923while (pinfo != NULL)1924{1925fprintf(STDERR, " %lu bytes at %p\n",1926(unsigned long)pinfo->size,1927pinfo->pointer);1928pinfo = pinfo->next;1929}1930}1931#endif1932}1933#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG1934fprintf(STDERR, " Current memory allocation: %10d bytes\n",1935current_allocation);1936fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",1937maximum_allocation);1938fprintf(STDERR, " Total memory allocation: %10d bytes\n",1939total_allocation);1940fprintf(STDERR, " Number of allocations: %10d\n",1941num_allocations);1942#endif1943}19441945else1946{1947int i;1948for (i = 0; i<3; ++i)1949{1950int kerror;1951#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG1952int allocation_now = current_allocation;1953#endif1954if (i == 1)1955status_dots_requested = 1;19561957else if (verbose == 0)1958status_dots_requested = 0;19591960if (i == 0 || verbose == 1 || ierror != 0)1961{1962fprintf(STDERR, "\n Testing %s:", inname);1963#if PNG_DEBUG > 01964fprintf(STDERR, "\n");1965#endif1966}19671968kerror = test_one_file(inname, outname);19691970if (kerror == 0)1971{1972if (verbose == 1 || i == 2)1973{1974#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED1975fprintf(STDERR, "\n PASS (%lu zero samples)\n",1976(unsigned long)zero_samples);1977#else1978fprintf(STDERR, " PASS\n");1979#endif1980#ifdef PNG_TIME_RFC1123_SUPPORTED1981if (tIME_chunk_present != 0)1982fprintf(STDERR, " tIME = %s\n", tIME_string);1983#endif /* TIME_RFC1123 */1984}1985}19861987else1988{1989if (verbose == 0 && i != 2)1990{1991fprintf(STDERR, "\n Testing %s:", inname);1992#if PNG_DEBUG > 01993fprintf(STDERR, "\n");1994#endif1995}19961997fprintf(STDERR, " FAIL\n");1998ierror += kerror;1999}2000#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG2001if (allocation_now != current_allocation)2002fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",2003current_allocation - allocation_now);20042005if (current_allocation != 0)2006{2007memory_infop pinfo = pinformation;20082009fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",2010current_allocation);20112012while (pinfo != NULL)2013{2014fprintf(STDERR, " %lu bytes at %p\n",2015(unsigned long)pinfo->size, pinfo->pointer);2016pinfo = pinfo->next;2017}2018}2019#endif2020}2021#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG2022fprintf(STDERR, " Current memory allocation: %10d bytes\n",2023current_allocation);2024fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",2025maximum_allocation);2026fprintf(STDERR, " Total memory allocation: %10d bytes\n",2027total_allocation);2028fprintf(STDERR, " Number of allocations: %10d\n",2029num_allocations);2030#endif2031}20322033#ifdef PNGTEST_TIMING2034t_stop = (float)clock();2035t_misc += (t_stop - t_start);2036t_start = t_stop;2037fprintf(STDERR, " CPU time used = %.3f seconds",2038(t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);2039fprintf(STDERR, " (decoding %.3f,\n",2040t_decode/(float)CLOCKS_PER_SEC);2041fprintf(STDERR, " encoding %.3f ,",2042t_encode/(float)CLOCKS_PER_SEC);2043fprintf(STDERR, " other %.3f seconds)\n\n",2044t_misc/(float)CLOCKS_PER_SEC);2045#endif20462047if (ierror == 0)2048fprintf(STDERR, " libpng passes test\n");20492050else2051fprintf(STDERR, " libpng FAILS test\n");20522053dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);2054fprintf(STDERR, " Default limits:\n");2055fprintf(STDERR, " width_max = %lu\n",2056(unsigned long) png_get_user_width_max(dummy_ptr));2057fprintf(STDERR, " height_max = %lu\n",2058(unsigned long) png_get_user_height_max(dummy_ptr));2059if (png_get_chunk_cache_max(dummy_ptr) == 0)2060fprintf(STDERR, " cache_max = unlimited\n");2061else2062fprintf(STDERR, " cache_max = %lu\n",2063(unsigned long) png_get_chunk_cache_max(dummy_ptr));2064if (png_get_chunk_malloc_max(dummy_ptr) == 0)2065fprintf(STDERR, " malloc_max = unlimited\n");2066else2067fprintf(STDERR, " malloc_max = %lu\n",2068(unsigned long) png_get_chunk_malloc_max(dummy_ptr));2069png_destroy_read_struct(&dummy_ptr, NULL, NULL);20702071return (int)(ierror != 0);2072}2073#else2074int2075main(void)2076{2077fprintf(STDERR,2078" test ignored because libpng was not built with read support\n");2079/* And skip this test */2080return PNG_LIBPNG_VER < 10600 ? 0 : 77;2081}2082#endif20832084/* Generate a compiler error if there is an old png.h in the search path. */2085typedef png_libpng_version_1_7_0beta90 Your_png_h_is_not_version_1_7_0beta90;208620872088