blob: 0629d94e9ea4fd2251bd9f1903c3ec14d51ce36c [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngset.c - storage of image information into info struct
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -05004 * Last changed in libpng 1.6.19 [(PENDING RELEASE)]
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06005 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * The functions here are used during reads to store data from the file
14 * into the info struct, and during writes to store application data
15 * into the info struct for writing into the file. This abstracts the
16 * info struct and allows us to change the structure in the future.
17 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050023#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -060025png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -050026 png_const_color_16p background)
Andreas Dilger47a0c421997-05-16 02:46:07 -050027{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050028 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050029
John Bowlerfcd301d2011-12-28 21:34:27 -060030 if (png_ptr == NULL || info_ptr == NULL || background == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050031 return;
32
John Bowlerfcd301d2011-12-28 21:34:27 -060033 info_ptr->background = *background;
Andreas Dilger47a0c421997-05-16 02:46:07 -050034 info_ptr->valid |= PNG_INFO_bKGD;
35}
36#endif
37
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050038#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050039void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -060040png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060041 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
42 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
43 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060044{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060045 png_xy xy;
46
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -060047 png_debug1(1, "in %s storage function", "cHRM fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050048
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060049 if (png_ptr == NULL || info_ptr == NULL)
50 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050051
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060052 xy.redx = red_x;
53 xy.redy = red_y;
54 xy.greenx = green_x;
55 xy.greeny = green_y;
56 xy.bluex = blue_x;
57 xy.bluey = blue_y;
58 xy.whitex = white_x;
59 xy.whitey = white_y;
60
61 if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -050062 2/* override with app values*/) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060063 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
64
65 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050067
John Bowler736f40f2011-08-25 16:19:44 -050068void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -060069png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
John Bowler736f40f2011-08-25 16:19:44 -050070 png_fixed_point int_red_X, png_fixed_point int_red_Y,
71 png_fixed_point int_red_Z, png_fixed_point int_green_X,
72 png_fixed_point int_green_Y, png_fixed_point int_green_Z,
73 png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
74 png_fixed_point int_blue_Z)
75{
76 png_XYZ XYZ;
John Bowler736f40f2011-08-25 16:19:44 -050077
78 png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
79
80 if (png_ptr == NULL || info_ptr == NULL)
81 return;
82
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060083 XYZ.red_X = int_red_X;
84 XYZ.red_Y = int_red_Y;
85 XYZ.red_Z = int_red_Z;
86 XYZ.green_X = int_green_X;
87 XYZ.green_Y = int_green_Y;
88 XYZ.green_Z = int_green_Z;
89 XYZ.blue_X = int_blue_X;
90 XYZ.blue_Y = int_blue_Y;
91 XYZ.blue_Z = int_blue_Z;
John Bowler736f40f2011-08-25 16:19:44 -050092
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -050093 if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,
94 &XYZ, 2) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060095 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
John Bowler736f40f2011-08-25 16:19:44 -050096
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060097 png_colorspace_sync_info(png_ptr, info_ptr);
John Bowler736f40f2011-08-25 16:19:44 -050098}
99
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500100# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500101void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600102png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500103 double white_x, double white_y, double red_x, double red_y,
104 double green_x, double green_y, double blue_x, double blue_y)
105{
106 png_set_cHRM_fixed(png_ptr, info_ptr,
107 png_fixed(png_ptr, white_x, "cHRM White X"),
108 png_fixed(png_ptr, white_y, "cHRM White Y"),
109 png_fixed(png_ptr, red_x, "cHRM Red X"),
110 png_fixed(png_ptr, red_y, "cHRM Red Y"),
111 png_fixed(png_ptr, green_x, "cHRM Green X"),
112 png_fixed(png_ptr, green_y, "cHRM Green Y"),
113 png_fixed(png_ptr, blue_x, "cHRM Blue X"),
114 png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
115}
John Bowler736f40f2011-08-25 16:19:44 -0500116
117void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600118png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
John Bowler736f40f2011-08-25 16:19:44 -0500119 double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
120 double blue_X, double blue_Y, double blue_Z)
121{
122 png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
123 png_fixed(png_ptr, red_X, "cHRM Red X"),
124 png_fixed(png_ptr, red_Y, "cHRM Red Y"),
125 png_fixed(png_ptr, red_Z, "cHRM Red Z"),
126 png_fixed(png_ptr, green_X, "cHRM Red X"),
127 png_fixed(png_ptr, green_Y, "cHRM Red Y"),
128 png_fixed(png_ptr, green_Z, "cHRM Red Z"),
129 png_fixed(png_ptr, blue_X, "cHRM Red X"),
130 png_fixed(png_ptr, blue_Y, "cHRM Red Y"),
131 png_fixed(png_ptr, blue_Z, "cHRM Red Z"));
132}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600133# endif /* FLOATING_POINT */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500134
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600135#endif /* cHRM */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600136
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500137#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500138void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -0600139png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
140 png_fixed_point file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500141{
142 png_debug1(1, "in %s storage function", "gAMA");
143
144 if (png_ptr == NULL || info_ptr == NULL)
145 return;
146
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600147 png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);
148 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500149}
150
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500151# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500152void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600153png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500155 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500156 "png_set_gAMA"));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500158# endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500159#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500161#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500162void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600163png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
164 png_const_uint_16p hist)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500165{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500166 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600167
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500168 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500169
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600170 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500172
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600174 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500175 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500176 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600177 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500179 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500180 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600182 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500183
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500184 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
185 * version 1.2.1
186 */
John Bowler4f67e402011-12-28 08:43:37 -0600187 info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600188 PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500189
John Bowler4f67e402011-12-28 08:43:37 -0600190 if (info_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500191 {
192 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600193
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500194 return;
195 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600196
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600197 info_ptr->free_me |= PNG_FREE_HIST;
John Bowler4f67e402011-12-28 08:43:37 -0600198
199 for (i = 0; i < info_ptr->num_palette; i++)
200 info_ptr->hist[i] = hist[i];
201
202 info_ptr->valid |= PNG_INFO_hIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500203}
204#endif
205
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500206void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600207png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500208 png_uint_32 width, png_uint_32 height, int bit_depth,
209 int color_type, int interlace_type, int compression_type,
210 int filter_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500212 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500213
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600214 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500215 return;
216
217 info_ptr->width = width;
218 info_ptr->height = height;
219 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500220 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 info_ptr->compression_type = (png_byte)compression_type;
222 info_ptr->filter_type = (png_byte)filter_type;
223 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500224
225 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
226 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
227 info_ptr->compression_type, info_ptr->filter_type);
228
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500229 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
230 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500231
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500232 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500234
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 else
236 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500237
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500238 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500240
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600242
Glenn Randers-Pehrson421f95d2013-06-04 15:05:12 -0500243 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500244}
245
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500246#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500247void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600248png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600249 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500251 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500252
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600253 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 return;
255
256 info_ptr->x_offset = offset_x;
257 info_ptr->y_offset = offset_y;
258 info_ptr->offset_unit_type = (png_byte)unit_type;
259 info_ptr->valid |= PNG_INFO_oFFs;
260}
261#endif
262
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500263#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500264void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600265png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500266 png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
267 int nparams, png_const_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500269 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600270 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500272 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500273
John Bowlerfcd301d2011-12-28 21:34:27 -0600274 if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600275 || (nparams > 0 && params == NULL))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276 return;
277
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600278 length = strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500279 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600280 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500281
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500282 /* TODO: validate format of calibration name and unit name */
283
284 /* Check that the type matches the specification. */
285 if (type < 0 || type > 3)
286 png_error(png_ptr, "Invalid pCAL equation type");
287
John Bowlerfcd301d2011-12-28 21:34:27 -0600288 if (nparams < 0 || nparams > 255)
289 png_error(png_ptr, "Invalid pCAL parameter count");
290
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500291 /* Validate params[nparams] */
292 for (i=0; i<nparams; ++i)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500293 {
John Bowlerfcd301d2011-12-28 21:34:27 -0600294 if (params[i] == NULL ||
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500295 !png_check_fp_string(params[i], strlen(params[i])))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500296 png_error(png_ptr, "Invalid format for pCAL parameter");
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500297 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500298
John Bowlerfcd301d2011-12-28 21:34:27 -0600299 info_ptr->pcal_purpose = png_voidcast(png_charp,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500300 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500301
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500302 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500303 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500304 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600305
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500306 return;
307 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500308
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600309 memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500311 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 info_ptr->pcal_X0 = X0;
313 info_ptr->pcal_X1 = X1;
314 info_ptr->pcal_type = (png_byte)type;
315 info_ptr->pcal_nparams = (png_byte)nparams;
316
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600317 length = strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500318 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500319 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500320
John Bowlerfcd301d2011-12-28 21:34:27 -0600321 info_ptr->pcal_units = png_voidcast(png_charp,
322 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500323
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500324 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500326 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600327
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500328 return;
329 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500330
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600331 memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332
John Bowlerfcd301d2011-12-28 21:34:27 -0600333 info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600334 (png_size_t)((nparams + 1) * (sizeof (png_charp)))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500335
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500336 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500337 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500338 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600339
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500340 return;
341 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500342
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600343 memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344
345 for (i = 0; i < nparams; i++)
346 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600347 length = strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500348 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500349 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500350
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500351 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500352
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500353 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500354 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500355 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600356
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500357 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500358 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500359
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600360 memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 }
362
363 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500364 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365}
366#endif
367
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500368#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500369void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600370png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500371 int unit, png_const_charp swidth, png_const_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600372{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500373 png_size_t lengthw = 0, lengthh = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500375 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500376
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600377 if (png_ptr == NULL || info_ptr == NULL)
378 return;
379
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500380 /* Double check the unit (should never get here with an invalid
381 * unit unless this is an API call.)
382 */
383 if (unit != 1 && unit != 2)
384 png_error(png_ptr, "Invalid sCAL unit");
385
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600386 if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500387 swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500388 png_error(png_ptr, "Invalid sCAL width");
389
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600390 if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500391 sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500392 png_error(png_ptr, "Invalid sCAL height");
393
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500396 ++lengthw;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500397
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600398 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500399
John Bowlerfcd301d2011-12-28 21:34:27 -0600400 info_ptr->scal_s_width = png_voidcast(png_charp,
401 png_malloc_warn(png_ptr, lengthw));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500402
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500403 if (info_ptr->scal_s_width == NULL)
404 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500405 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600406
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500407 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500408 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500409
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600410 memcpy(info_ptr->scal_s_width, swidth, lengthw);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600411
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500412 ++lengthh;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500413
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600414 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
John Bowlerfcd301d2011-12-28 21:34:27 -0600416 info_ptr->scal_s_height = png_voidcast(png_charp,
417 png_malloc_warn(png_ptr, lengthh));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500418
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500419 if (info_ptr->scal_s_height == NULL)
420 {
421 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500422 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500423
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500424 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600425
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500426 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500427 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500428
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600429 memcpy(info_ptr->scal_s_height, sheight, lengthh);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500430
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600431 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500432 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600433}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500434
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500435# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500436void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600437png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
438 double width, double height)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500439{
440 png_debug1(1, "in %s storage function", "sCAL");
441
442 /* Check the arguments. */
443 if (width <= 0)
444 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500445
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500446 else if (height <= 0)
447 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500448
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500449 else
450 {
451 /* Convert 'width' and 'height' to ASCII. */
452 char swidth[PNG_sCAL_MAX_DIGITS+1];
453 char sheight[PNG_sCAL_MAX_DIGITS+1];
454
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600455 png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500456 PNG_sCAL_PRECISION);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600457 png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500458 PNG_sCAL_PRECISION);
459
460 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
461 }
462}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500463# endif
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500464
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500465# ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500466void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600467png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500468 png_fixed_point width, png_fixed_point height)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500469{
470 png_debug1(1, "in %s storage function", "sCAL");
471
472 /* Check the arguments. */
473 if (width <= 0)
474 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500475
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500476 else if (height <= 0)
477 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500478
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500479 else
480 {
481 /* Convert 'width' and 'height' to ASCII. */
482 char swidth[PNG_sCAL_MAX_DIGITS+1];
483 char sheight[PNG_sCAL_MAX_DIGITS+1];
484
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600485 png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
486 png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500487
488 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
489 }
490}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500491# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600492#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600493
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500494#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500495void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600496png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600497 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500498{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500499 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500500
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600501 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500502 return;
503
504 info_ptr->x_pixels_per_unit = res_x;
505 info_ptr->y_pixels_per_unit = res_y;
506 info_ptr->phys_unit_type = (png_byte)unit_type;
507 info_ptr->valid |= PNG_INFO_pHYs;
508}
509#endif
510
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500511void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600512png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500513 png_const_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600515
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500516 png_uint_32 max_palette_length;
517
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500518 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500519
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600520 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521 return;
522
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500523 max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
524 (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
525
526 if (num_palette < 0 || num_palette > max_palette_length)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500527 {
528 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600529 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500530
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500531 else
532 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600533 png_warning(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600534
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600535 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500536 }
537 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600538
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600539 if ((num_palette > 0 && palette == NULL) ||
540 (num_palette == 0
541# ifdef PNG_MNG_FEATURES_SUPPORTED
542 && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
543# endif
544 ))
545 {
Glenn Randers-Pehrson67f101e2013-12-14 12:36:28 -0600546 png_error(png_ptr, "Invalid palette");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600547 }
548
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600549 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600550 * we do it for backward compatibility with the way the png_handle_tRNS
551 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600552 *
553 * 1.6.0: the above statement appears to be incorrect; something has to set
554 * the palette inside png_struct on read.
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600555 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600556 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500557
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600558 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500559 * of num_palette entries, in case of an invalid PNG file or incorrect
560 * call to png_set_PLTE() with too-large sample values.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500561 */
John Bowlerfcd301d2011-12-28 21:34:27 -0600562 png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600563 PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500564
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600565 if (num_palette > 0)
566 memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600567 info_ptr->palette = png_ptr->palette;
568 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600569
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600570 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600571
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600572 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500573}
574
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500575#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500576void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600577png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500578 png_const_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500580 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500581
John Bowlerfcd301d2011-12-28 21:34:27 -0600582 if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583 return;
584
John Bowlerfcd301d2011-12-28 21:34:27 -0600585 info_ptr->sig_bit = *sig_bit;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500586 info_ptr->valid |= PNG_INFO_sBIT;
587}
588#endif
589
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500590#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500591void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600592png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600593{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500594 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500595
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600596 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600597 return;
598
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600599 (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);
600 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600601}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600602
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500603void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600604png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson8d0bc0f2011-01-25 22:15:58 -0600605 int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600606{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500607 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500608
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600609 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610 return;
611
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500612 if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,
613 srgb_intent) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600614 {
615 /* This causes the gAMA and cHRM to be written too */
616 info_ptr->colorspace.flags |=
617 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
618 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600619
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600620 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600621}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600622#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600623
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600624
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500625#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500626void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600627png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500628 png_const_charp name, int compression_type,
629 png_const_bytep profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600630{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500631 png_charp new_iccp_name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500632 png_bytep new_iccp_profile;
John Bowlerf3f7e142011-09-09 07:32:37 -0500633 png_size_t length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500634
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500635 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500636
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600637 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
638 return;
639
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600640 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
641 png_app_error(png_ptr, "Invalid iCCP compression method");
642
643 /* Set the colorspace first because this validates the profile; do not
644 * override previously set app cHRM or gAMA here (because likely as not the
645 * application knows better than libpng what the correct values are.) Pass
646 * the info_ptr color_type field to png_colorspace_set_ICC because in the
647 * write case it has not yet been stored in png_ptr.
648 */
649 {
650 int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
651 proflen, profile, info_ptr->color_type);
652
653 png_colorspace_sync_info(png_ptr, info_ptr);
654
655 /* Don't do any of the copying if the profile was bad, or inconsistent. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600656 if (result == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600657 return;
658
659 /* But do write the gAMA and cHRM chunks from the profile. */
660 info_ptr->colorspace.flags |=
661 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
662 }
663
664 length = strlen(name)+1;
John Bowlerfcd301d2011-12-28 21:34:27 -0600665 new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500666
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500667 if (new_iccp_name == NULL)
668 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600669 png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600670
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500671 return;
672 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500673
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600674 memcpy(new_iccp_name, name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -0600675 new_iccp_profile = png_voidcast(png_bytep,
676 png_malloc_warn(png_ptr, proflen));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500677
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500678 if (new_iccp_profile == NULL)
679 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600680 png_free(png_ptr, new_iccp_name);
681 png_benign_error(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600682 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600683
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500684 return;
685 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500686
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600687 memcpy(new_iccp_profile, profile, proflen);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500688
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500689 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500690
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600691 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500692 info_ptr->iccp_name = new_iccp_name;
693 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600694 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 info_ptr->valid |= PNG_INFO_iCCP;
696}
697#endif
698
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500699#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500700void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600701png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
702 png_const_textp text_ptr, int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500704 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500705 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500706
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600707 if (ret != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500708 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500709}
710
711int /* PRIVATE */
John Bowler4f67e402011-12-28 08:43:37 -0600712png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500713 png_const_textp text_ptr, int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500714{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 int i;
716
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -0500717 png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500718 (unsigned long)png_ptr->chunk_name);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600720 if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500721 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722
723 /* Make sure we have enough space in the "text" array in info_struct
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600724 * to hold all of the incoming text_ptr objects. This compare can't overflow
725 * because max_text >= num_text (anyway, subtract of two positive integers
726 * can't overflow in any case.)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600728 if (num_text > info_ptr->max_text - info_ptr->num_text)
Glenn Randers-Pehrson590c8b02013-01-19 08:49:12 -0600729 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600730 int old_num_text = info_ptr->num_text;
731 int max_text;
732 png_textp new_text = NULL;
733
734 /* Calculate an appropriate max_text, checking for overflow. */
735 max_text = old_num_text;
736 if (num_text <= INT_MAX - max_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600738 max_text += num_text;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600740 /* Round up to a multiple of 8 */
741 if (max_text < INT_MAX-8)
742 max_text = (max_text + 8) & ~0x7;
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500743
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600744 else
745 max_text = INT_MAX;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500746
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -0500747 /* Now allocate a new array and copy the old members in; this does all
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600748 * the overflow checks.
749 */
750 new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
751 info_ptr->text, old_num_text, max_text-old_num_text,
752 sizeof *new_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500754
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600755 if (new_text == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600757 png_chunk_report(png_ptr, "too many text chunks",
758 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600759
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600760 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500761 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500762
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600763 png_free(png_ptr, info_ptr->text);
764
765 info_ptr->text = new_text;
766 info_ptr->free_me |= PNG_FREE_TEXT;
767 info_ptr->max_text = max_text;
768 /* num_text is adjusted below as the entries are copied in */
769
770 png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500771 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600772
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773 for (i = 0; i < num_text; i++)
774 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600775 size_t text_length, key_len;
776 size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
778
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500779 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600780 continue;
781
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500782 if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
783 text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500784 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600785 png_chunk_report(png_ptr, "text compression mode is out of range",
786 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500787 continue;
788 }
789
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600790 key_len = strlen(text_ptr[i].key);
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500791
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500793 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500794 lang_len = 0;
795 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500796 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500797
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500798 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500799# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600800 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500801 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600802
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500803 if (text_ptr[i].lang != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600804 lang_len = strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500805
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500806 else
807 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500808
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500809 if (text_ptr[i].lang_key != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600810 lang_key_len = strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500811
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500812 else
813 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600814 }
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600815# else /* iTXt */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500816 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600817 png_chunk_report(png_ptr, "iTXt chunk not supported",
818 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500819 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500820 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500821# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500822
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500823 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600825 text_length = 0;
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500826# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600828 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500829
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600830 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500831# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600832 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500833 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500834
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 else
836 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600837 text_length = strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500838 textp->compression = text_ptr[i].compression;
839 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500840
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600841 textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
842 key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500843
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500844 if (textp->key == NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600845 {
846 png_chunk_report(png_ptr, "text chunk: out of memory",
847 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600848
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600849 return 1;
850 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500851
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600852 png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600853 (unsigned long)(png_uint_32)
854 (key_len + lang_len + lang_key_len + text_length + 4),
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600855 textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500856
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600857 memcpy(textp->key, text_ptr[i].key, key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500858 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500859
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600860 if (text_ptr[i].compression > 0)
861 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500862 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600863 memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500864 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500865 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600866 memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500867 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500868 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600869 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500870
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600871 else
872 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500873 textp->lang=NULL;
874 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500875 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600876 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500877
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600878 if (text_length != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600879 memcpy(textp->text, text_ptr[i].text, text_length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500880
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500881 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600882
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500883# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500884 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600885 {
886 textp->text_length = 0;
887 textp->itxt_length = text_length;
888 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500889
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600890 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500891# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600892 {
893 textp->text_length = text_length;
894 textp->itxt_length = 0;
895 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500896
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500898 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600900
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500901 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500902}
903#endif
904
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500906void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600907png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
908 png_const_timep mod_time)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500909{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500910 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500911
John Bowlerfcd301d2011-12-28 21:34:27 -0600912 if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500913 (png_ptr->mode & PNG_WROTE_tIME) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914 return;
915
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600916 if (mod_time->month == 0 || mod_time->month > 12 ||
917 mod_time->day == 0 || mod_time->day > 31 ||
918 mod_time->hour > 23 || mod_time->minute > 59 ||
919 mod_time->second > 60)
920 {
921 png_warning(png_ptr, "Ignoring invalid time value");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600922
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600923 return;
924 }
925
John Bowlerfcd301d2011-12-28 21:34:27 -0600926 info_ptr->mod_time = *mod_time;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 info_ptr->valid |= PNG_INFO_tIME;
928}
929#endif
930
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500931#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500932void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600933png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500934 png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500935{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500936 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500937
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600938 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600939
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 return;
941
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500942 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600943 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500944 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500945 * we do it for backward compatibility with the way the png_handle_tRNS
946 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600947 *
948 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
949 * relies on png_set_tRNS storing the information in png_struct
950 * (otherwise it won't be there for the code in pngrtran.c).
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500951 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500952
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600953 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500954
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600955 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
John Bowler4f67e402011-12-28 08:43:37 -0600956 png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep,
957 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500958
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500959 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600960 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600961 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500962
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500963 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 {
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600965#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -0600966 if (info_ptr->bit_depth < 16)
967 {
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600968 int sample_max = (1 << info_ptr->bit_depth) - 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500969
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -0600970 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
971 trans_color->gray > sample_max) ||
972 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
973 (trans_color->red > sample_max ||
974 trans_color->green > sample_max ||
975 trans_color->blue > sample_max)))
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600976 png_warning(png_ptr,
977 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -0600978 }
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600979#endif
980
981 info_ptr->trans_color = *trans_color;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500982
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600983 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500984 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500985 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500986
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500988
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500989 if (num_trans != 0)
990 {
991 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500992 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500993 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500994}
995#endif
996
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500997#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500998void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600999png_set_sPLT(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -06001000 png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -06001001/*
1002 * entries - array of png_sPLT_t structures
1003 * to be added to the list of palettes
1004 * in the info structure.
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001005 *
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -06001006 * nentries - number of palette structures to be
1007 * added.
1008 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001010 png_sPLT_tp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001011
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001012 if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001013 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001015 /* Use the internal realloc function, which checks for all the possible
1016 * overflows. Notice that the parameters are (int) and (size_t)
1017 */
1018 np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
1019 info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1020 sizeof *np));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001021
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001022 if (np == NULL)
1023 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001024 /* Out of memory or too many chunks */
1025 png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001026
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001027 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001028 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001029
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001030 png_free(png_ptr, info_ptr->splt_palettes);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001031 info_ptr->splt_palettes = np;
1032 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001033
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001034 np += info_ptr->splt_palettes_num;
1035
1036 do
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037 {
John Bowlerf3f7e142011-09-09 07:32:37 -05001038 png_size_t length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001039
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001040 /* Skip invalid input entries */
1041 if (entries->name == NULL || entries->entries == NULL)
1042 {
1043 /* png_handle_sPLT doesn't do this, so this is an app error */
1044 png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
1045 /* Just skip the invalid entry */
1046 continue;
1047 }
1048
1049 np->depth = entries->depth;
1050
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001051 /* In the event of out-of-memory just return - there's no point keeping
1052 * on trying to add sPLT chunks.
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001053 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001054 length = strlen(entries->name) + 1;
1055 np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001056
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001057 if (np->name == NULL)
1058 break;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001059
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001060 memcpy(np->name, entries->name, length);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001061
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001062 /* IMPORTANT: we have memory now that won't get freed if something else
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001063 * goes wrong; this code must free it. png_malloc_array produces no
1064 * warnings; use a png_chunk_report (below) if there is an error.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001065 */
1066 np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
1067 entries->nentries, sizeof (png_sPLT_entry)));
1068
1069 if (np->entries == NULL)
John Bowler2414bd92013-01-19 23:18:59 -06001070 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001071 png_free(png_ptr, np->name);
Glenn Randers-Pehrson11d4a2a2014-10-29 08:23:19 -05001072 np->name = NULL;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001073 break;
John Bowler2414bd92013-01-19 23:18:59 -06001074 }
1075
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001076 np->nentries = entries->nentries;
1077 /* This multiply can't overflow because png_malloc_array has already
1078 * checked it when doing the allocation.
1079 */
1080 memcpy(np->entries, entries->entries,
1081 entries->nentries * sizeof (png_sPLT_entry));
John Bowler2414bd92013-01-19 23:18:59 -06001082
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001083 /* Note that 'continue' skips the advance of the out pointer and out
1084 * count, so an invalid entry is not added.
1085 */
1086 info_ptr->valid |= PNG_INFO_sPLT;
1087 ++(info_ptr->splt_palettes_num);
1088 ++np;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001089 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001090 while (++entries, --nentries);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001091
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001092 if (nentries > 0)
1093 png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001094}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001095#endif /* sPLT */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001097#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1098static png_byte
1099check_location(png_const_structrp png_ptr, int location)
1100{
1101 location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1102
1103 /* New in 1.6.0; copy the location and check it. This is an API
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001104 * change; previously the app had to use the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001105 * png_set_unknown_chunk_location API below for each chunk.
1106 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001107 if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001108 {
1109 /* Write struct, so unknown chunks come from the app */
1110 png_app_warning(png_ptr,
1111 "png_set_unknown_chunks now expects a valid location");
1112 /* Use the old behavior */
1113 location = (png_byte)(png_ptr->mode &
1114 (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
1115 }
1116
1117 /* This need not be an internal error - if the app calls
1118 * png_set_unknown_chunks on a read pointer it must get the location right.
1119 */
1120 if (location == 0)
1121 png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1122
1123 /* Now reduce the location to the top-most set bit by removing each least
1124 * significant bit in turn.
1125 */
1126 while (location != (location & -location))
1127 location &= ~(location & -location);
1128
1129 /* The cast is safe because 'location' is a bit mask and only the low four
1130 * bits are significant.
1131 */
1132 return (png_byte)location;
1133}
1134
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001135void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001136png_set_unknown_chunks(png_const_structrp png_ptr,
Glenn Randers-Pehrson590c8b02013-01-19 08:49:12 -06001137 png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001138{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001139 png_unknown_chunkp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001140
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001141 if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001142 unknowns == NULL)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001143 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001144
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001145 /* Check for the failure cases where support has been disabled at compile
1146 * time. This code is hardly ever compiled - it's here because
1147 * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
1148 * code) but may be meaningless if the read or write handling of unknown
1149 * chunks is not compiled in.
1150 */
1151# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
1152 defined(PNG_READ_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001153 if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001154 {
1155 png_app_error(png_ptr, "no unknown chunk support on read");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001156
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001157 return;
1158 }
1159# endif
1160# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
1161 defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001162 if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001163 {
1164 png_app_error(png_ptr, "no unknown chunk support on write");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001165
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001166 return;
1167 }
1168# endif
1169
1170 /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
1171 * unknown critical chunks could be lost with just a warning resulting in
1172 * undefined behavior. Now png_chunk_report is used to provide behavior
1173 * appropriate to read or write.
1174 */
1175 np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
1176 info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1177 sizeof *np));
Glenn Randers-Pehrson590c8b02013-01-19 08:49:12 -06001178
1179 if (np == NULL)
1180 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001181 png_chunk_report(png_ptr, "too many unknown chunks",
1182 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001183
John Bowler1c6e22a2013-01-10 10:55:34 -06001184 return;
1185 }
1186
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001187 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001188 info_ptr->unknown_chunks = np; /* safe because it is initialized */
1189 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001190
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001191 np += info_ptr->unknown_chunks_num;
1192
1193 /* Increment unknown_chunks_num each time round the loop to protect the
1194 * just-allocated chunk data.
1195 */
1196 for (; num_unknowns > 0; --num_unknowns, ++unknowns)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001197 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001198 memcpy(np->name, unknowns->name, (sizeof np->name));
1199 np->name[(sizeof np->name)-1] = '\0';
1200 np->location = check_location(png_ptr, unknowns->location);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001201
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001202 if (unknowns->size == 0)
1203 {
1204 np->data = NULL;
1205 np->size = 0;
1206 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001207
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001208 else
1209 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001210 np->data = png_voidcast(png_bytep,
1211 png_malloc_base(png_ptr, unknowns->size));
John Bowler2414bd92013-01-19 23:18:59 -06001212
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001213 if (np->data == NULL)
John Bowler2414bd92013-01-19 23:18:59 -06001214 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001215 png_chunk_report(png_ptr, "unknown chunk: out of memory",
1216 PNG_CHUNK_WRITE_ERROR);
1217 /* But just skip storing the unknown chunk */
1218 continue;
John Bowler2414bd92013-01-19 23:18:59 -06001219 }
1220
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001221 memcpy(np->data, unknowns->data, unknowns->size);
1222 np->size = unknowns->size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001223 }
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001224
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001225 /* These increments are skipped on out-of-memory for the data - the
1226 * unknown chunk entry gets overwritten if the png_chunk_report returns.
1227 * This is correct in the read case (the chunk is just dropped.)
1228 */
1229 ++np;
1230 ++(info_ptr->unknown_chunks_num);
1231 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001232}
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001233
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001234void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001235png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001236 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001237{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001238 /* This API is pretty pointless in 1.6.0 because the location can be set
1239 * before the call to png_set_unknown_chunks.
1240 *
1241 * TODO: add a png_app_warning in 1.7
1242 */
1243 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1244 chunk < info_ptr->unknown_chunks_num)
1245 {
1246 if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1247 {
1248 png_app_error(png_ptr, "invalid unknown chunk location");
1249 /* Fake out the pre 1.6.0 behavior: */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001250 if ((location & PNG_HAVE_IDAT) != 0) /* undocumented! */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001251 location = PNG_AFTER_IDAT;
1252
1253 else
1254 location = PNG_HAVE_IHDR; /* also undocumented */
1255 }
1256
1257 info_ptr->unknown_chunks[chunk].location =
1258 check_location(png_ptr, location);
1259 }
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001260}
Glenn Randers-Pehrson217d38c2015-03-07 10:35:03 -06001261#endif /* STORE_UNKNOWN_CHUNKS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001262
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001263#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001264png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001265png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001266{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001267 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001268
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001269 if (png_ptr == NULL)
John Bowler4f67e402011-12-28 08:43:37 -06001270 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001271
John Bowler4f67e402011-12-28 08:43:37 -06001272 png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001273
John Bowler4f67e402011-12-28 08:43:37 -06001274 return png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001275}
1276#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001277
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001278#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001279static unsigned int
1280add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1281{
1282 unsigned int i;
1283
1284 /* Utility function: update the 'keep' state of a chunk if it is already in
1285 * the list, otherwise add it to the list.
1286 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001287 for (i=0; i<count; ++i, list += 5)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001288 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001289 if (memcmp(list, add, 4) == 0)
1290 {
1291 list[4] = (png_byte)keep;
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001292
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001293 return count;
1294 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001295 }
1296
1297 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1298 {
1299 ++count;
1300 memcpy(list, add, 4);
1301 list[4] = (png_byte)keep;
1302 }
1303
1304 return count;
1305}
1306
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001307void PNGAPI
John Bowlere9567512012-08-15 22:53:00 -05001308png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001309 png_const_bytep chunk_list, int num_chunks_in)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001310{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001311 png_bytep new_list;
1312 unsigned int num_chunks, old_num_chunks;
1313
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001314 if (png_ptr == NULL)
1315 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001316
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001317 if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
John Bowleraa9dea52012-08-10 19:04:08 -05001318 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001319 png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001320
John Bowlere9567512012-08-15 22:53:00 -05001321 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001322 }
1323
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001324 if (num_chunks_in <= 0)
1325 {
1326 png_ptr->unknown_default = keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001327
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001328 /* '0' means just set the flags, so stop here */
1329 if (num_chunks_in == 0)
1330 return;
1331 }
1332
1333 if (num_chunks_in < 0)
1334 {
1335 /* Ignore all unknown chunks and all chunks recognized by
1336 * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1337 */
1338 static PNG_CONST png_byte chunks_to_ignore[] = {
1339 98, 75, 71, 68, '\0', /* bKGD */
1340 99, 72, 82, 77, '\0', /* cHRM */
1341 103, 65, 77, 65, '\0', /* gAMA */
1342 104, 73, 83, 84, '\0', /* hIST */
1343 105, 67, 67, 80, '\0', /* iCCP */
1344 105, 84, 88, 116, '\0', /* iTXt */
1345 111, 70, 70, 115, '\0', /* oFFs */
1346 112, 67, 65, 76, '\0', /* pCAL */
1347 112, 72, 89, 115, '\0', /* pHYs */
1348 115, 66, 73, 84, '\0', /* sBIT */
1349 115, 67, 65, 76, '\0', /* sCAL */
1350 115, 80, 76, 84, '\0', /* sPLT */
1351 115, 84, 69, 82, '\0', /* sTER */
1352 115, 82, 71, 66, '\0', /* sRGB */
1353 116, 69, 88, 116, '\0', /* tEXt */
1354 116, 73, 77, 69, '\0', /* tIME */
1355 122, 84, 88, 116, '\0' /* zTXt */
1356 };
1357
1358 chunk_list = chunks_to_ignore;
John Bowler03df1892014-11-05 17:19:36 -06001359 num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001360 }
1361
1362 else /* num_chunks_in > 0 */
1363 {
1364 if (chunk_list == NULL)
1365 {
1366 /* Prior to 1.6.0 this was silently ignored, now it is an app_error
1367 * which can be switched off.
1368 */
1369 png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001370
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001371 return;
1372 }
1373
1374 num_chunks = num_chunks_in;
1375 }
John Bowlerfcd301d2011-12-28 21:34:27 -06001376
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001377 old_num_chunks = png_ptr->num_chunk_list;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001378 if (png_ptr->chunk_list == NULL)
1379 old_num_chunks = 0;
John Bowleraa9dea52012-08-10 19:04:08 -05001380
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001381 /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1382 */
1383 if (num_chunks + old_num_chunks > UINT_MAX/5)
John Bowleraa9dea52012-08-10 19:04:08 -05001384 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001385 png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001386
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001387 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001388 }
1389
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001390 /* If these chunks are being reset to the default then no more memory is
1391 * required because add_one_chunk above doesn't extend the list if the 'keep'
1392 * parameter is the default.
1393 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001394 if (keep != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001395 {
1396 new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1397 5 * (num_chunks + old_num_chunks)));
John Bowlere9567512012-08-15 22:53:00 -05001398
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001399 if (old_num_chunks > 0)
1400 memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1401 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001402
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001403 else if (old_num_chunks > 0)
1404 new_list = png_ptr->chunk_list;
1405
1406 else
1407 new_list = NULL;
1408
1409 /* Add the new chunks together with each one's handling code. If the chunk
1410 * already exists the code is updated, otherwise the chunk is added to the
1411 * end. (In libpng 1.6.0 order no longer matters because this code enforces
1412 * the earlier convention that the last setting is the one that is used.)
1413 */
1414 if (new_list != NULL)
1415 {
1416 png_const_bytep inlist;
1417 png_bytep outlist;
1418 unsigned int i;
1419
1420 for (i=0; i<num_chunks; ++i)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001421 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001422 old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1423 chunk_list+5*i, keep);
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001424 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001425
1426 /* Now remove any spurious 'default' entries. */
1427 num_chunks = 0;
1428 for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001429 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001430 if (inlist[4])
1431 {
1432 if (outlist != inlist)
1433 memcpy(outlist, inlist, 5);
1434 outlist += 5;
1435 ++num_chunks;
1436 }
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001437 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001438
1439 /* This means the application has removed all the specialized handling. */
1440 if (num_chunks == 0)
1441 {
1442 if (png_ptr->chunk_list != new_list)
1443 png_free(png_ptr, new_list);
1444
1445 new_list = NULL;
1446 }
1447 }
1448
1449 else
1450 num_chunks = 0;
1451
1452 png_ptr->num_chunk_list = num_chunks;
1453
1454 if (png_ptr->chunk_list != new_list)
1455 {
1456 if (png_ptr->chunk_list != NULL)
1457 png_free(png_ptr, png_ptr->chunk_list);
1458
1459 png_ptr->chunk_list = new_list;
1460 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001461}
1462#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001463
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001464#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001465void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001466png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001467 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001468{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001469 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001470
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001471 if (png_ptr == NULL)
1472 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001473
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001474 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1475 png_ptr->user_chunk_ptr = user_chunk_ptr;
1476}
1477#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001478
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001479#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001480void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001481png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1482 png_bytepp row_pointers)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001483{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001484 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001485
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001486 if (png_ptr == NULL || info_ptr == NULL)
1487 return;
1488
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001489 if (info_ptr->row_pointers != NULL &&
1490 (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001491 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001492
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001493 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001494
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001495 if (row_pointers != NULL)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001496 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001497}
1498#endif
1499
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001500void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001501png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001502{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001503 if (png_ptr == NULL)
1504 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001505
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001506 if (size == 0 || size > PNG_UINT_31_MAX)
1507 png_error(png_ptr, "invalid compression buffer size");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001508
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001509# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001510 if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001511 {
1512 png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1513 return;
1514 }
1515# endif
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001516
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001517# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001518 if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001519 {
1520 if (png_ptr->zowner != 0)
1521 {
1522 png_warning(png_ptr,
1523 "Compression buffer size cannot be changed because it is in use");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001524
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001525 return;
1526 }
John Bowleraa9dea52012-08-10 19:04:08 -05001527
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001528#ifndef __COVERITY__
Glenn Randers-Pehrsonc861dc82015-04-01 12:06:01 -05001529 /* Some compilers complain that this is always false. However, it
1530 * can be true when integer overflow happens.
1531 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001532 if (size > ZLIB_IO_MAX)
1533 {
1534 png_warning(png_ptr,
1535 "Compression buffer size limited to system maximum");
1536 size = ZLIB_IO_MAX; /* must fit */
1537 }
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001538#endif
John Bowleraa9dea52012-08-10 19:04:08 -05001539
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001540 if (size < 6)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001541 {
1542 /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1543 * if this is permitted.
1544 */
1545 png_warning(png_ptr,
1546 "Compression buffer size cannot be reduced below 6");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001547
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001548 return;
1549 }
1550
1551 if (png_ptr->zbuffer_size != size)
1552 {
1553 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1554 png_ptr->zbuffer_size = (uInt)size;
1555 }
1556 }
1557# endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001558}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001559
1560void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001561png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001562{
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001563 if (png_ptr != NULL && info_ptr != NULL)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001564 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001565}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001566
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001567
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001568#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001569/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001570void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001571png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001572 png_uint_32 user_height_max)
1573{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001574 /* Images with dimensions larger than these limits will be
1575 * rejected by png_set_IHDR(). To accept any PNG datastream
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -05001576 * regardless of dimensions, set both limits to 0x7ffffff.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001577 */
1578 if (png_ptr == NULL)
1579 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001580
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001581 png_ptr->user_width_max = user_width_max;
1582 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001583}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001584
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001585/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001586void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001587png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001588{
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001589 if (png_ptr != NULL)
1590 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001591}
1592
1593/* This function was added to libpng 1.4.1 */
1594void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001595png_set_chunk_malloc_max (png_structrp png_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001596 png_alloc_size_t user_chunk_malloc_max)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001597{
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001598 if (png_ptr != NULL)
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001599 png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001600}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001601#endif /* ?SET_USER_LIMITS */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001602
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001603
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001604#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001605void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001606png_set_benign_errors(png_structrp png_ptr, int allowed)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001607{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001608 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001609
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001610 /* If allowed is 1, png_benign_error() is treated as a warning.
1611 *
1612 * If allowed is 0, png_benign_error() is treated as an error (which
1613 * is the default behavior if png_set_benign_errors() is not called).
1614 */
1615
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001616 if (allowed != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001617 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1618 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001619
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001620 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001621 png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1622 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001623}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001624#endif /* BENIGN_ERRORS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001625
1626#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
1627 /* Whether to report invalid palette index; added at libng-1.5.10.
1628 * It is possible for an indexed (color-type==3) PNG file to contain
1629 * pixels with invalid (out-of-range) indexes if the PLTE chunk has
1630 * fewer entries than the image's bit-depth would allow. We recover
Glenn Randers-Pehrson6cae24c2014-10-13 11:11:21 -05001631 * from this gracefully by filling any incomplete palette with zeros
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001632 * (opaque black). By default, when this occurs libpng will issue
1633 * a benign error. This API can be used to override that behavior.
1634 */
1635void PNGAPI
1636png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1637{
1638 png_debug(1, "in png_set_check_for_invalid_index");
1639
1640 if (allowed > 0)
1641 png_ptr->num_palette_max = 0;
1642
1643 else
1644 png_ptr->num_palette_max = -1;
1645}
1646#endif
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001647#endif /* READ || WRITE */