blob: ec75dbe36903e642a2fe9fe0db834b88a6ee7a81 [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 *
Cosmin Trutaa8738932018-07-28 18:47:21 -04004 * Copyright (c) 2018 Cosmin Truta
Cosmin Truta46aedd82018-07-15 23:58:00 -04005 * Copyright (c) 1998-2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * 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,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500107 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"));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500115}
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,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500123 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 Green X"),
127 png_fixed(png_ptr, green_Y, "cHRM Green Y"),
128 png_fixed(png_ptr, green_Z, "cHRM Green Z"),
129 png_fixed(png_ptr, blue_X, "cHRM Blue X"),
130 png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
131 png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
John Bowler736f40f2011-08-25 16:19:44 -0500132}
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-Pehrson68cb0aa2017-07-13 11:19:53 -0500137#ifdef PNG_eXIf_SUPPORTED
138void PNGAPI
Glenn Randers-Pehrson7c709f02017-08-02 16:48:11 -0500139png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,
Cosmin Trutaceb32772018-08-18 22:47:16 -0400140 png_bytep eXIf_buf)
Glenn Randers-Pehrson7c709f02017-08-02 16:48:11 -0500141{
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -0500142 png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");
143 PNG_UNUSED(info_ptr)
144 PNG_UNUSED(eXIf_buf)
145}
146
147void PNGAPI
148png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,
Cosmin Trutaceb32772018-08-18 22:47:16 -0400149 png_uint_32 num_exif, png_bytep eXIf_buf)
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -0500150{
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -0500151 int i;
152
153 png_debug1(1, "in %s storage function", "eXIf");
154
155 if (png_ptr == NULL || info_ptr == NULL)
156 return;
157
Glenn Randers-Pehrson71a56182017-08-01 21:42:16 -0500158 if (info_ptr->exif)
159 {
160 png_free(png_ptr, info_ptr->exif);
161 info_ptr->exif = NULL;
162 }
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -0500163
Glenn Randers-Pehrsond930d362017-08-03 10:29:10 -0500164 info_ptr->num_exif = num_exif;
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -0500165
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -0500166 info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr,
167 info_ptr->num_exif));
168
169 if (info_ptr->exif == NULL)
170 {
171 png_warning(png_ptr, "Insufficient memory for eXIf chunk data");
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -0500172 return;
173 }
174
175 info_ptr->free_me |= PNG_FREE_EXIF;
176
Glenn Randers-Pehrson3fa1df42017-08-02 17:27:41 -0500177 for (i = 0; i < (int) info_ptr->num_exif; i++)
Glenn Randers-Pehrson68cb0aa2017-07-13 11:19:53 -0500178 info_ptr->exif[i] = eXIf_buf[i];
179
180 info_ptr->valid |= PNG_INFO_eXIf;
181}
182#endif /* eXIf */
183
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500184#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500185void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -0600186png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
187 png_fixed_point file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500188{
189 png_debug1(1, "in %s storage function", "gAMA");
190
191 if (png_ptr == NULL || info_ptr == NULL)
192 return;
193
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600194 png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);
195 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500196}
197
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500198# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500199void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600200png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500201{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500202 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500203 "png_set_gAMA"));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500205# endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500206#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500208#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500209void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600210png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
211 png_const_uint_16p hist)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500212{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500213 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600214
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500215 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500216
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600217 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500219
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500220 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600221 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500222 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500223 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600224 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500225
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500226 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500227 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600229 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500230
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500231 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
232 * version 1.2.1
233 */
John Bowler4f67e402011-12-28 08:43:37 -0600234 info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600235 PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500236
John Bowler4f67e402011-12-28 08:43:37 -0600237 if (info_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500238 {
239 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600240
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500241 return;
242 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600243
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600244 info_ptr->free_me |= PNG_FREE_HIST;
John Bowler4f67e402011-12-28 08:43:37 -0600245
246 for (i = 0; i < info_ptr->num_palette; i++)
247 info_ptr->hist[i] = hist[i];
248
249 info_ptr->valid |= PNG_INFO_hIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250}
251#endif
252
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500253void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600254png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500255 png_uint_32 width, png_uint_32 height, int bit_depth,
256 int color_type, int interlace_type, int compression_type,
257 int filter_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500259 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500260
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600261 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500262 return;
263
264 info_ptr->width = width;
265 info_ptr->height = height;
266 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500267 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 info_ptr->compression_type = (png_byte)compression_type;
269 info_ptr->filter_type = (png_byte)filter_type;
270 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500271
272 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
273 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
274 info_ptr->compression_type, info_ptr->filter_type);
275
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500276 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
277 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500278
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500279 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500281
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282 else
283 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500284
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500285 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500287
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600289
Glenn Randers-Pehrson421f95d2013-06-04 15:05:12 -0500290 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291}
292
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500293#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500294void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600295png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600296 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500298 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500299
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600300 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 return;
302
303 info_ptr->x_offset = offset_x;
304 info_ptr->y_offset = offset_y;
305 info_ptr->offset_unit_type = (png_byte)unit_type;
306 info_ptr->valid |= PNG_INFO_oFFs;
307}
308#endif
309
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500310#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500311void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600312png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500313 png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
314 int nparams, png_const_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400316 size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600317 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500319 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500320
John Bowlerfcd301d2011-12-28 21:34:27 -0600321 if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600322 || (nparams > 0 && params == NULL))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323 return;
324
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600325 length = strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500326 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600327 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500328
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500329 /* TODO: validate format of calibration name and unit name */
330
331 /* Check that the type matches the specification. */
332 if (type < 0 || type > 3)
John Bowler7dc03292016-09-02 19:26:42 -0700333 {
334 png_chunk_report(png_ptr, "Invalid pCAL equation type",
335 PNG_CHUNK_WRITE_ERROR);
336 return;
337 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500338
John Bowlerfcd301d2011-12-28 21:34:27 -0600339 if (nparams < 0 || nparams > 255)
John Bowler7dc03292016-09-02 19:26:42 -0700340 {
341 png_chunk_report(png_ptr, "Invalid pCAL parameter count",
342 PNG_CHUNK_WRITE_ERROR);
343 return;
344 }
John Bowlerfcd301d2011-12-28 21:34:27 -0600345
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500346 /* Validate params[nparams] */
347 for (i=0; i<nparams; ++i)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500348 {
John Bowlerfcd301d2011-12-28 21:34:27 -0600349 if (params[i] == NULL ||
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500350 !png_check_fp_string(params[i], strlen(params[i])))
John Bowler7dc03292016-09-02 19:26:42 -0700351 {
352 png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
353 PNG_CHUNK_WRITE_ERROR);
354 return;
355 }
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500356 }
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500357
John Bowlerfcd301d2011-12-28 21:34:27 -0600358 info_ptr->pcal_purpose = png_voidcast(png_charp,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500359 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500360
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500361 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500362 {
John Bowler7dc03292016-09-02 19:26:42 -0700363 png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
364 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500365 return;
366 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500367
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600368 memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500370 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500371 info_ptr->pcal_X0 = X0;
372 info_ptr->pcal_X1 = X1;
373 info_ptr->pcal_type = (png_byte)type;
374 info_ptr->pcal_nparams = (png_byte)nparams;
375
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600376 length = strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500377 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500378 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500379
John Bowlerfcd301d2011-12-28 21:34:27 -0600380 info_ptr->pcal_units = png_voidcast(png_charp,
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -0500381 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500382
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500383 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500384 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500385 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600386
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500387 return;
388 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500389
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600390 memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391
John Bowlerfcd301d2011-12-28 21:34:27 -0600392 info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400393 (size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500394
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500395 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500396 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500397 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600398
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500399 return;
400 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500401
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -0500402 memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
403 (sizeof (png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404
405 for (i = 0; i < nparams; i++)
406 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600407 length = strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500408 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500409 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500411 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500412
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500413 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500414 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500415 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600416
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500417 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500418 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500419
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600420 memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 }
422
423 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500424 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425}
426#endif
427
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500428#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500429void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600430png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500431 int unit, png_const_charp swidth, png_const_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600432{
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400433 size_t lengthw = 0, lengthh = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500435 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500436
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600437 if (png_ptr == NULL || info_ptr == NULL)
438 return;
439
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500440 /* Double check the unit (should never get here with an invalid
441 * unit unless this is an API call.)
442 */
443 if (unit != 1 && unit != 2)
444 png_error(png_ptr, "Invalid sCAL unit");
445
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600446 if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500447 swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500448 png_error(png_ptr, "Invalid sCAL width");
449
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600450 if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500451 sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500452 png_error(png_ptr, "Invalid sCAL height");
453
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600454 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600455
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500456 ++lengthw;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500457
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600458 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500459
John Bowlerfcd301d2011-12-28 21:34:27 -0600460 info_ptr->scal_s_width = png_voidcast(png_charp,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500461 png_malloc_warn(png_ptr, lengthw));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500462
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500463 if (info_ptr->scal_s_width == NULL)
464 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500465 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600466
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500467 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500468 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500469
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600470 memcpy(info_ptr->scal_s_width, swidth, lengthw);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600471
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500472 ++lengthh;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500473
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600474 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500475
John Bowlerfcd301d2011-12-28 21:34:27 -0600476 info_ptr->scal_s_height = png_voidcast(png_charp,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500477 png_malloc_warn(png_ptr, lengthh));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500478
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500479 if (info_ptr->scal_s_height == NULL)
480 {
481 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500482 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500483
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500484 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600485
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500486 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500487 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500488
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600489 memcpy(info_ptr->scal_s_height, sheight, lengthh);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500490
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600491 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500492 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600493}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500494
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500495# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500496void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600497png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
498 double width, double height)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500499{
500 png_debug1(1, "in %s storage function", "sCAL");
501
502 /* Check the arguments. */
503 if (width <= 0)
504 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500505
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500506 else if (height <= 0)
507 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500508
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500509 else
510 {
511 /* Convert 'width' and 'height' to ASCII. */
512 char swidth[PNG_sCAL_MAX_DIGITS+1];
513 char sheight[PNG_sCAL_MAX_DIGITS+1];
514
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600515 png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500516 PNG_sCAL_PRECISION);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600517 png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500518 PNG_sCAL_PRECISION);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500519
520 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
521 }
522}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500523# endif
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500524
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500525# ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500526void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600527png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500528 png_fixed_point width, png_fixed_point height)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500529{
530 png_debug1(1, "in %s storage function", "sCAL");
531
532 /* Check the arguments. */
533 if (width <= 0)
534 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500535
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500536 else if (height <= 0)
537 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500538
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500539 else
540 {
541 /* Convert 'width' and 'height' to ASCII. */
542 char swidth[PNG_sCAL_MAX_DIGITS+1];
543 char sheight[PNG_sCAL_MAX_DIGITS+1];
544
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600545 png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
546 png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500547
548 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
549 }
550}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500551# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600552#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600553
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500554#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500555void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600556png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600557 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500559 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500560
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600561 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 return;
563
564 info_ptr->x_pixels_per_unit = res_x;
565 info_ptr->y_pixels_per_unit = res_y;
566 info_ptr->phys_unit_type = (png_byte)unit_type;
567 info_ptr->valid |= PNG_INFO_pHYs;
568}
569#endif
570
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600572png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500573 png_const_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600575
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500576 png_uint_32 max_palette_length;
577
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500578 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500579
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600580 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500581 return;
582
Glenn Randers-Pehrson0a9afc12015-11-22 20:53:02 -0600583 max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
584 (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500585
Glenn Randers-Pehrson1bef8e92015-10-30 11:34:37 -0500586 if (num_palette < 0 || num_palette > (int) max_palette_length)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500587 {
588 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600589 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500590
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500591 else
592 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600593 png_warning(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600594
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600595 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500596 }
597 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600598
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600599 if ((num_palette > 0 && palette == NULL) ||
600 (num_palette == 0
601# ifdef PNG_MNG_FEATURES_SUPPORTED
602 && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
603# endif
604 ))
605 {
Glenn Randers-Pehrson67f101e2013-12-14 12:36:28 -0600606 png_error(png_ptr, "Invalid palette");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600607 }
608
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600609 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600610 * we do it for backward compatibility with the way the png_handle_tRNS
611 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600612 *
613 * 1.6.0: the above statement appears to be incorrect; something has to set
614 * the palette inside png_struct on read.
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600615 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600616 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500617
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600618 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrsona901eb32015-10-30 07:57:49 -0500619 * of num_palette entries, in case of an invalid PNG file or incorrect
620 * call to png_set_PLTE() with too-large sample values.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500621 */
John Bowlerfcd301d2011-12-28 21:34:27 -0600622 png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600623 PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500624
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600625 if (num_palette > 0)
Glenn Randers-Pehrson0e06b592018-03-06 13:54:52 -0600626 memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -0500627 (sizeof (png_color)));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600628 info_ptr->palette = png_ptr->palette;
629 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600630
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600631 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600632
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600633 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634}
635
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500636#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500637void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600638png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500639 png_const_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500640{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500641 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500642
John Bowlerfcd301d2011-12-28 21:34:27 -0600643 if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 return;
645
John Bowlerfcd301d2011-12-28 21:34:27 -0600646 info_ptr->sig_bit = *sig_bit;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500647 info_ptr->valid |= PNG_INFO_sBIT;
648}
649#endif
650
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500651#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500652void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600653png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600654{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500655 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500656
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600657 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600658 return;
659
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600660 (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);
661 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600662}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600663
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500664void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600665png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson8d0bc0f2011-01-25 22:15:58 -0600666 int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600667{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500668 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500669
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600670 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600671 return;
672
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500673 if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,
674 srgb_intent) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600675 {
676 /* This causes the gAMA and cHRM to be written too */
677 info_ptr->colorspace.flags |=
678 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
679 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600680
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600681 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600682}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600683#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600684
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600685
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500686#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500687void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600688png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500689 png_const_charp name, int compression_type,
690 png_const_bytep profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600691{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500692 png_charp new_iccp_name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500693 png_bytep new_iccp_profile;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400694 size_t length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500695
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500696 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500697
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600698 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
699 return;
700
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600701 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
702 png_app_error(png_ptr, "Invalid iCCP compression method");
703
704 /* Set the colorspace first because this validates the profile; do not
705 * override previously set app cHRM or gAMA here (because likely as not the
706 * application knows better than libpng what the correct values are.) Pass
707 * the info_ptr color_type field to png_colorspace_set_ICC because in the
708 * write case it has not yet been stored in png_ptr.
709 */
710 {
711 int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500712 proflen, profile, info_ptr->color_type);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600713
714 png_colorspace_sync_info(png_ptr, info_ptr);
715
716 /* Don't do any of the copying if the profile was bad, or inconsistent. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600717 if (result == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600718 return;
719
720 /* But do write the gAMA and cHRM chunks from the profile. */
721 info_ptr->colorspace.flags |=
722 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
723 }
724
725 length = strlen(name)+1;
John Bowlerfcd301d2011-12-28 21:34:27 -0600726 new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500727
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500728 if (new_iccp_name == NULL)
729 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600730 png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600731
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500732 return;
733 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500734
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600735 memcpy(new_iccp_name, name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -0600736 new_iccp_profile = png_voidcast(png_bytep,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500737 png_malloc_warn(png_ptr, proflen));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500738
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500739 if (new_iccp_profile == NULL)
740 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600741 png_free(png_ptr, new_iccp_name);
742 png_benign_error(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600743 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600744
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500745 return;
746 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500747
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600748 memcpy(new_iccp_profile, profile, proflen);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500749
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500750 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500751
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600752 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500753 info_ptr->iccp_name = new_iccp_name;
754 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600755 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600756 info_ptr->valid |= PNG_INFO_iCCP;
757}
758#endif
759
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500760#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500761void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600762png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
763 png_const_textp text_ptr, int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500764{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500765 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500766 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500767
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600768 if (ret != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500769 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500770}
771
772int /* PRIVATE */
John Bowler4f67e402011-12-28 08:43:37 -0600773png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500774 png_const_textp text_ptr, int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500775{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500776 int i;
777
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -0500778 png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500779 (unsigned long)png_ptr->chunk_name);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600781 if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500782 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500783
784 /* Make sure we have enough space in the "text" array in info_struct
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600785 * to hold all of the incoming text_ptr objects. This compare can't overflow
786 * because max_text >= num_text (anyway, subtract of two positive integers
787 * can't overflow in any case.)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500788 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600789 if (num_text > info_ptr->max_text - info_ptr->num_text)
Glenn Randers-Pehrson590c8b02013-01-19 08:49:12 -0600790 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600791 int old_num_text = info_ptr->num_text;
792 int max_text;
793 png_textp new_text = NULL;
794
795 /* Calculate an appropriate max_text, checking for overflow. */
796 max_text = old_num_text;
797 if (num_text <= INT_MAX - max_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500798 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600799 max_text += num_text;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600801 /* Round up to a multiple of 8 */
802 if (max_text < INT_MAX-8)
803 max_text = (max_text + 8) & ~0x7;
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500804
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600805 else
806 max_text = INT_MAX;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -0500808 /* Now allocate a new array and copy the old members in; this does all
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600809 * the overflow checks.
810 */
811 new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500812 info_ptr->text, old_num_text, max_text-old_num_text,
813 sizeof *new_text));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500814 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500815
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600816 if (new_text == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500817 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600818 png_chunk_report(png_ptr, "too many text chunks",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500819 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600820
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600821 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500822 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500823
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600824 png_free(png_ptr, info_ptr->text);
825
826 info_ptr->text = new_text;
827 info_ptr->free_me |= PNG_FREE_TEXT;
828 info_ptr->max_text = max_text;
829 /* num_text is adjusted below as the entries are copied in */
830
831 png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600833
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834 for (i = 0; i < num_text; i++)
835 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600836 size_t text_length, key_len;
837 size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500838 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
839
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500840 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600841 continue;
842
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500843 if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
844 text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500845 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600846 png_chunk_report(png_ptr, "text compression mode is out of range",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500847 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500848 continue;
849 }
850
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600851 key_len = strlen(text_ptr[i].key);
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500852
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500853 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500854 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500855 lang_len = 0;
856 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500857 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500858
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500859 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500860# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600861 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500862 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600863
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500864 if (text_ptr[i].lang != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600865 lang_len = strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500866
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500867 else
868 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500869
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500870 if (text_ptr[i].lang_key != NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600871 lang_key_len = strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500872
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500873 else
874 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600875 }
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600876# else /* iTXt */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500877 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600878 png_chunk_report(png_ptr, "iTXt chunk not supported",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500879 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500880 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500881 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500882# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500883
Glenn Randers-Pehrsond7edcc42017-09-13 19:34:51 -0500884 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Glenn Randers-Pehrson1aabcfd2017-09-10 05:45:44 -0500885 {
886 text_length = 0;
887# ifdef PNG_iTXt_SUPPORTED
888 if (text_ptr[i].compression > 0)
889 textp->compression = PNG_ITXT_COMPRESSION_NONE;
890
891 else
892# endif
893 textp->compression = PNG_TEXT_COMPRESSION_NONE;
894 }
Glenn Randers-Pehrson066cb342017-09-19 15:52:36 -0500895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 else
897 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600898 text_length = strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 textp->compression = text_ptr[i].compression;
900 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500901
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600902 textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
903 key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500904
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500905 if (textp->key == NULL)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600906 {
907 png_chunk_report(png_ptr, "text chunk: out of memory",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500908 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600909
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600910 return 1;
911 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500912
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600913 png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600914 (unsigned long)(png_uint_32)
915 (key_len + lang_len + lang_key_len + text_length + 4),
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600916 textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500917
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600918 memcpy(textp->key, text_ptr[i].key, key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500919 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500920
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600921 if (text_ptr[i].compression > 0)
922 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500923 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600924 memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500925 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500926 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600927 memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500928 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500929 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600930 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500931
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600932 else
933 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500934 textp->lang=NULL;
935 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500936 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600937 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500938
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600939 if (text_length != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600940 memcpy(textp->text, text_ptr[i].text, text_length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500941
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500942 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600943
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500944# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500945 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600946 {
947 textp->text_length = 0;
948 textp->itxt_length = text_length;
949 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500950
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600951 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500952# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600953 {
954 textp->text_length = text_length;
955 textp->itxt_length = 0;
956 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500957
Andreas Dilger47a0c421997-05-16 02:46:07 -0500958 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500959 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500960 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600961
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500962 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500963}
964#endif
965
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500966#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500967void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600968png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
969 png_const_timep mod_time)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500970{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500971 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500972
John Bowlerfcd301d2011-12-28 21:34:27 -0600973 if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500974 (png_ptr->mode & PNG_WROTE_tIME) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 return;
976
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600977 if (mod_time->month == 0 || mod_time->month > 12 ||
978 mod_time->day == 0 || mod_time->day > 31 ||
979 mod_time->hour > 23 || mod_time->minute > 59 ||
980 mod_time->second > 60)
981 {
982 png_warning(png_ptr, "Ignoring invalid time value");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -0600983
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600984 return;
985 }
986
John Bowlerfcd301d2011-12-28 21:34:27 -0600987 info_ptr->mod_time = *mod_time;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500988 info_ptr->valid |= PNG_INFO_tIME;
989}
990#endif
991
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500992#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500993void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600994png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500995 png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500997 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500998
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600999 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001000
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 return;
1002
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001003 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001004 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -05001005 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001006 * we do it for backward compatibility with the way the png_handle_tRNS
1007 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -06001008 *
1009 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
1010 * relies on png_set_tRNS storing the information in png_struct
1011 * (otherwise it won't be there for the code in pngrtran.c).
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001012 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001013
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001014 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001015
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001016 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson8d167252016-05-27 19:55:44 -05001017 {
1018 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
Glenn Randers-Pehrson6c7459e2016-05-27 20:09:23 -05001019 info_ptr->trans_alpha = png_voidcast(png_bytep,
Glenn Randers-Pehrson2f8b82e2017-10-29 10:30:42 -05001020 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04001021 memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);
Glenn Randers-Pehrson8d167252016-05-27 19:55:44 -05001022 }
Glenn Randers-Pehrson6c7459e2016-05-27 20:09:23 -05001023 png_ptr->trans_alpha = info_ptr->trans_alpha;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001024 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001025
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -05001026 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027 {
Glenn Randers-Pehrson9bddba22017-10-17 16:22:22 -05001028#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -06001029 if (info_ptr->bit_depth < 16)
1030 {
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001031 int sample_max = (1 << info_ptr->bit_depth) - 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001032
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -06001033 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
1034 trans_color->gray > sample_max) ||
1035 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
1036 (trans_color->red > sample_max ||
1037 trans_color->green > sample_max ||
1038 trans_color->blue > sample_max)))
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001039 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001040 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrson2f56fe42015-01-13 09:25:48 -06001041 }
Glenn Randers-Pehrson9bddba22017-10-17 16:22:22 -05001042#endif
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001043
1044 info_ptr->trans_color = *trans_color;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001045
Glenn Randers-Pehrson3b5bcea2017-10-17 15:20:31 -05001046 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001047 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001049
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001051
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001052 if (num_trans != 0)
1053 {
1054 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001055 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001056 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057}
1058#endif
1059
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001060#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001061void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001062png_set_sPLT(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -06001063 png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -06001064/*
1065 * entries - array of png_sPLT_t structures
1066 * to be added to the list of palettes
1067 * in the info structure.
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001068 *
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -06001069 * nentries - number of palette structures to be
1070 * added.
1071 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001072{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001073 png_sPLT_tp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001074
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001075 if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001076 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001077
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001078 /* Use the internal realloc function, which checks for all the possible
1079 * overflows. Notice that the parameters are (int) and (size_t)
1080 */
1081 np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001082 info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
1083 sizeof *np));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001084
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001085 if (np == NULL)
1086 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001087 /* Out of memory or too many chunks */
1088 png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001089
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001090 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001091 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001092
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001093 png_free(png_ptr, info_ptr->splt_palettes);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001094 info_ptr->splt_palettes = np;
1095 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001097 np += info_ptr->splt_palettes_num;
1098
1099 do
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001100 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04001101 size_t length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001102
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001103 /* Skip invalid input entries */
1104 if (entries->name == NULL || entries->entries == NULL)
1105 {
1106 /* png_handle_sPLT doesn't do this, so this is an app error */
1107 png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
1108 /* Just skip the invalid entry */
1109 continue;
1110 }
1111
1112 np->depth = entries->depth;
1113
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001114 /* In the event of out-of-memory just return - there's no point keeping
1115 * on trying to add sPLT chunks.
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001116 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001117 length = strlen(entries->name) + 1;
1118 np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001119
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001120 if (np->name == NULL)
1121 break;
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001122
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001123 memcpy(np->name, entries->name, length);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001124
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001125 /* IMPORTANT: we have memory now that won't get freed if something else
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001126 * goes wrong; this code must free it. png_malloc_array produces no
1127 * warnings; use a png_chunk_report (below) if there is an error.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001128 */
1129 np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
1130 entries->nentries, sizeof (png_sPLT_entry)));
1131
1132 if (np->entries == NULL)
John Bowler2414bd92013-01-19 23:18:59 -06001133 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001134 png_free(png_ptr, np->name);
Glenn Randers-Pehrson11d4a2a2014-10-29 08:23:19 -05001135 np->name = NULL;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001136 break;
John Bowler2414bd92013-01-19 23:18:59 -06001137 }
1138
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001139 np->nentries = entries->nentries;
1140 /* This multiply can't overflow because png_malloc_array has already
1141 * checked it when doing the allocation.
1142 */
1143 memcpy(np->entries, entries->entries,
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001144 (unsigned int)entries->nentries * sizeof (png_sPLT_entry));
John Bowler2414bd92013-01-19 23:18:59 -06001145
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001146 /* Note that 'continue' skips the advance of the out pointer and out
1147 * count, so an invalid entry is not added.
1148 */
1149 info_ptr->valid |= PNG_INFO_sPLT;
1150 ++(info_ptr->splt_palettes_num);
1151 ++np;
Viktor Szakats8c50acb2017-03-29 23:54:40 +00001152 ++entries;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001153 }
Viktor Szakats8c50acb2017-03-29 23:54:40 +00001154 while (--nentries);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001155
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001156 if (nentries > 0)
1157 png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001158}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001159#endif /* sPLT */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001160
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001161#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1162static png_byte
1163check_location(png_const_structrp png_ptr, int location)
1164{
1165 location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1166
1167 /* New in 1.6.0; copy the location and check it. This is an API
Glenn Randers-Pehrson91271382014-10-03 20:23:31 -05001168 * change; previously the app had to use the
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001169 * png_set_unknown_chunk_location API below for each chunk.
1170 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001171 if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001172 {
1173 /* Write struct, so unknown chunks come from the app */
1174 png_app_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001175 "png_set_unknown_chunks now expects a valid location");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001176 /* Use the old behavior */
1177 location = (png_byte)(png_ptr->mode &
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001178 (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001179 }
1180
1181 /* This need not be an internal error - if the app calls
1182 * png_set_unknown_chunks on a read pointer it must get the location right.
1183 */
1184 if (location == 0)
1185 png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1186
1187 /* Now reduce the location to the top-most set bit by removing each least
1188 * significant bit in turn.
1189 */
1190 while (location != (location & -location))
1191 location &= ~(location & -location);
1192
1193 /* The cast is safe because 'location' is a bit mask and only the low four
1194 * bits are significant.
1195 */
1196 return (png_byte)location;
1197}
1198
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001199void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001200png_set_unknown_chunks(png_const_structrp png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001201 png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001202{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001203 png_unknown_chunkp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001204
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001205 if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001206 unknowns == NULL)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001207 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001208
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001209 /* Check for the failure cases where support has been disabled at compile
1210 * time. This code is hardly ever compiled - it's here because
1211 * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
1212 * code) but may be meaningless if the read or write handling of unknown
1213 * chunks is not compiled in.
1214 */
1215# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
1216 defined(PNG_READ_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001217 if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001218 {
1219 png_app_error(png_ptr, "no unknown chunk support on read");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001220
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001221 return;
1222 }
1223# endif
1224# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
1225 defined(PNG_WRITE_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001226 if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001227 {
1228 png_app_error(png_ptr, "no unknown chunk support on write");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001229
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001230 return;
1231 }
1232# endif
1233
1234 /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
1235 * unknown critical chunks could be lost with just a warning resulting in
1236 * undefined behavior. Now png_chunk_report is used to provide behavior
1237 * appropriate to read or write.
1238 */
1239 np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001240 info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
1241 sizeof *np));
Glenn Randers-Pehrson590c8b02013-01-19 08:49:12 -06001242
1243 if (np == NULL)
1244 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001245 png_chunk_report(png_ptr, "too many unknown chunks",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001246 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001247
John Bowler1c6e22a2013-01-10 10:55:34 -06001248 return;
1249 }
1250
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001251 png_free(png_ptr, info_ptr->unknown_chunks);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001252 info_ptr->unknown_chunks = np; /* safe because it is initialized */
1253 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001254
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001255 np += info_ptr->unknown_chunks_num;
1256
1257 /* Increment unknown_chunks_num each time round the loop to protect the
1258 * just-allocated chunk data.
1259 */
1260 for (; num_unknowns > 0; --num_unknowns, ++unknowns)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001261 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001262 memcpy(np->name, unknowns->name, (sizeof np->name));
1263 np->name[(sizeof np->name)-1] = '\0';
1264 np->location = check_location(png_ptr, unknowns->location);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001265
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001266 if (unknowns->size == 0)
1267 {
1268 np->data = NULL;
1269 np->size = 0;
1270 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001271
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001272 else
1273 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001274 np->data = png_voidcast(png_bytep,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001275 png_malloc_base(png_ptr, unknowns->size));
John Bowler2414bd92013-01-19 23:18:59 -06001276
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001277 if (np->data == NULL)
John Bowler2414bd92013-01-19 23:18:59 -06001278 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001279 png_chunk_report(png_ptr, "unknown chunk: out of memory",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001280 PNG_CHUNK_WRITE_ERROR);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001281 /* But just skip storing the unknown chunk */
1282 continue;
John Bowler2414bd92013-01-19 23:18:59 -06001283 }
1284
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001285 memcpy(np->data, unknowns->data, unknowns->size);
1286 np->size = unknowns->size;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001287 }
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -06001288
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001289 /* These increments are skipped on out-of-memory for the data - the
1290 * unknown chunk entry gets overwritten if the png_chunk_report returns.
1291 * This is correct in the read case (the chunk is just dropped.)
1292 */
1293 ++np;
1294 ++(info_ptr->unknown_chunks_num);
1295 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001296}
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001297
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001298void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001299png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001300 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001301{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001302 /* This API is pretty pointless in 1.6.0 because the location can be set
1303 * before the call to png_set_unknown_chunks.
1304 *
1305 * TODO: add a png_app_warning in 1.7
1306 */
1307 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1308 chunk < info_ptr->unknown_chunks_num)
1309 {
1310 if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1311 {
1312 png_app_error(png_ptr, "invalid unknown chunk location");
1313 /* Fake out the pre 1.6.0 behavior: */
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001314 if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001315 location = PNG_AFTER_IDAT;
1316
1317 else
1318 location = PNG_HAVE_IHDR; /* also undocumented */
1319 }
1320
1321 info_ptr->unknown_chunks[chunk].location =
1322 check_location(png_ptr, location);
1323 }
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001324}
Glenn Randers-Pehrson217d38c2015-03-07 10:35:03 -06001325#endif /* STORE_UNKNOWN_CHUNKS */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001326
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001327#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001328png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001329png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001330{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001331 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001332
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001333 if (png_ptr == NULL)
John Bowler4f67e402011-12-28 08:43:37 -06001334 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001335
John Bowler4f67e402011-12-28 08:43:37 -06001336 png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001337
John Bowler4f67e402011-12-28 08:43:37 -06001338 return png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001339}
1340#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001341
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001342#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001343static unsigned int
1344add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1345{
1346 unsigned int i;
1347
1348 /* Utility function: update the 'keep' state of a chunk if it is already in
1349 * the list, otherwise add it to the list.
1350 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001351 for (i=0; i<count; ++i, list += 5)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001352 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001353 if (memcmp(list, add, 4) == 0)
1354 {
1355 list[4] = (png_byte)keep;
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001356
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001357 return count;
1358 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001359 }
1360
1361 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1362 {
1363 ++count;
1364 memcpy(list, add, 4);
1365 list[4] = (png_byte)keep;
1366 }
1367
1368 return count;
1369}
1370
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001371void PNGAPI
John Bowlere9567512012-08-15 22:53:00 -05001372png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001373 png_const_bytep chunk_list, int num_chunks_in)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001374{
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001375 png_bytep new_list;
1376 unsigned int num_chunks, old_num_chunks;
1377
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001378 if (png_ptr == NULL)
1379 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001380
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001381 if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
John Bowleraa9dea52012-08-10 19:04:08 -05001382 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001383 png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001384
John Bowlere9567512012-08-15 22:53:00 -05001385 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001386 }
1387
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001388 if (num_chunks_in <= 0)
1389 {
1390 png_ptr->unknown_default = keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001391
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001392 /* '0' means just set the flags, so stop here */
1393 if (num_chunks_in == 0)
1394 return;
1395 }
1396
1397 if (num_chunks_in < 0)
1398 {
1399 /* Ignore all unknown chunks and all chunks recognized by
1400 * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1401 */
Cosmin Truta1ef88822018-08-18 21:01:02 -04001402 static const png_byte chunks_to_ignore[] = {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001403 98, 75, 71, 68, '\0', /* bKGD */
1404 99, 72, 82, 77, '\0', /* cHRM */
Glenn Randers-Pehrson0ec733c2017-08-05 08:01:53 -05001405 101, 88, 73, 102, '\0', /* eXIf */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001406 103, 65, 77, 65, '\0', /* gAMA */
1407 104, 73, 83, 84, '\0', /* hIST */
1408 105, 67, 67, 80, '\0', /* iCCP */
1409 105, 84, 88, 116, '\0', /* iTXt */
1410 111, 70, 70, 115, '\0', /* oFFs */
1411 112, 67, 65, 76, '\0', /* pCAL */
1412 112, 72, 89, 115, '\0', /* pHYs */
1413 115, 66, 73, 84, '\0', /* sBIT */
1414 115, 67, 65, 76, '\0', /* sCAL */
1415 115, 80, 76, 84, '\0', /* sPLT */
1416 115, 84, 69, 82, '\0', /* sTER */
1417 115, 82, 71, 66, '\0', /* sRGB */
1418 116, 69, 88, 116, '\0', /* tEXt */
1419 116, 73, 77, 69, '\0', /* tIME */
1420 122, 84, 88, 116, '\0' /* zTXt */
1421 };
1422
1423 chunk_list = chunks_to_ignore;
John Bowler03df1892014-11-05 17:19:36 -06001424 num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001425 }
1426
1427 else /* num_chunks_in > 0 */
1428 {
1429 if (chunk_list == NULL)
1430 {
1431 /* Prior to 1.6.0 this was silently ignored, now it is an app_error
1432 * which can be switched off.
1433 */
1434 png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001435
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001436 return;
1437 }
1438
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001439 num_chunks = (unsigned int)num_chunks_in;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001440 }
John Bowlerfcd301d2011-12-28 21:34:27 -06001441
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001442 old_num_chunks = png_ptr->num_chunk_list;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001443 if (png_ptr->chunk_list == NULL)
1444 old_num_chunks = 0;
John Bowleraa9dea52012-08-10 19:04:08 -05001445
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001446 /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1447 */
1448 if (num_chunks + old_num_chunks > UINT_MAX/5)
John Bowleraa9dea52012-08-10 19:04:08 -05001449 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001450 png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001451
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001452 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001453 }
1454
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001455 /* If these chunks are being reset to the default then no more memory is
1456 * required because add_one_chunk above doesn't extend the list if the 'keep'
1457 * parameter is the default.
1458 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001459 if (keep != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001460 {
1461 new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1462 5 * (num_chunks + old_num_chunks)));
John Bowlere9567512012-08-15 22:53:00 -05001463
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001464 if (old_num_chunks > 0)
1465 memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
1466 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001467
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001468 else if (old_num_chunks > 0)
1469 new_list = png_ptr->chunk_list;
1470
1471 else
1472 new_list = NULL;
1473
1474 /* Add the new chunks together with each one's handling code. If the chunk
1475 * already exists the code is updated, otherwise the chunk is added to the
1476 * end. (In libpng 1.6.0 order no longer matters because this code enforces
1477 * the earlier convention that the last setting is the one that is used.)
1478 */
1479 if (new_list != NULL)
1480 {
1481 png_const_bytep inlist;
1482 png_bytep outlist;
1483 unsigned int i;
1484
1485 for (i=0; i<num_chunks; ++i)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001486 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001487 old_num_chunks = add_one_chunk(new_list, old_num_chunks,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001488 chunk_list+5*i, keep);
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001489 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001490
1491 /* Now remove any spurious 'default' entries. */
1492 num_chunks = 0;
1493 for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001494 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001495 if (inlist[4])
1496 {
1497 if (outlist != inlist)
1498 memcpy(outlist, inlist, 5);
1499 outlist += 5;
1500 ++num_chunks;
1501 }
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001502 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001503
1504 /* This means the application has removed all the specialized handling. */
1505 if (num_chunks == 0)
1506 {
1507 if (png_ptr->chunk_list != new_list)
1508 png_free(png_ptr, new_list);
1509
1510 new_list = NULL;
1511 }
1512 }
1513
1514 else
1515 num_chunks = 0;
1516
1517 png_ptr->num_chunk_list = num_chunks;
1518
1519 if (png_ptr->chunk_list != new_list)
1520 {
1521 if (png_ptr->chunk_list != NULL)
1522 png_free(png_ptr, png_ptr->chunk_list);
1523
1524 png_ptr->chunk_list = new_list;
1525 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001526}
1527#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001528
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001529#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001530void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001531png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001532 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001533{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001534 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001535
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001536 if (png_ptr == NULL)
1537 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001538
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001539 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1540 png_ptr->user_chunk_ptr = user_chunk_ptr;
1541}
1542#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001543
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001544#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001545void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001546png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1547 png_bytepp row_pointers)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001548{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001549 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001550
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001551 if (png_ptr == NULL || info_ptr == NULL)
1552 return;
1553
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001554 if (info_ptr->row_pointers != NULL &&
1555 (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001556 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001557
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001558 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001559
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001560 if (row_pointers != NULL)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001561 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001562}
1563#endif
1564
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001565void PNGAPI
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04001566png_set_compression_buffer_size(png_structrp png_ptr, size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001567{
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001568 if (png_ptr == NULL)
1569 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001570
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001571 if (size == 0 || size > PNG_UINT_31_MAX)
1572 png_error(png_ptr, "invalid compression buffer size");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001573
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001574# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001575 if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
1576 {
1577 png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1578 return;
1579 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001580# endif
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001581
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001582# ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001583 if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
1584 {
1585 if (png_ptr->zowner != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001586 {
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001587 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001588 "Compression buffer size cannot be changed because it is in use");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001589
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001590 return;
1591 }
John Bowleraa9dea52012-08-10 19:04:08 -05001592
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001593#ifndef __COVERITY__
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001594 /* Some compilers complain that this is always false. However, it
1595 * can be true when integer overflow happens.
1596 */
1597 if (size > ZLIB_IO_MAX)
1598 {
1599 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001600 "Compression buffer size limited to system maximum");
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001601 size = ZLIB_IO_MAX; /* must fit */
1602 }
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001603#endif
John Bowleraa9dea52012-08-10 19:04:08 -05001604
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001605 if (size < 6)
1606 {
1607 /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1608 * if this is permitted.
1609 */
1610 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001611 "Compression buffer size cannot be reduced below 6");
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001612
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001613 return;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001614 }
Glenn Randers-Pehrson192e92d2016-07-13 14:43:42 -05001615
1616 if (png_ptr->zbuffer_size != size)
1617 {
1618 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1619 png_ptr->zbuffer_size = (uInt)size;
1620 }
1621 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001622# endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001623}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001624
1625void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001626png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001627{
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001628 if (png_ptr != NULL && info_ptr != NULL)
Glenn Randers-Pehrson3875d9a2016-10-02 17:08:46 -05001629 info_ptr->valid &= (unsigned int)(~mask);
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001630}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001631
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001632
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001633#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001634/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001635void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001636png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001637 png_uint_32 user_height_max)
1638{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001639 /* Images with dimensions larger than these limits will be
1640 * rejected by png_set_IHDR(). To accept any PNG datastream
Glenn Randers-Pehrsonf1cdf4e2015-11-12 22:16:56 -06001641 * regardless of dimensions, set both limits to 0x7fffffff.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001642 */
1643 if (png_ptr == NULL)
1644 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001645
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001646 png_ptr->user_width_max = user_width_max;
1647 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001648}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001649
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001650/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001651void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001652png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001653{
Glenn Randers-Pehrsonbd769652015-01-27 06:53:56 -06001654 if (png_ptr != NULL)
1655 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001656}
1657
1658/* This function was added to libpng 1.4.1 */
1659void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001660png_set_chunk_malloc_max (png_structrp png_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001661 png_alloc_size_t user_chunk_malloc_max)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001662{
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001663 if (png_ptr != NULL)
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001664 png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001665}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001666#endif /* ?SET_USER_LIMITS */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001667
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001668
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001669#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001670void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001671png_set_benign_errors(png_structrp png_ptr, int allowed)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001672{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001673 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001674
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001675 /* If allowed is 1, png_benign_error() is treated as a warning.
1676 *
1677 * If allowed is 0, png_benign_error() is treated as an error (which
1678 * is the default behavior if png_set_benign_errors() is not called).
1679 */
1680
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001681 if (allowed != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001682 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1683 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001684
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001685 else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001686 png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1687 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001688}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001689#endif /* BENIGN_ERRORS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001690
1691#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
1692 /* Whether to report invalid palette index; added at libng-1.5.10.
1693 * It is possible for an indexed (color-type==3) PNG file to contain
1694 * pixels with invalid (out-of-range) indexes if the PLTE chunk has
1695 * fewer entries than the image's bit-depth would allow. We recover
Glenn Randers-Pehrson6cae24c2014-10-13 11:11:21 -05001696 * from this gracefully by filling any incomplete palette with zeros
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001697 * (opaque black). By default, when this occurs libpng will issue
1698 * a benign error. This API can be used to override that behavior.
1699 */
1700void PNGAPI
1701png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1702{
1703 png_debug(1, "in png_set_check_for_invalid_index");
1704
1705 if (allowed > 0)
1706 png_ptr->num_palette_max = 0;
1707
1708 else
1709 png_ptr->num_palette_max = -1;
1710}
1711#endif
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001712
1713#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
1714 defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
1715/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1716 * and if invalid, correct the keyword rather than discarding the entire
1717 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1718 * length, forbids leading or trailing whitespace, multiple internal spaces,
1719 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1720 *
1721 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
1722 * trailing '\0'). If this routine returns 0 then there was no keyword, or a
1723 * valid one could not be generated, and the caller must png_error.
1724 */
1725png_uint_32 /* PRIVATE */
1726png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
1727{
John Bowler0ac91cc2016-06-28 19:18:09 -07001728#ifdef PNG_WARNINGS_SUPPORTED
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001729 png_const_charp orig_key = key;
John Bowler0ac91cc2016-06-28 19:18:09 -07001730#endif
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001731 png_uint_32 key_len = 0;
1732 int bad_character = 0;
1733 int space = 1;
1734
1735 png_debug(1, "in png_check_keyword");
1736
1737 if (key == NULL)
1738 {
1739 *new_key = 0;
1740 return 0;
1741 }
1742
1743 while (*key && key_len < 79)
1744 {
1745 png_byte ch = (png_byte)*key++;
1746
1747 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
Viktor Szakats8c50acb2017-03-29 23:54:40 +00001748 {
1749 *new_key++ = ch; ++key_len; space = 0;
1750 }
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001751
1752 else if (space == 0)
1753 {
1754 /* A space or an invalid character when one wasn't seen immediately
1755 * before; output just a space.
1756 */
Viktor Szakats8c50acb2017-03-29 23:54:40 +00001757 *new_key++ = 32; ++key_len; space = 1;
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001758
1759 /* If the character was not a space then it is invalid. */
1760 if (ch != 32)
1761 bad_character = ch;
1762 }
1763
1764 else if (bad_character == 0)
1765 bad_character = ch; /* just skip it, record the first error */
1766 }
1767
1768 if (key_len > 0 && space != 0) /* trailing space */
1769 {
Viktor Szakats8c50acb2017-03-29 23:54:40 +00001770 --key_len; --new_key;
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001771 if (bad_character == 0)
1772 bad_character = 32;
1773 }
1774
1775 /* Terminate the keyword */
1776 *new_key = 0;
1777
1778 if (key_len == 0)
1779 return 0;
1780
1781#ifdef PNG_WARNINGS_SUPPORTED
1782 /* Try to only output one warning per keyword: */
1783 if (*key != 0) /* keyword too long */
1784 png_warning(png_ptr, "keyword truncated");
1785
1786 else if (bad_character != 0)
1787 {
1788 PNG_WARNING_PARAMETERS(p)
1789
1790 png_warning_parameter(p, 1, orig_key);
1791 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
1792
1793 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
1794 }
John Bowler0ac91cc2016-06-28 19:18:09 -07001795#else /* !WARNINGS */
1796 PNG_UNUSED(png_ptr)
1797#endif /* !WARNINGS */
Glenn Randers-Pehrson975cbbb2015-12-13 15:04:18 -06001798
1799 return key_len;
1800}
1801#endif /* TEXT || pCAL || iCCP || sPLT */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001802#endif /* READ || WRITE */