blob: c7d2d5e4746f2408e57cfec68645fee0bd92a3ab [file] [log] [blame]
Andreas Dilger47a0c421997-05-16 02:46:07 -05001
2/* pngset.c - storage of image information into info struct
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson67ee8ce2011-12-22 08:21:00 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060013 * The functions here are used during reads to store data from the file
14 * into the info struct, and during writes to store application data
15 * into the info struct for writing into the file. This abstracts the
16 * info struct and allows us to change the structure in the future.
17 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
22
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050023#ifdef PNG_bKGD_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -060025png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -050026 png_const_color_16p background)
Andreas Dilger47a0c421997-05-16 02:46:07 -050027{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050028 png_debug1(1, "in %s storage function", "bKGD");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050029
John Bowlerfcd301d2011-12-28 21:34:27 -060030 if (png_ptr == NULL || info_ptr == NULL || background == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -050031 return;
32
John Bowlerfcd301d2011-12-28 21:34:27 -060033 info_ptr->background = *background;
Andreas Dilger47a0c421997-05-16 02:46:07 -050034 info_ptr->valid |= PNG_INFO_bKGD;
35}
36#endif
37
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050038#ifdef PNG_cHRM_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050039void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -060040png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -060041 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
42 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
43 png_fixed_point blue_x, png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060044{
John Bowlerb11b31a2012-03-21 07:55:46 -050045 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
John Bowlerb11b31a2012-03-21 07:55:46 -050052 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,
62 2/* override with app values*/))
63 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-Pehrson702053d2012-08-17 21:23:06 -050083 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
John Bowlerb11b31a2012-03-21 07:55:46 -050093 if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace, &XYZ, 2))
94 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
John Bowler736f40f2011-08-25 16:19:44 -050095
John Bowlerb11b31a2012-03-21 07:55:46 -050096 png_colorspace_sync_info(png_ptr, info_ptr);
John Bowler736f40f2011-08-25 16:19:44 -050097}
98
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -050099# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500100void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600101png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500102 double white_x, double white_y, double red_x, double red_y,
103 double green_x, double green_y, double blue_x, double blue_y)
104{
105 png_set_cHRM_fixed(png_ptr, info_ptr,
106 png_fixed(png_ptr, white_x, "cHRM White X"),
107 png_fixed(png_ptr, white_y, "cHRM White Y"),
108 png_fixed(png_ptr, red_x, "cHRM Red X"),
109 png_fixed(png_ptr, red_y, "cHRM Red Y"),
110 png_fixed(png_ptr, green_x, "cHRM Green X"),
111 png_fixed(png_ptr, green_y, "cHRM Green Y"),
112 png_fixed(png_ptr, blue_x, "cHRM Blue X"),
113 png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
114}
John Bowler736f40f2011-08-25 16:19:44 -0500115
116void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600117png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
John Bowler736f40f2011-08-25 16:19:44 -0500118 double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
119 double blue_X, double blue_Y, double blue_Z)
120{
121 png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
122 png_fixed(png_ptr, red_X, "cHRM Red X"),
123 png_fixed(png_ptr, red_Y, "cHRM Red Y"),
124 png_fixed(png_ptr, red_Z, "cHRM Red Z"),
125 png_fixed(png_ptr, green_X, "cHRM Red X"),
126 png_fixed(png_ptr, green_Y, "cHRM Red Y"),
127 png_fixed(png_ptr, green_Z, "cHRM Red Z"),
128 png_fixed(png_ptr, blue_X, "cHRM Red X"),
129 png_fixed(png_ptr, blue_Y, "cHRM Red Y"),
130 png_fixed(png_ptr, blue_Z, "cHRM Red Z"));
131}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500132# endif /* PNG_FLOATING_POINT_SUPPORTED */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500133
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -0600134#endif /* PNG_cHRM_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600135
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500136#ifdef PNG_gAMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500137void PNGFAPI
John Bowler4f67e402011-12-28 08:43:37 -0600138png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
139 png_fixed_point file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500140{
141 png_debug1(1, "in %s storage function", "gAMA");
142
143 if (png_ptr == NULL || info_ptr == NULL)
144 return;
145
John Bowlerd2f0bc22011-06-11 06:42:06 -0500146 /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
147 * occur. Since the fixed point representation is assymetrical it is
148 * possible for 1/gamma to overflow the limit of 21474 and this means the
149 * gamma value must be at least 5/100000 and hence at most 20000.0. For
150 * safety the limits here are a little narrower. The values are 0.00016 to
Glenn Randers-Pehrson8f4d6722012-05-23 12:30:59 -0500151 * 6250.0, which are truly ridiculous gamma values (and will produce
John Bowlerd2f0bc22011-06-11 06:42:06 -0500152 * displays that are all black or all white.)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500153 */
John Bowlerd2f0bc22011-06-11 06:42:06 -0500154 if (file_gamma < 16 || file_gamma > 625000000)
John Bowlerb11b31a2012-03-21 07:55:46 -0500155 png_app_error(png_ptr, "Out of range gamma value ignored");
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500156
157 else
158 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500159 if (png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma,
160 2/* overrided with app value */))
161 info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_gAMA;
162
163 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500164 }
165}
166
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500167# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500168void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600169png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500171 png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500172 "png_set_gAMA"));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500174# endif
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500175#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_hIST_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500178void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600179png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
180 png_const_uint_16p hist)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181{
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500182 int i;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600183
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500184 png_debug1(1, "in %s storage function", "hIST");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500185
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600186 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500188
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500189 if (info_ptr->num_palette == 0 || info_ptr->num_palette
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600190 > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500191 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500192 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600193 "Invalid palette size, hIST allocation skipped");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500195 return;
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600198 png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500199
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500200 /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
201 * version 1.2.1
202 */
John Bowler4f67e402011-12-28 08:43:37 -0600203 info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500204 PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500205
John Bowler4f67e402011-12-28 08:43:37 -0600206 if (info_ptr->hist == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 {
208 png_warning(png_ptr, "Insufficient memory for hIST chunk data");
209 return;
210 }
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600211
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600212 info_ptr->free_me |= PNG_FREE_HIST;
John Bowler4f67e402011-12-28 08:43:37 -0600213
214 for (i = 0; i < info_ptr->num_palette; i++)
215 info_ptr->hist[i] = hist[i];
216
217 info_ptr->valid |= PNG_INFO_hIST;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218}
219#endif
220
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500221void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600222png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500223 png_uint_32 width, png_uint_32 height, int bit_depth,
224 int color_type, int interlace_type, int compression_type,
225 int filter_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500226{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500227 png_debug1(1, "in %s storage function", "IHDR");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500228
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600229 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 return;
231
232 info_ptr->width = width;
233 info_ptr->height = height;
234 info_ptr->bit_depth = (png_byte)bit_depth;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500235 info_ptr->color_type = (png_byte)color_type;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 info_ptr->compression_type = (png_byte)compression_type;
237 info_ptr->filter_type = (png_byte)filter_type;
238 info_ptr->interlace_type = (png_byte)interlace_type;
Glenn Randers-Pehrsonb35a7452009-09-30 23:12:13 -0500239
240 png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
241 info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
242 info_ptr->compression_type, info_ptr->filter_type);
243
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500244 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
245 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500246
Glenn Randers-Pehrson25d82242002-05-01 11:51:26 -0500247 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 else
251 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500252
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
254 info_ptr->channels++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500255
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600257
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500258 /* Check for potential overflow */
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500259 if (width >
260 (PNG_UINT_32_MAX >> 3) /* 8-byte RRGGBBAA pixels */
261 - 48 /* bigrowbuf hack */
262 - 1 /* filter byte */
263 - 7*8 /* rounding of width to multiple of 8 pixels */
264 - 8) /* extra max_pixel_depth pad */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500265 info_ptr->rowbytes = 0;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500266 else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500267 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268}
269
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500270#ifdef PNG_oFFs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500271void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600272png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600273 png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500275 png_debug1(1, "in %s storage function", "oFFs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500276
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600277 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 return;
279
280 info_ptr->x_offset = offset_x;
281 info_ptr->y_offset = offset_y;
282 info_ptr->offset_unit_type = (png_byte)unit_type;
283 info_ptr->valid |= PNG_INFO_oFFs;
284}
285#endif
286
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500287#ifdef PNG_pCAL_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500288void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600289png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500290 png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
291 int nparams, png_const_charp units, png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500292{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500293 png_size_t length;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600294 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500296 png_debug1(1, "in %s storage function", "pCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500297
John Bowlerfcd301d2011-12-28 21:34:27 -0600298 if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
299 || (nparams > 0 && params == NULL))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 return;
301
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500302 length = strlen(purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500303 png_debug1(3, "allocating purpose for info (%lu bytes)",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600304 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500306 /* TODO: validate format of calibration name and unit name */
307
308 /* Check that the type matches the specification. */
309 if (type < 0 || type > 3)
310 png_error(png_ptr, "Invalid pCAL equation type");
311
John Bowlerfcd301d2011-12-28 21:34:27 -0600312 if (nparams < 0 || nparams > 255)
313 png_error(png_ptr, "Invalid pCAL parameter count");
314
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500315 /* Validate params[nparams] */
316 for (i=0; i<nparams; ++i)
John Bowlerfcd301d2011-12-28 21:34:27 -0600317 if (params[i] == NULL ||
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500318 !png_check_fp_string(params[i], strlen(params[i])))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500319 png_error(png_ptr, "Invalid format for pCAL parameter");
320
John Bowlerfcd301d2011-12-28 21:34:27 -0600321 info_ptr->pcal_purpose = png_voidcast(png_charp,
322 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500323
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500324 if (info_ptr->pcal_purpose == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500326 png_warning(png_ptr, "Insufficient memory for pCAL purpose");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500327 return;
328 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500329
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500330 memcpy(info_ptr->pcal_purpose, purpose, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500332 png_debug(3, "storing X0, X1, type, and nparams in info");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333 info_ptr->pcal_X0 = X0;
334 info_ptr->pcal_X1 = X1;
335 info_ptr->pcal_type = (png_byte)type;
336 info_ptr->pcal_nparams = (png_byte)nparams;
337
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500338 length = strlen(units) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500339 png_debug1(3, "allocating units for info (%lu bytes)",
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -0500340 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500341
John Bowlerfcd301d2011-12-28 21:34:27 -0600342 info_ptr->pcal_units = png_voidcast(png_charp,
343 png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500344
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500345 if (info_ptr->pcal_units == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500347 png_warning(png_ptr, "Insufficient memory for pCAL units");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 return;
349 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500350
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500351 memcpy(info_ptr->pcal_units, units, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352
John Bowlerfcd301d2011-12-28 21:34:27 -0600353 info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500354 (png_size_t)((nparams + 1) * (sizeof (png_charp)))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500355
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500356 if (info_ptr->pcal_params == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500357 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500358 png_warning(png_ptr, "Insufficient memory for pCAL params");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500359 return;
360 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500361
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500362 memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363
364 for (i = 0; i < nparams; i++)
365 {
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500366 length = strlen(params[i]) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500367 png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500368 (unsigned long)length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500369
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500370 info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500371
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500372 if (info_ptr->pcal_params[i] == NULL)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500373 {
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -0500374 png_warning(png_ptr, "Insufficient memory for pCAL parameter");
375 return;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500376 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500377
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500378 memcpy(info_ptr->pcal_params[i], params[i], length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500379 }
380
381 info_ptr->valid |= PNG_INFO_pCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500382 info_ptr->free_me |= PNG_FREE_PCAL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383}
384#endif
385
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -0500386#ifdef PNG_sCAL_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500387void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600388png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500389 int unit, png_const_charp swidth, png_const_charp sheight)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500391 png_size_t lengthw = 0, lengthh = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500393 png_debug1(1, "in %s storage function", "sCAL");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500394
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 if (png_ptr == NULL || info_ptr == NULL)
396 return;
397
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500398 /* Double check the unit (should never get here with an invalid
399 * unit unless this is an API call.)
400 */
401 if (unit != 1 && unit != 2)
402 png_error(png_ptr, "Invalid sCAL unit");
403
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500404 if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500405 swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500406 png_error(png_ptr, "Invalid sCAL width");
407
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500408 if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
Glenn Randers-Pehrsonb75b2412011-04-16 19:35:05 -0500409 sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500410 png_error(png_ptr, "Invalid sCAL height");
411
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600412 info_ptr->scal_unit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600413
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500414 ++lengthw;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500415
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600416 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500417
John Bowlerfcd301d2011-12-28 21:34:27 -0600418 info_ptr->scal_s_width = png_voidcast(png_charp,
419 png_malloc_warn(png_ptr, lengthw));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500420
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500421 if (info_ptr->scal_s_width == NULL)
422 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500423 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500424 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500425 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500426
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500427 memcpy(info_ptr->scal_s_width, swidth, lengthw);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600428
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500429 ++lengthh;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500430
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600431 png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500432
John Bowlerfcd301d2011-12-28 21:34:27 -0600433 info_ptr->scal_s_height = png_voidcast(png_charp,
434 png_malloc_warn(png_ptr, lengthh));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500436 if (info_ptr->scal_s_height == NULL)
437 {
438 png_free (png_ptr, info_ptr->scal_s_width);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500439 info_ptr->scal_s_width = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500440
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500441 png_warning(png_ptr, "Memory allocation failed while processing sCAL");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500442 return;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500443 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500444
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500445 memcpy(info_ptr->scal_s_height, sheight, lengthh);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500446
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600447 info_ptr->valid |= PNG_INFO_sCAL;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500448 info_ptr->free_me |= PNG_FREE_SCAL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600449}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500450
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500451# ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500452void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600453png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
454 double width, double height)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500455{
456 png_debug1(1, "in %s storage function", "sCAL");
457
458 /* Check the arguments. */
459 if (width <= 0)
460 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500461
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500462 else if (height <= 0)
463 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500464
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500465 else
466 {
467 /* Convert 'width' and 'height' to ASCII. */
468 char swidth[PNG_sCAL_MAX_DIGITS+1];
469 char sheight[PNG_sCAL_MAX_DIGITS+1];
470
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500471 png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500472 PNG_sCAL_PRECISION);
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500473 png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500474 PNG_sCAL_PRECISION);
475
476 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
477 }
478}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500479# endif
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500480
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500481# ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500482void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600483png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500484 png_fixed_point width, png_fixed_point height)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500485{
486 png_debug1(1, "in %s storage function", "sCAL");
487
488 /* Check the arguments. */
489 if (width <= 0)
490 png_warning(png_ptr, "Invalid sCAL width ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500491
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500492 else if (height <= 0)
493 png_warning(png_ptr, "Invalid sCAL height ignored");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500494
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500495 else
496 {
497 /* Convert 'width' and 'height' to ASCII. */
498 char swidth[PNG_sCAL_MAX_DIGITS+1];
499 char sheight[PNG_sCAL_MAX_DIGITS+1];
500
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500501 png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
502 png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500503
504 png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
505 }
506}
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500507# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600508#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600509
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500510#ifdef PNG_pHYs_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500511void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600512png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600513 png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500514{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500515 png_debug1(1, "in %s storage function", "pHYs");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500516
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600517 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 return;
519
520 info_ptr->x_pixels_per_unit = res_x;
521 info_ptr->y_pixels_per_unit = res_y;
522 info_ptr->phys_unit_type = (png_byte)unit_type;
523 info_ptr->valid |= PNG_INFO_pHYs;
524}
525#endif
526
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600528png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500529 png_const_colorp palette, int num_palette)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600531
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500532 png_debug1(1, "in %s storage function", "PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500533
John Bowlerfcd301d2011-12-28 21:34:27 -0600534 if (png_ptr == NULL || info_ptr == NULL || palette == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 return;
536
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600537 if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500538 {
539 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600540 png_error(png_ptr, "Invalid palette length");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500541
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500542 else
543 {
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600544 png_warning(png_ptr, "Invalid palette length");
545 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500546 }
547 }
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600548
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600549 /* It may not actually be necessary to set png_ptr->palette here;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600550 * we do it for backward compatibility with the way the png_handle_tRNS
551 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600552 *
553 * 1.6.0: the above statement appears to be incorrect; something has to set
554 * the palette inside png_struct on read.
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600555 */
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600556 png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500557
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600558 /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500559 * of num_palette entries, in case of an invalid PNG file that has
560 * too-large sample values.
561 */
John Bowlerfcd301d2011-12-28 21:34:27 -0600562 png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500563 PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500564
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500565 memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600566 info_ptr->palette = png_ptr->palette;
567 info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600568
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600569 info_ptr->free_me |= PNG_FREE_PLTE;
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600570
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600571 info_ptr->valid |= PNG_INFO_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572}
573
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500574#ifdef PNG_sBIT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500575void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600576png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500577 png_const_color_8p sig_bit)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500579 png_debug1(1, "in %s storage function", "sBIT");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500580
John Bowlerfcd301d2011-12-28 21:34:27 -0600581 if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582 return;
583
John Bowlerfcd301d2011-12-28 21:34:27 -0600584 info_ptr->sig_bit = *sig_bit;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 info_ptr->valid |= PNG_INFO_sBIT;
586}
587#endif
588
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500589#ifdef PNG_sRGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600591png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600592{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500593 png_debug1(1, "in %s storage function", "sRGB");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500594
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600595 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600596 return;
597
John Bowlerb11b31a2012-03-21 07:55:46 -0500598 (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent,
599 2/* app value overrides*/);
600
601 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600602}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600603
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600605png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson8d0bc0f2011-01-25 22:15:58 -0600606 int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600607{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500608 png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500609
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600610 if (png_ptr == NULL || info_ptr == NULL)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611 return;
612
John Bowlerb11b31a2012-03-21 07:55:46 -0500613 if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent,
614 2/* app value overrides*/))
615 {
616 /* And cause the gAMA and cHRM to be written too */
617 info_ptr->colorspace.flags |=
618 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
619 }
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600620
John Bowlerb11b31a2012-03-21 07:55:46 -0500621 png_colorspace_sync_info(png_ptr, info_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600622}
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600623#endif /* sRGB */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600624
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600625
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500626#ifdef PNG_iCCP_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500627void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600628png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500629 png_const_charp name, int compression_type,
630 png_const_bytep profile, png_uint_32 proflen)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600631{
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500632 png_charp new_iccp_name;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500633 png_bytep new_iccp_profile;
John Bowlerf3f7e142011-09-09 07:32:37 -0500634 png_size_t length;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500635
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500636 png_debug1(1, "in %s storage function", "iCCP");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500637
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600638 if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
639 return;
640
John Bowlerb11b31a2012-03-21 07:55:46 -0500641 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
642 png_app_error(png_ptr, "Invalid iCCP compression method");
643
644 /* Set the colorspace first because this validates the profile; do not
645 * override previously set app cHRM or gAMA here (because likely as not the
John Bowler14d0ca62012-08-25 16:21:46 -0500646 * application knows better than libpng what the correct values are.) Pass
647 * the info_ptr color_type field to png_colorspace_set_ICC because in the
648 * write case it has not yet been stored in png_ptr.
John Bowlerb11b31a2012-03-21 07:55:46 -0500649 */
650 {
651 int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
John Bowler14d0ca62012-08-25 16:21:46 -0500652 proflen, profile, 0/* do *not* override the app cHRM or gAMA */,
653 info_ptr->color_type);
John Bowlerb11b31a2012-03-21 07:55:46 -0500654
655 png_colorspace_sync_info(png_ptr, info_ptr);
656
657 /* Don't do any of the copying if the profile was bad, or inconsistent. */
658 if (!result)
659 return;
660
661 /* But do write the gAMA and cHRM chunks from the profile. */
662 info_ptr->colorspace.flags |=
663 PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
664 }
665
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500666 length = strlen(name)+1;
John Bowlerfcd301d2011-12-28 21:34:27 -0600667 new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500668
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500669 if (new_iccp_name == NULL)
670 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500671 png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500672 return;
673 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500674
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500675 memcpy(new_iccp_name, name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -0600676 new_iccp_profile = png_voidcast(png_bytep,
677 png_malloc_warn(png_ptr, proflen));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500678
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500679 if (new_iccp_profile == NULL)
680 {
John Bowlerb11b31a2012-03-21 07:55:46 -0500681 png_free(png_ptr, new_iccp_name);
682 png_benign_error(png_ptr,
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -0600683 "Insufficient memory to process iCCP profile");
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500684 return;
685 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500686
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500687 memcpy(new_iccp_profile, profile, proflen);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500688
Glenn Randers-Pehrson38e6e772000-04-09 19:06:13 -0500689 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500690
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600691 info_ptr->iccp_proflen = proflen;
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500692 info_ptr->iccp_name = new_iccp_name;
693 info_ptr->iccp_profile = new_iccp_profile;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600694 info_ptr->free_me |= PNG_FREE_ICCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 info_ptr->valid |= PNG_INFO_iCCP;
696}
697#endif
698
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500699#ifdef PNG_TEXT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500700void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600701png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
702 png_const_textp text_ptr, int num_text)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500703{
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500704 int ret;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500705 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500706
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500707 if (ret)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500708 png_error(png_ptr, "Insufficient memory to store text");
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500709}
710
711int /* PRIVATE */
John Bowler4f67e402011-12-28 08:43:37 -0600712png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500713 png_const_textp text_ptr, int num_text)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500714{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 int i;
716
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500717 png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
718 (unsigned long)png_ptr->chunk_name);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600720 if (png_ptr == NULL || info_ptr == NULL || num_text == 0)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500721 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722
723 /* Make sure we have enough space in the "text" array in info_struct
724 * to hold all of the incoming text_ptr objects.
725 */
726 if (info_ptr->num_text + num_text > info_ptr->max_text)
727 {
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500728 int old_max_text = info_ptr->max_text;
729 int old_num_text = info_ptr->num_text;
730
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 if (info_ptr->text != NULL)
732 {
733 png_textp old_text;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735 info_ptr->max_text = info_ptr->num_text + num_text + 8;
736 old_text = info_ptr->text;
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500737
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500738 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500739 (png_size_t)(info_ptr->max_text * (sizeof (png_text))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500740
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500741 if (info_ptr->text == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500742 {
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500743 /* Restore to previous condition */
744 info_ptr->max_text = old_max_text;
745 info_ptr->text = old_text;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500746 return(1);
747 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500748
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500749 memcpy(info_ptr->text, old_text, (png_size_t)(old_max_text *
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500750 (sizeof (png_text))));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751 png_free(png_ptr, old_text);
752 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500753
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 else
755 {
756 info_ptr->max_text = num_text + 8;
757 info_ptr->num_text = 0;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500758 info_ptr->text = (png_textp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -0500759 (png_size_t)(info_ptr->max_text * (sizeof (png_text))));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500760 if (info_ptr->text == NULL)
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500761 {
762 /* Restore to previous condition */
763 info_ptr->num_text = old_num_text;
764 info_ptr->max_text = old_max_text;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500765 return(1);
Glenn Randers-Pehrson96027d92012-03-29 06:15:55 -0500766 }
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500767 info_ptr->free_me |= PNG_FREE_TEXT;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500768 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500769
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500770 png_debug1(3, "allocated %d entries for info_ptr->text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600771 info_ptr->max_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500772 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500773 for (i = 0; i < num_text; i++)
774 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500775 png_size_t text_length, key_len;
776 png_size_t lang_len, lang_key_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777 png_textp textp = &(info_ptr->text[info_ptr->num_text]);
778
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500779 if (text_ptr[i].key == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600780 continue;
781
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500782 if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
783 text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
Glenn Randers-Pehrsone34f80e2010-10-13 06:55:30 -0500784 {
785 png_warning(png_ptr, "text compression mode is out of range");
786 continue;
787 }
788
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500789 key_len = strlen(text_ptr[i].key);
Glenn Randers-Pehrson79b2d642010-10-13 07:31:14 -0500790
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500791 if (text_ptr[i].compression <= 0)
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500792 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793 lang_len = 0;
794 lang_key_len = 0;
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -0500795 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500796
Glenn Randers-Pehrsondd78d522010-03-30 08:34:02 -0500797 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500798# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500800 /* Set iTXt data */
Glenn Randers-Pehrsond6ea40a2009-11-02 07:32:00 -0600801
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500802 if (text_ptr[i].lang != NULL)
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500803 lang_len = strlen(text_ptr[i].lang);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500804
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500805 else
806 lang_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500808 if (text_ptr[i].lang_key != NULL)
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500809 lang_key_len = strlen(text_ptr[i].lang_key);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500810
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500811 else
812 lang_key_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600813 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500814# else /* PNG_iTXt_SUPPORTED */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500815 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500816 png_warning(png_ptr, "iTXt chunk not supported");
817 continue;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500818 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500819# endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500820
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500821 if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
Andreas Dilger47a0c421997-05-16 02:46:07 -0500822 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600823 text_length = 0;
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500824# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500825 if (text_ptr[i].compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600826 textp->compression = PNG_ITXT_COMPRESSION_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500827
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600828 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500829# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600830 textp->compression = PNG_TEXT_COMPRESSION_NONE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831 }
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500832
Andreas Dilger47a0c421997-05-16 02:46:07 -0500833 else
834 {
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -0500835 text_length = strlen(text_ptr[i].text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500836 textp->compression = text_ptr[i].compression;
837 }
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500838
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500839 textp->key = (png_charp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600840 (png_size_t)
841 (key_len + text_length + lang_len + lang_key_len + 4));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500842
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500843 if (textp->key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500844 return(1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500845
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600846 png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600847 (unsigned long)(png_uint_32)
848 (key_len + lang_len + lang_key_len + text_length + 4),
Glenn Randers-Pehrsonb764c602011-01-14 21:18:37 -0600849 textp->key);
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500850
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500851 memcpy(textp->key, text_ptr[i].key,(png_size_t)(key_len));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500852 *(textp->key + key_len) = '\0';
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500853
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600854 if (text_ptr[i].compression > 0)
855 {
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500856 textp->lang = textp->key + key_len + 1;
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500857 memcpy(textp->lang, text_ptr[i].lang, lang_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500858 *(textp->lang + lang_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500859 textp->lang_key = textp->lang + lang_len + 1;
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500860 memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500861 *(textp->lang_key + lang_key_len) = '\0';
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500862 textp->text = textp->lang_key + lang_key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600863 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500864
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600865 else
866 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500867 textp->lang=NULL;
868 textp->lang_key=NULL;
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500869 textp->text = textp->key + key_len + 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600870 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500871
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500872 if (text_length)
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500873 memcpy(textp->text, text_ptr[i].text,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -0600874 (png_size_t)(text_length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500875
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500876 *(textp->text + text_length) = '\0';
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600877
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500878# ifdef PNG_iTXt_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500879 if (textp->compression > 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600880 {
881 textp->text_length = 0;
882 textp->itxt_length = text_length;
883 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500884
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600885 else
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500886# endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600887 {
888 textp->text_length = text_length;
889 textp->itxt_length = 0;
890 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500891
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 info_ptr->num_text++;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500893 png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500894 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500895 return(0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896}
897#endif
898
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500899#ifdef PNG_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500900void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600901png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
902 png_const_timep mod_time)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500903{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500904 png_debug1(1, "in %s storage function", "tIME");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500905
John Bowlerfcd301d2011-12-28 21:34:27 -0600906 if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600907 (png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 return;
909
Glenn Randers-Pehrson29fca792011-11-19 15:08:04 -0600910 if (mod_time->month == 0 || mod_time->month > 12 ||
911 mod_time->day == 0 || mod_time->day > 31 ||
912 mod_time->hour > 23 || mod_time->minute > 59 ||
913 mod_time->second > 60)
914 {
915 png_warning(png_ptr, "Ignoring invalid time value");
916 return;
917 }
918
John Bowlerfcd301d2011-12-28 21:34:27 -0600919 info_ptr->mod_time = *mod_time;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 info_ptr->valid |= PNG_INFO_tIME;
921}
922#endif
923
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500924#ifdef PNG_tRNS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500925void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600926png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500927 png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500928{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500929 png_debug1(1, "in %s storage function", "tRNS");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500930
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600931 if (png_ptr == NULL || info_ptr == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500932 return;
933
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500934 if (trans_alpha != NULL)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600935 {
Glenn Randers-Pehrsonef29a5e2009-10-31 19:37:05 -0500936 /* It may not actually be necessary to set png_ptr->trans_alpha here;
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500937 * we do it for backward compatibility with the way the png_handle_tRNS
938 * function used to do the allocation.
John Bowler4f67e402011-12-28 08:43:37 -0600939 *
940 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
941 * relies on png_set_tRNS storing the information in png_struct
942 * (otherwise it won't be there for the code in pngrtran.c).
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -0500943 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500944
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600945 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500946
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600947 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
John Bowler4f67e402011-12-28 08:43:37 -0600948 png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep,
949 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500950
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500951 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500952 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600953 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500955 if (trans_color != NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500956 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500957 int sample_max = (1 << info_ptr->bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500958
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500959 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
John Bowler4f67e402011-12-28 08:43:37 -0600960 trans_color->gray > sample_max) ||
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500961 (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
John Bowler4f67e402011-12-28 08:43:37 -0600962 (trans_color->red > sample_max ||
963 trans_color->green > sample_max ||
964 trans_color->blue > sample_max)))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500965 png_warning(png_ptr,
966 "tRNS chunk has out-of-range samples for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500967
John Bowlerfcd301d2011-12-28 21:34:27 -0600968 info_ptr->trans_color = *trans_color;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500969
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600970 if (num_trans == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500971 num_trans = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500972 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500973
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 info_ptr->num_trans = (png_uint_16)num_trans;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500975
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500976 if (num_trans != 0)
977 {
978 info_ptr->valid |= PNG_INFO_tRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500979 info_ptr->free_me |= PNG_FREE_TRNS;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500980 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500981}
982#endif
983
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500984#ifdef PNG_sPLT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500985void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -0600986png_set_sPLT(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -0600987 png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600988/*
989 * entries - array of png_sPLT_t structures
990 * to be added to the list of palettes
991 * in the info structure.
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -0500992 *
Glenn Randers-Pehrson3779c692006-11-07 20:38:11 -0600993 * nentries - number of palette structures to be
994 * added.
995 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600996{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500997 png_sPLT_tp np;
998 int i;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600999
John Bowlerfcd301d2011-12-28 21:34:27 -06001000 if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 ||
1001 entries == NULL)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -05001002 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003
John Bowlerfcd301d2011-12-28 21:34:27 -06001004 np = png_voidcast(png_sPLT_tp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001005 (info_ptr->splt_palettes_num + nentries) * (sizeof (png_sPLT_t))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001006
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001007 if (np == NULL)
1008 {
1009 png_warning(png_ptr, "No memory for sPLT palettes");
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001010 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001011 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001012
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001013 memcpy(np, info_ptr->splt_palettes,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001014 info_ptr->splt_palettes_num * (sizeof (png_sPLT_t)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001015
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001016 png_free(png_ptr, info_ptr->splt_palettes);
1017 info_ptr->splt_palettes=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001018
John Bowlerfcd301d2011-12-28 21:34:27 -06001019 /* TODO: fix this, it apparently leaves NULL entries in the event of OOM
1020 * below.
1021 */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001022 for (i = 0; i < nentries; i++)
1023 {
1024 png_sPLT_tp to = np + info_ptr->splt_palettes_num + i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001025 png_const_sPLT_tp from = entries + i;
John Bowlerf3f7e142011-09-09 07:32:37 -05001026 png_size_t length;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027
John Bowlerfcd301d2011-12-28 21:34:27 -06001028 /* In event of error below the name and entries fields must be set to
1029 * NULL, otherwise libpng will crash later on while trying to free the
1030 * uninitialized pointers.
1031 */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001032 memset(to, 0, (sizeof *to));
John Bowlerfcd301d2011-12-28 21:34:27 -06001033
1034 if (from->name == NULL || from->entries == NULL)
1035 continue;
1036
Glenn Randers-Pehrsonece07cf2012-08-10 18:08:02 -05001037 length = strlen(from->name) + 1;
John Bowlerfcd301d2011-12-28 21:34:27 -06001038 to->name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001039
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001040 if (to->name == NULL)
1041 {
1042 png_warning(png_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001043 "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001044 continue;
1045 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001046
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001047 memcpy(to->name, from->name, length);
John Bowlerfcd301d2011-12-28 21:34:27 -06001048 to->entries = png_voidcast(png_sPLT_entryp, png_malloc_warn(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001049 from->nentries * (sizeof (png_sPLT_entry))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001050
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001051 if (to->entries == NULL)
1052 {
John Bowlerfcd301d2011-12-28 21:34:27 -06001053 png_warning(png_ptr, "Out of memory while processing sPLT chunk");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001054 png_free(png_ptr, to->name);
1055 to->name = NULL;
1056 continue;
1057 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001058
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001059 memcpy(to->entries, from->entries,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001060 from->nentries * (sizeof (png_sPLT_entry)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062 to->nentries = from->nentries;
1063 to->depth = from->depth;
1064 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001065
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001066 info_ptr->splt_palettes = np;
1067 info_ptr->splt_palettes_num += nentries;
1068 info_ptr->valid |= PNG_INFO_sPLT;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001069 info_ptr->free_me |= PNG_FREE_SPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070}
1071#endif /* PNG_sPLT_SUPPORTED */
1072
John Bowlere9567512012-08-15 22:53:00 -05001073#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
1074static png_byte
1075check_location(png_const_structrp png_ptr, unsigned int location)
1076{
1077 location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1078
1079 /* New in 1.6.0; copy the location and check it. This is an API
1080 * change, previously the app had to use the
1081 * png_set_unknown_chunk_location API below for each chunk.
1082 */
1083 if (location == 0 && !(png_ptr->mode & PNG_IS_READ_STRUCT))
1084 {
1085 /* Write struct, so unknown chunks come from the app */
1086 png_app_warning(png_ptr,
1087 "png_set_unknown_chunks now expects a valid location");
1088 /* Use the old behavior */
1089 location = png_ptr->mode &
1090 (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
1091 }
1092
1093 if (location == 0)
1094 png_error(png_ptr, "invalid location in png_set_unknown_chunks");
1095
1096 /* Now reduce the location to the top-most set bit by removing each least
1097 * significant bit in turn.
1098 */
1099 while (location != (location & -location))
1100 location &= (png_byte)~(location & -location);
1101
1102 /* The cast is safe because 'location' is a bit mask and only the low four
1103 * bits are significant.
1104 */
1105 return (png_byte)location;
1106}
1107
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001108void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001109png_set_unknown_chunks(png_const_structrp png_ptr,
John Bowler5d567862011-12-24 09:12:00 -06001110 png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001111{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001112 png_unknown_chunkp np;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001113
John Bowlere9567512012-08-15 22:53:00 -05001114 if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0)
Glenn Randers-Pehrson83fb8552009-06-16 17:52:23 -05001115 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001116
John Bowler4a6c6df2012-08-16 16:12:13 -05001117 /* Check for the failure cases where support has been disabled at compile
1118 * time. This code is hardly ever compiled - it's here because
1119 * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
1120 * code) but may be meaningless if the read or write handling of unknown
1121 * chunks is not compiled in.
1122 */
1123# if !(defined PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
1124 (defined PNG_READ_SUPPORTED)
1125 if (png_ptr->mode & PNG_IS_READ_STRUCT)
1126 {
1127 png_app_error(png_ptr, "no unknown chunk support on read");
1128 return;
1129 }
1130# endif
1131# if !(defined PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
1132 (defined PNG_WRITE_SUPPORTED)
1133 if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
1134 {
1135 png_app_error(png_ptr, "no unknown chunk support on write");
1136 return;
1137 }
1138# endif
1139
John Bowlere9567512012-08-15 22:53:00 -05001140 /* Prior to 1.6.0 this code used png_malloc_warn, however this meant that
1141 * unknown critical chunks could be lost with just a warning resulting in
1142 * undefined behavior. Changing to png_malloc fixes this by producing a
1143 * png_error. The (png_size_t) cast was also removed as it hides a potential
1144 * overflow.
1145 *
1146 * TODO: fix the potential overflow in the multiply
1147 */
1148 np = png_voidcast(png_unknown_chunkp, png_malloc(png_ptr,
1149 (info_ptr->unknown_chunks_num + (unsigned int)num_unknowns) *
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001150 (sizeof (png_unknown_chunk))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001151
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001152 memcpy(np, info_ptr->unknown_chunks,
John Bowlere9567512012-08-15 22:53:00 -05001153 info_ptr->unknown_chunks_num * (sizeof (png_unknown_chunk)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001154
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001155 png_free(png_ptr, info_ptr->unknown_chunks);
John Bowlere9567512012-08-15 22:53:00 -05001156 info_ptr->unknown_chunks = np; /* safe because it is initialized */
1157 info_ptr->free_me |= PNG_FREE_UNKN;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001158
John Bowlere9567512012-08-15 22:53:00 -05001159 np += info_ptr->unknown_chunks_num;
1160
1161 /* Increment unknown_chunks_num each time round the loop to protect the
1162 * just-allocated chunk data.
1163 */
1164 for (; --num_unknowns >= 0;
1165 ++np, ++unknowns, ++(info_ptr->unknown_chunks_num))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001166 {
John Bowlere9567512012-08-15 22:53:00 -05001167 memcpy(np->name, unknowns->name, (sizeof unknowns->name));
1168 np->name[(sizeof np->name)-1] = '\0';
1169 np->size = unknowns->size;
1170 np->location = check_location(png_ptr, unknowns->location);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001171
John Bowlere9567512012-08-15 22:53:00 -05001172 if (unknowns->size == 0)
1173 np->data = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001174
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001175 else
1176 {
John Bowlere9567512012-08-15 22:53:00 -05001177 /* png_error is safe here because the list is stored in png_ptr */
1178 np->data = png_voidcast(png_bytep,
1179 png_malloc(png_ptr, unknowns->size));
1180 memcpy(np->data, unknowns->data, unknowns->size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001181 }
1182 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001183}
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001184
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001185void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001186png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001187 int chunk, int location)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001188{
John Bowlere9567512012-08-15 22:53:00 -05001189 /* This API is pretty pointless in 1.6.0 because the location can be set
1190 * before the call to png_set_unknown_chunks.
1191 *
1192 * TODO: add a png_app_warning in 1.7
1193 */
1194 if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
1195 (unsigned int)chunk < info_ptr->unknown_chunks_num)
1196 {
1197 if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
1198 {
1199 png_app_error(png_ptr, "invalid unknown chunk location");
1200 /* Fake out the pre 1.6.0 behavior: */
1201 if ((location & PNG_HAVE_IDAT)) /* undocumented! */
1202 location = PNG_AFTER_IDAT;
1203
1204 else
1205 location = PNG_HAVE_IHDR; /* also undocumented */
1206 }
1207
1208 info_ptr->unknown_chunks[chunk].location =
1209 check_location(png_ptr, (png_byte)location);
1210 }
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001211}
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001212#endif
1213
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001214
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001215#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001216png_uint_32 PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001217png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001218{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001219 png_debug(1, "in png_permit_mng_features");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001220
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001221 if (png_ptr == NULL)
John Bowler4f67e402011-12-28 08:43:37 -06001222 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001223
John Bowler4f67e402011-12-28 08:43:37 -06001224 png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001225
John Bowler4f67e402011-12-28 08:43:37 -06001226 return png_ptr->mng_features_permitted;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001227}
1228#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001229
Glenn Randers-Pehrson6ba90882009-12-25 14:26:13 -06001230#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -05001231static unsigned int
1232add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
1233{
1234 unsigned int i;
1235
1236 /* Utility function: update the 'keep' state of a chunk if it is already in
1237 * the list, otherwise add it to the list.
1238 */
1239 for (i=0; i<count; ++i, list += 5) if (memcmp(list, add, 4) == 0)
1240 {
1241 list[4] = (png_byte)keep;
1242 return count;
1243 }
1244
1245 if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
1246 {
1247 ++count;
1248 memcpy(list, add, 4);
1249 list[4] = (png_byte)keep;
1250 }
1251
1252 return count;
1253}
1254
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001255void PNGAPI
John Bowlere9567512012-08-15 22:53:00 -05001256png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001257 png_const_bytep chunk_list, int num_chunks_in)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001258{
John Bowlere9567512012-08-15 22:53:00 -05001259 png_bytep new_list;
1260 unsigned int num_chunks, old_num_chunks;
1261
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001262 if (png_ptr == NULL)
1263 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001264
John Bowlere9567512012-08-15 22:53:00 -05001265 if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
John Bowleraa9dea52012-08-10 19:04:08 -05001266 {
John Bowlere9567512012-08-15 22:53:00 -05001267 png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
1268 return;
John Bowleraa9dea52012-08-10 19:04:08 -05001269 }
1270
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001271 if (num_chunks_in <= 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001272 {
John Bowlere9567512012-08-15 22:53:00 -05001273 png_ptr->unknown_default = keep;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001274
John Bowlere9567512012-08-15 22:53:00 -05001275 /* '0' means just set the flags, so stop here */
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001276 if (num_chunks_in == 0)
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001277 return;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001278 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001279
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001280 if (num_chunks_in < 0)
John Bowleraa9dea52012-08-10 19:04:08 -05001281 {
1282 /* Ignore all unknown chunks and all chunks recognized by
1283 * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
1284 */
1285 static PNG_CONST png_byte chunks_to_ignore[] = {
1286 98, 75, 71, 68, '\0', /* bKGD */
1287 99, 72, 82, 77, '\0', /* cHRM */
1288 103, 65, 77, 65, '\0', /* gAMA */
1289 104, 73, 83, 84, '\0', /* hIST */
1290 105, 67, 67, 80, '\0', /* iCCP */
1291 105, 84, 88, 116, '\0', /* iTXt */
1292 111, 70, 70, 115, '\0', /* oFFs */
1293 112, 67, 65, 76, '\0', /* pCAL */
1294 112, 72, 89, 115, '\0', /* pHYs */
1295 115, 66, 73, 84, '\0', /* sBIT */
1296 115, 67, 65, 76, '\0', /* sCAL */
1297 115, 80, 76, 84, '\0', /* sPLT */
1298 115, 84, 69, 82, '\0', /* sTER */
1299 115, 82, 71, 66, '\0', /* sRGB */
1300 116, 69, 88, 116, '\0', /* tEXt */
1301 116, 73, 77, 69, '\0', /* tIME */
1302 122, 84, 88, 116, '\0' /* zTXt */
1303 };
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001304
John Bowleraa9dea52012-08-10 19:04:08 -05001305 chunk_list = chunks_to_ignore;
1306 num_chunks = (sizeof chunks_to_ignore)/5;
1307 }
Glenn Randers-Pehrson96b91702012-06-16 13:26:28 -05001308
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001309 else /* num_chunks_in > 0 */
John Bowleraa9dea52012-08-10 19:04:08 -05001310 {
1311 if (chunk_list == NULL)
John Bowlere9567512012-08-15 22:53:00 -05001312 {
1313 /* Prior to 1.6.0 this was silently ignored, now it is an app_error
1314 * which can be switched off.
1315 */
1316 png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
John Bowleraa9dea52012-08-10 19:04:08 -05001317 return;
John Bowlere9567512012-08-15 22:53:00 -05001318 }
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001319
Glenn Randers-Pehrson702053d2012-08-17 21:23:06 -05001320 num_chunks = num_chunks_in;
John Bowleraa9dea52012-08-10 19:04:08 -05001321 }
John Bowlerfcd301d2011-12-28 21:34:27 -06001322
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001323 old_num_chunks = png_ptr->num_chunk_list;
John Bowlere9567512012-08-15 22:53:00 -05001324 if (png_ptr->chunk_list == NULL)
1325 old_num_chunks = 0;
John Bowleraa9dea52012-08-10 19:04:08 -05001326
1327 /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
1328 */
1329 if (num_chunks + old_num_chunks > UINT_MAX/5)
1330 {
1331 png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
1332 return;
1333 }
1334
John Bowlere9567512012-08-15 22:53:00 -05001335 /* If these chunks are being reset to the default then no more memory is
1336 * required because add_one_chunk above doesn't extend the list if the 'keep'
1337 * parameter is the default.
1338 */
1339 if (keep)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001340 {
John Bowlere9567512012-08-15 22:53:00 -05001341 new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
1342 5 * (num_chunks + old_num_chunks)));
1343
1344 if (old_num_chunks > 0)
1345 memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001346 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001347
John Bowlere9567512012-08-15 22:53:00 -05001348 else if (old_num_chunks > 0)
1349 new_list = png_ptr->chunk_list;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001350
John Bowlere9567512012-08-15 22:53:00 -05001351 else
1352 new_list = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001353
John Bowlere9567512012-08-15 22:53:00 -05001354 /* Add the new chunks together with each one's handling code. If the chunk
1355 * already exists the code is updated, otherwise the chunk is added to the
1356 * end. (In libpng 1.6.0 order no longer matters because this code enforces
1357 * the earlier convention that the last setting is the one that is used.)
1358 */
1359 if (new_list != NULL)
1360 {
1361 png_const_bytep inlist;
1362 png_bytep outlist;
1363 unsigned int i;
1364
1365 for (i=0; i<num_chunks; ++i)
1366 old_num_chunks = add_one_chunk(new_list, old_num_chunks,
1367 chunk_list+5*i, keep);
1368
1369 /* Now remove any spurious 'default' entries. */
1370 num_chunks = 0;
1371 for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
1372 if (inlist[4])
1373 {
1374 if (outlist != inlist)
1375 memcpy(outlist, inlist, 5);
1376 outlist += 5;
1377 ++num_chunks;
1378 }
1379
1380 /* This means the application has removed all the specialized handling. */
1381 if (num_chunks == 0)
1382 {
1383 if (png_ptr->chunk_list != new_list)
1384 png_free(png_ptr, new_list);
1385
1386 new_list = NULL;
1387 }
1388 }
1389
1390 else
1391 num_chunks = 0;
1392
1393 png_ptr->num_chunk_list = num_chunks;
1394
1395 if (png_ptr->chunk_list != new_list)
1396 {
1397 if (png_ptr->chunk_list != NULL)
1398 png_free(png_ptr, png_ptr->chunk_list);
1399
1400 png_ptr->chunk_list = new_list;
1401 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001402}
1403#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001404
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001405#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001406void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001407png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
Glenn Randers-Pehrsonda301352010-03-06 06:42:48 -06001408 png_user_chunk_ptr read_user_chunk_fn)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001409{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001410 png_debug(1, "in png_set_read_user_chunk_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001411
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001412 if (png_ptr == NULL)
1413 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001414
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001415 png_ptr->read_user_chunk_fn = read_user_chunk_fn;
1416 png_ptr->user_chunk_ptr = user_chunk_ptr;
1417}
1418#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001419
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001420#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001421void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001422png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
1423 png_bytepp row_pointers)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001424{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001425 png_debug1(1, "in %s storage function", "rows");
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001426
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001427 if (png_ptr == NULL || info_ptr == NULL)
1428 return;
1429
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001430 if (info_ptr->row_pointers && (info_ptr->row_pointers != row_pointers))
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -05001431 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001432
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001433 info_ptr->row_pointers = row_pointers;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001434
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001435 if (row_pointers)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001436 info_ptr->valid |= PNG_INFO_IDAT;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001437}
1438#endif
1439
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001440void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001441png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001442{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001443 if (png_ptr == NULL)
1444 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001445
John Bowlerb5d00512012-03-09 09:15:18 -06001446 if (size == 0 || size > PNG_UINT_31_MAX)
1447 png_error(png_ptr, "invalid compression buffer size");
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001448
John Bowlerb5d00512012-03-09 09:15:18 -06001449# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1450 if (png_ptr->mode & PNG_IS_READ_STRUCT)
1451 {
1452 png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
1453 return;
1454 }
1455# endif
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001456
John Bowler134c5762012-08-10 10:46:45 -05001457# ifdef PNG_WRITE_SUPPORTED
John Bowlerb5d00512012-03-09 09:15:18 -06001458 if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
1459 {
1460 if (png_ptr->zowner != 0)
1461 {
1462 png_warning(png_ptr,
1463 "Compression buffer size cannot be changed because it is in use");
1464 return;
1465 }
John Bowleraa9dea52012-08-10 19:04:08 -05001466
John Bowlerb5d00512012-03-09 09:15:18 -06001467 if (size > ZLIB_IO_MAX)
1468 {
1469 png_warning(png_ptr,
1470 "Compression buffer size limited to system maximum");
1471 size = ZLIB_IO_MAX; /* must fit */
1472 }
John Bowleraa9dea52012-08-10 19:04:08 -05001473
John Bowlerb5d00512012-03-09 09:15:18 -06001474 else if (size < 6)
1475 {
1476 /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
1477 * if this is permitted.
1478 */
1479 png_warning(png_ptr,
1480 "Compression buffer size cannot be reduced below 6");
1481 return;
1482 }
John Bowleraa9dea52012-08-10 19:04:08 -05001483
John Bowlerb5d00512012-03-09 09:15:18 -06001484 if (png_ptr->zbuffer_size != size)
1485 {
1486 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
1487 png_ptr->zbuffer_size = (uInt)size;
1488 }
1489 }
1490# endif
Glenn Randers-Pehrson228bd392000-04-23 23:14:02 -05001491}
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001492
1493void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001494png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001495{
1496 if (png_ptr && info_ptr)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001497 info_ptr->valid &= ~mask;
Glenn Randers-Pehrsonfc4a1432000-05-17 17:39:34 -05001498}
Glenn Randers-Pehrson231e6872001-01-12 15:13:06 -06001499
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001500
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001501#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001502/* This function was added to libpng 1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001503void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001504png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001505 png_uint_32 user_height_max)
1506{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001507 /* Images with dimensions larger than these limits will be
1508 * rejected by png_set_IHDR(). To accept any PNG datastream
1509 * regardless of dimensions, set both limits to 0x7ffffffL.
1510 */
1511 if (png_ptr == NULL)
1512 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001513
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001514 png_ptr->user_width_max = user_width_max;
1515 png_ptr->user_height_max = user_height_max;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001516}
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001517
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -06001518/* This function was added to libpng 1.4.0 */
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001519void PNGAPI
John Bowler4f67e402011-12-28 08:43:37 -06001520png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001521{
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001522 if (png_ptr)
1523 png_ptr->user_chunk_cache_max = user_chunk_cache_max;
1524}
1525
1526/* This function was added to libpng 1.4.1 */
1527void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001528png_set_chunk_malloc_max (png_structrp png_ptr,
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001529 png_alloc_size_t user_chunk_malloc_max)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06001530{
Glenn Randers-Pehrson62333ba2010-10-23 08:48:51 -05001531 if (png_ptr)
1532 png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
Glenn Randers-Pehrson800d1e92008-08-20 17:25:21 -05001533}
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001534#endif /* ?PNG_SET_USER_LIMITS_SUPPORTED */
1535
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001536
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001537#ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001538void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001539png_set_benign_errors(png_structrp png_ptr, int allowed)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001540{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001541 png_debug(1, "in png_set_benign_errors");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001542
Glenn Randers-Pehrsondd6ecbe2012-03-16 07:33:38 -05001543 /* If allowed is 1, png_benign_error() is treated as a warning.
1544 *
1545 * If allowed is 0, png_benign_error() is treated as an error (which
1546 * is the default behavior if png_set_benign_errors() is not called).
1547 */
1548
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001549 if (allowed)
Glenn Randers-Pehrsonce9d8a32012-03-18 22:39:52 -05001550 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
1551 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001552
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001553 else
Glenn Randers-Pehrsonce9d8a32012-03-18 22:39:52 -05001554 png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
1555 PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001556}
1557#endif /* PNG_BENIGN_ERRORS_SUPPORTED */
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001558
Glenn Randers-Pehrsone243a1f2012-04-13 12:35:21 -05001559#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001560 /* Do not report invalid palette index; added at libng-1.5.10 */
1561void PNGAPI
1562png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
1563{
1564 png_debug(1, "in png_set_check_for_invalid_index");
1565
Glenn Randers-Pehrson945cb1f2012-03-10 08:48:04 -06001566 if (allowed > 0)
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001567 png_ptr->num_palette_max = 0;
1568
1569 else
1570 png_ptr->num_palette_max = -1;
1571}
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -06001572#endif
1573
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001574#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */