blob: dd5113dbcb96c147d18c219b0a124de65d9f332f [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngtest.c - a simple test program to test libpng
3 *
xNombred07bb0d2020-03-10 20:17:12 +01004 * Copyright (c) 2018-2019 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
The Android Open Source Project893912b2009-03-03 19:30:05 -08008 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04009 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
The Android Open Source Project893912b2009-03-03 19:30:05 -080013 * 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 *
19 * The program will report "FAIL" in certain legitimate cases:
20 * 1) when the compression level or filter selection method is changed.
21 * 2) when the maximum IDAT size (PNG_ZBUF_SIZE in pngconf.h) is not 8192.
22 * 3) unknown unsafe-to-copy ancillary chunks or unknown critical chunks
23 * exist in the input file.
24 * 4) others not listed here...
25 * In these cases, it is best to check with another tool such as "pngcheck"
26 * to see what the differences between the two files are.
27 *
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
30 * 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 ..."
32 */
33
Chris Craikb50c2172013-07-29 15:28:30 -070034#define _POSIX_SOURCE 1
35
36#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
The Android Open Source Project893912b2009-03-03 19:30:05 -080044#include "png.h"
45
Chris Craikb50c2172013-07-29 15:28:30 -070046/* 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
Sireesh Tripurarib478e662014-05-09 15:15:10 +053048 *
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).
Chris Craikb50c2172013-07-29 15:28:30 -070053 */
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 &&\
Matt Sarett06f10872016-01-04 12:56:20 -050064 defined PNG_READ_sPLT_SUPPORTED &&\
Chris Craikb50c2172013-07-29 15:28:30 -070065 defined PNG_READ_tEXt_SUPPORTED &&\
66 defined PNG_READ_tIME_SUPPORTED &&\
Sireesh Tripurarib478e662014-05-09 15:15:10 +053067 defined PNG_READ_zTXt_SUPPORTED &&\
Matt Sarett06f10872016-01-04 12:56:20 -050068 (defined PNG_WRITE_INTERLACING_SUPPORTED || PNG_LIBPNG_VER >= 10700)
Chris Craikb50c2172013-07-29 15:28:30 -070069
Sireesh Tripurarib478e662014-05-09 15:15:10 +053070#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
Chris Craikb50c2172013-07-29 15:28:30 -070076/* Copied from pngpriv.h but only used in error messages below. */
77#ifndef PNG_ZBUF_SIZE
78# define PNG_ZBUF_SIZE 8192
The Android Open Source Project893912b2009-03-03 19:30:05 -080079#endif
Chris Craikb50c2172013-07-29 15:28:30 -070080#define FCLOSE(file) fclose(file)
The Android Open Source Project893912b2009-03-03 19:30:05 -080081
Patrick Scott5f6bd842010-06-28 16:55:16 -040082#ifndef PNG_STDIO_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -070083typedef FILE * png_FILE_p;
The Android Open Source Project893912b2009-03-03 19:30:05 -080084#endif
85
Chris Craikb50c2172013-07-29 15:28:30 -070086/* Makes pngtest verbose so we can find problems. */
The Android Open Source Project893912b2009-03-03 19:30:05 -080087#ifndef PNG_DEBUG
88# define PNG_DEBUG 0
89#endif
90
Chris Craikb50c2172013-07-29 15:28:30 -070091#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
100
The Android Open Source Project893912b2009-03-03 19:30:05 -0800101#if !PNG_DEBUG
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400102# define SINGLE_ROWBUF_ALLOC /* Makes buffer overruns easier to nail */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800103#endif
104
Matt Sarett06f10872016-01-04 12:56:20 -0500105#ifndef PNG_UNUSED
106# define PNG_UNUSED(param) (void)param;
107#endif
108
The Android Open Source Project893912b2009-03-03 19:30:05 -0800109/* Turn on CPU timing
110#define PNGTEST_TIMING
111*/
112
Patrick Scott5f6bd842010-06-28 16:55:16 -0400113#ifndef PNG_FLOATING_POINT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800114#undef PNGTEST_TIMING
115#endif
116
117#ifdef PNGTEST_TIMING
118static float t_start, t_stop, t_decode, t_encode, t_misc;
119#include <time.h>
120#endif
121
Patrick Scott5f6bd842010-06-28 16:55:16 -0400122#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700123#define PNG_tIME_STRING_LENGTH 29
124static int tIME_chunk_present = 0;
125static char tIME_string[PNG_tIME_STRING_LENGTH] = "tIME chunk is not present";
Matt Sarett06f10872016-01-04 12:56:20 -0500126
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{
Alex Naidis7a055fd2016-10-01 12:23:07 +0200133 png_const_charp str = png_convert_to_rfc1123(png_ptr, t);
Matt Sarett06f10872016-01-04 12:56:20 -0500134
Alex Naidis7a055fd2016-10-01 12:23:07 +0200135 if (str == NULL)
136 return 0;
Matt Sarett06f10872016-01-04 12:56:20 -0500137
Alex Naidis7a055fd2016-10-01 12:23:07 +0200138 strcpy(ts, str);
139 return 1;
Matt Sarett06f10872016-01-04 12:56:20 -0500140}
141#endif /* older libpng */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800142#endif
143
144static int verbose = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700145static int strict = 0;
146static int relaxed = 0;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400147static int xfail = 0;
Chris Craikb50c2172013-07-29 15:28:30 -0700148static 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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800151
The Android Open Source Project893912b2009-03-03 19:30:05 -0800152/* 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
Chris Craikb50c2172013-07-29 15:28:30 -0700157/* 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
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400168/* Example of using row callbacks to make a simple progress meter */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700169static int status_pass = 1;
170static int status_dots_requested = 0;
171static int status_dots = 1;
172
Chris Craikb50c2172013-07-29 15:28:30 -0700173static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800174read_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
175{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400176 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX)
177 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700178
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400179 if (status_pass != pass)
180 {
181 fprintf(stdout, "\n Pass %d: ", pass);
182 status_pass = pass;
183 status_dots = 31;
184 }
Chris Craikb50c2172013-07-29 15:28:30 -0700185
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400186 status_dots--;
Chris Craikb50c2172013-07-29 15:28:30 -0700187
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400188 if (status_dots == 0)
189 {
190 fprintf(stdout, "\n ");
191 status_dots=30;
192 }
Chris Craikb50c2172013-07-29 15:28:30 -0700193
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400194 fprintf(stdout, "r");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800195}
196
Chris Craikb50c2172013-07-29 15:28:30 -0700197#ifdef PNG_WRITE_SUPPORTED
198static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800199write_row_callback(png_structp png_ptr, png_uint_32 row_number, int pass)
200{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400201 if (png_ptr == NULL || row_number > PNG_UINT_31_MAX || pass > 7)
202 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700203
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400204 fprintf(stdout, "w");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800205}
Chris Craikb50c2172013-07-29 15:28:30 -0700206#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800207
208
Patrick Scott5f6bd842010-06-28 16:55:16 -0400209#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Matt Sarett06f10872016-01-04 12:56:20 -0500210/* Example of using a user transform callback (doesn't do anything at present).
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400211 */
Chris Craikb50c2172013-07-29 15:28:30 -0700212static void PNGCBAPI
Matt Sarett06f10872016-01-04 12:56:20 -0500213read_user_callback(png_structp png_ptr, png_row_infop row_info, png_bytep data)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800214{
Matt Sarett06f10872016-01-04 12:56:20 -0500215 PNG_UNUSED(png_ptr)
216 PNG_UNUSED(row_info)
217 PNG_UNUSED(data)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800218}
219#endif
220
Patrick Scott5f6bd842010-06-28 16:55:16 -0400221#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400222/* Example of using user transform callback (we don't transform anything,
223 * but merely count the zero samples)
224 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800225
226static png_uint_32 zero_samples;
227
Chris Craikb50c2172013-07-29 15:28:30 -0700228static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800229count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
230{
231 png_bytep dp = data;
Chris Craikb50c2172013-07-29 15:28:30 -0700232 if (png_ptr == NULL)
233 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800234
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400235 /* Contents of row_info:
The Android Open Source Project893912b2009-03-03 19:30:05 -0800236 * 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
Alex Naidis7a055fd2016-10-01 12:23:07 +0200244 /* Counts the number of zero samples (or zero pixels if color_type is 3 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800245
Alex Naidis7a055fd2016-10-01 12:23:07 +0200246 if (row_info->color_type == 0 || row_info->color_type == 3)
247 {
248 int pos = 0;
249 png_uint_32 n, nstop;
Chris Craikb50c2172013-07-29 15:28:30 -0700250
Alex Naidis7a055fd2016-10-01 12:23:07 +0200251 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++;
Chris Craikb50c2172013-07-29 15:28:30 -0700257
Alex Naidis7a055fd2016-10-01 12:23:07 +0200258 if (pos == 8)
259 {
260 pos = 0;
261 dp++;
262 }
263 }
Chris Craikb50c2172013-07-29 15:28:30 -0700264
Alex Naidis7a055fd2016-10-01 12:23:07 +0200265 if (row_info->bit_depth == 2)
266 {
267 if (((*dp << (pos+=2)) & 0xc0) == 0)
268 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700269
Alex Naidis7a055fd2016-10-01 12:23:07 +0200270 if (pos == 8)
271 {
272 pos = 0;
273 dp++;
274 }
275 }
Chris Craikb50c2172013-07-29 15:28:30 -0700276
Alex Naidis7a055fd2016-10-01 12:23:07 +0200277 if (row_info->bit_depth == 4)
278 {
279 if (((*dp << (pos+=4)) & 0xf0) == 0)
280 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700281
Alex Naidis7a055fd2016-10-01 12:23:07 +0200282 if (pos == 8)
283 {
284 pos = 0;
285 dp++;
286 }
287 }
Chris Craikb50c2172013-07-29 15:28:30 -0700288
Alex Naidis7a055fd2016-10-01 12:23:07 +0200289 if (row_info->bit_depth == 8)
290 if (*dp++ == 0)
291 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700292
Alex Naidis7a055fd2016-10-01 12:23:07 +0200293 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--;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800308
Alex Naidis7a055fd2016-10-01 12:23:07 +0200309 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++;
Chris Craikb50c2172013-07-29 15:28:30 -0700316
Alex Naidis7a055fd2016-10-01 12:23:07 +0200317 if (row_info->bit_depth == 16)
318 {
319 if ((*dp | *(dp+1)) == 0)
320 zero_samples++;
Chris Craikb50c2172013-07-29 15:28:30 -0700321
Alex Naidis7a055fd2016-10-01 12:23:07 +0200322 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 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800333}
Matt Sarett9b1fe632015-11-25 10:21:17 -0500334#endif /* WRITE_USER_TRANSFORM */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800335
Patrick Scott5f6bd842010-06-28 16:55:16 -0400336#ifndef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800337/* START of code to validate stdio-free compilation */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400338/* 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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800345
Chris Craikb50c2172013-07-29 15:28:30 -0700346#ifdef PNG_IO_STATE_SUPPORTED
347void
xNombred07bb0d2020-03-10 20:17:12 +0100348pngtest_check_io_state(png_structp png_ptr, size_t data_length,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200349 png_uint_32 io_op);
Chris Craikb50c2172013-07-29 15:28:30 -0700350void
xNombred07bb0d2020-03-10 20:17:12 +0100351pngtest_check_io_state(png_structp png_ptr, size_t data_length,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200352 png_uint_32 io_op)
Chris Craikb50c2172013-07-29 15:28:30 -0700353{
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 }
Matt Sarett9b1fe632015-11-25 10:21:17 -0500383 if (err != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700384 png_error(png_ptr, "Bad I/O state or buffer size");
385}
386#endif
387
388static void PNGCBAPI
xNombred07bb0d2020-03-10 20:17:12 +0100389pngtest_read_data(png_structp png_ptr, png_bytep data, size_t length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800390{
xNombred07bb0d2020-03-10 20:17:12 +0100391 size_t check = 0;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400392 png_voidp io_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800393
xNombred07bb0d2020-03-10 20:17:12 +0100394 /* fread() returns 0 on error, so it is OK to store this in a size_t
The Android Open Source Project893912b2009-03-03 19:30:05 -0800395 * instead of an int, which is what fread() actually returns.
396 */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400397 io_ptr = png_get_io_ptr(png_ptr);
398 if (io_ptr != NULL)
399 {
Chris Craikb50c2172013-07-29 15:28:30 -0700400 check = fread(data, 1, length, (png_FILE_p)io_ptr);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400401 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800402
403 if (check != length)
404 {
Chris Craikb50c2172013-07-29 15:28:30 -0700405 png_error(png_ptr, "Read Error");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800406 }
Chris Craikb50c2172013-07-29 15:28:30 -0700407
408#ifdef PNG_IO_STATE_SUPPORTED
409 pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
410#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800411}
The Android Open Source Project893912b2009-03-03 19:30:05 -0800412
Patrick Scott5f6bd842010-06-28 16:55:16 -0400413#ifdef PNG_WRITE_FLUSH_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700414static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800415pngtest_flush(png_structp png_ptr)
416{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400417 /* Do nothing; fflush() is said to be just a waste of energy. */
Chris Craikb50c2172013-07-29 15:28:30 -0700418 PNG_UNUSED(png_ptr) /* Stifle compiler warning */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800419}
420#endif
421
422/* This is the function that does the actual writing of data. If you are
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400423 * 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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700427static void PNGCBAPI
xNombred07bb0d2020-03-10 20:17:12 +0100428pngtest_write_data(png_structp png_ptr, png_bytep data, size_t length)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800429{
xNombred07bb0d2020-03-10 20:17:12 +0100430 size_t check;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800431
Chris Craikb50c2172013-07-29 15:28:30 -0700432 check = fwrite(data, 1, length, (png_FILE_p)png_get_io_ptr(png_ptr));
433
The Android Open Source Project893912b2009-03-03 19:30:05 -0800434 if (check != length)
435 {
436 png_error(png_ptr, "Write Error");
437 }
Chris Craikb50c2172013-07-29 15:28:30 -0700438
439#ifdef PNG_IO_STATE_SUPPORTED
440 pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
441#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800442}
Matt Sarett9b1fe632015-11-25 10:21:17 -0500443#endif /* !STDIO */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800444
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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700450typedef struct
451{
xNombred07bb0d2020-03-10 20:17:12 +0100452 const char *file_name;
Chris Craikb50c2172013-07-29 15:28:30 -0700453} pngtest_error_parameters;
454
455static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800456pngtest_warning(png_structp png_ptr, png_const_charp message)
457{
xNombred07bb0d2020-03-10 20:17:12 +0100458 const char *name = "UNKNOWN (ERROR!)";
Chris Craikb50c2172013-07-29 15:28:30 -0700459 pngtest_error_parameters *test =
460 (pngtest_error_parameters*)png_get_error_ptr(png_ptr);
461
462 ++warning_count;
463
464 if (test != NULL && test->file_name != NULL)
465 name = test->file_name;
466
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400467 fprintf(STDERR, "\n%s: libpng warning: %s\n", name, message);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800468}
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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700475static void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800476pngtest_error(png_structp png_ptr, png_const_charp message)
477{
Chris Craikb50c2172013-07-29 15:28:30 -0700478 ++error_count;
479
The Android Open Source Project893912b2009-03-03 19:30:05 -0800480 pngtest_warning(png_ptr, message);
481 /* We can return because png_error calls the default handler, which is
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400482 * actually OK in this case.
483 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800484}
Chris Craikb50c2172013-07-29 15:28:30 -0700485
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700486/* END of code to validate stdio-free compilation */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800487
488/* START of code to validate memory allocation and deallocation */
489#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
490
491/* Allocate memory. For reasonable files, size should never exceed
Matt Sarett9b1fe632015-11-25 10:21:17 -0500492 * 64K. However, zlib may allocate more than 64K if you don't tell
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400493 * 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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800500typedef struct memory_information
501{
Chris Craikb50c2172013-07-29 15:28:30 -0700502 png_alloc_size_t size;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800503 png_voidp pointer;
Chris Craikb50c2172013-07-29 15:28:30 -0700504 struct memory_information *next;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800505} memory_information;
Chris Craikb50c2172013-07-29 15:28:30 -0700506typedef memory_information *memory_infop;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800507
508static memory_infop pinformation = NULL;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400509static int current_allocation = 0;
510static int maximum_allocation = 0;
511static int total_allocation = 0;
512static int num_allocations = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800513
Chris Craikb50c2172013-07-29 15:28:30 -0700514png_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));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800517
518png_voidp
Chris Craikb50c2172013-07-29 15:28:30 -0700519PNGCBAPI png_debug_malloc(png_structp png_ptr, png_alloc_size_t size)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800520{
521
522 /* png_malloc has already tested for NULL; png_create_struct calls
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400523 * png_debug_malloc directly, with png_ptr == NULL which is OK
524 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800525
526 if (size == 0)
527 return (NULL);
528
529 /* This calls the library allocator twice, once to get the requested
530 buffer and once to get a new free list entry. */
531 {
532 /* Disable malloc_fn and free_fn */
533 memory_infop pinfo;
534 png_set_mem_fn(png_ptr, NULL, NULL, NULL);
535 pinfo = (memory_infop)png_malloc(png_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200536 (sizeof *pinfo));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800537 pinfo->size = size;
538 current_allocation += size;
539 total_allocation += size;
540 num_allocations ++;
Chris Craikb50c2172013-07-29 15:28:30 -0700541
The Android Open Source Project893912b2009-03-03 19:30:05 -0800542 if (current_allocation > maximum_allocation)
543 maximum_allocation = current_allocation;
Chris Craikb50c2172013-07-29 15:28:30 -0700544
545 pinfo->pointer = png_malloc(png_ptr, size);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800546 /* Restore malloc_fn and free_fn */
Chris Craikb50c2172013-07-29 15:28:30 -0700547
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700548 png_set_mem_fn(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700549 NULL, png_debug_malloc, png_debug_free);
550
The Android Open Source Project893912b2009-03-03 19:30:05 -0800551 if (size != 0 && pinfo->pointer == NULL)
552 {
553 current_allocation -= size;
554 total_allocation -= size;
555 png_error(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700556 "out of memory in pngtest->png_debug_malloc");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800557 }
Chris Craikb50c2172013-07-29 15:28:30 -0700558
The Android Open Source Project893912b2009-03-03 19:30:05 -0800559 pinfo->next = pinformation;
560 pinformation = pinfo;
561 /* Make sure the caller isn't assuming zeroed memory. */
Chris Craikb50c2172013-07-29 15:28:30 -0700562 memset(pinfo->pointer, 0xdd, pinfo->size);
563
Matt Sarett9b1fe632015-11-25 10:21:17 -0500564 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700565 printf("png_malloc %lu bytes at %p\n", (unsigned long)size,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200566 pinfo->pointer);
Chris Craikb50c2172013-07-29 15:28:30 -0700567
The Android Open Source Project893912b2009-03-03 19:30:05 -0800568 return (png_voidp)(pinfo->pointer);
569 }
570}
571
572/* Free a pointer. It is removed from the list at the same time. */
Chris Craikb50c2172013-07-29 15:28:30 -0700573void PNGCBAPI
The Android Open Source Project893912b2009-03-03 19:30:05 -0800574png_debug_free(png_structp png_ptr, png_voidp ptr)
575{
576 if (png_ptr == NULL)
577 fprintf(STDERR, "NULL pointer to png_debug_free.\n");
Chris Craikb50c2172013-07-29 15:28:30 -0700578
The Android Open Source Project893912b2009-03-03 19:30:05 -0800579 if (ptr == 0)
580 {
581#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. */
Matt Sarett9b1fe632015-11-25 10:21:17 -0500588 if (pinformation != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800589 {
Chris Craikb50c2172013-07-29 15:28:30 -0700590 memory_infop *ppinfo = &pinformation;
591
The Android Open Source Project893912b2009-03-03 19:30:05 -0800592 for (;;)
593 {
594 memory_infop pinfo = *ppinfo;
Chris Craikb50c2172013-07-29 15:28:30 -0700595
The Android Open Source Project893912b2009-03-03 19:30:05 -0800596 if (pinfo->pointer == ptr)
597 {
598 *ppinfo = pinfo->next;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400599 current_allocation -= pinfo->size;
600 if (current_allocation < 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800601 fprintf(STDERR, "Duplicate free of memory\n");
602 /* We must free the list element too, but first kill
603 the memory that is to be freed. */
Chris Craikb50c2172013-07-29 15:28:30 -0700604 memset(ptr, 0x55, pinfo->size);
Matt Sarett9b1fe632015-11-25 10:21:17 -0500605 free(pinfo);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700606 pinfo = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800607 break;
608 }
Chris Craikb50c2172013-07-29 15:28:30 -0700609
The Android Open Source Project893912b2009-03-03 19:30:05 -0800610 if (pinfo->next == NULL)
611 {
Matt Sarett9b1fe632015-11-25 10:21:17 -0500612 fprintf(STDERR, "Pointer %p not found\n", ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800613 break;
614 }
Chris Craikb50c2172013-07-29 15:28:30 -0700615
The Android Open Source Project893912b2009-03-03 19:30:05 -0800616 ppinfo = &pinfo->next;
617 }
618 }
619
620 /* Finally free the data. */
Matt Sarett9b1fe632015-11-25 10:21:17 -0500621 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700622 printf("Freeing %p\n", ptr);
623
Matt Sarett9b1fe632015-11-25 10:21:17 -0500624 if (ptr != NULL)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530625 free(ptr);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700626 ptr = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800627}
Matt Sarett9b1fe632015-11-25 10:21:17 -0500628#endif /* USER_MEM && DEBUG */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800629/* END of code to test memory allocation/deallocation */
630
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700631
Chris Craikb50c2172013-07-29 15:28:30 -0700632#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700633/* Demonstration of user chunk support of the sTER and vpAg chunks */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700634
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400635/* (sTER is a public chunk not yet known by libpng. vpAg is a private
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700636chunk used in ImageMagick to store "virtual page" size). */
637
Chris Craikb50c2172013-07-29 15:28:30 -0700638static 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;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700647
Chris Craikb50c2172013-07-29 15:28:30 -0700648/* 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
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700654
Chris Craikb50c2172013-07-29 15:28:30 -0700655static 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
Matt Sarett9b1fe632015-11-25 10:21:17 -0500667 if ((data->location[0] & what) != 0 || (data->location[1] & what) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700668 return 0; /* already have one of these */
669
Matt Sarett9b1fe632015-11-25 10:21:17 -0500670 /* Find where we are (the code below zeroes info_ptr to indicate that the
Chris Craikb50c2172013-07-29 15:28:30 -0700671 * chunks before the first IDAT have been read.)
672 */
673 if (data->info_ptr == NULL) /* after IDAT */
674 location = what | after_IDAT;
675
Matt Sarett9b1fe632015-11-25 10:21:17 -0500676 else if (png_get_valid(png_ptr, data->info_ptr, PNG_INFO_PLTE) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700677 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}
690
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530691static int PNGCBAPI
692read_user_chunk_callback(png_struct *png_ptr, png_unknown_chunkp chunk)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700693{
Chris Craikb50c2172013-07-29 15:28:30 -0700694 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");
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700699
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400700 /* 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;
xNombred07bb0d2020-03-10 20:17:12 +0100708 * size_t size;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400709 *
710 * Note that libpng has already taken care of the CRC handling.
711 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700712
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400713 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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700719
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400720 if (chunk->data[0] != 0 && chunk->data[0] != 1)
721 return (-1); /* Invalid mode */
Chris Craikb50c2172013-07-29 15:28:30 -0700722
Matt Sarett9b1fe632015-11-25 10:21:17 -0500723 if (set_location(png_ptr, my_user_chunk_data, have_sTER) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700724 {
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 */
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400731 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700732
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400733 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 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700736
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400737 /* Found ImageMagick vpAg chunk */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700738
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400739 if (chunk->size != 9)
740 return (-1); /* Error return */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700741
Matt Sarett9b1fe632015-11-25 10:21:17 -0500742 if (set_location(png_ptr, my_user_chunk_data, have_vpAg) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700743 return (0); /* duplicate vpAg */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700744
Chris Craikb50c2172013-07-29 15:28:30 -0700745 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];
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700748
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400749 return (1);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700750}
Chris Craikb50c2172013-07-29 15:28:30 -0700751
752#ifdef PNG_WRITE_SUPPORTED
753static void
754write_sTER_chunk(png_structp write_ptr)
755{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530756 png_byte sTER[5] = {115, 84, 69, 82, '\0'};
Chris Craikb50c2172013-07-29 15:28:30 -0700757
Matt Sarett9b1fe632015-11-25 10:21:17 -0500758 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700759 fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
760
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530761 png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
Chris Craikb50c2172013-07-29 15:28:30 -0700762}
763
764static void
765write_vpAg_chunk(png_structp write_ptr)
766{
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530767 png_byte vpAg[5] = {118, 112, 65, 103, '\0'};
Chris Craikb50c2172013-07-29 15:28:30 -0700768
769 png_byte vpag_chunk_data[9];
770
Matt Sarett9b1fe632015-11-25 10:21:17 -0500771 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700772 fprintf(STDERR, " vpAg = %lu x %lu, units = %d\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +0200773 (unsigned long)user_chunk_data.vpAg_width,
774 (unsigned long)user_chunk_data.vpAg_height,
775 user_chunk_data.vpAg_units);
Chris Craikb50c2172013-07-29 15:28:30 -0700776
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;
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530780 png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
Chris Craikb50c2172013-07-29 15:28:30 -0700781}
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}
Matt Sarett9b1fe632015-11-25 10:21:17 -0500803#endif /* WRITE */
804#else /* !READ_USER_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -0700805# define write_chunks(pp,loc) ((void)0)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700806#endif
807/* END of code to demonstrate user chunk support */
808
Chris Craikb50c2172013-07-29 15:28:30 -0700809/* 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
Matt Sarett06f10872016-01-04 12:56:20 -0500815pngtest_check_text_support(png_structp png_ptr, png_textp text_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200816 int num_text)
Chris Craikb50c2172013-07-29 15:28:30 -0700817{
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;
Matt Sarett06f10872016-01-04 12:56:20 -0500828 /* In libpng 1.7 this now does an app-error, so stop it: */
829 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Chris Craikb50c2172013-07-29 15:28:30 -0700830# 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;
Matt Sarett06f10872016-01-04 12:56:20 -0500837 text_ptr[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
Chris Craikb50c2172013-07-29 15:28:30 -0700838# 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
The Android Open Source Project893912b2009-03-03 19:30:05 -0800851/* Test one file */
Chris Craikb50c2172013-07-29 15:28:30 -0700852static int
xNombred07bb0d2020-03-10 20:17:12 +0100853test_one_file(const char *inname, const char *outname)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800854{
855 static png_FILE_p fpin;
856 static png_FILE_p fpout; /* "static" prevents setjmp corruption */
Chris Craikb50c2172013-07-29 15:28:30 -0700857 pngtest_error_parameters error_parameters;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800858 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;
Matt Sarett06f10872016-01-04 12:56:20 -0500864#ifdef PNG_WRITE_FILTER_SUPPORTED
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530865 int interlace_preserved = 1;
Matt Sarett06f10872016-01-04 12:56:20 -0500866#endif /* WRITE_FILTER */
867#else /* !WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800868 png_structp write_ptr = NULL;
869 png_infop write_info_ptr = NULL;
870 png_infop write_end_info_ptr = NULL;
Matt Sarett06f10872016-01-04 12:56:20 -0500871#endif /* !WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800872 png_bytep row_buf;
873 png_uint_32 y;
874 png_uint_32 width, height;
Matt Sarett06f10872016-01-04 12:56:20 -0500875 volatile int num_passes;
876 int pass;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800877 int bit_depth, color_type;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800878
879 row_buf = NULL;
Chris Craikb50c2172013-07-29 15:28:30 -0700880 error_parameters.file_name = inname;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800881
The Android Open Source Project893912b2009-03-03 19:30:05 -0800882 if ((fpin = fopen(inname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800883 {
884 fprintf(STDERR, "Could not find input file %s\n", inname);
885 return (1);
886 }
887
The Android Open Source Project893912b2009-03-03 19:30:05 -0800888 if ((fpout = fopen(outname, "wb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800889 {
890 fprintf(STDERR, "Could not open output file %s\n", outname);
891 FCLOSE(fpin);
892 return (1);
893 }
894
Chris Craikb50c2172013-07-29 15:28:30 -0700895 pngtest_debug("Allocating read and write structures");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800896#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700897 read_ptr =
Alex Naidis7a055fd2016-10-01 12:23:07 +0200898 png_create_read_struct_2(PNG_LIBPNG_VER_STRING, NULL,
899 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800900#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700901 read_ptr =
Alex Naidis7a055fd2016-10-01 12:23:07 +0200902 png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800903#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700904 png_set_error_fn(read_ptr, &error_parameters, pngtest_error,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200905 pngtest_warning);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700906
The Android Open Source Project893912b2009-03-03 19:30:05 -0800907#ifdef PNG_WRITE_SUPPORTED
908#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700909 write_ptr =
Alex Naidis7a055fd2016-10-01 12:23:07 +0200910 png_create_write_struct_2(PNG_LIBPNG_VER_STRING, NULL,
911 NULL, NULL, NULL, png_debug_malloc, png_debug_free);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800912#else
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700913 write_ptr =
Alex Naidis7a055fd2016-10-01 12:23:07 +0200914 png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800915#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700916 png_set_error_fn(write_ptr, &error_parameters, pngtest_error,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200917 pngtest_warning);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800918#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700919 pngtest_debug("Allocating read_info, write_info and end_info structures");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800920 read_info_ptr = png_create_info_struct(read_ptr);
921 end_info_ptr = png_create_info_struct(read_ptr);
922#ifdef PNG_WRITE_SUPPORTED
923 write_info_ptr = png_create_info_struct(write_ptr);
924 write_end_info_ptr = png_create_info_struct(write_ptr);
925#endif
926
Chris Craikb50c2172013-07-29 15:28:30 -0700927#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,
Alex Naidis7a055fd2016-10-01 12:23:07 +0200930 read_user_chunk_callback);
931#endif
932
Chris Craikb50c2172013-07-29 15:28:30 -0700933#ifdef PNG_SETJMP_SUPPORTED
934 pngtest_debug("Setting jmpbuf for read struct");
935 if (setjmp(png_jmpbuf(read_ptr)))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800936 {
937 fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname);
938 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700939 row_buf = NULL;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400940 if (verbose != 0)
941 fprintf(STDERR, " destroy read structs\n");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800942 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
943#ifdef PNG_WRITE_SUPPORTED
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400944 if (verbose != 0)
945 fprintf(STDERR, " destroy write structs\n");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800946 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
947 png_destroy_write_struct(&write_ptr, &write_info_ptr);
948#endif
949 FCLOSE(fpin);
950 FCLOSE(fpout);
951 return (1);
952 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800953
954#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700955 pngtest_debug("Setting jmpbuf for write struct");
956
The Android Open Source Project893912b2009-03-03 19:30:05 -0800957 if (setjmp(png_jmpbuf(write_ptr)))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800958 {
959 fprintf(STDERR, "%s -> %s: libpng write error\n", inname, outname);
xNombred07bb0d2020-03-10 20:17:12 +0100960 png_free(read_ptr, row_buf);
961 row_buf = NULL;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400962 if (verbose != 0)
963 fprintf(STDERR, " destroying read structs\n");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800964 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400965 if (verbose != 0)
966 fprintf(STDERR, " destroying write structs\n");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800967 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800968 png_destroy_write_struct(&write_ptr, &write_info_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800969 FCLOSE(fpin);
970 FCLOSE(fpout);
971 return (1);
972 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800973#endif
974#endif
975
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400976#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -0500977 if (strict != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700978 {
979 /* Treat png_benign_error() as errors on read */
980 png_set_benign_errors(read_ptr, 0);
981
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400982# ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700983 /* Treat them as errors on write */
984 png_set_benign_errors(write_ptr, 0);
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400985# endif
Chris Craikb50c2172013-07-29 15:28:30 -0700986
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
Matt Sarett9b1fe632015-11-25 10:21:17 -0500993 else if (relaxed != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700994 {
995 /* Allow application (pngtest) errors and warnings to pass */
996 png_set_benign_errors(read_ptr, 1);
997
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -0400998 /* Turn off CRC checking while reading */
999 png_set_crc_action(read_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
1000
1001#ifdef PNG_IGNORE_ADLER32
1002 /* Turn off ADLER32 checking while reading */
1003 png_set_option(read_ptr, PNG_IGNORE_ADLER32, PNG_OPTION_ON);
Matt Sarett851c6772016-11-23 20:24:06 +00001004#endif
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001005
1006# ifdef PNG_WRITE_SUPPORTED
1007 png_set_benign_errors(write_ptr, 1);
1008# endif
1009
Chris Craikb50c2172013-07-29 15:28:30 -07001010 }
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001011#endif /* BENIGN_ERRORS */
Chris Craikb50c2172013-07-29 15:28:30 -07001012
1013 pngtest_debug("Initializing input and output streams");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001014#ifdef PNG_STDIO_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001015 png_init_io(read_ptr, fpin);
1016# ifdef PNG_WRITE_SUPPORTED
1017 png_init_io(write_ptr, fpout);
1018# endif
1019#else
1020 png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data);
1021# ifdef PNG_WRITE_SUPPORTED
1022 png_set_write_fn(write_ptr, (png_voidp)fpout, pngtest_write_data,
Patrick Scott5f6bd842010-06-28 16:55:16 -04001023# ifdef PNG_WRITE_FLUSH_SUPPORTED
Alex Naidis7a055fd2016-10-01 12:23:07 +02001024 pngtest_flush);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001025# else
Alex Naidis7a055fd2016-10-01 12:23:07 +02001026 NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001027# endif
1028# endif
1029#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001030
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001031 if (status_dots_requested == 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001032 {
1033#ifdef PNG_WRITE_SUPPORTED
1034 png_set_write_status_fn(write_ptr, write_row_callback);
1035#endif
1036 png_set_read_status_fn(read_ptr, read_row_callback);
1037 }
Chris Craikb50c2172013-07-29 15:28:30 -07001038
The Android Open Source Project893912b2009-03-03 19:30:05 -08001039 else
1040 {
1041#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001042 png_set_write_status_fn(write_ptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001043#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001044 png_set_read_status_fn(read_ptr, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001045 }
1046
Patrick Scott5f6bd842010-06-28 16:55:16 -04001047#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Matt Sarett06f10872016-01-04 12:56:20 -05001048 png_set_read_user_transform_fn(read_ptr, read_user_callback);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001049#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001050#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001051 zero_samples = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001052 png_set_write_user_transform_fn(write_ptr, count_zero_samples);
1053#endif
1054
Chris Craikb50c2172013-07-29 15:28:30 -07001055#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
The Android Open Source Project893912b2009-03-03 19:30:05 -08001065 png_set_keep_unknown_chunks(read_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001066 NULL, 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001067#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001068#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001069 png_set_keep_unknown_chunks(write_ptr, PNG_HANDLE_CHUNK_ALWAYS,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001070 NULL, 0);
Chris Craikb50c2172013-07-29 15:28:30 -07001071#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001072#endif
1073
Chris Craikb50c2172013-07-29 15:28:30 -07001074 pngtest_debug("Reading info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001075 png_read_info(read_ptr, read_info_ptr);
1076
Chris Craikb50c2172013-07-29 15:28:30 -07001077#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
1086 pngtest_debug("Transferring info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001087 {
1088 int interlace_type, compression_type, filter_type;
1089
1090 if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth,
Matt Sarett9b1fe632015-11-25 10:21:17 -05001091 &color_type, &interlace_type, &compression_type, &filter_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001092 {
1093 png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001094 color_type, interlace_type, compression_type, filter_type);
Matt Sarett06f10872016-01-04 12:56:20 -05001095 /* num_passes may not be available below if interlace support is not
1096 * provided by libpng for both read and write.
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301097 */
1098 switch (interlace_type)
1099 {
1100 case PNG_INTERLACE_NONE:
Matt Sarett06f10872016-01-04 12:56:20 -05001101 num_passes = 1;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301102 break;
1103
1104 case PNG_INTERLACE_ADAM7:
Matt Sarett06f10872016-01-04 12:56:20 -05001105 num_passes = 7;
1106 break;
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301107
1108 default:
Matt Sarett06f10872016-01-04 12:56:20 -05001109 png_error(read_ptr, "invalid interlace type");
1110 /*NOT REACHED*/
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301111 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001112 }
Matt Sarett9b1fe632015-11-25 10:21:17 -05001113
Matt Sarett06f10872016-01-04 12:56:20 -05001114 else
1115 png_error(read_ptr, "png_get_IHDR failed");
1116 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04001117#ifdef PNG_FIXED_POINT_SUPPORTED
1118#ifdef PNG_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001119 {
1120 png_fixed_point white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001121 blue_y;
Chris Craikb50c2172013-07-29 15:28:30 -07001122
Patrick Scott5f6bd842010-06-28 16:55:16 -04001123 if (png_get_cHRM_fixed(read_ptr, read_info_ptr, &white_x, &white_y,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001124 &red_x, &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001125 {
1126 png_set_cHRM_fixed(write_ptr, write_info_ptr, white_x, white_y, red_x,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001127 red_y, green_x, green_y, blue_x, blue_y);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001128 }
1129 }
1130#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001131#ifdef PNG_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001132 {
1133 png_fixed_point gamma;
1134
Matt Sarett9b1fe632015-11-25 10:21:17 -05001135 if (png_get_gAMA_fixed(read_ptr, read_info_ptr, &gamma) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001136 png_set_gAMA_fixed(write_ptr, write_info_ptr, gamma);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001137 }
1138#endif
1139#else /* Use floating point versions */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001140#ifdef PNG_FLOATING_POINT_SUPPORTED
1141#ifdef PNG_cHRM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001142 {
1143 double white_x, white_y, red_x, red_y, green_x, green_y, blue_x,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001144 blue_y;
Chris Craikb50c2172013-07-29 15:28:30 -07001145
The Android Open Source Project893912b2009-03-03 19:30:05 -08001146 if (png_get_cHRM(read_ptr, read_info_ptr, &white_x, &white_y, &red_x,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001147 &red_y, &green_x, &green_y, &blue_x, &blue_y) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001148 {
1149 png_set_cHRM(write_ptr, write_info_ptr, white_x, white_y, red_x,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001150 red_y, green_x, green_y, blue_x, blue_y);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001151 }
1152 }
1153#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001154#ifdef PNG_gAMA_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001155 {
1156 double gamma;
1157
Matt Sarett9b1fe632015-11-25 10:21:17 -05001158 if (png_get_gAMA(read_ptr, read_info_ptr, &gamma) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001159 png_set_gAMA(write_ptr, write_info_ptr, gamma);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001160 }
1161#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001162#endif /* Floating point */
1163#endif /* Fixed point */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001164#ifdef PNG_iCCP_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001165 {
1166 png_charp name;
Chris Craikb50c2172013-07-29 15:28:30 -07001167 png_bytep profile;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001168 png_uint_32 proflen;
1169 int compression_type;
1170
1171 if (png_get_iCCP(read_ptr, read_info_ptr, &name, &compression_type,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001172 &profile, &proflen) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001173 {
1174 png_set_iCCP(write_ptr, write_info_ptr, name, compression_type,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001175 profile, proflen);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001176 }
1177 }
1178#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001179#ifdef PNG_sRGB_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001180 {
1181 int intent;
1182
Matt Sarett9b1fe632015-11-25 10:21:17 -05001183 if (png_get_sRGB(read_ptr, read_info_ptr, &intent) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001184 png_set_sRGB(write_ptr, write_info_ptr, intent);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001185 }
1186#endif
1187 {
1188 png_colorp palette;
1189 int num_palette;
1190
Matt Sarett9b1fe632015-11-25 10:21:17 -05001191 if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001192 png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001193 }
Patrick Scott5f6bd842010-06-28 16:55:16 -04001194#ifdef PNG_bKGD_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001195 {
1196 png_color_16p background;
1197
Matt Sarett9b1fe632015-11-25 10:21:17 -05001198 if (png_get_bKGD(read_ptr, read_info_ptr, &background) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001199 {
1200 png_set_bKGD(write_ptr, write_info_ptr, background);
1201 }
1202 }
1203#endif
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001204#ifdef PNG_READ_eXIf_SUPPORTED
1205 {
1206 png_bytep exif=NULL;
1207 png_uint_32 exif_length;
1208
1209 if (png_get_eXIf_1(read_ptr, read_info_ptr, &exif_length, &exif) != 0)
1210 {
1211 if (exif_length > 1)
1212 fprintf(STDERR," eXIf type %c%c, %lu bytes\n",exif[0],exif[1],
1213 (unsigned long)exif_length);
1214# ifdef PNG_WRITE_eXIf_SUPPORTED
1215 png_set_eXIf_1(write_ptr, write_info_ptr, exif_length, exif);
1216# endif
1217 }
1218 }
1219#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001220#ifdef PNG_hIST_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001221 {
1222 png_uint_16p hist;
1223
Matt Sarett9b1fe632015-11-25 10:21:17 -05001224 if (png_get_hIST(read_ptr, read_info_ptr, &hist) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001225 png_set_hIST(write_ptr, write_info_ptr, hist);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001226 }
1227#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001228#ifdef PNG_oFFs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001229 {
1230 png_int_32 offset_x, offset_y;
1231 int unit_type;
1232
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001233 if (png_get_oFFs(read_ptr, read_info_ptr, &offset_x, &offset_y,
Matt Sarett9b1fe632015-11-25 10:21:17 -05001234 &unit_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001235 {
1236 png_set_oFFs(write_ptr, write_info_ptr, offset_x, offset_y, unit_type);
1237 }
1238 }
1239#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001240#ifdef PNG_pCAL_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001241 {
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,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001248 &nparams, &units, &params) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001249 {
1250 png_set_pCAL(write_ptr, write_info_ptr, purpose, X0, X1, type,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001251 nparams, units, params);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001252 }
1253 }
1254#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001255#ifdef PNG_pHYs_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001256 {
1257 png_uint_32 res_x, res_y;
1258 int unit_type;
1259
Matt Sarett9b1fe632015-11-25 10:21:17 -05001260 if (png_get_pHYs(read_ptr, read_info_ptr, &res_x, &res_y,
1261 &unit_type) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001262 png_set_pHYs(write_ptr, write_info_ptr, res_x, res_y, unit_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001263 }
1264#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001265#ifdef PNG_sBIT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001266 {
1267 png_color_8p sig_bit;
1268
Matt Sarett9b1fe632015-11-25 10:21:17 -05001269 if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001270 png_set_sBIT(write_ptr, write_info_ptr, sig_bit);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001271 }
1272#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001273#ifdef PNG_sCAL_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001274#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
1275 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001276 {
1277 int unit;
1278 double scal_width, scal_height;
1279
1280 if (png_get_sCAL(read_ptr, read_info_ptr, &unit, &scal_width,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001281 &scal_height) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001282 {
1283 png_set_sCAL(write_ptr, write_info_ptr, unit, scal_width, scal_height);
1284 }
1285 }
1286#else
1287#ifdef PNG_FIXED_POINT_SUPPORTED
1288 {
1289 int unit;
1290 png_charp scal_width, scal_height;
1291
1292 if (png_get_sCAL_s(read_ptr, read_info_ptr, &unit, &scal_width,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001293 &scal_height) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001294 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001295 png_set_sCAL_s(write_ptr, write_info_ptr, unit, scal_width,
1296 scal_height);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001297 }
1298 }
Matt Sarett06f10872016-01-04 12:56:20 -05001299#endif
1300#endif
1301#endif
Matt Sarett9b1fe632015-11-25 10:21:17 -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 }
Matt Sarett06f10872016-01-04 12:56:20 -05001313#endif
Matt Sarett9b1fe632015-11-25 10:21:17 -05001314
Patrick Scott5f6bd842010-06-28 16:55:16 -04001315#ifdef PNG_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001316 {
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 {
Chris Craikb50c2172013-07-29 15:28:30 -07001322 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1323
1324 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1325
Matt Sarett9b1fe632015-11-25 10:21:17 -05001326 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001327 {
1328 int i;
1329
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001330 fprintf(STDERR,"\n");
Chris Craikb50c2172013-07-29 15:28:30 -07001331 for (i=0; i<num_text; i++)
1332 {
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001333 fprintf(STDERR," Text compression[%d]=%d\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001334 i, text_ptr[i].compression);
Chris Craikb50c2172013-07-29 15:28:30 -07001335 }
1336 }
1337
The Android Open Source Project893912b2009-03-03 19:30:05 -08001338 png_set_text(write_ptr, write_info_ptr, text_ptr, num_text);
1339 }
1340 }
Matt Sarett06f10872016-01-04 12:56:20 -05001341#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001342#ifdef PNG_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001343 {
1344 png_timep mod_time;
1345
Matt Sarett9b1fe632015-11-25 10:21:17 -05001346 if (png_get_tIME(read_ptr, read_info_ptr, &mod_time) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001347 {
1348 png_set_tIME(write_ptr, write_info_ptr, mod_time);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001349#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -05001350 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001351 tIME_string[(sizeof tIME_string) - 1] = '\0';
1352
1353 else
1354 {
1355 strncpy(tIME_string, "*** invalid time ***", (sizeof tIME_string));
1356 tIME_string[(sizeof tIME_string) - 1] = '\0';
1357 }
1358
The Android Open Source Project893912b2009-03-03 19:30:05 -08001359 tIME_chunk_present++;
Matt Sarett9b1fe632015-11-25 10:21:17 -05001360#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001361 }
1362 }
Matt Sarett06f10872016-01-04 12:56:20 -05001363#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001364#ifdef PNG_tRNS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001365 {
Chris Craikb50c2172013-07-29 15:28:30 -07001366 png_bytep trans_alpha;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001367 int num_trans;
Chris Craikb50c2172013-07-29 15:28:30 -07001368 png_color_16p trans_color;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001369
Chris Craikb50c2172013-07-29 15:28:30 -07001370 if (png_get_tRNS(read_ptr, read_info_ptr, &trans_alpha, &num_trans,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001371 &trans_color) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001372 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001373 int sample_max = (1 << bit_depth);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001374 /* libpng doesn't reject a tRNS chunk with out-of-range samples */
Patrick Scott5f6bd842010-06-28 16:55:16 -04001375 if (!((color_type == PNG_COLOR_TYPE_GRAY &&
Chris Craikb50c2172013-07-29 15:28:30 -07001376 (int)trans_color->gray > sample_max) ||
Patrick Scott5f6bd842010-06-28 16:55:16 -04001377 (color_type == PNG_COLOR_TYPE_RGB &&
Chris Craikb50c2172013-07-29 15:28:30 -07001378 ((int)trans_color->red > sample_max ||
1379 (int)trans_color->green > sample_max ||
1380 (int)trans_color->blue > sample_max))))
1381 png_set_tRNS(write_ptr, write_info_ptr, trans_alpha, num_trans,
1382 trans_color);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001383 }
1384 }
Matt Sarett06f10872016-01-04 12:56:20 -05001385#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001386#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001387 {
1388 png_unknown_chunkp unknowns;
Chris Craikb50c2172013-07-29 15:28:30 -07001389 int num_unknowns = png_get_unknown_chunks(read_ptr, read_info_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001390 &unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001391
Matt Sarett9b1fe632015-11-25 10:21:17 -05001392 if (num_unknowns != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001393 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001394 png_set_unknown_chunks(write_ptr, write_info_ptr, unknowns,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001395 num_unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001396#if PNG_LIBPNG_VER < 10600
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001397 /* Copy the locations from the read_info_ptr. The automatically
Chris Craikb50c2172013-07-29 15:28:30 -07001398 * 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).
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001400 */
Chris Craikb50c2172013-07-29 15:28:30 -07001401 {
1402 int i;
1403 for (i = 0; i < num_unknowns; i++)
1404 png_set_unknown_chunk_location(write_ptr, write_info_ptr, i,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001405 unknowns[i].location);
Chris Craikb50c2172013-07-29 15:28:30 -07001406 }
1407#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001408 }
1409 }
1410#endif
1411
1412#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001413 pngtest_debug("Writing info struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001414
Chris Craikb50c2172013-07-29 15:28:30 -07001415 /* 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
The Android Open Source Project893912b2009-03-03 19:30:05 -08001422 png_write_info(write_ptr, write_info_ptr);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001423
Chris Craikb50c2172013-07-29 15:28:30 -07001424 write_chunks(write_ptr, before_IDAT); /* after PLTE */
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001425
1426 png_write_info(write_ptr, write_end_info_ptr);
1427
1428 write_chunks(write_ptr, after_IDAT); /* after IDAT */
1429
1430#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
The Android Open Source Project893912b2009-03-03 19:30:05 -08001434#endif
1435
1436#ifdef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001437 pngtest_debug("Allocating row buffer...");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001438 row_buf = (png_bytep)png_malloc(read_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001439 png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001440
xNombred07bb0d2020-03-10 20:17:12 +01001441 pngtest_debug1("\t%p", row_buf);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001442#endif /* SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001443 pngtest_debug("Writing row data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001444
Matt Sarett06f10872016-01-04 12:56:20 -05001445#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,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001453 "png_set_interlace_handling(read): wrong pass count ");
Matt Sarett06f10872016-01-04 12:56:20 -05001454 if (png_set_interlace_handling(write_ptr) != num_passes)
1455 png_error(write_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001456 "png_set_interlace_handling(write): wrong pass count ");
Matt Sarett06f10872016-01-04 12:56:20 -05001457#else /* png_set_interlace_handling not called on either read or write */
1458# define calc_pass_height
1459#endif /* not using libpng interlace handling */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001460
1461#ifdef PNGTEST_TIMING
1462 t_stop = (float)clock();
1463 t_misc += (t_stop - t_start);
1464 t_start = t_stop;
1465#endif
Matt Sarett06f10872016-01-04 12:56:20 -05001466 for (pass = 0; pass < num_passes; pass++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001467 {
Matt Sarett06f10872016-01-04 12:56:20 -05001468# ifdef calc_pass_height
1469 png_uint_32 pass_height;
1470
1471 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
Chris Craikb50c2172013-07-29 15:28:30 -07001486 pngtest_debug1("Writing row data for pass %d", pass);
Matt Sarett06f10872016-01-04 12:56:20 -05001487 for (y = 0; y < pass_height; y++)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001488 {
1489#ifndef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001490 pngtest_debug2("Allocating row buffer (pass %d, y = %u)...", pass, y);
Matt Sarett9b1fe632015-11-25 10:21:17 -05001491
The Android Open Source Project893912b2009-03-03 19:30:05 -08001492 row_buf = (png_bytep)png_malloc(read_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001493 png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001494
xNombred07bb0d2020-03-10 20:17:12 +01001495 pngtest_debug2("\t%p (%lu bytes)", row_buf,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001496 (unsigned long)png_get_rowbytes(read_ptr, read_info_ptr));
Chris Craikb50c2172013-07-29 15:28:30 -07001497
The Android Open Source Project893912b2009-03-03 19:30:05 -08001498#endif /* !SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001499 png_read_rows(read_ptr, (png_bytepp)&row_buf, NULL, 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001500
1501#ifdef PNG_WRITE_SUPPORTED
1502#ifdef PNGTEST_TIMING
1503 t_stop = (float)clock();
1504 t_decode += (t_stop - t_start);
1505 t_start = t_stop;
1506#endif
1507 png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
1508#ifdef PNGTEST_TIMING
1509 t_stop = (float)clock();
1510 t_encode += (t_stop - t_start);
1511 t_start = t_stop;
1512#endif
Matt Sarett9b1fe632015-11-25 10:21:17 -05001513#endif /* WRITE */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001514
1515#ifndef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001516 pngtest_debug2("Freeing row buffer (pass %d, y = %u)", pass, y);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001517 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001518 row_buf = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001519#endif /* !SINGLE_ROWBUF_ALLOC */
1520 }
1521 }
1522
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301523#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
The Android Open Source Project893912b2009-03-03 19:30:05 -08001530#endif
1531
Chris Craikb50c2172013-07-29 15:28:30 -07001532 pngtest_debug("Reading and writing end_info data");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001533
1534 png_read_end(read_ptr, end_info_ptr);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001535#ifdef PNG_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001536 {
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 {
Chris Craikb50c2172013-07-29 15:28:30 -07001542 pngtest_debug1("Handling %d iTXt/tEXt/zTXt chunks", num_text);
1543
1544 pngtest_check_text_support(read_ptr, text_ptr, num_text);
1545
Matt Sarett9b1fe632015-11-25 10:21:17 -05001546 if (verbose != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001547 {
1548 int i;
1549
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001550 fprintf(STDERR,"\n");
Chris Craikb50c2172013-07-29 15:28:30 -07001551 for (i=0; i<num_text; i++)
1552 {
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001553 fprintf(STDERR," Text compression[%d]=%d\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001554 i, text_ptr[i].compression);
Chris Craikb50c2172013-07-29 15:28:30 -07001555 }
1556 }
1557
The Android Open Source Project893912b2009-03-03 19:30:05 -08001558 png_set_text(write_ptr, write_end_info_ptr, text_ptr, num_text);
1559 }
1560 }
1561#endif
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001562#ifdef PNG_READ_eXIf_SUPPORTED
1563 {
1564 png_bytep exif=NULL;
1565 png_uint_32 exif_length;
1566
1567 if (png_get_eXIf_1(read_ptr, end_info_ptr, &exif_length, &exif) != 0)
1568 {
1569 if (exif_length > 1)
1570 fprintf(STDERR," eXIf type %c%c, %lu bytes\n",exif[0],exif[1],
1571 (unsigned long)exif_length);
1572# ifdef PNG_WRITE_eXIf_SUPPORTED
1573 png_set_eXIf_1(write_ptr, write_end_info_ptr, exif_length, exif);
1574# endif
1575 }
1576 }
1577#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001578#ifdef PNG_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001579 {
1580 png_timep mod_time;
1581
Matt Sarett9b1fe632015-11-25 10:21:17 -05001582 if (png_get_tIME(read_ptr, end_info_ptr, &mod_time) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001583 {
1584 png_set_tIME(write_ptr, write_end_info_ptr, mod_time);
Patrick Scott5f6bd842010-06-28 16:55:16 -04001585#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -05001586 if (png_convert_to_rfc1123_buffer(tIME_string, mod_time) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001587 tIME_string[(sizeof tIME_string) - 1] = '\0';
1588
1589 else
1590 {
1591 strncpy(tIME_string, "*** invalid time ***", sizeof tIME_string);
1592 tIME_string[(sizeof tIME_string)-1] = '\0';
1593 }
1594
The Android Open Source Project893912b2009-03-03 19:30:05 -08001595 tIME_chunk_present++;
Matt Sarett9b1fe632015-11-25 10:21:17 -05001596#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001597 }
1598 }
1599#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001600#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001601 {
1602 png_unknown_chunkp unknowns;
Chris Craikb50c2172013-07-29 15:28:30 -07001603 int num_unknowns = png_get_unknown_chunks(read_ptr, end_info_ptr,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001604 &unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001605
Matt Sarett9b1fe632015-11-25 10:21:17 -05001606 if (num_unknowns != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001607 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001608 png_set_unknown_chunks(write_ptr, write_end_info_ptr, unknowns,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001609 num_unknowns);
Chris Craikb50c2172013-07-29 15:28:30 -07001610#if PNG_LIBPNG_VER < 10600
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001611 /* Copy the locations from the read_info_ptr. The automatically
Chris Craikb50c2172013-07-29 15:28:30 -07001612 * 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).
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001614 */
Chris Craikb50c2172013-07-29 15:28:30 -07001615 {
1616 int i;
1617 for (i = 0; i < num_unknowns; i++)
1618 png_set_unknown_chunk_location(write_ptr, write_end_info_ptr, i,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001619 unknowns[i].location);
Chris Craikb50c2172013-07-29 15:28:30 -07001620 }
1621#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001622 }
1623 }
1624#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001625
The Android Open Source Project893912b2009-03-03 19:30:05 -08001626#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001627#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
The Android Open Source Project893912b2009-03-03 19:30:05 -08001643 png_write_end(write_ptr, write_end_info_ptr);
1644#endif
1645
1646#ifdef PNG_EASY_ACCESS_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -05001647 if (verbose != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001648 {
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);
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001652 fprintf(STDERR, "\n Image width = %lu, height = %lu\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001653 (unsigned long)iwidth, (unsigned long)iheight);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001654 }
1655#endif
1656
Chris Craikb50c2172013-07-29 15:28:30 -07001657 pngtest_debug("Destroying data structs");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001658#ifdef SINGLE_ROWBUF_ALLOC
Chris Craikb50c2172013-07-29 15:28:30 -07001659 pngtest_debug("destroying row_buf for read_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001660 png_free(read_ptr, row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001661 row_buf = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001662#endif /* SINGLE_ROWBUF_ALLOC */
Chris Craikb50c2172013-07-29 15:28:30 -07001663 pngtest_debug("destroying read_ptr, read_info_ptr, end_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001664 png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr);
1665#ifdef PNG_WRITE_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001666 pngtest_debug("destroying write_end_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001667 png_destroy_info_struct(write_ptr, &write_end_info_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -07001668 pngtest_debug("destroying write_ptr, write_info_ptr");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001669 png_destroy_write_struct(&write_ptr, &write_info_ptr);
1670#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001671 pngtest_debug("Destruction complete.");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001672
1673 FCLOSE(fpin);
1674 FCLOSE(fpout);
1675
Chris Craikb50c2172013-07-29 15:28:30 -07001676 /* 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)",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001686 inname, error_count, warning_count);
Chris Craikb50c2172013-07-29 15:28:30 -07001687
1688 if (strict != 0)
1689 return (1);
1690 }
1691
1692# ifdef PNG_WRITE_SUPPORTED
Matt Sarett06f10872016-01-04 12:56:20 -05001693 /* If there is no write support nothing was written! */
Chris Craikb50c2172013-07-29 15:28:30 -07001694 else if (unsupported_chunks > 0)
1695 {
1696 fprintf(STDERR, "\n %s: unsupported chunks (%d)%s",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001697 inname, unsupported_chunks, strict ? ": IGNORED --strict!" : "");
Chris Craikb50c2172013-07-29 15:28:30 -07001698 }
1699# endif
1700
1701 else if (warning_count > 0)
1702 {
1703 fprintf(STDERR, "\n %s: %d libpng warnings found",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001704 inname, warning_count);
Chris Craikb50c2172013-07-29 15:28:30 -07001705
1706 if (strict != 0)
1707 return (1);
1708 }
1709
1710 pngtest_debug("Opening files for comparison");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001711 if ((fpin = fopen(inname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001712 {
1713 fprintf(STDERR, "Could not find file %s\n", inname);
1714 return (1);
1715 }
1716
The Android Open Source Project893912b2009-03-03 19:30:05 -08001717 if ((fpout = fopen(outname, "rb")) == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001718 {
1719 fprintf(STDERR, "Could not find file %s\n", outname);
1720 FCLOSE(fpin);
1721 return (1);
1722 }
1723
Matt Sarett06f10872016-01-04 12:56:20 -05001724#if defined (PNG_WRITE_SUPPORTED) /* else nothing was written */ &&\
1725 defined (PNG_WRITE_FILTER_SUPPORTED)
Matt Sarett9b1fe632015-11-25 10:21:17 -05001726 if (interlace_preserved != 0) /* else the files will be changed */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001727 {
Chris Craikb50c2172013-07-29 15:28:30 -07001728 for (;;)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001729 {
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301730 static int wrote_question = 0;
xNombred07bb0d2020-03-10 20:17:12 +01001731 size_t num_in, num_out;
Chris Craikb50c2172013-07-29 15:28:30 -07001732 char inbuf[256], outbuf[256];
The Android Open Source Project893912b2009-03-03 19:30:05 -08001733
Chris Craikb50c2172013-07-29 15:28:30 -07001734 num_in = fread(inbuf, 1, sizeof inbuf, fpin);
1735 num_out = fread(outbuf, 1, sizeof outbuf, fpout);
1736
1737 if (num_in != num_out)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001738 {
Chris Craikb50c2172013-07-29 15:28:30 -07001739 fprintf(STDERR, "\nFiles %s and %s are of a different size\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001740 inname, outname);
Chris Craikb50c2172013-07-29 15:28:30 -07001741
1742 if (wrote_question == 0 && unsupported_chunks == 0)
1743 {
1744 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001745 " Was %s written with the same maximum IDAT"
1746 " chunk size (%d bytes),",
1747 inname, PNG_ZBUF_SIZE);
Chris Craikb50c2172013-07-29 15:28:30 -07001748 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001749 "\n filtering heuristic (libpng default), compression");
Chris Craikb50c2172013-07-29 15:28:30 -07001750 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001751 " level (zlib default),\n and zlib version (%s)?\n\n",
1752 ZLIB_VERSION);
Chris Craikb50c2172013-07-29 15:28:30 -07001753 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001764 }
Chris Craikb50c2172013-07-29 15:28:30 -07001765
Matt Sarett9b1fe632015-11-25 10:21:17 -05001766 if (num_in == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001767 break;
1768
1769 if (memcmp(inbuf, outbuf, num_in))
1770 {
1771 fprintf(STDERR, "\nFiles %s and %s are different\n", inname,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001772 outname);
Chris Craikb50c2172013-07-29 15:28:30 -07001773
1774 if (wrote_question == 0 && unsupported_chunks == 0)
1775 {
1776 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001777 " Was %s written with the same maximum"
1778 " IDAT chunk size (%d bytes),",
Chris Craikb50c2172013-07-29 15:28:30 -07001779 inname, PNG_ZBUF_SIZE);
1780 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001781 "\n filtering heuristic (libpng default), compression");
Chris Craikb50c2172013-07-29 15:28:30 -07001782 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001783 " level (zlib default),\n and zlib version (%s)?\n\n",
Chris Craikb50c2172013-07-29 15:28:30 -07001784 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 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001802 }
1803 }
Matt Sarett06f10872016-01-04 12:56:20 -05001804#endif /* WRITE && WRITE_FILTER */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001805
1806 FCLOSE(fpin);
1807 FCLOSE(fpout);
1808
1809 return (0);
1810}
1811
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001812/* Input and output filenames */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001813#ifdef RISCOS
xNombred07bb0d2020-03-10 20:17:12 +01001814static const char *inname = "pngtest/png";
1815static const char *outname = "pngout/png";
The Android Open Source Project893912b2009-03-03 19:30:05 -08001816#else
xNombred07bb0d2020-03-10 20:17:12 +01001817static const char *inname = "pngtest.png";
1818static const char *outname = "pngout.png";
The Android Open Source Project893912b2009-03-03 19:30:05 -08001819#endif
1820
1821int
1822main(int argc, char *argv[])
1823{
1824 int multiple = 0;
1825 int ierror = 0;
1826
Matt Sarett9b1fe632015-11-25 10:21:17 -05001827 png_structp dummy_ptr;
1828
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001829 fprintf(STDERR, "\n Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001830 fprintf(STDERR, " with zlib version %s\n", ZLIB_VERSION);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001831 fprintf(STDERR, "%s", png_get_copyright(NULL));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001832 /* Show the version of libpng used in building the library */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001833 fprintf(STDERR, " library (%lu):%s",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001834 (unsigned long)png_access_version_number(),
1835 png_get_header_version(NULL));
Chris Craikb50c2172013-07-29 15:28:30 -07001836
The Android Open Source Project893912b2009-03-03 19:30:05 -08001837 /* Show the version of libpng used in building the application */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001838 fprintf(STDERR, " pngtest (%lu):%s", (unsigned long)PNG_LIBPNG_VER,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001839 PNG_HEADER_VERSION_STRING);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001840
1841 /* Do some consistency checking on the memory allocation settings, I'm
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001842 * 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 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001846#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K)
1847 fprintf(STDERR, " NOTE: Zlib compiled for max 64k, libpng not\n");
1848#endif
1849 /* I think the following can happen. */
1850#if !defined(MAXSEG_64K) && defined(PNG_MAX_MALLOC_64K)
1851 fprintf(STDERR, " NOTE: libpng compiled for max 64k, zlib not\n");
1852#endif
1853
1854 if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
1855 {
1856 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02001857 "Warning: versions are different between png.h and png.c\n");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001858 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 {
1865 if (strcmp(argv[1], "-m") == 0)
1866 {
1867 multiple = 1;
1868 status_dots_requested = 0;
1869 }
Chris Craikb50c2172013-07-29 15:28:30 -07001870
The Android Open Source Project893912b2009-03-03 19:30:05 -08001871 else if (strcmp(argv[1], "-mv") == 0 ||
1872 strcmp(argv[1], "-vm") == 0 )
1873 {
1874 multiple = 1;
1875 verbose = 1;
1876 status_dots_requested = 1;
1877 }
Chris Craikb50c2172013-07-29 15:28:30 -07001878
The Android Open Source Project893912b2009-03-03 19:30:05 -08001879 else if (strcmp(argv[1], "-v") == 0)
1880 {
1881 verbose = 1;
1882 status_dots_requested = 1;
1883 inname = argv[2];
1884 }
Chris Craikb50c2172013-07-29 15:28:30 -07001885
1886 else if (strcmp(argv[1], "--strict") == 0)
1887 {
1888 status_dots_requested = 0;
1889 verbose = 1;
1890 inname = argv[2];
1891 strict++;
1892 relaxed = 0;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001893 multiple=1;
Chris Craikb50c2172013-07-29 15:28:30 -07001894 }
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++;
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001903 multiple=1;
1904 }
1905 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++;
1913 multiple=1;
Chris Craikb50c2172013-07-29 15:28:30 -07001914 }
1915
The Android Open Source Project893912b2009-03-03 19:30:05 -08001916 else
1917 {
1918 inname = argv[1];
1919 status_dots_requested = 0;
1920 }
1921 }
1922
Matt Sarett9b1fe632015-11-25 10:21:17 -05001923 if (multiple == 0 && argc == 3 + verbose)
Alex Naidis7a055fd2016-10-01 12:23:07 +02001924 outname = argv[2 + verbose];
The Android Open Source Project893912b2009-03-03 19:30:05 -08001925
Matt Sarett9b1fe632015-11-25 10:21:17 -05001926 if ((multiple == 0 && argc > 3 + verbose) ||
1927 (multiple != 0 && argc < 2))
The Android Open Source Project893912b2009-03-03 19:30:05 -08001928 {
Alex Naidis7a055fd2016-10-01 12:23:07 +02001929 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001937 }
1938
Matt Sarett9b1fe632015-11-25 10:21:17 -05001939 if (multiple != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001940 {
1941 int i;
1942#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001943 int allocation_now = current_allocation;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001944#endif
1945 for (i=2; i<argc; ++i)
1946 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001947 int kerror;
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001948 fprintf(STDERR, "\n Testing %s:", argv[i]);
Matt Sarett9b1fe632015-11-25 10:21:17 -05001949#if PNG_DEBUG > 0
1950 fprintf(STDERR, "\n");
1951#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001952 kerror = test_one_file(argv[i], outname);
1953 if (kerror == 0)
1954 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001955#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001956 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001957 (unsigned long)zero_samples);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001958#else
1959 fprintf(STDERR, " PASS\n");
1960#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04001961#ifdef PNG_TIME_RFC1123_SUPPORTED
Matt Sarett9b1fe632015-11-25 10:21:17 -05001962 if (tIME_chunk_present != 0)
1963 fprintf(STDERR, " tIME = %s\n", tIME_string);
Chris Craikb50c2172013-07-29 15:28:30 -07001964
Matt Sarett9b1fe632015-11-25 10:21:17 -05001965 tIME_chunk_present = 0;
1966#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001967 }
Chris Craikb50c2172013-07-29 15:28:30 -07001968
The Android Open Source Project893912b2009-03-03 19:30:05 -08001969 else
1970 {
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001971 if (xfail)
1972 fprintf(STDERR, " XFAIL\n");
1973 else
1974 {
1975 fprintf(STDERR, " FAIL\n");
1976 ierror += kerror;
1977 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001978 }
1979#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
1980 if (allocation_now != current_allocation)
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001981 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
1982 current_allocation - allocation_now);
Chris Craikb50c2172013-07-29 15:28:30 -07001983
The Android Open Source Project893912b2009-03-03 19:30:05 -08001984 if (current_allocation != 0)
1985 {
1986 memory_infop pinfo = pinformation;
1987
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04001988 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
1989 current_allocation);
Chris Craikb50c2172013-07-29 15:28:30 -07001990
The Android Open Source Project893912b2009-03-03 19:30:05 -08001991 while (pinfo != NULL)
1992 {
Matt Sarett9b1fe632015-11-25 10:21:17 -05001993 fprintf(STDERR, " %lu bytes at %p\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02001994 (unsigned long)pinfo->size,
1995 pinfo->pointer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001996 pinfo = pinfo->next;
1997 }
1998 }
1999#endif
2000 }
2001#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002002 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002010#endif
2011 }
Chris Craikb50c2172013-07-29 15:28:30 -07002012
The Android Open Source Project893912b2009-03-03 19:30:05 -08002013 else
2014 {
2015 int i;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002016 for (i = 0; i<3; ++i)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002017 {
2018 int kerror;
2019#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002020 int allocation_now = current_allocation;
The Android Open Source Project893912b2009-03-03 19:30:05 -08002021#endif
Chris Craikb50c2172013-07-29 15:28:30 -07002022 if (i == 1)
2023 status_dots_requested = 1;
2024
2025 else if (verbose == 0)
2026 status_dots_requested = 0;
2027
The Android Open Source Project893912b2009-03-03 19:30:05 -08002028 if (i == 0 || verbose == 1 || ierror != 0)
Matt Sarett9b1fe632015-11-25 10:21:17 -05002029 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002030 fprintf(STDERR, "\n Testing %s:", inname);
Matt Sarett9b1fe632015-11-25 10:21:17 -05002031#if PNG_DEBUG > 0
2032 fprintf(STDERR, "\n");
2033#endif
2034 }
Chris Craikb50c2172013-07-29 15:28:30 -07002035
The Android Open Source Project893912b2009-03-03 19:30:05 -08002036 kerror = test_one_file(inname, outname);
Chris Craikb50c2172013-07-29 15:28:30 -07002037
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002038 if (kerror == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002039 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002040 if (verbose == 1 || i == 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -08002041 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04002042#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08002043 fprintf(STDERR, "\n PASS (%lu zero samples)\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002044 (unsigned long)zero_samples);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002045#else
2046 fprintf(STDERR, " PASS\n");
2047#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -04002048#ifdef PNG_TIME_RFC1123_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002049 if (tIME_chunk_present != 0)
2050 fprintf(STDERR, " tIME = %s\n", tIME_string);
Matt Sarett9b1fe632015-11-25 10:21:17 -05002051#endif /* TIME_RFC1123 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08002052 }
2053 }
Chris Craikb50c2172013-07-29 15:28:30 -07002054
The Android Open Source Project893912b2009-03-03 19:30:05 -08002055 else
2056 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002057 if (verbose == 0 && i != 2)
Matt Sarett9b1fe632015-11-25 10:21:17 -05002058 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002059 fprintf(STDERR, "\n Testing %s:", inname);
Matt Sarett9b1fe632015-11-25 10:21:17 -05002060#if PNG_DEBUG > 0
2061 fprintf(STDERR, "\n");
2062#endif
2063 }
Chris Craikb50c2172013-07-29 15:28:30 -07002064
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002065 if (xfail)
2066 fprintf(STDERR, " XFAIL\n");
2067 else
2068 {
2069 fprintf(STDERR, " FAIL\n");
2070 ierror += kerror;
2071 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08002072 }
2073#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
2074 if (allocation_now != current_allocation)
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002075 fprintf(STDERR, "MEMORY ERROR: %d bytes lost\n",
2076 current_allocation - allocation_now);
Chris Craikb50c2172013-07-29 15:28:30 -07002077
The Android Open Source Project893912b2009-03-03 19:30:05 -08002078 if (current_allocation != 0)
2079 {
2080 memory_infop pinfo = pinformation;
2081
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002082 fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
2083 current_allocation);
Chris Craikb50c2172013-07-29 15:28:30 -07002084
The Android Open Source Project893912b2009-03-03 19:30:05 -08002085 while (pinfo != NULL)
2086 {
Matt Sarett9b1fe632015-11-25 10:21:17 -05002087 fprintf(STDERR, " %lu bytes at %p\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002088 (unsigned long)pinfo->size, pinfo->pointer);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002089 pinfo = pinfo->next;
2090 }
2091 }
2092#endif
2093 }
2094#if defined(PNG_USER_MEM_SUPPORTED) && PNG_DEBUG
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002095 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);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002103#endif
2104 }
2105
2106#ifdef PNGTEST_TIMING
2107 t_stop = (float)clock();
2108 t_misc += (t_stop - t_start);
2109 t_start = t_stop;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002110 fprintf(STDERR, " CPU time used = %.3f seconds",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002111 (t_misc+t_decode+t_encode)/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002112 fprintf(STDERR, " (decoding %.3f,\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002113 t_decode/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002114 fprintf(STDERR, " encoding %.3f ,",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002115 t_encode/(float)CLOCKS_PER_SEC);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07002116 fprintf(STDERR, " other %.3f seconds)\n\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002117 t_misc/(float)CLOCKS_PER_SEC);
The Android Open Source Project893912b2009-03-03 19:30:05 -08002118#endif
2119
2120 if (ierror == 0)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002121 fprintf(STDERR, " libpng passes test\n");
Chris Craikb50c2172013-07-29 15:28:30 -07002122
The Android Open Source Project893912b2009-03-03 19:30:05 -08002123 else
Patrick Scotta0bb96c2009-07-22 11:50:02 -04002124 fprintf(STDERR, " libpng FAILS test\n");
Chris Craikb50c2172013-07-29 15:28:30 -07002125
Matt Sarett9b1fe632015-11-25 10:21:17 -05002126 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",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002129 (unsigned long) png_get_user_width_max(dummy_ptr));
Matt Sarett9b1fe632015-11-25 10:21:17 -05002130 fprintf(STDERR, " height_max = %lu\n",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002131 (unsigned long) png_get_user_height_max(dummy_ptr));
Matt Sarett9b1fe632015-11-25 10:21:17 -05002132 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",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002136 (unsigned long) png_get_chunk_cache_max(dummy_ptr));
Matt Sarett9b1fe632015-11-25 10:21:17 -05002137 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",
Alex Naidis7a055fd2016-10-01 12:23:07 +02002141 (unsigned long) png_get_chunk_malloc_max(dummy_ptr));
Matt Sarett9b1fe632015-11-25 10:21:17 -05002142 png_destroy_read_struct(&dummy_ptr, NULL, NULL);
2143
The Android Open Source Project893912b2009-03-03 19:30:05 -08002144 return (int)(ierror != 0);
2145}
Chris Craikb50c2172013-07-29 15:28:30 -07002146#else
2147int
2148main(void)
2149{
2150 fprintf(STDERR,
Alex Naidis7a055fd2016-10-01 12:23:07 +02002151 " test ignored because libpng was not built with read support\n");
Chris Craikb50c2172013-07-29 15:28:30 -07002152 /* And skip this test */
Leon Scroggins III3cc83ac2017-10-06 11:02:56 -04002153 return PNG_LIBPNG_VER < 10600 ? 0 : 77;
Chris Craikb50c2172013-07-29 15:28:30 -07002154}
2155#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08002156
2157/* Generate a compiler error if there is an old png.h in the search path. */
xNombred07bb0d2020-03-10 20:17:12 +01002158typedef png_libpng_version_1_6_38_git Your_png_h_is_not_version_1_6_38_git;