blob: a715ae1123ba564b0a8aff26cd611c177599cb24 [file] [log] [blame]
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001
Guy Schalnat4ee97b01996-01-16 01:51:56 -06002/* pngtest.c - a simple test program to test libpng
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Cosmin Truta84395342019-02-03 21:00:49 -05004 * Copyright (c) 2018-2019 Cosmin Truta
Cosmin Truta46aedd82018-07-15 23:58:00 -04005 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
Cosmin Trutad4fb6212018-07-28 18:34:58 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * This program reads in a PNG image, writes it out again, and then
14 * compares the two files. If the files are identical, this shows that
15 * the basic chunk handling, filtering, and (de)compression code is working
16 * properly. It does not currently test all of the transforms, although
17 * it probably should.
18 *
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050019 * The program will report "FAIL" in certain legitimate cases:
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060020 * 1) when the compression level or filter selection method is changed.
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060021 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -060022 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
23 * exist in the input file.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060024 * 4) others not listed here...
25 * In these cases, it is best to check with another tool such as "pngcheck"
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060026 * to see what the differences between the two files are.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060027 *
28 * If a filename is given on the command-line, then this file is used
29 * for the input, rather than the default "pngtest.png". This allows
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050030 * testing a wide variety of files easily. You can also test a number
31 * of files at once by typing "pngtest -m file1.png file2.png ..."
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060032 */
Guy Schalnat0d580581995-07-20 02:43:20 -050033
Glenn Randers-Pehrsonb3b71682011-05-03 22:30:19 -050034#define _POSIX_SOURCE 1
35
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40/* Defined so I can write to a file on gui/windowing platforms */
41/* #define STDERR stderr */
42#define STDERR stdout /* For DOS */
43
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -060044#include "png.h"
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060045
John Bowlere4413a72013-04-17 21:27:47 -050046/* Known chunks that exist in pngtest.png must be supported or pngtest will fail
47 * simply as a result of re-ordering them. This may be fixed in 1.7
John Bowlera8472472013-12-29 10:50:51 -060048 *
49 * pngtest allocates a single row buffer for each row and overwrites it,
50 * therefore if the write side doesn't support the writing of interlaced images
51 * nothing can be done for an interlaced image (and the code below will fail
52 * horribly trying to write extra data after writing garbage).
John Bowlere4413a72013-04-17 21:27:47 -050053 */
54#if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
55 defined PNG_READ_bKGD_SUPPORTED &&\
56 defined PNG_READ_cHRM_SUPPORTED &&\
57 defined PNG_READ_gAMA_SUPPORTED &&\
58 defined PNG_READ_oFFs_SUPPORTED &&\
59 defined PNG_READ_pCAL_SUPPORTED &&\
60 defined PNG_READ_pHYs_SUPPORTED &&\
61 defined PNG_READ_sBIT_SUPPORTED &&\
62 defined PNG_READ_sCAL_SUPPORTED &&\
63 defined PNG_READ_sRGB_SUPPORTED &&\
John Bowler8a08b642015-11-23 20:15:51 -080064 defined PNG_READ_sPLT_SUPPORTED &&\
John Bowlere4413a72013-04-17 21:27:47 -050065 defined PNG_READ_tEXt_SUPPORTED &&\
66 defined PNG_READ_tIME_SUPPORTED &&\
John Bowlera8472472013-12-29 10:50:51 -060067 defined PNG_READ_zTXt_SUPPORTED &&\
John Bowler8a08b642015-11-23 20:15:51 -080068 (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
John Bowlere4413a72013-04-17 21:27:47 -050069
John Bowlera8472472013-12-29 10:50:51 -060070#ifdef PNG_ZLIB_HEADER
71# include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
72#else
73# include "zlib.h"
74#endif
75
Glenn Randers-Pehrson72531442010-04-17 08:17:51 -050076/* Copied from pngpriv.h but only used in error messages below. */
77#ifndef PNG_ZBUF_SIZE
78# define PNG_ZBUF_SIZE 8192
79#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060080#define FCLOSE(file) fclose(file)
Andreas Dilger47a0c421997-05-16 02:46:07 -050081
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -050082#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050083typedef FILE * png_FILE_p;
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -060084#endif
85
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060086/* Makes pngtest verbose so we can find problems. */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087#ifndef PNG_DEBUG
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050088# define PNG_DEBUG 0
89#endif
90
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -060091#if PNG_DEBUG > 1
92# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
93# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
94# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
95#else
96# define pngtest_debug(m) ((void)0)
97# define pngtest_debug1(m,p1) ((void)0)
98# define pngtest_debug2(m,p1,p2) ((void)0)
99#endif
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -0600100
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500101#if !PNG_DEBUG
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500102# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600103#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104
John Bowler8a08b642015-11-23 20:15:51 -0800105#ifndef PNG_UNUSED
106# define PNG_UNUSED(param) (void)param;
107#endif
108
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500109/* Turn on CPU timing
110#define PNGTEST_TIMING
111*/
112
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500113#ifndef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600114#undef PNGTEST_TIMING
115#endif
116
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -0500117#ifdef PNGTEST_TIMING
118static float t_start, t_stop, t_decode, t_encode, t_misc;
119#include <time.h>
120#endif
121
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500122#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -0500123#define PNG_tIME_STRING_LENGTH 29
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500124static int tIME_chunk_present = 0;
Glenn Randers-Pehrsona5fa5c92008-09-06 07:06:22 -0500125static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
John Bowler8a08b642015-11-23 20:15:51 -0800126
127#if PNG_LIBPNG_VER < 10619
128#define png_convert_to_rfc1123_buffer(ts, t) tIME_to_str(read_ptr, ts, t)
129
130static int
131tIME_to_str(png_structp png_ptr, png_charp ts, png_const_timep t)
132{
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500133 png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
John Bowler8a08b642015-11-23 20:15:51 -0800134
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500135 if (str == NULL)
136 return 0;
John Bowler8a08b642015-11-23 20:15:51 -0800137
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500138 strcpy(ts, str);
139 return 1;
John Bowler8a08b642015-11-23 20:15:51 -0800140}
141#endif /* older libpng */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500142#endif
143
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500144static int verbose = 0;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -0500145static int strict = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600146static int relaxed = 0;
Glenn Randers-Pehrson20739282017-08-04 14:17:28 -0500147static int xfail = 0;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600148static int unsupported_chunks = 0; /* chunk unsupported by libpng in input */
149static int error_count = 0; /* count calls to png_error */
150static int warning_count = 0; /* count calls to png_warning */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500151
Glenn Randers-Pehrsona7dbcba2007-05-15 16:16:34 -0500152/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
153#ifndef png_jmpbuf
154# define png_jmpbuf(png_ptr) png_ptr->jmpbuf
155#endif
156
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600157/* Defines for unknown chunk handling if required. */
158#ifndef PNG_HANDLE_CHUNK_ALWAYS
159# define PNG_HANDLE_CHUNK_ALWAYS 3
160#endif
161#ifndef PNG_HANDLE_CHUNK_IF_SAFE
162# define PNG_HANDLE_CHUNK_IF_SAFE 2
163#endif
164
165/* Utility to save typing/errors, the argument must be a name */
166#define MEMZERO(var) ((void)memset(&var, 0, sizeof var))
167
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500168/* Example of using row callbacks to make a simple progress meter */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500169static int status_pass = 1;
170static int status_dots_requested = 0;
171static int status_dots = 1;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600172
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600173static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600174read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600175{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500176 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
177 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500179 if (status_pass != pass)
180 {
181 fprintf(stdout, "\n Pass %d: ", pass);
182 status_pass = pass;
183 status_dots = 31;
184 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500185
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500186 status_dots--;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500187
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500188 if (status_dots == 0)
189 {
190 fprintf(stdout, "\n ");
191 status_dots=30;
192 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500193
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500194 fprintf(stdout, "r");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600195}
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600196
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600197#ifdef PNG_WRITE_SUPPORTED
198static void PNGCBAPI
Glenn Randers-Pehrson7cd899c1998-03-07 16:17:42 -0600199write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600200{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500201 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
202 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500203
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204 fprintf(stdout, "w");
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600205}
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600206#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600207
208
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500209#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
John Bowler8a08b642015-11-23 20:15:51 -0800210/* Example of using a user transform callback (doesn't do anything at present).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500211 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600212static void PNGCBAPI
John Bowler8a08b642015-11-23 20:15:51 -0800213read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500214{
John Bowler8a08b642015-11-23 20:15:51 -0800215 PNG_UNUSED(png_ptr)
216 PNG_UNUSED(row_info)
217 PNG_UNUSED(data)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500218}
219#endif
220
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500221#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500222/* Example of using user transform callback (we don't transform anything,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500223 * but merely count the zero samples)
224 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600225
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500226static png_uint_32 zero_samples;
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600227
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600228static void PNGCBAPI
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500229count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600230{
231 png_bytep dp = data;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500232 if (png_ptr == NULL)
233 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600234
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500235 /* Contents of row_info:
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600236 * png_uint_32 width width of row
237 * png_uint_32 rowbytes number of bytes in row
238 * png_byte color_type color type of pixels
239 * png_byte bit_depth bit depth of samples
240 * png_byte channels number of channels (1-4)
241 * png_byte pixel_depth bits per pixel (depth*channels)
242 */
243
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500244 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600245
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500246 if (row_info->color_type == 0 || row_info->color_type == 3)
247 {
248 int pos = 0;
249 png_uint_32 n, nstop;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500250
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500251 for (n = 0, nstop=row_info->width; n<nstop; n++)
252 {
253 if (row_info->bit_depth == 1)
254 {
255 if (((*dp << pos++ ) & 0x80) == 0)
256 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500257
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500258 if (pos == 8)
259 {
260 pos = 0;
261 dp++;
262 }
263 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500264
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500265 if (row_info->bit_depth == 2)
266 {
267 if (((*dp << (pos+=2)) & 0xc0) == 0)
268 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500269
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500270 if (pos == 8)
271 {
272 pos = 0;
273 dp++;
274 }
275 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500276
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500277 if (row_info->bit_depth == 4)
278 {
279 if (((*dp << (pos+=4)) & 0xf0) == 0)
280 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500281
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500282 if (pos == 8)
283 {
284 pos = 0;
285 dp++;
286 }
287 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500288
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500289 if (row_info->bit_depth == 8)
290 if (*dp++ == 0)
291 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500292
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500293 if (row_info->bit_depth == 16)
294 {
295 if ((*dp | *(dp+1)) == 0)
296 zero_samples++;
297 dp+=2;
298 }
299 }
300 }
301 else /* Other color types */
302 {
303 png_uint_32 n, nstop;
304 int channel;
305 int color_channels = row_info->channels;
306 if (row_info->color_type > 3)
307 color_channels--;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600308
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500309 for (n = 0, nstop=row_info->width; n<nstop; n++)
310 {
311 for (channel = 0; channel < color_channels; channel++)
312 {
313 if (row_info->bit_depth == 8)
314 if (*dp++ == 0)
315 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500316
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500317 if (row_info->bit_depth == 16)
318 {
319 if ((*dp | *(dp+1)) == 0)
320 zero_samples++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500321
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500322 dp+=2;
323 }
324 }
325 if (row_info->color_type > 3)
326 {
327 dp++;
328 if (row_info->bit_depth == 16)
329 dp++;
330 }
331 }
332 }
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600333}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600334#endif /* WRITE_USER_TRANSFORM */
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600335
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500336#ifndef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600337/* START of code to validate stdio-free compilation */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500338/* These copies of the default read/write functions come from pngrio.c and
339 * pngwio.c. They allow "don't include stdio" testing of the library.
340 * This is the function that does the actual reading of data. If you are
341 * not reading from a standard C stream, you should create a replacement
342 * read_data function and use it at run time with png_set_read_fn(), rather
343 * than changing the library.
344 */
Glenn Randers-Pehrsonbe9de0f2001-01-22 08:52:16 -0600345
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600346#ifdef PNG_IO_STATE_SUPPORTED
347void
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400348pngtest_check_io_state(png_structp png_ptr, size_t data_length,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500349 png_uint_32 io_op);
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600350void
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400351pngtest_check_io_state(png_structp png_ptr, size_t data_length,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500352 png_uint_32 io_op)
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600353{
354 png_uint_32 io_state = png_get_io_state(png_ptr);
355 int err = 0;
356
357 /* Check if the current operation (reading / writing) is as expected. */
358 if ((io_state & PNG_IO_MASK_OP) != io_op)
359 png_error(png_ptr, "Incorrect operation in I/O state");
360
361 /* Check if the buffer size specific to the current location
362 * (file signature / header / data / crc) is as expected.
363 */
364 switch (io_state & PNG_IO_MASK_LOC)
365 {
366 case PNG_IO_SIGNATURE:
367 if (data_length > 8)
368 err = 1;
369 break;
370 case PNG_IO_CHUNK_HDR:
371 if (data_length != 8)
372 err = 1;
373 break;
374 case PNG_IO_CHUNK_DATA:
375 break; /* no restrictions here */
376 case PNG_IO_CHUNK_CRC:
377 if (data_length != 4)
378 err = 1;
379 break;
380 default:
381 err = 1; /* uninitialized */
382 }
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600383 if (err != 0)
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600384 png_error(png_ptr, "Bad I/O state or buffer size");
385}
386#endif
387
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600388static void PNGCBAPI
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400389pngtest_read_data(png_structp png_ptr, png_bytep data, size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600390{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400391 size_t check = 0;
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600392 png_voidp io_ptr;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600393
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400394 /* fread() returns 0 on error, so it is OK to store this in a size_t
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600395 * instead of an int, which is what fread() actually returns.
396 */
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600397 io_ptr = png_get_io_ptr(png_ptr);
398 if (io_ptr != NULL)
399 {
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -0600400 check = fread(data, 1, length, (png_FILE_p)io_ptr);
Glenn Randers-Pehrsona0e0c6c2010-01-01 18:31:26 -0600401 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500402
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600403 if (check != length)
404 {
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600405 png_error(png_ptr, "Read Error");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600406 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600407
408#ifdef PNG_IO_STATE_SUPPORTED
409 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
410#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600411}
Guy Schalnat0d580581995-07-20 02:43:20 -0500412
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500413#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600414static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500415pngtest_flush(png_structp png_ptr)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600416{
Glenn Randers-Pehrson8fb550c2009-03-21 08:15:32 -0500417 /* Do nothing; fflush() is said to be just a waste of energy. */
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -0600418 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600419}
420#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500421
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500422/* This is the function that does the actual writing of data. If you are
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500423 * not writing to a standard C stream, you should create a replacement
424 * write_data function and use it at run time with png_set_write_fn(), rather
425 * than changing the library.
426 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600427static void PNGCBAPI
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400428pngtest_write_data(png_structp png_ptr, png_bytep data, size_t length)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600429{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400430 size_t check;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600431
Glenn Randers-Pehrson526a6ad2010-03-11 05:42:20 -0600432 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500433
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600434 if (check != length)
435 {
436 png_error(png_ptr, "Write Error");
437 }
Glenn Randers-Pehrsona5815562010-11-20 21:48:29 -0600438
439#ifdef PNG_IO_STATE_SUPPORTED
440 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
441#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600442}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600443#endif /* !STDIO */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600444
445/* This function is called when there is a warning, but the library thinks
446 * it can continue anyway. Replacement functions don't have to do anything
447 * here if you don't want to. In the default configuration, png_ptr is
448 * not used, but it is passed in case it may be useful.
449 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600450typedef struct
451{
Cosmin Truta1ef88822018-08-18 21:01:02 -0400452 const char *file_name;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600453} pngtest_error_parameters;
454
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600455static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500456pngtest_warning(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600457{
Cosmin Truta1ef88822018-08-18 21:01:02 -0400458 const char *name = "UNKNOWN (ERROR!)";
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600459 pngtest_error_parameters *test =
460 (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500461
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600462 ++warning_count;
John Bowlerad5a9932012-08-10 13:15:07 -0500463
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600464 if (test != NULL && test->file_name != NULL)
465 name = test->file_name;
466
Glenn Randers-Pehrsona64c8ca2017-08-04 20:04:58 -0500467 fprintf(STDERR, "\n%s: libpng warning: %s\n", name, message);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600468}
469
470/* This is the default error handling function. Note that replacements for
471 * this function MUST NOT RETURN, or the program will likely crash. This
472 * function is used by default, or if the program supplies NULL for the
473 * error function pointer in png_set_error_fn().
474 */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600475static void PNGCBAPI
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500476pngtest_error(png_structp png_ptr, png_const_charp message)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600477{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600478 ++error_count;
479
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500480 pngtest_warning(png_ptr, message);
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500481 /* We can return because png_error calls the default handler, which is
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500482 * actually OK in this case.
483 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600484}
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600485
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600486/* END of code to validate stdio-free compilation */
487
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600488/* START of code to validate memory allocation and deallocation */
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500489#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600490
491/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrsonbbe2be32015-03-07 13:13:11 -0600492 * 64K. However, zlib may allocate more than 64K if you don't tell
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500493 * it not to. See zconf.h and png.h for more information. zlib does
494 * need to allocate exactly 64K, so whatever you call here must
495 * have the ability to do that.
496 *
497 * This piece of code can be compiled to validate max 64K allocations
498 * by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
499 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600500typedef struct memory_information
501{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500502 png_alloc_size_t size;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500503 png_voidp pointer;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600504 struct memory_information *next;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600505} memory_information;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600506typedef memory_information *memory_infop;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600507
508static memory_infop pinformation = NULL;
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -0500509static int current_allocation = 0;
510static int maximum_allocation = 0;
511static int total_allocation = 0;
512static int num_allocations = 0;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600513
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600514png_voidp PNGCBAPI png_debug_malloc PNGARG((png_structp png_ptr,
515 png_alloc_size_t size));
516void PNGCBAPI png_debug_free PNGARG((png_structp png_ptr, png_voidp ptr));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600517
518png_voidp
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600519PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600520{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500521
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500522 /* png_malloc has already tested for NULL; png_create_struct calls
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500523 * png_debug_malloc directly, with png_ptr == NULL which is OK
524 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500525
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600526 if (size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500527 return (NULL);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600528
529 /* This calls the library allocator twice, once to get the requested
530 buffer and once to get a new free list entry. */
531 {
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500532 /* Disable malloc_fn and free_fn */
Glenn Randers-Pehrson36d7bc72004-08-10 06:55:02 -0500533 memory_infop pinfo;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500534 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500535 pinfo = (memory_infop)png_malloc(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500536 (sizeof *pinfo));
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600537 pinfo->size = size;
538 current_allocation += size;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500539 total_allocation += size;
540 num_allocations ++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600542 if (current_allocation > maximum_allocation)
543 maximum_allocation = current_allocation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500544
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500545 pinfo->pointer = png_malloc(png_ptr, size);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500546 /* Restore malloc_fn and free_fn */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500547
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500548 png_set_mem_fn(png_ptr,
549 NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500550
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500551 if (size != 0 && pinfo->pointer == NULL)
552 {
553 current_allocation -= size;
554 total_allocation -= size;
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500555 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500556 "out of memory in pngtest->png_debug_malloc");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500557 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500558
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600559 pinfo->next = pinformation;
560 pinformation = pinfo;
561 /* Make sure the caller isn't assuming zeroed memory. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600562 memset(pinfo->pointer, 0xdd, pinfo->size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500563
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600564 if (verbose != 0)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600565 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500566 pinfo->pointer);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500567
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600568 return (png_voidp)(pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600569 }
570}
571
572/* Free a pointer. It is removed from the list at the same time. */
Glenn Randers-Pehrsond7da8bb2010-03-13 20:30:10 -0600573void PNGCBAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500574png_debug_free(png_structp png_ptr, png_voidp ptr)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600575{
576 if (png_ptr == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500577 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500578
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600579 if (ptr == 0)
580 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600581#if 0 /* This happens all the time. */
582 fprintf(STDERR, "WARNING: freeing NULL pointer\n");
583#endif
584 return;
585 }
586
587 /* Unlink the element from the list. */
Glenn Randers-Pehrsond2fedd62015-05-09 21:47:00 -0500588 if (pinformation != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600589 {
John Bowlerbaeb6d12011-11-26 18:21:02 -0600590 memory_infop *ppinfo = &pinformation;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600592 for (;;)
593 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600594 memory_infop pinfo = *ppinfo;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500595
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600596 if (pinfo->pointer == ptr)
597 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600598 *ppinfo = pinfo->next;
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -0500599 current_allocation -= pinfo->size;
600 if (current_allocation < 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600601 fprintf(STDERR, "Duplicate free of memory\n");
602 /* We must free the list element too, but first kill
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500603 the memory that is to be freed. */
John Bowlerbaeb6d12011-11-26 18:21:02 -0600604 memset(ptr, 0x55, pinfo->size);
Glenn Randers-Pehrsond2fedd62015-05-09 21:47:00 -0500605 free(pinfo);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500606 pinfo = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600607 break;
608 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500609
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600610 if (pinfo->next == NULL)
611 {
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -0500612 fprintf(STDERR, "Pointer %p not found\n", ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600613 break;
614 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500615
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600616 ppinfo = &pinfo->next;
617 }
618 }
619
620 /* Finally free the data. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600621 if (verbose != 0)
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600622 printf("Freeing %p\n", ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500623
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600624 if (ptr != NULL)
Glenn Randers-Pehrson83b132f2013-11-28 14:00:04 -0600625 free(ptr);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500626 ptr = NULL;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600627}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600628#endif /* USER_MEM && DEBUG */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600629/* END of code to test memory allocation/deallocation */
630
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500631
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600632#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500633/* Demonstration of user chunk support of the sTER and vpAg chunks */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500634
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500635/* (sTER is a public chunk not yet known by libpng. vpAg is a private
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500636chunk used in ImageMagick to store "virtual page" size). */
637
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600638static struct user_chunk_data
639{
640 png_const_infop info_ptr;
641 png_uint_32 vpAg_width, vpAg_height;
642 png_byte vpAg_units;
643 png_byte sTER_mode;
644 int location[2];
645}
646user_chunk_data;
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500647
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600648/* Used for location and order; zero means nothing. */
649#define have_sTER 0x01
650#define have_vpAg 0x02
651#define before_PLTE 0x10
652#define before_IDAT 0x20
653#define after_IDAT 0x40
654
655static void
656init_callback_info(png_const_infop info_ptr)
657{
658 MEMZERO(user_chunk_data);
659 user_chunk_data.info_ptr = info_ptr;
660}
661
662static int
663set_location(png_structp png_ptr, struct user_chunk_data *data, int what)
664{
665 int location;
666
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500667 if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600668 return 0; /* already have one of these */
669
Glenn Randers-Pehrson6cae24c2014-10-13 11:11:21 -0500670 /* Find where we are (the code below zeroes info_ptr to indicate that the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600671 * chunks before the first IDAT have been read.)
672 */
673 if (data->info_ptr == NULL) /* after IDAT */
674 location = what | after_IDAT;
675
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500676 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600677 location = what | before_IDAT;
678
679 else
680 location = what | before_PLTE;
681
682 if (data->location[0] == 0)
683 data->location[0] = location;
684
685 else
686 data->location[1] = location;
687
688 return 1; /* handled */
689}
John Bowler3c1f6982012-08-16 20:47:34 -0500690
Glenn Randers-Pehrson24afd072014-03-05 16:55:19 -0600691static int PNGCBAPI
Glenn Randers-Pehrsonc9786422014-03-05 17:14:16 -0600692read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500693{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600694 struct user_chunk_data *my_user_chunk_data =
695 (struct user_chunk_data*)png_get_user_chunk_ptr(png_ptr);
696
697 if (my_user_chunk_data == NULL)
698 png_error(png_ptr, "lost user chunk pointer");
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500699
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500700 /* Return one of the following:
701 * return (-n); chunk had an error
702 * return (0); did not recognize
703 * return (n); success
704 *
705 * The unknown chunk structure contains the chunk data:
706 * png_byte name[5];
707 * png_byte *data;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400708 * size_t size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500709 *
710 * Note that libpng has already taken care of the CRC handling.
711 */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500712
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500713 if (chunk->name[0] == 115 && chunk->name[1] == 84 && /* s T */
714 chunk->name[2] == 69 && chunk->name[3] == 82) /* E R */
715 {
716 /* Found sTER chunk */
717 if (chunk->size != 1)
718 return (-1); /* Error return */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500719
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500720 if (chunk->data[0] != 0 && chunk->data[0] != 1)
721 return (-1); /* Invalid mode */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500722
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500723 if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600724 {
725 my_user_chunk_data->sTER_mode=chunk->data[0];
726 return (1);
727 }
728
729 else
730 return (0); /* duplicate sTER - give it to libpng */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500731 }
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500732
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500733 if (chunk->name[0] != 118 || chunk->name[1] != 112 || /* v p */
734 chunk->name[2] != 65 || chunk->name[3] != 103) /* A g */
735 return (0); /* Did not recognize */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500736
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500737 /* Found ImageMagick vpAg chunk */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500738
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500739 if (chunk->size != 9)
740 return (-1); /* Error return */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500741
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500742 if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600743 return (0); /* duplicate vpAg */
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500744
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600745 my_user_chunk_data->vpAg_width = png_get_uint_31(png_ptr, chunk->data);
746 my_user_chunk_data->vpAg_height = png_get_uint_31(png_ptr, chunk->data + 4);
747 my_user_chunk_data->vpAg_units = chunk->data[8];
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500748
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500749 return (1);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500750}
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600751
752#ifdef PNG_WRITE_SUPPORTED
753static void
754write_sTER_chunk(png_structp write_ptr)
755{
John Bowlera8472472013-12-29 10:50:51 -0600756 png_byte sTER[5] = {115, 84, 69, 82, '\0'};
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600757
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600758 if (verbose != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600759 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
760
John Bowlera8472472013-12-29 10:50:51 -0600761 png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600762}
763
764static void
765write_vpAg_chunk(png_structp write_ptr)
766{
John Bowlera8472472013-12-29 10:50:51 -0600767 png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600768
769 png_byte vpag_chunk_data[9];
770
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600771 if (verbose != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600772 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500773 (unsigned long)user_chunk_data.vpAg_width,
774 (unsigned long)user_chunk_data.vpAg_height,
775 user_chunk_data.vpAg_units);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600776
777 png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
778 png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
779 vpag_chunk_data[8] = user_chunk_data.vpAg_units;
John Bowlera8472472013-12-29 10:50:51 -0600780 png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600781}
782
783static void
784write_chunks(png_structp write_ptr, int location)
785{
786 int i;
787
788 /* Notice that this preserves the original chunk order, however chunks
789 * intercepted by the callback will be written *after* chunks passed to
790 * libpng. This will actually reverse a pair of sTER chunks or a pair of
791 * vpAg chunks, resulting in an error later. This is not worth worrying
792 * about - the chunks should not be duplicated!
793 */
794 for (i=0; i<2; ++i)
795 {
796 if (user_chunk_data.location[i] == (location | have_sTER))
797 write_sTER_chunk(write_ptr);
798
799 else if (user_chunk_data.location[i] == (location | have_vpAg))
800 write_vpAg_chunk(write_ptr);
801 }
802}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600803#endif /* WRITE */
804#else /* !READ_USER_CHUNKS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600805# define write_chunks(pp,loc) ((void)0)
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500806#endif
807/* END of code to demonstrate user chunk support */
808
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600809/* START of code to check that libpng has the required text support; this only
810 * checks for the write support because if read support is missing the chunk
811 * will simply not be reported back to pngtest.
812 */
813#ifdef PNG_TEXT_SUPPORTED
814static void
John Bowler8a08b642015-11-23 20:15:51 -0800815pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500816 int num_text)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600817{
818 while (num_text > 0)
819 {
820 switch (text_ptr[--num_text].compression)
821 {
822 case PNG_TEXT_COMPRESSION_NONE:
823 break;
824
825 case PNG_TEXT_COMPRESSION_zTXt:
826# ifndef PNG_WRITE_zTXt_SUPPORTED
827 ++unsupported_chunks;
John Bowler8a08b642015-11-23 20:15:51 -0800828 /* In libpng 1.7 this now does an app-error, so stop it: */
829 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600830# endif
831 break;
832
833 case PNG_ITXT_COMPRESSION_NONE:
834 case PNG_ITXT_COMPRESSION_zTXt:
835# ifndef PNG_WRITE_iTXt_SUPPORTED
836 ++unsupported_chunks;
John Bowler8a08b642015-11-23 20:15:51 -0800837 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600838# endif
839 break;
840
841 default:
842 /* This is an error */
843 png_error(png_ptr, "invalid text chunk compression field");
844 break;
845 }
846 }
847}
848#endif
849/* END of code to check that libpng has the required text support */
850
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600851/* Test one file */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600852static int
Cosmin Truta1ef88822018-08-18 21:01:02 -0400853test_one_file(const char *inname, const char *outname)
Guy Schalnat0d580581995-07-20 02:43:20 -0500854{
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500855 static png_FILE_p fpin;
856 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600857 pngtest_error_parameters error_parameters;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500858 png_structp read_ptr;
859 png_infop read_info_ptr, end_info_ptr;
860#ifdef PNG_WRITE_SUPPORTED
861 png_structp write_ptr;
862 png_infop write_info_ptr;
863 png_infop write_end_info_ptr;
John Bowler8a08b642015-11-23 20:15:51 -0800864#ifdef PNG_WRITE_FILTER_SUPPORTED
John Bowlera8472472013-12-29 10:50:51 -0600865 int interlace_preserved = 1;
John Bowler8a08b642015-11-23 20:15:51 -0800866#endif /* WRITE_FILTER */
867#else /* !WRITE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500868 png_structp write_ptr = NULL;
869 png_infop write_info_ptr = NULL;
870 png_infop write_end_info_ptr = NULL;
John Bowler8a08b642015-11-23 20:15:51 -0800871#endif /* !WRITE */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 png_bytep row_buf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500873 png_uint_32 y;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500874 png_uint_32 width, height;
John Bowler8a08b642015-11-23 20:15:51 -0800875 volatile int num_passes;
876 int pass;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877 int bit_depth, color_type;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600878
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500879 row_buf = NULL;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600880 error_parameters.file_name = inname;
Guy Schalnat0d580581995-07-20 02:43:20 -0500881
Andreas Dilger47a0c421997-05-16 02:46:07 -0500882 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600883 {
884 fprintf(STDERR, "Could not find input file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600885 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600886 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500887
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 if ((fpout = fopen(outname, "wb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600889 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500890 fprintf(STDERR, "Could not open output file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500891 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600892 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600893 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500894
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600895 pngtest_debug("Allocating read and write structures");
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500896#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500897 read_ptr =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500898 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
899 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500900#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500901 read_ptr =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500902 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500903#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600904 png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500905 pngtest_warning);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -0500906
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500907#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -0500908#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500909 write_ptr =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500910 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
911 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500912#else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500913 write_ptr =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500914 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500915#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600916 png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500917 pngtest_warning);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500918#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600919 pngtest_debug("Allocating read_info, write_info and end_info structures");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 read_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500921 end_info_ptr = png_create_info_struct(read_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500922#ifdef PNG_WRITE_SUPPORTED
923 write_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500924 write_end_info_ptr = png_create_info_struct(write_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500925#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500926
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600927#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
928 init_callback_info(read_info_ptr);
929 png_set_read_user_chunk_fn(read_ptr, &user_chunk_data,
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500930 read_user_chunk_callback);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600931#endif
932
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600934 pngtest_debug("Setting jmpbuf for read struct");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600935 if (setjmp(png_jmpbuf(read_ptr)))
Guy Schalnat0f716451995-11-28 11:22:13 -0600936 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600937 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500938 png_free(read_ptr, row_buf);
939 row_buf = NULL;
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -0500940 if (verbose != 0)
941 fprintf(STDERR, " destroy read structs\n");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500943#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -0500944 if (verbose != 0)
945 fprintf(STDERR, " destroy write structs\n");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500946 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500948#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500949 FCLOSE(fpin);
950 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600951 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600952 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500953
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500954#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -0600955 pngtest_debug("Setting jmpbuf for write struct");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500956
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600957 if (setjmp(png_jmpbuf(write_ptr)))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600958 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600959 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
Cosmin Truta84395342019-02-03 21:00:49 -0500960 png_free(read_ptr, row_buf);
961 row_buf = NULL;
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -0500962 if (verbose != 0)
963 fprintf(STDERR, " destroying read structs\n");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -0500965 if (verbose != 0)
966 fprintf(STDERR, " destroying write structs\n");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500967 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500968 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500969 FCLOSE(fpin);
970 FCLOSE(fpout);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600971 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600972 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600973#endif
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500974#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600975
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -0500976#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600977 if (strict != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600978 {
979 /* Treat png_benign_error() as errors on read */
980 png_set_benign_errors(read_ptr, 0);
981
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -0500982# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600983 /* Treat them as errors on write */
984 png_set_benign_errors(write_ptr, 0);
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -0500985# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600986
987 /* if strict is not set, then app warnings and errors are treated as
988 * warnings in release builds, but not in unstable builds; this can be
989 * changed with '--relaxed'.
990 */
991 }
992
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600993 else if (relaxed != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600994 {
995 /* Allow application (pngtest) errors and warnings to pass */
996 png_set_benign_errors(read_ptr, 1);
997
Glenn Randers-Pehrsondbb5fce2016-12-24 16:12:20 -0600998 /* Turn off CRC checking while reading */
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -0500999 png_set_crc_action(read_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
1000
Glenn Randers-Pehrson8187ba12016-12-26 18:15:02 -06001001#ifdef PNG_IGNORE_ADLER32
Glenn Randers-Pehrsondbb5fce2016-12-24 16:12:20 -06001002 /* Turn off ADLER32 checking while reading */
1003 png_set_option(read_ptr, PNG_IGNORE_ADLER32, PNG_OPTION_ON);
1004#endif
1005
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -05001006# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001007 png_set_benign_errors(write_ptr, 1);
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -05001008# endif
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001009
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001010 }
Glenn Randers-Pehrsoncb1787f2016-09-11 22:02:57 -05001011#endif /* BENIGN_ERRORS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001012
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001013 pngtest_debug("Initializing input and output streams");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001014#ifdef PNG_STDIO_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001015 png_init_io(read_ptr, fpin);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001016# ifdef PNG_WRITE_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001017 png_init_io(write_ptr, fpout);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001018# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001019#else
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001020 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001021# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001022 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001023# ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001024 pngtest_flush);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001025# else
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001026 NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001027# endif
1028# endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001029#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001030
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001031 if (status_dots_requested == 1)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001032 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001033#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001034 png_set_write_status_fn(write_ptr, write_row_callback);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001035#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001036 png_set_read_status_fn(read_ptr, read_row_callback);
1037 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001039 else
1040 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001041#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001042 png_set_write_status_fn(write_ptr, NULL);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001043#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001044 png_set_read_status_fn(read_ptr, NULL);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001045 }
1046
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001047#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
John Bowler8a08b642015-11-23 20:15:51 -08001048 png_set_read_user_transform_fn(read_ptr, read_user_callback);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001049#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001050#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001051 zero_samples = 0;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001052 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1053#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001054
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001055#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
1056 /* Preserve all the unknown chunks, if possible. If this is disabled then,
1057 * even if the png_{get,set}_unknown_chunks stuff is enabled, we can't use
1058 * libpng to *save* the unknown chunks on read (because we can't switch the
1059 * save option on!)
1060 *
1061 * Notice that if SET_UNKNOWN_CHUNKS is *not* supported read will discard all
1062 * unknown chunks and write will write them all.
1063 */
1064#ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001065 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001066 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001067#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001068#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001069 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001070 NULL, 0);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001071#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001072#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001073
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001074 pngtest_debug("Reading info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001075 png_read_info(read_ptr, read_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001076
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001077#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1078 /* This is a bit of a hack; there is no obvious way in the callback function
1079 * to determine that the chunks before the first IDAT have been read, so
1080 * remove the info_ptr (which is only used to determine position relative to
1081 * PLTE) here to indicate that we are after the IDAT.
1082 */
1083 user_chunk_data.info_ptr = NULL;
1084#endif
1085
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001086 pngtest_debug("Transferring info struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087 {
1088 int interlace_type, compression_type, filter_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001089
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001091 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 {
1093 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001094 color_type, interlace_type, compression_type, filter_type);
John Bowler8a08b642015-11-23 20:15:51 -08001095 /* num_passes may not be available below if interlace support is not
1096 * provided by libpng for both read and write.
John Bowlera8472472013-12-29 10:50:51 -06001097 */
1098 switch (interlace_type)
1099 {
1100 case PNG_INTERLACE_NONE:
John Bowler8a08b642015-11-23 20:15:51 -08001101 num_passes = 1;
John Bowlera8472472013-12-29 10:50:51 -06001102 break;
1103
1104 case PNG_INTERLACE_ADAM7:
John Bowler8a08b642015-11-23 20:15:51 -08001105 num_passes = 7;
1106 break;
John Bowlera8472472013-12-29 10:50:51 -06001107
1108 default:
John Bowler8a08b642015-11-23 20:15:51 -08001109 png_error(read_ptr, "invalid interlace type");
1110 /*NOT REACHED*/
John Bowlera8472472013-12-29 10:50:51 -06001111 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 }
Glenn Randers-Pehrson59e655b2015-10-10 12:05:55 -05001113
John Bowler8a08b642015-11-23 20:15:51 -08001114 else
1115 png_error(read_ptr, "png_get_IHDR failed");
1116 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001117#ifdef PNG_FIXED_POINT_SUPPORTED
1118#ifdef PNG_cHRM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001120 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001121 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001122
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001123 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001124 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001126 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001127 red_y, green_x, green_y, blue_x, blue_y);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 }
1129 }
1130#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001131#ifdef PNG_gAMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001133 png_fixed_point gamma;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001134
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001135 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001136 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 }
1138#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001139#else /* Use floating point versions */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001140#ifdef PNG_FLOATING_POINT_SUPPORTED
1141#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001142 {
1143 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001144 blue_y;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001145
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001146 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001147 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001148 {
1149 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001150 red_y, green_x, green_y, blue_x, blue_y);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001151 }
1152 }
1153#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001154#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001155 {
1156 double gamma;
1157
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001158 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001159 png_set_gAMA(write_ptr, write_info_ptr, gamma);
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001160 }
1161#endif
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001162#endif /* Floating point */
1163#endif /* Fixed point */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001164#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001165 {
1166 png_charp name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001167 png_bytep profile;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001168 png_uint_32 proflen;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001169 int compression_type;
1170
1171 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001172 &profile, &proflen) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001173 {
1174 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001175 profile, proflen);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001176 }
1177 }
1178#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001179#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001180 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001181 int intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001182
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001183 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001184 png_set_sRGB(write_ptr, write_info_ptr, intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001185 }
1186#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001187 {
1188 png_colorp palette;
1189 int num_palette;
1190
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001191 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001192 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001193 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001194#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001195 {
1196 png_color_16p background;
1197
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001198 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001199 {
1200 png_set_bKGD(write_ptr, write_info_ptr, background);
1201 }
1202 }
1203#endif
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001204#ifdef PNG_READ_eXIf_SUPPORTED
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001205 {
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001206 png_bytep exif=NULL;
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001207 png_uint_32 exif_length;
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001208
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001209 if (png_get_eXIf_1(read_ptr, read_info_ptr, &exif_length, &exif) != 0)
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001210 {
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001211 if (exif_length > 1)
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001212 fprintf(STDERR," eXIf type %c%c, %lu bytes\n",exif[0],exif[1],
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001213 (unsigned long)exif_length);
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001214# ifdef PNG_WRITE_eXIf_SUPPORTED
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001215 png_set_eXIf_1(write_ptr, write_info_ptr, exif_length, exif);
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001216# endif
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001217 }
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001218 }
1219#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001220#ifdef PNG_hIST_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 {
1222 png_uint_16p hist;
1223
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001224 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001225 png_set_hIST(write_ptr, write_info_ptr, hist);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 }
1227#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001228#ifdef PNG_oFFs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001230 png_int_32 offset_x, offset_y;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001231 int unit_type;
1232
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001233 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001234 &unit_type) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 {
1236 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1237 }
1238 }
1239#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001240#ifdef PNG_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 {
1242 png_charp purpose, units;
1243 png_charpp params;
1244 png_int_32 X0, X1;
1245 int type, nparams;
1246
1247 if (png_get_pCAL(read_ptr, read_info_ptr, &purpose, &X0, &X1, &type,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001248 &nparams, &units, &params) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 {
1250 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001251 nparams, units, params);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001252 }
1253 }
1254#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001255#ifdef PNG_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 {
1257 png_uint_32 res_x, res_y;
1258 int unit_type;
1259
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001260 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1261 &unit_type) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001262 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 }
1264#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001265#ifdef PNG_sBIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 {
1267 png_color_8p sig_bit;
1268
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001269 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271 }
1272#endif
Glenn Randers-Pehrson9c1bb202009-09-17 10:55:49 -05001273#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson8d9e4942013-04-18 07:23:59 -05001274#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1275 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001276 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001277 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001278 double scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001279
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001280 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001281 &scal_height) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001282 {
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001283 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001284 }
1285 }
1286#else
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001287#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001288 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001289 int unit;
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001290 png_charp scal_width, scal_height;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001291
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001292 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001293 &scal_height) != 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001294 {
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001295 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1296 scal_height);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001297 }
1298 }
John Bowler8a08b642015-11-23 20:15:51 -08001299#endif
1300#endif
1301#endif
Glenn Randers-Pehrson59e655b2015-10-10 12:05:55 -05001302
1303#ifdef PNG_sPLT_SUPPORTED
1304 {
1305 png_sPLT_tp entries;
1306
1307 int num_entries = (int) png_get_sPLT(read_ptr, read_info_ptr, &entries);
1308 if (num_entries)
1309 {
1310 png_set_sPLT(write_ptr, write_info_ptr, entries, num_entries);
1311 }
1312 }
John Bowler8a08b642015-11-23 20:15:51 -08001313#endif
Glenn Randers-Pehrson59e655b2015-10-10 12:05:55 -05001314
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001315#ifdef PNG_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 {
1317 png_textp text_ptr;
1318 int num_text;
1319
1320 if (png_get_text(read_ptr, read_info_ptr, &text_ptr, &num_text) > 0)
1321 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001322 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001323
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001324 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1325
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001326 if (verbose != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001327 {
1328 int i;
1329
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001330 fprintf(STDERR,"\n");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001331 for (i=0; i<num_text; i++)
1332 {
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001333 fprintf(STDERR," Text compression[%d]=%d\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001334 i, text_ptr[i].compression);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001335 }
1336 }
Glenn Randers-Pehrson15ea1fa2011-11-23 15:28:01 -06001337
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1339 }
1340 }
John Bowler8a08b642015-11-23 20:15:51 -08001341#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001342#ifdef PNG_tIME_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001343 {
1344 png_timep mod_time;
1345
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001346 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001347 {
1348 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001349#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001350 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001351 tIME_string[(sizeof tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001352
John Bowler40b26032011-12-22 08:09:15 -06001353 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001354 {
1355 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1356 tIME_string[(sizeof tIME_string) - 1] = '\0';
1357 }
John Bowler40b26032011-12-22 08:09:15 -06001358
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001359 tIME_chunk_present++;
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001360#endif /* TIME_RFC1123 */
Glenn Randers-Pehrsonc9442291999-01-06 21:50:16 -06001361 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 }
John Bowler8a08b642015-11-23 20:15:51 -08001363#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001364#ifdef PNG_tRNS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001366 png_bytep trans_alpha;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367 int num_trans;
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001368 png_color_16p trans_color;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001369
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001370 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001371 &trans_color) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372 {
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001373 int sample_max = (1 << bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001374 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001375 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001376 (int)trans_color->gray > sample_max) ||
Glenn Randers-Pehrsond29033f2009-11-07 10:46:42 -06001377 (color_type == PNG_COLOR_TYPE_RGB &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001378 ((int)trans_color->red > sample_max ||
1379 (int)trans_color->green > sample_max ||
1380 (int)trans_color->blue > sample_max))))
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001381 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001382 trans_color);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 }
1384 }
John Bowler8a08b642015-11-23 20:15:51 -08001385#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001386#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001387 {
1388 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001389 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001390 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001391
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001392 if (num_unknowns != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001393 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001394 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001395 num_unknowns);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001396#if PNG_LIBPNG_VER < 10600
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001397 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001398 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1399 * because they are reset from the write pointer (removed in 1.6.0).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001400 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001401 {
1402 int i;
1403 for (i = 0; i < num_unknowns; i++)
1404 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001405 unknowns[i].location);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001406 }
1407#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001408 }
1409 }
1410#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001411
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001412#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001413 pngtest_debug("Writing info struct");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001414
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001415 /* Write the info in two steps so that if we write the 'unknown' chunks here
1416 * they go to the correct place.
1417 */
1418 png_write_info_before_PLTE(write_ptr, write_info_ptr);
1419
1420 write_chunks(write_ptr, before_PLTE); /* before PLTE */
1421
Andreas Dilger47a0c421997-05-16 02:46:07 -05001422 png_write_info(write_ptr, write_info_ptr);
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001423
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001424 write_chunks(write_ptr, before_IDAT); /* after PLTE */
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001425
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001426 png_write_info(write_ptr, write_end_info_ptr);
1427
1428 write_chunks(write_ptr, after_IDAT); /* after IDAT */
1429
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001430#ifdef PNG_COMPRESSION_COMPAT
1431 /* Test the 'compatibility' setting here, if it is available. */
1432 png_set_compression(write_ptr, PNG_COMPRESSION_COMPAT);
1433#endif
Glenn Randers-Pehrsond1209962006-06-21 19:40:52 -05001434#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001435
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001436#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001437 pngtest_debug("Allocating row buffer...");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001438 row_buf = (png_bytep)png_malloc(read_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001439 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001440
Cosmin Truta84395342019-02-03 21:00:49 -05001441 pngtest_debug1("\t%p", row_buf);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001442#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001443 pngtest_debug("Writing row data");
Guy Schalnat0d580581995-07-20 02:43:20 -05001444
John Bowler8a08b642015-11-23 20:15:51 -08001445#if defined(PNG_READ_INTERLACING_SUPPORTED) &&\
1446 defined(PNG_WRITE_INTERLACING_SUPPORTED)
1447 /* Both must be defined for libpng to be able to handle the interlace,
1448 * otherwise it gets handled below by simply reading and writing the passes
1449 * directly.
1450 */
1451 if (png_set_interlace_handling(read_ptr) != num_passes)
1452 png_error(write_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001453 "png_set_interlace_handling(read): wrong pass count ");
John Bowler8a08b642015-11-23 20:15:51 -08001454 if (png_set_interlace_handling(write_ptr) != num_passes)
1455 png_error(write_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001456 "png_set_interlace_handling(write): wrong pass count ");
John Bowler8a08b642015-11-23 20:15:51 -08001457#else /* png_set_interlace_handling not called on either read or write */
1458# define calc_pass_height
1459#endif /* not using libpng interlace handling */
Guy Schalnat0d580581995-07-20 02:43:20 -05001460
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001461#ifdef PNGTEST_TIMING
1462 t_stop = (float)clock();
1463 t_misc += (t_stop - t_start);
1464 t_start = t_stop;
1465#endif
John Bowler8a08b642015-11-23 20:15:51 -08001466 for (pass = 0; pass < num_passes; pass++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001467 {
John Bowler8a08b642015-11-23 20:15:51 -08001468# ifdef calc_pass_height
1469 png_uint_32 pass_height;
Glenn Randers-Pehrson92ec30a2015-11-24 07:34:37 -06001470
John Bowler8a08b642015-11-23 20:15:51 -08001471 if (num_passes == 7) /* interlaced */
1472 {
1473 if (PNG_PASS_COLS(width, pass) > 0)
1474 pass_height = PNG_PASS_ROWS(height, pass);
1475
1476 else
1477 pass_height = 0;
1478 }
1479
1480 else /* not interlaced */
1481 pass_height = height;
1482# else
1483# define pass_height height
1484# endif
1485
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001486 pngtest_debug1("Writing row data for pass %d", pass);
John Bowler8a08b642015-11-23 20:15:51 -08001487 for (y = 0; y < pass_height; y++)
Guy Schalnat0f716451995-11-28 11:22:13 -06001488 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001489#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001490 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05001491
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001492 row_buf = (png_bytep)png_malloc(read_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001493 png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001494
Cosmin Truta84395342019-02-03 21:00:49 -05001495 pngtest_debug2("\t%p (%lu bytes)", row_buf,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001496 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001497
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001498#endif /* !SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001499 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001500
1501#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001502#ifdef PNGTEST_TIMING
1503 t_stop = (float)clock();
1504 t_decode += (t_stop - t_start);
1505 t_start = t_stop;
1506#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001507 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05001508#ifdef PNGTEST_TIMING
1509 t_stop = (float)clock();
1510 t_encode += (t_stop - t_start);
1511 t_start = t_stop;
1512#endif
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001513#endif /* WRITE */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001514
1515#ifndef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001516 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001517 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001518 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001519#endif /* !SINGLE_ROWBUF_ALLOC */
Guy Schalnat0f716451995-11-28 11:22:13 -06001520 }
1521 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001522
Glenn Randers-Pehrsonb3721752013-09-30 13:56:44 -05001523#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1524# ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1525 png_free_data(read_ptr, read_info_ptr, PNG_FREE_UNKN, -1);
1526# endif
1527# ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
1528 png_free_data(write_ptr, write_info_ptr, PNG_FREE_UNKN, -1);
1529# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001530#endif
1531
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001532 pngtest_debug("Reading and writing end_info data");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001533
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534 png_read_end(read_ptr, end_info_ptr);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001535#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001536 {
1537 png_textp text_ptr;
1538 int num_text;
1539
1540 if (png_get_text(read_ptr, end_info_ptr, &text_ptr, &num_text) > 0)
1541 {
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001542 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001543
1544 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1545
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001546 if (verbose != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001547 {
1548 int i;
1549
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001550 fprintf(STDERR,"\n");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001551 for (i=0; i<num_text; i++)
1552 {
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001553 fprintf(STDERR," Text compression[%d]=%d\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001554 i, text_ptr[i].compression);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001555 }
1556 }
1557
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001558 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1559 }
1560 }
1561#endif
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001562#ifdef PNG_READ_eXIf_SUPPORTED
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001563 {
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001564 png_bytep exif=NULL;
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001565 png_uint_32 exif_length;
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001566
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001567 if (png_get_eXIf_1(read_ptr, end_info_ptr, &exif_length, &exif) != 0)
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001568 {
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001569 if (exif_length > 1)
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001570 fprintf(STDERR," eXIf type %c%c, %lu bytes\n",exif[0],exif[1],
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001571 (unsigned long)exif_length);
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001572# ifdef PNG_WRITE_eXIf_SUPPORTED
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -05001573 png_set_eXIf_1(write_ptr, write_end_info_ptr, exif_length, exif);
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -05001574# endif
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -05001575 }
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -05001576 }
1577#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001578#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001579 {
1580 png_timep mod_time;
1581
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001582 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001583 {
1584 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001585#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001586 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001587 tIME_string[(sizeof tIME_string) - 1] = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001588
John Bowler40b26032011-12-22 08:09:15 -06001589 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001590 {
1591 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1592 tIME_string[(sizeof tIME_string)-1] = '\0';
1593 }
John Bowler40b26032011-12-22 08:09:15 -06001594
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001595 tIME_chunk_present++;
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001596#endif /* TIME_RFC1123 */
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001597 }
1598 }
1599#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001600#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001601 {
1602 png_unknown_chunkp unknowns;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001603 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001604 &unknowns);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001605
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001606 if (num_unknowns != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001607 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001608 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001609 num_unknowns);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001610#if PNG_LIBPNG_VER < 10600
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001611 /* Copy the locations from the read_info_ptr. The automatically
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001612 * generated locations in write_end_info_ptr are wrong prior to 1.6.0
1613 * because they are reset from the write pointer (removed in 1.6.0).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001614 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001615 {
1616 int i;
1617 for (i = 0; i < num_unknowns; i++)
1618 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001619 unknowns[i].location);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001620 }
1621#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001622 }
1623 }
1624#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001625
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001626#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001627#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1628 /* Normally one would use Z_DEFAULT_STRATEGY for text compression.
1629 * This is here just to make pngtest replicate the results from libpng
1630 * versions prior to 1.5.4, and to test this new API.
1631 */
1632 png_set_text_compression_strategy(write_ptr, Z_FILTERED);
1633#endif
1634
1635 /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
1636 * do it is to write them *before* calling png_write_end. When unknown
1637 * chunks are written by libpng, however, they are written just before IEND.
1638 * There seems to be no way round this, however vpAg/sTER are not expected
1639 * after IDAT.
1640 */
1641 write_chunks(write_ptr, after_IDAT);
1642
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001643 png_write_end(write_ptr, write_end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001644#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001645
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001646#ifdef PNG_EASY_ACCESS_SUPPORTED
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001647 if (verbose != 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001648 {
1649 png_uint_32 iwidth, iheight;
1650 iwidth = png_get_image_width(write_ptr, write_info_ptr);
1651 iheight = png_get_image_height(write_ptr, write_info_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001652 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001653 (unsigned long)iwidth, (unsigned long)iheight);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001654 }
1655#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001656
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001657 pngtest_debug("Destroying data structs");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001658#ifdef SINGLE_ROWBUF_ALLOC
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001659 pngtest_debug("destroying row_buf for read_ptr");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001660 png_free(read_ptr, row_buf);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05001661 row_buf = NULL;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001662#endif /* SINGLE_ROWBUF_ALLOC */
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001663 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001664 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001665#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001666 pngtest_debug("destroying write_end_info_ptr");
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001667 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001668 pngtest_debug("destroying write_ptr, write_info_ptr");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001669 png_destroy_write_struct(&write_ptr, &write_info_ptr);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001670#endif
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001671 pngtest_debug("Destruction complete.");
Guy Schalnat0d580581995-07-20 02:43:20 -05001672
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001673 FCLOSE(fpin);
1674 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001675
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001676 /* Summarize any warnings or errors and in 'strict' mode fail the test.
1677 * Unsupported chunks can result in warnings, in that case ignore the strict
1678 * setting, otherwise fail the test on warnings as well as errors.
1679 */
1680 if (error_count > 0)
1681 {
1682 /* We don't really expect to get here because of the setjmp handling
1683 * above, but this is safe.
1684 */
1685 fprintf(STDERR, "\n %s: %d libpng errors found (%d warnings)",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001686 inname, error_count, warning_count);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001687
1688 if (strict != 0)
1689 return (1);
1690 }
1691
1692# ifdef PNG_WRITE_SUPPORTED
John Bowler8a08b642015-11-23 20:15:51 -08001693 /* If there is no write support nothing was written! */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001694 else if (unsupported_chunks > 0)
1695 {
1696 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001697 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001698 }
1699# endif
1700
1701 else if (warning_count > 0)
1702 {
1703 fprintf(STDERR, "\n %s: %d libpng warnings found",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001704 inname, warning_count);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001705
1706 if (strict != 0)
1707 return (1);
1708 }
1709
Glenn Randers-Pehrson31f92b02010-03-09 16:47:59 -06001710 pngtest_debug("Opening files for comparison");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001711 if ((fpin = fopen(inname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001712 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001713 fprintf(STDERR, "Could not find file %s\n", inname);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001714 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001715 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001716
Andreas Dilger47a0c421997-05-16 02:46:07 -05001717 if ((fpout = fopen(outname, "rb")) == NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -06001718 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001719 fprintf(STDERR, "Could not find file %s\n", outname);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001720 FCLOSE(fpin);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001721 return (1);
Guy Schalnat0f716451995-11-28 11:22:13 -06001722 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001723
John Bowler8a08b642015-11-23 20:15:51 -08001724#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1725 defined (PNG_WRITE_FILTER_SUPPORTED)
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001726 if (interlace_preserved != 0) /* else the files will be changed */
Guy Schalnat0f716451995-11-28 11:22:13 -06001727 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001728 for (;;)
Guy Schalnat0f716451995-11-28 11:22:13 -06001729 {
John Bowler81dfd002013-12-01 15:07:09 -06001730 static int wrote_question = 0;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04001731 size_t num_in, num_out;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001732 char inbuf[256], outbuf[256];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001733
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06001734 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1735 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001736
1737 if (num_in != num_out)
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001738 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001739 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001740 inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001741
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001742 if (wrote_question == 0 && unsupported_chunks == 0)
1743 {
1744 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001745 " Was %s written with the same maximum IDAT"
1746 " chunk size (%d bytes),",
1747 inname, PNG_ZBUF_SIZE);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001748 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001749 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001750 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001751 " level (zlib default),\n and zlib version (%s)?\n\n",
1752 ZLIB_VERSION);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001753 wrote_question = 1;
1754 }
1755
1756 FCLOSE(fpin);
1757 FCLOSE(fpout);
1758
1759 if (strict != 0 && unsupported_chunks == 0)
1760 return (1);
1761
1762 else
1763 return (0);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06001764 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001765
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001766 if (num_in == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001767 break;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001768
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001769 if (memcmp(inbuf, outbuf, num_in))
1770 {
1771 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001772 outname);
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001773
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001774 if (wrote_question == 0 && unsupported_chunks == 0)
1775 {
1776 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001777 " Was %s written with the same maximum"
1778 " IDAT chunk size (%d bytes),",
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001779 inname, PNG_ZBUF_SIZE);
1780 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001781 "\n filtering heuristic (libpng default), compression");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001782 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001783 " level (zlib default),\n and zlib version (%s)?\n\n",
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001784 ZLIB_VERSION);
1785 wrote_question = 1;
1786 }
1787
1788 FCLOSE(fpin);
1789 FCLOSE(fpout);
1790
1791 /* NOTE: the unsupported_chunks escape is permitted here because
1792 * unsupported text chunk compression will result in the compression
1793 * mode being changed (to NONE) yet, in the test case, the result
1794 * can be exactly the same size!
1795 */
1796 if (strict != 0 && unsupported_chunks == 0)
1797 return (1);
1798
1799 else
1800 return (0);
1801 }
Guy Schalnat0f716451995-11-28 11:22:13 -06001802 }
1803 }
John Bowler8a08b642015-11-23 20:15:51 -08001804#endif /* WRITE && WRITE_FILTER */
Guy Schalnat0f716451995-11-28 11:22:13 -06001805
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001806 FCLOSE(fpin);
1807 FCLOSE(fpout);
Guy Schalnat0d580581995-07-20 02:43:20 -05001808
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001809 return (0);
Guy Schalnat0d580581995-07-20 02:43:20 -05001810}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001811
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001812/* Input and output filenames */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001813#ifdef RISCOS
Cosmin Truta1ef88822018-08-18 21:01:02 -04001814static const char *inname = "pngtest/png";
1815static const char *outname = "pngout/png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001816#else
Cosmin Truta1ef88822018-08-18 21:01:02 -04001817static const char *inname = "pngtest.png";
1818static const char *outname = "pngout.png";
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001819#endif
1820
1821int
1822main(int argc, char *argv[])
1823{
1824 int multiple = 0;
1825 int ierror = 0;
1826
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06001827 png_structp dummy_ptr;
1828
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001829 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001830 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001831 fprintf(STDERR, "%s", png_get_copyright(NULL));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001832 /* Show the version of libpng used in building the library */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001833 fprintf(STDERR, " library (%lu):%s",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001834 (unsigned long)png_access_version_number(),
1835 png_get_header_version(NULL));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001836
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001837 /* Show the version of libpng used in building the application */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001838 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001839 PNG_HEADER_VERSION_STRING);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001840
1841 /* Do some consistency checking on the memory allocation settings, I'm
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001842 * not sure this matters, but it is nice to know, the first of these
1843 * tests should be impossible because of the way the macros are set
1844 * in pngconf.h
1845 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001846#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001847 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001848#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001849 /* I think the following can happen. */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001850#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001851 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001852#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001853
1854 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1855 {
1856 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001857 "Warning: versions are different between png.h and png.c\n");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001858 fprintf(STDERR, " png.h version: %s\n", PNG_LIBPNG_VER_STRING);
1859 fprintf(STDERR, " png.c version: %s\n\n", png_libpng_ver);
1860 ++ierror;
1861 }
1862
1863 if (argc > 1)
1864 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001865 if (strcmp(argv[1], "-m") == 0)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001866 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001867 multiple = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001868 status_dots_requested = 0;
1869 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001870
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001871 else if (strcmp(argv[1], "-mv") == 0 ||
1872 strcmp(argv[1], "-vm") == 0 )
1873 {
1874 multiple = 1;
1875 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001876 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001877 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001878
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001879 else if (strcmp(argv[1], "-v") == 0)
1880 {
1881 verbose = 1;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001882 status_dots_requested = 1;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001883 inname = argv[2];
1884 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001885
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001886 else if (strcmp(argv[1], "--strict") == 0)
1887 {
1888 status_dots_requested = 0;
1889 verbose = 1;
1890 inname = argv[2];
1891 strict++;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001892 relaxed = 0;
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001893 multiple=1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001894 }
1895
1896 else if (strcmp(argv[1], "--relaxed") == 0)
1897 {
1898 status_dots_requested = 0;
1899 verbose = 1;
1900 inname = argv[2];
1901 strict = 0;
1902 relaxed++;
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001903 multiple=1;
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001904 }
Glenn Randers-Pehrson20739282017-08-04 14:17:28 -05001905 else if (strcmp(argv[1], "--xfail") == 0)
1906 {
1907 status_dots_requested = 0;
1908 verbose = 1;
1909 inname = argv[2];
1910 strict = 0;
1911 xfail++;
1912 relaxed++;
Glenn Randers-Pehrsonc33d6242017-08-05 11:01:18 -05001913 multiple=1;
Glenn Randers-Pehrson20739282017-08-04 14:17:28 -05001914 }
Glenn Randers-Pehrson8e25a612011-09-26 20:57:33 -05001915
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001916 else
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001917 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001918 inname = argv[1];
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001919 status_dots_requested = 0;
1920 }
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001921 }
1922
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05001923 if (multiple == 0 && argc == 3 + verbose)
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001924 outname = argv[2 + verbose];
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001925
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05001926 if ((multiple == 0 && argc > 3 + verbose) ||
1927 (multiple != 0 && argc < 2))
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001928 {
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001929 fprintf(STDERR,
1930 "usage: %s [infile.png] [outfile.png]\n\t%s -m {infile.png}\n",
1931 argv[0], argv[0]);
1932 fprintf(STDERR,
1933 " reads/writes one PNG file (without -m) or multiple files (-m)\n");
1934 fprintf(STDERR,
1935 " with -m %s is used as a temporary file\n", outname);
1936 exit(1);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001937 }
1938
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001939 if (multiple != 0)
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001940 {
1941 int i;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001942#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001943 int allocation_now = current_allocation;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001944#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001945 for (i=2; i<argc; ++i)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001946 {
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001947 int kerror;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05001948 fprintf(STDERR, "\n Testing %s:", argv[i]);
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05001949#if PNG_DEBUG > 0
1950 fprintf(STDERR, "\n");
1951#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001952 kerror = test_one_file(argv[i], outname);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001953 if (kerror == 0)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001954 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001955#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001956 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001957 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001958#else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001959 fprintf(STDERR, " PASS\n");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001960#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001961#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrsond2fedd62015-05-09 21:47:00 -05001962 if (tIME_chunk_present != 0)
1963 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001964
Glenn Randers-Pehrsond2fedd62015-05-09 21:47:00 -05001965 tIME_chunk_present = 0;
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001966#endif /* TIME_RFC1123 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001967 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001968
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001969 else
1970 {
Glenn Randers-Pehrson20739282017-08-04 14:17:28 -05001971 if (xfail)
1972 fprintf(STDERR, " XFAIL\n");
1973 else
1974 {
1975 fprintf(STDERR, " FAIL\n");
1976 ierror += kerror;
1977 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001978 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05001979#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001980 if (allocation_now != current_allocation)
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001981 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1982 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001983
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001984 if (current_allocation != 0)
1985 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001986 memory_infop pinfo = pinformation;
1987
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05001988 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1989 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001990
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001991 while (pinfo != NULL)
1992 {
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05001993 fprintf(STDERR, " %lu bytes at %p\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001994 (unsigned long)pinfo->size,
1995 pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001996 pinfo = pinfo->next;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001997 }
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06001998 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001999#endif
2000 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05002001#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002002 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2003 current_allocation);
2004 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2005 maximum_allocation);
2006 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2007 total_allocation);
2008 fprintf(STDERR, " Number of allocations: %10d\n",
2009 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002010#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002011 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002012
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002013 else
2014 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002015 int i;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002016 for (i = 0; i<3; ++i)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002017 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002018 int kerror;
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05002019#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002020 int allocation_now = current_allocation;
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002021#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002022 if (i == 1)
2023 status_dots_requested = 1;
2024
2025 else if (verbose == 0)
2026 status_dots_requested = 0;
2027
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002028 if (i == 0 || verbose == 1 || ierror != 0)
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05002029 {
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05002030 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05002031#if PNG_DEBUG > 0
2032 fprintf(STDERR, "\n");
2033#endif
2034 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002035
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002036 kerror = test_one_file(inname, outname);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002037
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002038 if (kerror == 0)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002039 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002040 if (verbose == 1 || i == 2)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002041 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002042#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05002043 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002044 (unsigned long)zero_samples);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06002045#else
2046 fprintf(STDERR, " PASS\n");
2047#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002048#ifdef PNG_TIME_RFC1123_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002049 if (tIME_chunk_present != 0)
2050 fprintf(STDERR, " tIME = %s\n", tIME_string);
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06002051#endif /* TIME_RFC1123 */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002052 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002053 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002054
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002055 else
2056 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002057 if (verbose == 0 && i != 2)
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05002058 {
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05002059 fprintf(STDERR, "\n Testing %s:", inname);
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05002060#if PNG_DEBUG > 0
2061 fprintf(STDERR, "\n");
2062#endif
2063 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002064
Glenn Randers-Pehrson20739282017-08-04 14:17:28 -05002065 if (xfail)
2066 fprintf(STDERR, " XFAIL\n");
2067 else
2068 {
2069 fprintf(STDERR, " FAIL\n");
2070 ierror += kerror;
2071 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002072 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05002073#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002074 if (allocation_now != current_allocation)
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002075 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
2076 current_allocation - allocation_now);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002077
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002078 if (current_allocation != 0)
2079 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002080 memory_infop pinfo = pinformation;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002081
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002082 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
2083 current_allocation);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002084
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002085 while (pinfo != NULL)
2086 {
Glenn Randers-Pehrson7f682632014-10-28 10:22:37 -05002087 fprintf(STDERR, " %lu bytes at %p\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002088 (unsigned long)pinfo->size, pinfo->pointer);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002089 pinfo = pinfo->next;
2090 }
2091 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002092#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002093 }
Glenn Randers-Pehrson37f116a2004-08-15 07:15:39 -05002094#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002095 fprintf(STDERR, " Current memory allocation: %10d bytes\n",
2096 current_allocation);
2097 fprintf(STDERR, " Maximum memory allocation: %10d bytes\n",
2098 maximum_allocation);
2099 fprintf(STDERR, " Total memory allocation: %10d bytes\n",
2100 total_allocation);
2101 fprintf(STDERR, " Number of allocations: %10d\n",
2102 num_allocations);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002103#endif
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002104 }
2105
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05002106#ifdef PNGTEST_TIMING
2107 t_stop = (float)clock();
2108 t_misc += (t_stop - t_start);
2109 t_start = t_stop;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002110 fprintf(STDERR, " CPU time used = %.3f seconds",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002111 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002112 fprintf(STDERR, " (decoding %.3f,\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002113 t_decode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002114 fprintf(STDERR, " encoding %.3f ,",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002115 t_encode/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002116 fprintf(STDERR, " other %.3f seconds)\n\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002117 t_misc/(float)CLOCKS_PER_SEC);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05002118#endif
2119
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002120 if (ierror == 0)
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05002121 fprintf(STDERR, " libpng passes test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002122
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002123 else
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -05002124 fprintf(STDERR, " libpng FAILS test\n");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002125
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06002126 dummy_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
2127 fprintf(STDERR, " Default limits:\n");
2128 fprintf(STDERR, " width_max = %lu\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002129 (unsigned long) png_get_user_width_max(dummy_ptr));
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06002130 fprintf(STDERR, " height_max = %lu\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002131 (unsigned long) png_get_user_height_max(dummy_ptr));
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06002132 if (png_get_chunk_cache_max(dummy_ptr) == 0)
2133 fprintf(STDERR, " cache_max = unlimited\n");
2134 else
2135 fprintf(STDERR, " cache_max = %lu\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002136 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06002137 if (png_get_chunk_malloc_max(dummy_ptr) == 0)
2138 fprintf(STDERR, " malloc_max = unlimited\n");
2139 else
2140 fprintf(STDERR, " malloc_max = %lu\n",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002141 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
Glenn Randers-Pehrson4b65a892015-02-16 22:52:07 -06002142 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2143
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002144 return (int)(ierror != 0);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002145}
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002146#else
2147int
2148main(void)
2149{
2150 fprintf(STDERR,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002151 " test ignored because libpng was not built with read support\n");
John Bowlere4413a72013-04-17 21:27:47 -05002152 /* And skip this test */
Glenn Randers-Pehrson8aa16382016-09-26 08:09:44 -05002153 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002154}
2155#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002156
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05002157/* Generate a compiler error if there is an old png.h in the search path. */
Cosmin Trutaa40189c2019-04-14 14:10:32 -04002158typedef png_libpng_version_1_6_37 Your_png_h_is_not_version_1_6_37;