/src/freeimage-svn/FreeImage/trunk/Source/LibPNG/pngset.c
Line  | Count  | Source  | 
1  |  |  | 
2  |  | /* pngset.c - storage of image information into info struct  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2018-2022 Cosmin Truta  | 
5  |  |  * Copyright (c) 1998-2018 Glenn Randers-Pehrson  | 
6  |  |  * Copyright (c) 1996-1997 Andreas Dilger  | 
7  |  |  * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.  | 
8  |  |  *  | 
9  |  |  * This code is released under the libpng license.  | 
10  |  |  * For conditions of distribution and use, see the disclaimer  | 
11  |  |  * and license in png.h  | 
12  |  |  *  | 
13  |  |  * 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  |  |  */  | 
18  |  |  | 
19  |  | #include "pngpriv.h"  | 
20  |  |  | 
21  |  | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)  | 
22  |  |  | 
23  |  | #ifdef PNG_bKGD_SUPPORTED  | 
24  |  | void PNGAPI  | 
25  |  | png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,  | 
26  |  |     png_const_color_16p background)  | 
27  | 0  | { | 
28  | 0  |    png_debug1(1, "in %s storage function", "bKGD");  | 
29  |  | 
  | 
30  | 0  |    if (png_ptr == NULL || info_ptr == NULL || background == NULL)  | 
31  | 0  |       return;  | 
32  |  |  | 
33  | 0  |    info_ptr->background = *background;  | 
34  | 0  |    info_ptr->valid |= PNG_INFO_bKGD;  | 
35  | 0  | }  | 
36  |  | #endif  | 
37  |  |  | 
38  |  | #ifdef PNG_cHRM_SUPPORTED  | 
39  |  | void PNGFAPI  | 
40  |  | png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,  | 
41  |  |     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)  | 
44  | 0  | { | 
45  | 0  |    png_xy xy;  | 
46  |  | 
  | 
47  | 0  |    png_debug1(1, "in %s storage function", "cHRM fixed");  | 
48  |  | 
  | 
49  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
50  | 0  |       return;  | 
51  |  |  | 
52  | 0  |    xy.redx = red_x;  | 
53  | 0  |    xy.redy = red_y;  | 
54  | 0  |    xy.greenx = green_x;  | 
55  | 0  |    xy.greeny = green_y;  | 
56  | 0  |    xy.bluex = blue_x;  | 
57  | 0  |    xy.bluey = blue_y;  | 
58  | 0  |    xy.whitex = white_x;  | 
59  | 0  |    xy.whitey = white_y;  | 
60  |  | 
  | 
61  | 0  |    if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,  | 
62  | 0  |        2/* override with app values*/) != 0)  | 
63  | 0  |       info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;  | 
64  |  | 
  | 
65  | 0  |    png_colorspace_sync_info(png_ptr, info_ptr);  | 
66  | 0  | }  | 
67  |  |  | 
68  |  | void PNGFAPI  | 
69  |  | png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,  | 
70  |  |     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  | 0  | { | 
76  | 0  |    png_XYZ XYZ;  | 
77  |  | 
  | 
78  | 0  |    png_debug1(1, "in %s storage function", "cHRM XYZ fixed");  | 
79  |  | 
  | 
80  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
81  | 0  |       return;  | 
82  |  |  | 
83  | 0  |    XYZ.red_X = int_red_X;  | 
84  | 0  |    XYZ.red_Y = int_red_Y;  | 
85  | 0  |    XYZ.red_Z = int_red_Z;  | 
86  | 0  |    XYZ.green_X = int_green_X;  | 
87  | 0  |    XYZ.green_Y = int_green_Y;  | 
88  | 0  |    XYZ.green_Z = int_green_Z;  | 
89  | 0  |    XYZ.blue_X = int_blue_X;  | 
90  | 0  |    XYZ.blue_Y = int_blue_Y;  | 
91  | 0  |    XYZ.blue_Z = int_blue_Z;  | 
92  |  | 
  | 
93  | 0  |    if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,  | 
94  | 0  |        &XYZ, 2) != 0)  | 
95  | 0  |       info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;  | 
96  |  | 
  | 
97  | 0  |    png_colorspace_sync_info(png_ptr, info_ptr);  | 
98  | 0  | }  | 
99  |  |  | 
100  |  | #  ifdef PNG_FLOATING_POINT_SUPPORTED  | 
101  |  | void PNGAPI  | 
102  |  | png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,  | 
103  |  |     double white_x, double white_y, double red_x, double red_y,  | 
104  |  |     double green_x, double green_y, double blue_x, double blue_y)  | 
105  | 0  | { | 
106  | 0  |    png_set_cHRM_fixed(png_ptr, info_ptr,  | 
107  | 0  |        png_fixed(png_ptr, white_x, "cHRM White X"),  | 
108  | 0  |        png_fixed(png_ptr, white_y, "cHRM White Y"),  | 
109  | 0  |        png_fixed(png_ptr, red_x, "cHRM Red X"),  | 
110  | 0  |        png_fixed(png_ptr, red_y, "cHRM Red Y"),  | 
111  | 0  |        png_fixed(png_ptr, green_x, "cHRM Green X"),  | 
112  | 0  |        png_fixed(png_ptr, green_y, "cHRM Green Y"),  | 
113  | 0  |        png_fixed(png_ptr, blue_x, "cHRM Blue X"),  | 
114  | 0  |        png_fixed(png_ptr, blue_y, "cHRM Blue Y"));  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | void PNGAPI  | 
118  |  | png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,  | 
119  |  |     double red_Y, double red_Z, double green_X, double green_Y, double green_Z,  | 
120  |  |     double blue_X, double blue_Y, double blue_Z)  | 
121  | 0  | { | 
122  | 0  |    png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,  | 
123  | 0  |        png_fixed(png_ptr, red_X, "cHRM Red X"),  | 
124  | 0  |        png_fixed(png_ptr, red_Y, "cHRM Red Y"),  | 
125  | 0  |        png_fixed(png_ptr, red_Z, "cHRM Red Z"),  | 
126  | 0  |        png_fixed(png_ptr, green_X, "cHRM Green X"),  | 
127  | 0  |        png_fixed(png_ptr, green_Y, "cHRM Green Y"),  | 
128  | 0  |        png_fixed(png_ptr, green_Z, "cHRM Green Z"),  | 
129  | 0  |        png_fixed(png_ptr, blue_X, "cHRM Blue X"),  | 
130  | 0  |        png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),  | 
131  | 0  |        png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));  | 
132  | 0  | }  | 
133  |  | #  endif /* FLOATING_POINT */  | 
134  |  |  | 
135  |  | #endif /* cHRM */  | 
136  |  |  | 
137  |  | #ifdef PNG_eXIf_SUPPORTED  | 
138  |  | void PNGAPI  | 
139  |  | png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,  | 
140  |  |     png_bytep eXIf_buf)  | 
141  |  | { | 
142  |  |   png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");  | 
143  |  |   PNG_UNUSED(info_ptr)  | 
144  |  |   PNG_UNUSED(eXIf_buf)  | 
145  |  | }  | 
146  |  |  | 
147  |  | void PNGAPI  | 
148  |  | png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,  | 
149  |  |     png_uint_32 num_exif, png_bytep eXIf_buf)  | 
150  |  | { | 
151  |  |    int i;  | 
152  |  |  | 
153  |  |    png_debug1(1, "in %s storage function", "eXIf");  | 
154  |  |  | 
155  |  |    if (png_ptr == NULL || info_ptr == NULL)  | 
156  |  |       return;  | 
157  |  |  | 
158  |  |    if (info_ptr->exif)  | 
159  |  |    { | 
160  |  |       png_free(png_ptr, info_ptr->exif);  | 
161  |  |       info_ptr->exif = NULL;  | 
162  |  |    }  | 
163  |  |  | 
164  |  |    info_ptr->num_exif = num_exif;  | 
165  |  |  | 
166  |  |    info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr,  | 
167  |  |        info_ptr->num_exif));  | 
168  |  |  | 
169  |  |    if (info_ptr->exif == NULL)  | 
170  |  |    { | 
171  |  |       png_warning(png_ptr, "Insufficient memory for eXIf chunk data");  | 
172  |  |       return;  | 
173  |  |    }  | 
174  |  |  | 
175  |  |    info_ptr->free_me |= PNG_FREE_EXIF;  | 
176  |  |  | 
177  |  |    for (i = 0; i < (int) info_ptr->num_exif; i++)  | 
178  |  |       info_ptr->exif[i] = eXIf_buf[i];  | 
179  |  |  | 
180  |  |    info_ptr->valid |= PNG_INFO_eXIf;  | 
181  |  | }  | 
182  |  | #endif /* eXIf */  | 
183  |  |  | 
184  |  | #ifdef PNG_gAMA_SUPPORTED  | 
185  |  | void PNGFAPI  | 
186  |  | png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,  | 
187  |  |     png_fixed_point file_gamma)  | 
188  | 0  | { | 
189  | 0  |    png_debug1(1, "in %s storage function", "gAMA");  | 
190  |  | 
  | 
191  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
192  | 0  |       return;  | 
193  |  |  | 
194  | 0  |    png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);  | 
195  | 0  |    png_colorspace_sync_info(png_ptr, info_ptr);  | 
196  | 0  | }  | 
197  |  |  | 
198  |  | #  ifdef PNG_FLOATING_POINT_SUPPORTED  | 
199  |  | void PNGAPI  | 
200  |  | png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)  | 
201  | 0  | { | 
202  | 0  |    png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,  | 
203  | 0  |        "png_set_gAMA"));  | 
204  | 0  | }  | 
205  |  | #  endif  | 
206  |  | #endif  | 
207  |  |  | 
208  |  | #ifdef PNG_hIST_SUPPORTED  | 
209  |  | void PNGAPI  | 
210  |  | png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,  | 
211  |  |     png_const_uint_16p hist)  | 
212  | 0  | { | 
213  | 0  |    int i;  | 
214  |  | 
  | 
215  | 0  |    png_debug1(1, "in %s storage function", "hIST");  | 
216  |  | 
  | 
217  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
218  | 0  |       return;  | 
219  |  |  | 
220  | 0  |    if (info_ptr->num_palette == 0 || info_ptr->num_palette  | 
221  | 0  |        > PNG_MAX_PALETTE_LENGTH)  | 
222  | 0  |    { | 
223  | 0  |       png_warning(png_ptr,  | 
224  | 0  |           "Invalid palette size, hIST allocation skipped");  | 
225  |  | 
  | 
226  | 0  |       return;  | 
227  | 0  |    }  | 
228  |  |  | 
229  | 0  |    png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);  | 
230  |  |  | 
231  |  |    /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in  | 
232  |  |     * version 1.2.1  | 
233  |  |     */  | 
234  | 0  |    info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,  | 
235  | 0  |        PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));  | 
236  |  | 
  | 
237  | 0  |    if (info_ptr->hist == NULL)  | 
238  | 0  |    { | 
239  | 0  |       png_warning(png_ptr, "Insufficient memory for hIST chunk data");  | 
240  |  | 
  | 
241  | 0  |       return;  | 
242  | 0  |    }  | 
243  |  |  | 
244  | 0  |    info_ptr->free_me |= PNG_FREE_HIST;  | 
245  |  | 
  | 
246  | 0  |    for (i = 0; i < info_ptr->num_palette; i++)  | 
247  | 0  |       info_ptr->hist[i] = hist[i];  | 
248  |  | 
  | 
249  | 0  |    info_ptr->valid |= PNG_INFO_hIST;  | 
250  | 0  | }  | 
251  |  | #endif  | 
252  |  |  | 
253  |  | void PNGAPI  | 
254  |  | png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,  | 
255  |  |     png_uint_32 width, png_uint_32 height, int bit_depth,  | 
256  |  |     int color_type, int interlace_type, int compression_type,  | 
257  |  |     int filter_type)  | 
258  | 0  | { | 
259  | 0  |    png_debug1(1, "in %s storage function", "IHDR");  | 
260  |  | 
  | 
261  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
262  | 0  |       return;  | 
263  |  |  | 
264  | 0  |    info_ptr->width = width;  | 
265  | 0  |    info_ptr->height = height;  | 
266  | 0  |    info_ptr->bit_depth = (png_byte)bit_depth;  | 
267  | 0  |    info_ptr->color_type = (png_byte)color_type;  | 
268  | 0  |    info_ptr->compression_type = (png_byte)compression_type;  | 
269  | 0  |    info_ptr->filter_type = (png_byte)filter_type;  | 
270  | 0  |    info_ptr->interlace_type = (png_byte)interlace_type;  | 
271  |  | 
  | 
272  | 0  |    png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,  | 
273  | 0  |        info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,  | 
274  | 0  |        info_ptr->compression_type, info_ptr->filter_type);  | 
275  |  | 
  | 
276  | 0  |    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)  | 
277  | 0  |       info_ptr->channels = 1;  | 
278  |  |  | 
279  | 0  |    else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)  | 
280  | 0  |       info_ptr->channels = 3;  | 
281  |  |  | 
282  | 0  |    else  | 
283  | 0  |       info_ptr->channels = 1;  | 
284  |  | 
  | 
285  | 0  |    if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)  | 
286  | 0  |       info_ptr->channels++;  | 
287  |  | 
  | 
288  | 0  |    info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);  | 
289  |  | 
  | 
290  | 0  |    info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);  | 
291  | 0  | }  | 
292  |  |  | 
293  |  | #ifdef PNG_oFFs_SUPPORTED  | 
294  |  | void PNGAPI  | 
295  |  | png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,  | 
296  |  |     png_int_32 offset_x, png_int_32 offset_y, int unit_type)  | 
297  | 0  | { | 
298  | 0  |    png_debug1(1, "in %s storage function", "oFFs");  | 
299  |  | 
  | 
300  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
301  | 0  |       return;  | 
302  |  |  | 
303  | 0  |    info_ptr->x_offset = offset_x;  | 
304  | 0  |    info_ptr->y_offset = offset_y;  | 
305  | 0  |    info_ptr->offset_unit_type = (png_byte)unit_type;  | 
306  | 0  |    info_ptr->valid |= PNG_INFO_oFFs;  | 
307  | 0  | }  | 
308  |  | #endif  | 
309  |  |  | 
310  |  | #ifdef PNG_pCAL_SUPPORTED  | 
311  |  | void PNGAPI  | 
312  |  | png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,  | 
313  |  |     png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,  | 
314  |  |     int nparams, png_const_charp units, png_charpp params)  | 
315  | 0  | { | 
316  | 0  |    size_t length;  | 
317  | 0  |    int i;  | 
318  |  | 
  | 
319  | 0  |    png_debug1(1, "in %s storage function", "pCAL");  | 
320  |  | 
  | 
321  | 0  |    if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL  | 
322  | 0  |        || (nparams > 0 && params == NULL))  | 
323  | 0  |       return;  | 
324  |  |  | 
325  | 0  |    length = strlen(purpose) + 1;  | 
326  | 0  |    png_debug1(3, "allocating purpose for info (%lu bytes)",  | 
327  | 0  |        (unsigned long)length);  | 
328  |  |  | 
329  |  |    /* TODO: validate format of calibration name and unit name */  | 
330  |  |  | 
331  |  |    /* Check that the type matches the specification. */  | 
332  | 0  |    if (type < 0 || type > 3)  | 
333  | 0  |    { | 
334  | 0  |       png_chunk_report(png_ptr, "Invalid pCAL equation type",  | 
335  | 0  |             PNG_CHUNK_WRITE_ERROR);  | 
336  | 0  |       return;  | 
337  | 0  |    }  | 
338  |  |  | 
339  | 0  |    if (nparams < 0 || nparams > 255)  | 
340  | 0  |    { | 
341  | 0  |       png_chunk_report(png_ptr, "Invalid pCAL parameter count",  | 
342  | 0  |             PNG_CHUNK_WRITE_ERROR);  | 
343  | 0  |       return;  | 
344  | 0  |    }  | 
345  |  |  | 
346  |  |    /* Validate params[nparams] */  | 
347  | 0  |    for (i=0; i<nparams; ++i)  | 
348  | 0  |    { | 
349  | 0  |       if (params[i] == NULL ||  | 
350  | 0  |           !png_check_fp_string(params[i], strlen(params[i])))  | 
351  | 0  |       { | 
352  | 0  |          png_chunk_report(png_ptr, "Invalid format for pCAL parameter",  | 
353  | 0  |                PNG_CHUNK_WRITE_ERROR);  | 
354  | 0  |          return;  | 
355  | 0  |       }  | 
356  | 0  |    }  | 
357  |  |  | 
358  | 0  |    info_ptr->pcal_purpose = png_voidcast(png_charp,  | 
359  | 0  |        png_malloc_warn(png_ptr, length));  | 
360  |  | 
  | 
361  | 0  |    if (info_ptr->pcal_purpose == NULL)  | 
362  | 0  |    { | 
363  | 0  |       png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",  | 
364  | 0  |             PNG_CHUNK_WRITE_ERROR);  | 
365  | 0  |       return;  | 
366  | 0  |    }  | 
367  |  |  | 
368  | 0  |    memcpy(info_ptr->pcal_purpose, purpose, length);  | 
369  |  | 
  | 
370  | 0  |    png_debug(3, "storing X0, X1, type, and nparams in info");  | 
371  | 0  |    info_ptr->pcal_X0 = X0;  | 
372  | 0  |    info_ptr->pcal_X1 = X1;  | 
373  | 0  |    info_ptr->pcal_type = (png_byte)type;  | 
374  | 0  |    info_ptr->pcal_nparams = (png_byte)nparams;  | 
375  |  | 
  | 
376  | 0  |    length = strlen(units) + 1;  | 
377  | 0  |    png_debug1(3, "allocating units for info (%lu bytes)",  | 
378  | 0  |        (unsigned long)length);  | 
379  |  | 
  | 
380  | 0  |    info_ptr->pcal_units = png_voidcast(png_charp,  | 
381  | 0  |        png_malloc_warn(png_ptr, length));  | 
382  |  | 
  | 
383  | 0  |    if (info_ptr->pcal_units == NULL)  | 
384  | 0  |    { | 
385  | 0  |       png_warning(png_ptr, "Insufficient memory for pCAL units");  | 
386  |  | 
  | 
387  | 0  |       return;  | 
388  | 0  |    }  | 
389  |  |  | 
390  | 0  |    memcpy(info_ptr->pcal_units, units, length);  | 
391  |  | 
  | 
392  | 0  |    info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,  | 
393  | 0  |        (size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));  | 
394  |  | 
  | 
395  | 0  |    if (info_ptr->pcal_params == NULL)  | 
396  | 0  |    { | 
397  | 0  |       png_warning(png_ptr, "Insufficient memory for pCAL params");  | 
398  |  | 
  | 
399  | 0  |       return;  | 
400  | 0  |    }  | 
401  |  |  | 
402  | 0  |    memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *  | 
403  | 0  |        (sizeof (png_charp)));  | 
404  |  | 
  | 
405  | 0  |    for (i = 0; i < nparams; i++)  | 
406  | 0  |    { | 
407  | 0  |       length = strlen(params[i]) + 1;  | 
408  | 0  |       png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,  | 
409  | 0  |           (unsigned long)length);  | 
410  |  | 
  | 
411  | 0  |       info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);  | 
412  |  | 
  | 
413  | 0  |       if (info_ptr->pcal_params[i] == NULL)  | 
414  | 0  |       { | 
415  | 0  |          png_warning(png_ptr, "Insufficient memory for pCAL parameter");  | 
416  |  | 
  | 
417  | 0  |          return;  | 
418  | 0  |       }  | 
419  |  |  | 
420  | 0  |       memcpy(info_ptr->pcal_params[i], params[i], length);  | 
421  | 0  |    }  | 
422  |  |  | 
423  | 0  |    info_ptr->valid |= PNG_INFO_pCAL;  | 
424  | 0  |    info_ptr->free_me |= PNG_FREE_PCAL;  | 
425  | 0  | }  | 
426  |  | #endif  | 
427  |  |  | 
428  |  | #ifdef PNG_sCAL_SUPPORTED  | 
429  |  | void PNGAPI  | 
430  |  | png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,  | 
431  |  |     int unit, png_const_charp swidth, png_const_charp sheight)  | 
432  | 0  | { | 
433  | 0  |    size_t lengthw = 0, lengthh = 0;  | 
434  |  | 
  | 
435  | 0  |    png_debug1(1, "in %s storage function", "sCAL");  | 
436  |  | 
  | 
437  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
438  | 0  |       return;  | 
439  |  |  | 
440  |  |    /* Double check the unit (should never get here with an invalid  | 
441  |  |     * unit unless this is an API call.)  | 
442  |  |     */  | 
443  | 0  |    if (unit != 1 && unit != 2)  | 
444  | 0  |       png_error(png_ptr, "Invalid sCAL unit");  | 
445  |  |  | 
446  | 0  |    if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||  | 
447  | 0  |        swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))  | 
448  | 0  |       png_error(png_ptr, "Invalid sCAL width");  | 
449  |  |  | 
450  | 0  |    if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||  | 
451  | 0  |        sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))  | 
452  | 0  |       png_error(png_ptr, "Invalid sCAL height");  | 
453  |  |  | 
454  | 0  |    info_ptr->scal_unit = (png_byte)unit;  | 
455  |  | 
  | 
456  | 0  |    ++lengthw;  | 
457  |  | 
  | 
458  | 0  |    png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);  | 
459  |  | 
  | 
460  | 0  |    info_ptr->scal_s_width = png_voidcast(png_charp,  | 
461  | 0  |        png_malloc_warn(png_ptr, lengthw));  | 
462  |  | 
  | 
463  | 0  |    if (info_ptr->scal_s_width == NULL)  | 
464  | 0  |    { | 
465  | 0  |       png_warning(png_ptr, "Memory allocation failed while processing sCAL");  | 
466  |  | 
  | 
467  | 0  |       return;  | 
468  | 0  |    }  | 
469  |  |  | 
470  | 0  |    memcpy(info_ptr->scal_s_width, swidth, lengthw);  | 
471  |  | 
  | 
472  | 0  |    ++lengthh;  | 
473  |  | 
  | 
474  | 0  |    png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);  | 
475  |  | 
  | 
476  | 0  |    info_ptr->scal_s_height = png_voidcast(png_charp,  | 
477  | 0  |        png_malloc_warn(png_ptr, lengthh));  | 
478  |  | 
  | 
479  | 0  |    if (info_ptr->scal_s_height == NULL)  | 
480  | 0  |    { | 
481  | 0  |       png_free (png_ptr, info_ptr->scal_s_width);  | 
482  | 0  |       info_ptr->scal_s_width = NULL;  | 
483  |  | 
  | 
484  | 0  |       png_warning(png_ptr, "Memory allocation failed while processing sCAL");  | 
485  |  | 
  | 
486  | 0  |       return;  | 
487  | 0  |    }  | 
488  |  |  | 
489  | 0  |    memcpy(info_ptr->scal_s_height, sheight, lengthh);  | 
490  |  | 
  | 
491  | 0  |    info_ptr->valid |= PNG_INFO_sCAL;  | 
492  | 0  |    info_ptr->free_me |= PNG_FREE_SCAL;  | 
493  | 0  | }  | 
494  |  |  | 
495  |  | #  ifdef PNG_FLOATING_POINT_SUPPORTED  | 
496  |  | void PNGAPI  | 
497  |  | png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,  | 
498  |  |     double width, double height)  | 
499  | 0  | { | 
500  | 0  |    png_debug1(1, "in %s storage function", "sCAL");  | 
501  |  |  | 
502  |  |    /* Check the arguments. */  | 
503  | 0  |    if (width <= 0)  | 
504  | 0  |       png_warning(png_ptr, "Invalid sCAL width ignored");  | 
505  |  |  | 
506  | 0  |    else if (height <= 0)  | 
507  | 0  |       png_warning(png_ptr, "Invalid sCAL height ignored");  | 
508  |  |  | 
509  | 0  |    else  | 
510  | 0  |    { | 
511  |  |       /* Convert 'width' and 'height' to ASCII. */  | 
512  | 0  |       char swidth[PNG_sCAL_MAX_DIGITS+1];  | 
513  | 0  |       char sheight[PNG_sCAL_MAX_DIGITS+1];  | 
514  |  | 
  | 
515  | 0  |       png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,  | 
516  | 0  |           PNG_sCAL_PRECISION);  | 
517  | 0  |       png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,  | 
518  | 0  |           PNG_sCAL_PRECISION);  | 
519  |  | 
  | 
520  | 0  |       png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);  | 
521  | 0  |    }  | 
522  | 0  | }  | 
523  |  | #  endif  | 
524  |  |  | 
525  |  | #  ifdef PNG_FIXED_POINT_SUPPORTED  | 
526  |  | void PNGAPI  | 
527  |  | png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,  | 
528  |  |     png_fixed_point width, png_fixed_point height)  | 
529  | 0  | { | 
530  | 0  |    png_debug1(1, "in %s storage function", "sCAL");  | 
531  |  |  | 
532  |  |    /* Check the arguments. */  | 
533  | 0  |    if (width <= 0)  | 
534  | 0  |       png_warning(png_ptr, "Invalid sCAL width ignored");  | 
535  |  |  | 
536  | 0  |    else if (height <= 0)  | 
537  | 0  |       png_warning(png_ptr, "Invalid sCAL height ignored");  | 
538  |  |  | 
539  | 0  |    else  | 
540  | 0  |    { | 
541  |  |       /* Convert 'width' and 'height' to ASCII. */  | 
542  | 0  |       char swidth[PNG_sCAL_MAX_DIGITS+1];  | 
543  | 0  |       char sheight[PNG_sCAL_MAX_DIGITS+1];  | 
544  |  | 
  | 
545  | 0  |       png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);  | 
546  | 0  |       png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);  | 
547  |  | 
  | 
548  | 0  |       png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);  | 
549  | 0  |    }  | 
550  | 0  | }  | 
551  |  | #  endif  | 
552  |  | #endif  | 
553  |  |  | 
554  |  | #ifdef PNG_pHYs_SUPPORTED  | 
555  |  | void PNGAPI  | 
556  |  | png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,  | 
557  |  |     png_uint_32 res_x, png_uint_32 res_y, int unit_type)  | 
558  | 0  | { | 
559  | 0  |    png_debug1(1, "in %s storage function", "pHYs");  | 
560  |  | 
  | 
561  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
562  | 0  |       return;  | 
563  |  |  | 
564  | 0  |    info_ptr->x_pixels_per_unit = res_x;  | 
565  | 0  |    info_ptr->y_pixels_per_unit = res_y;  | 
566  | 0  |    info_ptr->phys_unit_type = (png_byte)unit_type;  | 
567  | 0  |    info_ptr->valid |= PNG_INFO_pHYs;  | 
568  | 0  | }  | 
569  |  | #endif  | 
570  |  |  | 
571  |  | void PNGAPI  | 
572  |  | png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,  | 
573  |  |     png_const_colorp palette, int num_palette)  | 
574  | 0  | { | 
575  |  | 
  | 
576  | 0  |    png_uint_32 max_palette_length;  | 
577  |  | 
  | 
578  | 0  |    png_debug1(1, "in %s storage function", "PLTE");  | 
579  |  | 
  | 
580  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
581  | 0  |       return;  | 
582  |  |  | 
583  | 0  |    max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?  | 
584  | 0  |       (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;  | 
585  |  | 
  | 
586  | 0  |    if (num_palette < 0 || num_palette > (int) max_palette_length)  | 
587  | 0  |    { | 
588  | 0  |       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)  | 
589  | 0  |          png_error(png_ptr, "Invalid palette length");  | 
590  |  |  | 
591  | 0  |       else  | 
592  | 0  |       { | 
593  | 0  |          png_warning(png_ptr, "Invalid palette length");  | 
594  |  | 
  | 
595  | 0  |          return;  | 
596  | 0  |       }  | 
597  | 0  |    }  | 
598  |  |  | 
599  | 0  |    if ((num_palette > 0 && palette == NULL) ||  | 
600  | 0  |       (num_palette == 0  | 
601  | 0  | #        ifdef PNG_MNG_FEATURES_SUPPORTED  | 
602  | 0  |             && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0  | 
603  | 0  | #        endif  | 
604  | 0  |       ))  | 
605  | 0  |    { | 
606  | 0  |       png_error(png_ptr, "Invalid palette");  | 
607  | 0  |    }  | 
608  |  |  | 
609  |  |    /* It may not actually be necessary to set png_ptr->palette here;  | 
610  |  |     * we do it for backward compatibility with the way the png_handle_tRNS  | 
611  |  |     * function used to do the allocation.  | 
612  |  |     *  | 
613  |  |     * 1.6.0: the above statement appears to be incorrect; something has to set  | 
614  |  |     * the palette inside png_struct on read.  | 
615  |  |     */  | 
616  | 0  |    png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);  | 
617  |  |  | 
618  |  |    /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead  | 
619  |  |     * of num_palette entries, in case of an invalid PNG file or incorrect  | 
620  |  |     * call to png_set_PLTE() with too-large sample values.  | 
621  |  |     */  | 
622  | 0  |    png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,  | 
623  | 0  |        PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));  | 
624  |  | 
  | 
625  | 0  |    if (num_palette > 0)  | 
626  | 0  |       memcpy(png_ptr->palette, palette, (unsigned int)num_palette *  | 
627  | 0  |           (sizeof (png_color)));  | 
628  | 0  |    info_ptr->palette = png_ptr->palette;  | 
629  | 0  |    info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;  | 
630  |  | 
  | 
631  | 0  |    info_ptr->free_me |= PNG_FREE_PLTE;  | 
632  |  | 
  | 
633  | 0  |    info_ptr->valid |= PNG_INFO_PLTE;  | 
634  | 0  | }  | 
635  |  |  | 
636  |  | #ifdef PNG_sBIT_SUPPORTED  | 
637  |  | void PNGAPI  | 
638  |  | png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,  | 
639  |  |     png_const_color_8p sig_bit)  | 
640  | 0  | { | 
641  | 0  |    png_debug1(1, "in %s storage function", "sBIT");  | 
642  |  | 
  | 
643  | 0  |    if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)  | 
644  | 0  |       return;  | 
645  |  |  | 
646  | 0  |    info_ptr->sig_bit = *sig_bit;  | 
647  | 0  |    info_ptr->valid |= PNG_INFO_sBIT;  | 
648  | 0  | }  | 
649  |  | #endif  | 
650  |  |  | 
651  |  | #ifdef PNG_sRGB_SUPPORTED  | 
652  |  | void PNGAPI  | 
653  |  | png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)  | 
654  | 0  | { | 
655  | 0  |    png_debug1(1, "in %s storage function", "sRGB");  | 
656  |  | 
  | 
657  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
658  | 0  |       return;  | 
659  |  |  | 
660  | 0  |    (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);  | 
661  | 0  |    png_colorspace_sync_info(png_ptr, info_ptr);  | 
662  | 0  | }  | 
663  |  |  | 
664  |  | void PNGAPI  | 
665  |  | png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,  | 
666  |  |     int srgb_intent)  | 
667  | 0  | { | 
668  | 0  |    png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");  | 
669  |  | 
  | 
670  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
671  | 0  |       return;  | 
672  |  |  | 
673  | 0  |    if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,  | 
674  | 0  |        srgb_intent) != 0)  | 
675  | 0  |    { | 
676  |  |       /* This causes the gAMA and cHRM to be written too */  | 
677  | 0  |       info_ptr->colorspace.flags |=  | 
678  | 0  |          PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;  | 
679  | 0  |    }  | 
680  |  | 
  | 
681  | 0  |    png_colorspace_sync_info(png_ptr, info_ptr);  | 
682  | 0  | }  | 
683  |  | #endif /* sRGB */  | 
684  |  |  | 
685  |  |  | 
686  |  | #ifdef PNG_iCCP_SUPPORTED  | 
687  |  | void PNGAPI  | 
688  |  | png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,  | 
689  |  |     png_const_charp name, int compression_type,  | 
690  |  |     png_const_bytep profile, png_uint_32 proflen)  | 
691  | 0  | { | 
692  | 0  |    png_charp new_iccp_name;  | 
693  | 0  |    png_bytep new_iccp_profile;  | 
694  | 0  |    size_t length;  | 
695  |  | 
  | 
696  | 0  |    png_debug1(1, "in %s storage function", "iCCP");  | 
697  |  | 
  | 
698  | 0  |    if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)  | 
699  | 0  |       return;  | 
700  |  |  | 
701  | 0  |    if (compression_type != PNG_COMPRESSION_TYPE_BASE)  | 
702  | 0  |       png_app_error(png_ptr, "Invalid iCCP compression method");  | 
703  |  |  | 
704  |  |    /* Set the colorspace first because this validates the profile; do not  | 
705  |  |     * override previously set app cHRM or gAMA here (because likely as not the  | 
706  |  |     * application knows better than libpng what the correct values are.)  Pass  | 
707  |  |     * the info_ptr color_type field to png_colorspace_set_ICC because in the  | 
708  |  |     * write case it has not yet been stored in png_ptr.  | 
709  |  |     */  | 
710  | 0  |    { | 
711  | 0  |       int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,  | 
712  | 0  |           proflen, profile, info_ptr->color_type);  | 
713  |  | 
  | 
714  | 0  |       png_colorspace_sync_info(png_ptr, info_ptr);  | 
715  |  |  | 
716  |  |       /* Don't do any of the copying if the profile was bad, or inconsistent. */  | 
717  | 0  |       if (result == 0)  | 
718  | 0  |          return;  | 
719  |  |  | 
720  |  |       /* But do write the gAMA and cHRM chunks from the profile. */  | 
721  | 0  |       info_ptr->colorspace.flags |=  | 
722  | 0  |          PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;  | 
723  | 0  |    }  | 
724  |  |  | 
725  | 0  |    length = strlen(name)+1;  | 
726  | 0  |    new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));  | 
727  |  | 
  | 
728  | 0  |    if (new_iccp_name == NULL)  | 
729  | 0  |    { | 
730  | 0  |       png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");  | 
731  |  | 
  | 
732  | 0  |       return;  | 
733  | 0  |    }  | 
734  |  |  | 
735  | 0  |    memcpy(new_iccp_name, name, length);  | 
736  | 0  |    new_iccp_profile = png_voidcast(png_bytep,  | 
737  | 0  |        png_malloc_warn(png_ptr, proflen));  | 
738  |  | 
  | 
739  | 0  |    if (new_iccp_profile == NULL)  | 
740  | 0  |    { | 
741  | 0  |       png_free(png_ptr, new_iccp_name);  | 
742  | 0  |       png_benign_error(png_ptr,  | 
743  | 0  |           "Insufficient memory to process iCCP profile");  | 
744  |  | 
  | 
745  | 0  |       return;  | 
746  | 0  |    }  | 
747  |  |  | 
748  | 0  |    memcpy(new_iccp_profile, profile, proflen);  | 
749  |  | 
  | 
750  | 0  |    png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);  | 
751  |  | 
  | 
752  | 0  |    info_ptr->iccp_proflen = proflen;  | 
753  | 0  |    info_ptr->iccp_name = new_iccp_name;  | 
754  | 0  |    info_ptr->iccp_profile = new_iccp_profile;  | 
755  | 0  |    info_ptr->free_me |= PNG_FREE_ICCP;  | 
756  | 0  |    info_ptr->valid |= PNG_INFO_iCCP;  | 
757  | 0  | }  | 
758  |  | #endif  | 
759  |  |  | 
760  |  | #ifdef PNG_TEXT_SUPPORTED  | 
761  |  | void PNGAPI  | 
762  |  | png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,  | 
763  |  |     png_const_textp text_ptr, int num_text)  | 
764  | 0  | { | 
765  | 0  |    int ret;  | 
766  | 0  |    ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);  | 
767  |  | 
  | 
768  | 0  |    if (ret != 0)  | 
769  | 0  |       png_error(png_ptr, "Insufficient memory to store text");  | 
770  | 0  | }  | 
771  |  |  | 
772  |  | int /* PRIVATE */  | 
773  |  | png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,  | 
774  |  |     png_const_textp text_ptr, int num_text)  | 
775  | 0  | { | 
776  | 0  |    int i;  | 
777  |  | 
  | 
778  | 0  |    png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :  | 
779  | 0  |       (unsigned long)png_ptr->chunk_name);  | 
780  |  | 
  | 
781  | 0  |    if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)  | 
782  | 0  |       return(0);  | 
783  |  |  | 
784  |  |    /* Make sure we have enough space in the "text" array in info_struct  | 
785  |  |     * to hold all of the incoming text_ptr objects.  This compare can't overflow  | 
786  |  |     * because max_text >= num_text (anyway, subtract of two positive integers  | 
787  |  |     * can't overflow in any case.)  | 
788  |  |     */  | 
789  | 0  |    if (num_text > info_ptr->max_text - info_ptr->num_text)  | 
790  | 0  |    { | 
791  | 0  |       int old_num_text = info_ptr->num_text;  | 
792  | 0  |       int max_text;  | 
793  | 0  |       png_textp new_text = NULL;  | 
794  |  |  | 
795  |  |       /* Calculate an appropriate max_text, checking for overflow. */  | 
796  | 0  |       max_text = old_num_text;  | 
797  | 0  |       if (num_text <= INT_MAX - max_text)  | 
798  | 0  |       { | 
799  | 0  |          max_text += num_text;  | 
800  |  |  | 
801  |  |          /* Round up to a multiple of 8 */  | 
802  | 0  |          if (max_text < INT_MAX-8)  | 
803  | 0  |             max_text = (max_text + 8) & ~0x7;  | 
804  |  |  | 
805  | 0  |          else  | 
806  | 0  |             max_text = INT_MAX;  | 
807  |  |  | 
808  |  |          /* Now allocate a new array and copy the old members in; this does all  | 
809  |  |           * the overflow checks.  | 
810  |  |           */  | 
811  | 0  |          new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,  | 
812  | 0  |              info_ptr->text, old_num_text, max_text-old_num_text,  | 
813  | 0  |              sizeof *new_text));  | 
814  | 0  |       }  | 
815  |  | 
  | 
816  | 0  |       if (new_text == NULL)  | 
817  | 0  |       { | 
818  | 0  |          png_chunk_report(png_ptr, "too many text chunks",  | 
819  | 0  |              PNG_CHUNK_WRITE_ERROR);  | 
820  |  | 
  | 
821  | 0  |          return 1;  | 
822  | 0  |       }  | 
823  |  |  | 
824  | 0  |       png_free(png_ptr, info_ptr->text);  | 
825  |  | 
  | 
826  | 0  |       info_ptr->text = new_text;  | 
827  | 0  |       info_ptr->free_me |= PNG_FREE_TEXT;  | 
828  | 0  |       info_ptr->max_text = max_text;  | 
829  |  |       /* num_text is adjusted below as the entries are copied in */  | 
830  |  | 
  | 
831  | 0  |       png_debug1(3, "allocated %d entries for info_ptr->text", max_text);  | 
832  | 0  |    }  | 
833  |  |  | 
834  | 0  |    for (i = 0; i < num_text; i++)  | 
835  | 0  |    { | 
836  | 0  |       size_t text_length, key_len;  | 
837  | 0  |       size_t lang_len, lang_key_len;  | 
838  | 0  |       png_textp textp = &(info_ptr->text[info_ptr->num_text]);  | 
839  |  | 
  | 
840  | 0  |       if (text_ptr[i].key == NULL)  | 
841  | 0  |           continue;  | 
842  |  |  | 
843  | 0  |       if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||  | 
844  | 0  |           text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)  | 
845  | 0  |       { | 
846  | 0  |          png_chunk_report(png_ptr, "text compression mode is out of range",  | 
847  | 0  |              PNG_CHUNK_WRITE_ERROR);  | 
848  | 0  |          continue;  | 
849  | 0  |       }  | 
850  |  |  | 
851  | 0  |       key_len = strlen(text_ptr[i].key);  | 
852  |  | 
  | 
853  | 0  |       if (text_ptr[i].compression <= 0)  | 
854  | 0  |       { | 
855  | 0  |          lang_len = 0;  | 
856  | 0  |          lang_key_len = 0;  | 
857  | 0  |       }  | 
858  |  |  | 
859  | 0  |       else  | 
860  | 0  | #  ifdef PNG_iTXt_SUPPORTED  | 
861  | 0  |       { | 
862  |  |          /* Set iTXt data */  | 
863  |  | 
  | 
864  | 0  |          if (text_ptr[i].lang != NULL)  | 
865  | 0  |             lang_len = strlen(text_ptr[i].lang);  | 
866  |  |  | 
867  | 0  |          else  | 
868  | 0  |             lang_len = 0;  | 
869  |  | 
  | 
870  | 0  |          if (text_ptr[i].lang_key != NULL)  | 
871  | 0  |             lang_key_len = strlen(text_ptr[i].lang_key);  | 
872  |  |  | 
873  | 0  |          else  | 
874  | 0  |             lang_key_len = 0;  | 
875  | 0  |       }  | 
876  |  | #  else /* iTXt */  | 
877  |  |       { | 
878  |  |          png_chunk_report(png_ptr, "iTXt chunk not supported",  | 
879  |  |              PNG_CHUNK_WRITE_ERROR);  | 
880  |  |          continue;  | 
881  |  |       }  | 
882  |  | #  endif  | 
883  |  | 
  | 
884  | 0  |       if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')  | 
885  | 0  |       { | 
886  | 0  |          text_length = 0;  | 
887  | 0  | #  ifdef PNG_iTXt_SUPPORTED  | 
888  | 0  |          if (text_ptr[i].compression > 0)  | 
889  | 0  |             textp->compression = PNG_ITXT_COMPRESSION_NONE;  | 
890  |  |  | 
891  | 0  |          else  | 
892  | 0  | #  endif  | 
893  | 0  |             textp->compression = PNG_TEXT_COMPRESSION_NONE;  | 
894  | 0  |       }  | 
895  |  |  | 
896  | 0  |       else  | 
897  | 0  |       { | 
898  | 0  |          text_length = strlen(text_ptr[i].text);  | 
899  | 0  |          textp->compression = text_ptr[i].compression;  | 
900  | 0  |       }  | 
901  |  | 
  | 
902  | 0  |       textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,  | 
903  | 0  |           key_len + text_length + lang_len + lang_key_len + 4));  | 
904  |  | 
  | 
905  | 0  |       if (textp->key == NULL)  | 
906  | 0  |       { | 
907  | 0  |          png_chunk_report(png_ptr, "text chunk: out of memory",  | 
908  | 0  |              PNG_CHUNK_WRITE_ERROR);  | 
909  |  | 
  | 
910  | 0  |          return 1;  | 
911  | 0  |       }  | 
912  |  |  | 
913  | 0  |       png_debug2(2, "Allocated %lu bytes at %p in png_set_text",  | 
914  | 0  |           (unsigned long)(png_uint_32)  | 
915  | 0  |           (key_len + lang_len + lang_key_len + text_length + 4),  | 
916  | 0  |           textp->key);  | 
917  |  | 
  | 
918  | 0  |       memcpy(textp->key, text_ptr[i].key, key_len);  | 
919  | 0  |       *(textp->key + key_len) = '\0';  | 
920  |  | 
  | 
921  | 0  |       if (text_ptr[i].compression > 0)  | 
922  | 0  |       { | 
923  | 0  |          textp->lang = textp->key + key_len + 1;  | 
924  | 0  |          memcpy(textp->lang, text_ptr[i].lang, lang_len);  | 
925  | 0  |          *(textp->lang + lang_len) = '\0';  | 
926  | 0  |          textp->lang_key = textp->lang + lang_len + 1;  | 
927  | 0  |          memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);  | 
928  | 0  |          *(textp->lang_key + lang_key_len) = '\0';  | 
929  | 0  |          textp->text = textp->lang_key + lang_key_len + 1;  | 
930  | 0  |       }  | 
931  |  |  | 
932  | 0  |       else  | 
933  | 0  |       { | 
934  | 0  |          textp->lang=NULL;  | 
935  | 0  |          textp->lang_key=NULL;  | 
936  | 0  |          textp->text = textp->key + key_len + 1;  | 
937  | 0  |       }  | 
938  |  | 
  | 
939  | 0  |       if (text_length != 0)  | 
940  | 0  |          memcpy(textp->text, text_ptr[i].text, text_length);  | 
941  |  | 
  | 
942  | 0  |       *(textp->text + text_length) = '\0';  | 
943  |  | 
  | 
944  | 0  | #  ifdef PNG_iTXt_SUPPORTED  | 
945  | 0  |       if (textp->compression > 0)  | 
946  | 0  |       { | 
947  | 0  |          textp->text_length = 0;  | 
948  | 0  |          textp->itxt_length = text_length;  | 
949  | 0  |       }  | 
950  |  |  | 
951  | 0  |       else  | 
952  | 0  | #  endif  | 
953  | 0  |       { | 
954  | 0  |          textp->text_length = text_length;  | 
955  | 0  |          textp->itxt_length = 0;  | 
956  | 0  |       }  | 
957  |  | 
  | 
958  | 0  |       info_ptr->num_text++;  | 
959  | 0  |       png_debug1(3, "transferred text chunk %d", info_ptr->num_text);  | 
960  | 0  |    }  | 
961  |  |  | 
962  | 0  |    return(0);  | 
963  | 0  | }  | 
964  |  | #endif  | 
965  |  |  | 
966  |  | #ifdef PNG_tIME_SUPPORTED  | 
967  |  | void PNGAPI  | 
968  |  | png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,  | 
969  |  |     png_const_timep mod_time)  | 
970  | 0  | { | 
971  | 0  |    png_debug1(1, "in %s storage function", "tIME");  | 
972  |  | 
  | 
973  | 0  |    if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||  | 
974  | 0  |        (png_ptr->mode & PNG_WROTE_tIME) != 0)  | 
975  | 0  |       return;  | 
976  |  |  | 
977  | 0  |    if (mod_time->month == 0   || mod_time->month > 12  ||  | 
978  | 0  |        mod_time->day   == 0   || mod_time->day   > 31  ||  | 
979  | 0  |        mod_time->hour  > 23   || mod_time->minute > 59 ||  | 
980  | 0  |        mod_time->second > 60)  | 
981  | 0  |    { | 
982  | 0  |       png_warning(png_ptr, "Ignoring invalid time value");  | 
983  |  | 
  | 
984  | 0  |       return;  | 
985  | 0  |    }  | 
986  |  |  | 
987  | 0  |    info_ptr->mod_time = *mod_time;  | 
988  | 0  |    info_ptr->valid |= PNG_INFO_tIME;  | 
989  | 0  | }  | 
990  |  | #endif  | 
991  |  |  | 
992  |  | #ifdef PNG_tRNS_SUPPORTED  | 
993  |  | void PNGAPI  | 
994  |  | png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,  | 
995  |  |     png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)  | 
996  | 0  | { | 
997  | 0  |    png_debug1(1, "in %s storage function", "tRNS");  | 
998  |  | 
  | 
999  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
1000  |  |  | 
1001  | 0  |       return;  | 
1002  |  |  | 
1003  | 0  |    if (trans_alpha != NULL)  | 
1004  | 0  |    { | 
1005  |  |        /* It may not actually be necessary to set png_ptr->trans_alpha here;  | 
1006  |  |         * we do it for backward compatibility with the way the png_handle_tRNS  | 
1007  |  |         * function used to do the allocation.  | 
1008  |  |         *  | 
1009  |  |         * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively  | 
1010  |  |         * relies on png_set_tRNS storing the information in png_struct  | 
1011  |  |         * (otherwise it won't be there for the code in pngrtran.c).  | 
1012  |  |         */  | 
1013  |  | 
  | 
1014  | 0  |        png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);  | 
1015  |  | 
  | 
1016  | 0  |        if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)  | 
1017  | 0  |        { | 
1018  |  |          /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */  | 
1019  | 0  |           info_ptr->trans_alpha = png_voidcast(png_bytep,  | 
1020  | 0  |               png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));  | 
1021  | 0  |           memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);  | 
1022  |  | 
  | 
1023  | 0  |           info_ptr->valid |= PNG_INFO_tRNS;  | 
1024  | 0  |           info_ptr->free_me |= PNG_FREE_TRNS;  | 
1025  | 0  |        }  | 
1026  | 0  |        png_ptr->trans_alpha = info_ptr->trans_alpha;  | 
1027  | 0  |    }  | 
1028  |  | 
  | 
1029  | 0  |    if (trans_color != NULL)  | 
1030  | 0  |    { | 
1031  | 0  | #ifdef PNG_WARNINGS_SUPPORTED  | 
1032  | 0  |       if (info_ptr->bit_depth < 16)  | 
1033  | 0  |       { | 
1034  | 0  |          int sample_max = (1 << info_ptr->bit_depth) - 1;  | 
1035  |  | 
  | 
1036  | 0  |          if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&  | 
1037  | 0  |              trans_color->gray > sample_max) ||  | 
1038  | 0  |              (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&  | 
1039  | 0  |              (trans_color->red > sample_max ||  | 
1040  | 0  |              trans_color->green > sample_max ||  | 
1041  | 0  |              trans_color->blue > sample_max)))  | 
1042  | 0  |             png_warning(png_ptr,  | 
1043  | 0  |                 "tRNS chunk has out-of-range samples for bit_depth");  | 
1044  | 0  |       }  | 
1045  | 0  | #endif  | 
1046  |  | 
  | 
1047  | 0  |       info_ptr->trans_color = *trans_color;  | 
1048  |  | 
  | 
1049  | 0  |       if (num_trans == 0)  | 
1050  | 0  |          num_trans = 1;  | 
1051  | 0  |    }  | 
1052  |  | 
  | 
1053  | 0  |    info_ptr->num_trans = (png_uint_16)num_trans;  | 
1054  |  | 
  | 
1055  | 0  |    if (num_trans != 0)  | 
1056  | 0  |    { | 
1057  | 0  |       info_ptr->valid |= PNG_INFO_tRNS;  | 
1058  | 0  |       info_ptr->free_me |= PNG_FREE_TRNS;  | 
1059  | 0  |    }  | 
1060  | 0  | }  | 
1061  |  | #endif  | 
1062  |  |  | 
1063  |  | #ifdef PNG_sPLT_SUPPORTED  | 
1064  |  | void PNGAPI  | 
1065  |  | png_set_sPLT(png_const_structrp png_ptr,  | 
1066  |  |     png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)  | 
1067  |  | /*  | 
1068  |  |  *  entries        - array of png_sPLT_t structures  | 
1069  |  |  *                   to be added to the list of palettes  | 
1070  |  |  *                   in the info structure.  | 
1071  |  |  *  | 
1072  |  |  *  nentries       - number of palette structures to be  | 
1073  |  |  *                   added.  | 
1074  |  |  */  | 
1075  | 0  | { | 
1076  | 0  |    png_sPLT_tp np;  | 
1077  |  | 
  | 
1078  | 0  |    if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)  | 
1079  | 0  |       return;  | 
1080  |  |  | 
1081  |  |    /* Use the internal realloc function, which checks for all the possible  | 
1082  |  |     * overflows.  Notice that the parameters are (int) and (size_t)  | 
1083  |  |     */  | 
1084  | 0  |    np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,  | 
1085  | 0  |        info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,  | 
1086  | 0  |        sizeof *np));  | 
1087  |  | 
  | 
1088  | 0  |    if (np == NULL)  | 
1089  | 0  |    { | 
1090  |  |       /* Out of memory or too many chunks */  | 
1091  | 0  |       png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);  | 
1092  |  | 
  | 
1093  | 0  |       return;  | 
1094  | 0  |    }  | 
1095  |  |  | 
1096  | 0  |    png_free(png_ptr, info_ptr->splt_palettes);  | 
1097  | 0  |    info_ptr->splt_palettes = np;  | 
1098  | 0  |    info_ptr->free_me |= PNG_FREE_SPLT;  | 
1099  |  | 
  | 
1100  | 0  |    np += info_ptr->splt_palettes_num;  | 
1101  |  | 
  | 
1102  | 0  |    do  | 
1103  | 0  |    { | 
1104  | 0  |       size_t length;  | 
1105  |  |  | 
1106  |  |       /* Skip invalid input entries */  | 
1107  | 0  |       if (entries->name == NULL || entries->entries == NULL)  | 
1108  | 0  |       { | 
1109  |  |          /* png_handle_sPLT doesn't do this, so this is an app error */  | 
1110  | 0  |          png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");  | 
1111  |  |          /* Just skip the invalid entry */  | 
1112  | 0  |          continue;  | 
1113  | 0  |       }  | 
1114  |  |  | 
1115  | 0  |       np->depth = entries->depth;  | 
1116  |  |  | 
1117  |  |       /* In the event of out-of-memory just return - there's no point keeping  | 
1118  |  |        * on trying to add sPLT chunks.  | 
1119  |  |        */  | 
1120  | 0  |       length = strlen(entries->name) + 1;  | 
1121  | 0  |       np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));  | 
1122  |  | 
  | 
1123  | 0  |       if (np->name == NULL)  | 
1124  | 0  |          break;  | 
1125  |  |  | 
1126  | 0  |       memcpy(np->name, entries->name, length);  | 
1127  |  |  | 
1128  |  |       /* IMPORTANT: we have memory now that won't get freed if something else  | 
1129  |  |        * goes wrong; this code must free it.  png_malloc_array produces no  | 
1130  |  |        * warnings; use a png_chunk_report (below) if there is an error.  | 
1131  |  |        */  | 
1132  | 0  |       np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,  | 
1133  | 0  |           entries->nentries, sizeof (png_sPLT_entry)));  | 
1134  |  | 
  | 
1135  | 0  |       if (np->entries == NULL)  | 
1136  | 0  |       { | 
1137  | 0  |          png_free(png_ptr, np->name);  | 
1138  | 0  |          np->name = NULL;  | 
1139  | 0  |          break;  | 
1140  | 0  |       }  | 
1141  |  |  | 
1142  | 0  |       np->nentries = entries->nentries;  | 
1143  |  |       /* This multiply can't overflow because png_malloc_array has already  | 
1144  |  |        * checked it when doing the allocation.  | 
1145  |  |        */  | 
1146  | 0  |       memcpy(np->entries, entries->entries,  | 
1147  | 0  |           (unsigned int)entries->nentries * sizeof (png_sPLT_entry));  | 
1148  |  |  | 
1149  |  |       /* Note that 'continue' skips the advance of the out pointer and out  | 
1150  |  |        * count, so an invalid entry is not added.  | 
1151  |  |        */  | 
1152  | 0  |       info_ptr->valid |= PNG_INFO_sPLT;  | 
1153  | 0  |       ++(info_ptr->splt_palettes_num);  | 
1154  | 0  |       ++np;  | 
1155  | 0  |       ++entries;  | 
1156  | 0  |    }  | 
1157  | 0  |    while (--nentries);  | 
1158  |  | 
  | 
1159  | 0  |    if (nentries > 0)  | 
1160  | 0  |       png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);  | 
1161  | 0  | }  | 
1162  |  | #endif /* sPLT */  | 
1163  |  |  | 
1164  |  | #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED  | 
1165  |  | static png_byte  | 
1166  |  | check_location(png_const_structrp png_ptr, int location)  | 
1167  | 0  | { | 
1168  | 0  |    location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);  | 
1169  |  |  | 
1170  |  |    /* New in 1.6.0; copy the location and check it.  This is an API  | 
1171  |  |     * change; previously the app had to use the  | 
1172  |  |     * png_set_unknown_chunk_location API below for each chunk.  | 
1173  |  |     */  | 
1174  | 0  |    if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)  | 
1175  | 0  |    { | 
1176  |  |       /* Write struct, so unknown chunks come from the app */  | 
1177  | 0  |       png_app_warning(png_ptr,  | 
1178  | 0  |           "png_set_unknown_chunks now expects a valid location");  | 
1179  |  |       /* Use the old behavior */  | 
1180  | 0  |       location = (png_byte)(png_ptr->mode &  | 
1181  | 0  |           (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));  | 
1182  | 0  |    }  | 
1183  |  |  | 
1184  |  |    /* This need not be an internal error - if the app calls  | 
1185  |  |     * png_set_unknown_chunks on a read pointer it must get the location right.  | 
1186  |  |     */  | 
1187  | 0  |    if (location == 0)  | 
1188  | 0  |       png_error(png_ptr, "invalid location in png_set_unknown_chunks");  | 
1189  |  |  | 
1190  |  |    /* Now reduce the location to the top-most set bit by removing each least  | 
1191  |  |     * significant bit in turn.  | 
1192  |  |     */  | 
1193  | 0  |    while (location != (location & -location))  | 
1194  | 0  |       location &= ~(location & -location);  | 
1195  |  |  | 
1196  |  |    /* The cast is safe because 'location' is a bit mask and only the low four  | 
1197  |  |     * bits are significant.  | 
1198  |  |     */  | 
1199  | 0  |    return (png_byte)location;  | 
1200  | 0  | }  | 
1201  |  |  | 
1202  |  | void PNGAPI  | 
1203  |  | png_set_unknown_chunks(png_const_structrp png_ptr,  | 
1204  |  |     png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)  | 
1205  | 0  | { | 
1206  | 0  |    png_unknown_chunkp np;  | 
1207  |  | 
  | 
1208  | 0  |    if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||  | 
1209  | 0  |        unknowns == NULL)  | 
1210  | 0  |       return;  | 
1211  |  |  | 
1212  |  |    /* Check for the failure cases where support has been disabled at compile  | 
1213  |  |     * time.  This code is hardly ever compiled - it's here because  | 
1214  |  |     * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this  | 
1215  |  |     * code) but may be meaningless if the read or write handling of unknown  | 
1216  |  |     * chunks is not compiled in.  | 
1217  |  |     */  | 
1218  |  | #  if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \  | 
1219  |  |       defined(PNG_READ_SUPPORTED)  | 
1220  |  |       if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)  | 
1221  |  |       { | 
1222  |  |          png_app_error(png_ptr, "no unknown chunk support on read");  | 
1223  |  |  | 
1224  |  |          return;  | 
1225  |  |       }  | 
1226  |  | #  endif  | 
1227  |  | #  if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \  | 
1228  |  |       defined(PNG_WRITE_SUPPORTED)  | 
1229  |  |       if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)  | 
1230  |  |       { | 
1231  |  |          png_app_error(png_ptr, "no unknown chunk support on write");  | 
1232  |  |  | 
1233  |  |          return;  | 
1234  |  |       }  | 
1235  |  | #  endif  | 
1236  |  |  | 
1237  |  |    /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that  | 
1238  |  |     * unknown critical chunks could be lost with just a warning resulting in  | 
1239  |  |     * undefined behavior.  Now png_chunk_report is used to provide behavior  | 
1240  |  |     * appropriate to read or write.  | 
1241  |  |     */  | 
1242  | 0  |    np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,  | 
1243  | 0  |        info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,  | 
1244  | 0  |        sizeof *np));  | 
1245  |  | 
  | 
1246  | 0  |    if (np == NULL)  | 
1247  | 0  |    { | 
1248  | 0  |       png_chunk_report(png_ptr, "too many unknown chunks",  | 
1249  | 0  |           PNG_CHUNK_WRITE_ERROR);  | 
1250  |  | 
  | 
1251  | 0  |       return;  | 
1252  | 0  |    }  | 
1253  |  |  | 
1254  | 0  |    png_free(png_ptr, info_ptr->unknown_chunks);  | 
1255  | 0  |    info_ptr->unknown_chunks = np; /* safe because it is initialized */  | 
1256  | 0  |    info_ptr->free_me |= PNG_FREE_UNKN;  | 
1257  |  | 
  | 
1258  | 0  |    np += info_ptr->unknown_chunks_num;  | 
1259  |  |  | 
1260  |  |    /* Increment unknown_chunks_num each time round the loop to protect the  | 
1261  |  |     * just-allocated chunk data.  | 
1262  |  |     */  | 
1263  | 0  |    for (; num_unknowns > 0; --num_unknowns, ++unknowns)  | 
1264  | 0  |    { | 
1265  | 0  |       memcpy(np->name, unknowns->name, (sizeof np->name));  | 
1266  | 0  |       np->name[(sizeof np->name)-1] = '\0';  | 
1267  | 0  |       np->location = check_location(png_ptr, unknowns->location);  | 
1268  |  | 
  | 
1269  | 0  |       if (unknowns->size == 0)  | 
1270  | 0  |       { | 
1271  | 0  |          np->data = NULL;  | 
1272  | 0  |          np->size = 0;  | 
1273  | 0  |       }  | 
1274  |  |  | 
1275  | 0  |       else  | 
1276  | 0  |       { | 
1277  | 0  |          np->data = png_voidcast(png_bytep,  | 
1278  | 0  |              png_malloc_base(png_ptr, unknowns->size));  | 
1279  |  | 
  | 
1280  | 0  |          if (np->data == NULL)  | 
1281  | 0  |          { | 
1282  | 0  |             png_chunk_report(png_ptr, "unknown chunk: out of memory",  | 
1283  | 0  |                 PNG_CHUNK_WRITE_ERROR);  | 
1284  |  |             /* But just skip storing the unknown chunk */  | 
1285  | 0  |             continue;  | 
1286  | 0  |          }  | 
1287  |  |  | 
1288  | 0  |          memcpy(np->data, unknowns->data, unknowns->size);  | 
1289  | 0  |          np->size = unknowns->size;  | 
1290  | 0  |       }  | 
1291  |  |  | 
1292  |  |       /* These increments are skipped on out-of-memory for the data - the  | 
1293  |  |        * unknown chunk entry gets overwritten if the png_chunk_report returns.  | 
1294  |  |        * This is correct in the read case (the chunk is just dropped.)  | 
1295  |  |        */  | 
1296  | 0  |       ++np;  | 
1297  | 0  |       ++(info_ptr->unknown_chunks_num);  | 
1298  | 0  |    }  | 
1299  | 0  | }  | 
1300  |  |  | 
1301  |  | void PNGAPI  | 
1302  |  | png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,  | 
1303  |  |     int chunk, int location)  | 
1304  | 0  | { | 
1305  |  |    /* This API is pretty pointless in 1.6.0 because the location can be set  | 
1306  |  |     * before the call to png_set_unknown_chunks.  | 
1307  |  |     *  | 
1308  |  |     * TODO: add a png_app_warning in 1.7  | 
1309  |  |     */  | 
1310  | 0  |    if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&  | 
1311  | 0  |       chunk < info_ptr->unknown_chunks_num)  | 
1312  | 0  |    { | 
1313  | 0  |       if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)  | 
1314  | 0  |       { | 
1315  | 0  |          png_app_error(png_ptr, "invalid unknown chunk location");  | 
1316  |  |          /* Fake out the pre 1.6.0 behavior: */  | 
1317  | 0  |          if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */  | 
1318  | 0  |             location = PNG_AFTER_IDAT;  | 
1319  |  |  | 
1320  | 0  |          else  | 
1321  | 0  |             location = PNG_HAVE_IHDR; /* also undocumented */  | 
1322  | 0  |       }  | 
1323  |  | 
  | 
1324  | 0  |       info_ptr->unknown_chunks[chunk].location =  | 
1325  | 0  |          check_location(png_ptr, location);  | 
1326  | 0  |    }  | 
1327  | 0  | }  | 
1328  |  | #endif /* STORE_UNKNOWN_CHUNKS */  | 
1329  |  |  | 
1330  |  | #ifdef PNG_MNG_FEATURES_SUPPORTED  | 
1331  |  | png_uint_32 PNGAPI  | 
1332  |  | png_permit_mng_features(png_structrp png_ptr, png_uint_32 mng_features)  | 
1333  | 0  | { | 
1334  | 0  |    png_debug(1, "in png_permit_mng_features");  | 
1335  |  | 
  | 
1336  | 0  |    if (png_ptr == NULL)  | 
1337  | 0  |       return 0;  | 
1338  |  |  | 
1339  | 0  |    png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;  | 
1340  |  | 
  | 
1341  | 0  |    return png_ptr->mng_features_permitted;  | 
1342  | 0  | }  | 
1343  |  | #endif  | 
1344  |  |  | 
1345  |  | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED  | 
1346  |  | static unsigned int  | 
1347  |  | add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)  | 
1348  | 0  | { | 
1349  | 0  |    unsigned int i;  | 
1350  |  |  | 
1351  |  |    /* Utility function: update the 'keep' state of a chunk if it is already in  | 
1352  |  |     * the list, otherwise add it to the list.  | 
1353  |  |     */  | 
1354  | 0  |    for (i=0; i<count; ++i, list += 5)  | 
1355  | 0  |    { | 
1356  | 0  |       if (memcmp(list, add, 4) == 0)  | 
1357  | 0  |       { | 
1358  | 0  |          list[4] = (png_byte)keep;  | 
1359  |  | 
  | 
1360  | 0  |          return count;  | 
1361  | 0  |       }  | 
1362  | 0  |    }  | 
1363  |  |  | 
1364  | 0  |    if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)  | 
1365  | 0  |    { | 
1366  | 0  |       ++count;  | 
1367  | 0  |       memcpy(list, add, 4);  | 
1368  | 0  |       list[4] = (png_byte)keep;  | 
1369  | 0  |    }  | 
1370  |  | 
  | 
1371  | 0  |    return count;  | 
1372  | 0  | }  | 
1373  |  |  | 
1374  |  | void PNGAPI  | 
1375  |  | png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,  | 
1376  |  |     png_const_bytep chunk_list, int num_chunks_in)  | 
1377  | 0  | { | 
1378  | 0  |    png_bytep new_list;  | 
1379  | 0  |    unsigned int num_chunks, old_num_chunks;  | 
1380  |  | 
  | 
1381  | 0  |    if (png_ptr == NULL)  | 
1382  | 0  |       return;  | 
1383  |  |  | 
1384  | 0  |    if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)  | 
1385  | 0  |    { | 
1386  | 0  |       png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");  | 
1387  |  | 
  | 
1388  | 0  |       return;  | 
1389  | 0  |    }  | 
1390  |  |  | 
1391  | 0  |    if (num_chunks_in <= 0)  | 
1392  | 0  |    { | 
1393  | 0  |       png_ptr->unknown_default = keep;  | 
1394  |  |  | 
1395  |  |       /* '0' means just set the flags, so stop here */  | 
1396  | 0  |       if (num_chunks_in == 0)  | 
1397  | 0  |         return;  | 
1398  | 0  |    }  | 
1399  |  |  | 
1400  | 0  |    if (num_chunks_in < 0)  | 
1401  | 0  |    { | 
1402  |  |       /* Ignore all unknown chunks and all chunks recognized by  | 
1403  |  |        * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND  | 
1404  |  |        */  | 
1405  | 0  |       static const png_byte chunks_to_ignore[] = { | 
1406  | 0  |          98,  75,  71,  68, '\0',  /* bKGD */  | 
1407  | 0  |          99,  72,  82,  77, '\0',  /* cHRM */  | 
1408  | 0  |         101,  88,  73, 102, '\0',  /* eXIf */  | 
1409  | 0  |         103,  65,  77,  65, '\0',  /* gAMA */  | 
1410  | 0  |         104,  73,  83,  84, '\0',  /* hIST */  | 
1411  | 0  |         105,  67,  67,  80, '\0',  /* iCCP */  | 
1412  | 0  |         105,  84,  88, 116, '\0',  /* iTXt */  | 
1413  | 0  |         111,  70,  70, 115, '\0',  /* oFFs */  | 
1414  | 0  |         112,  67,  65,  76, '\0',  /* pCAL */  | 
1415  | 0  |         112,  72,  89, 115, '\0',  /* pHYs */  | 
1416  | 0  |         115,  66,  73,  84, '\0',  /* sBIT */  | 
1417  | 0  |         115,  67,  65,  76, '\0',  /* sCAL */  | 
1418  | 0  |         115,  80,  76,  84, '\0',  /* sPLT */  | 
1419  | 0  |         115,  84,  69,  82, '\0',  /* sTER */  | 
1420  | 0  |         115,  82,  71,  66, '\0',  /* sRGB */  | 
1421  | 0  |         116,  69,  88, 116, '\0',  /* tEXt */  | 
1422  | 0  |         116,  73,  77,  69, '\0',  /* tIME */  | 
1423  | 0  |         122,  84,  88, 116, '\0'   /* zTXt */  | 
1424  | 0  |       };  | 
1425  |  | 
  | 
1426  | 0  |       chunk_list = chunks_to_ignore;  | 
1427  | 0  |       num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;  | 
1428  | 0  |    }  | 
1429  |  |  | 
1430  | 0  |    else /* num_chunks_in > 0 */  | 
1431  | 0  |    { | 
1432  | 0  |       if (chunk_list == NULL)  | 
1433  | 0  |       { | 
1434  |  |          /* Prior to 1.6.0 this was silently ignored, now it is an app_error  | 
1435  |  |           * which can be switched off.  | 
1436  |  |           */  | 
1437  | 0  |          png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");  | 
1438  |  | 
  | 
1439  | 0  |          return;  | 
1440  | 0  |       }  | 
1441  |  |  | 
1442  | 0  |       num_chunks = (unsigned int)num_chunks_in;  | 
1443  | 0  |    }  | 
1444  |  |  | 
1445  | 0  |    old_num_chunks = png_ptr->num_chunk_list;  | 
1446  | 0  |    if (png_ptr->chunk_list == NULL)  | 
1447  | 0  |       old_num_chunks = 0;  | 
1448  |  |  | 
1449  |  |    /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.  | 
1450  |  |     */  | 
1451  | 0  |    if (num_chunks + old_num_chunks > UINT_MAX/5)  | 
1452  | 0  |    { | 
1453  | 0  |       png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");  | 
1454  |  | 
  | 
1455  | 0  |       return;  | 
1456  | 0  |    }  | 
1457  |  |  | 
1458  |  |    /* If these chunks are being reset to the default then no more memory is  | 
1459  |  |     * required because add_one_chunk above doesn't extend the list if the 'keep'  | 
1460  |  |     * parameter is the default.  | 
1461  |  |     */  | 
1462  | 0  |    if (keep != 0)  | 
1463  | 0  |    { | 
1464  | 0  |       new_list = png_voidcast(png_bytep, png_malloc(png_ptr,  | 
1465  | 0  |           5 * (num_chunks + old_num_chunks)));  | 
1466  |  | 
  | 
1467  | 0  |       if (old_num_chunks > 0)  | 
1468  | 0  |          memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);  | 
1469  | 0  |    }  | 
1470  |  |  | 
1471  | 0  |    else if (old_num_chunks > 0)  | 
1472  | 0  |       new_list = png_ptr->chunk_list;  | 
1473  |  |  | 
1474  | 0  |    else  | 
1475  | 0  |       new_list = NULL;  | 
1476  |  |  | 
1477  |  |    /* Add the new chunks together with each one's handling code.  If the chunk  | 
1478  |  |     * already exists the code is updated, otherwise the chunk is added to the  | 
1479  |  |     * end.  (In libpng 1.6.0 order no longer matters because this code enforces  | 
1480  |  |     * the earlier convention that the last setting is the one that is used.)  | 
1481  |  |     */  | 
1482  | 0  |    if (new_list != NULL)  | 
1483  | 0  |    { | 
1484  | 0  |       png_const_bytep inlist;  | 
1485  | 0  |       png_bytep outlist;  | 
1486  | 0  |       unsigned int i;  | 
1487  |  | 
  | 
1488  | 0  |       for (i=0; i<num_chunks; ++i)  | 
1489  | 0  |       { | 
1490  | 0  |          old_num_chunks = add_one_chunk(new_list, old_num_chunks,  | 
1491  | 0  |              chunk_list+5*i, keep);  | 
1492  | 0  |       }  | 
1493  |  |  | 
1494  |  |       /* Now remove any spurious 'default' entries. */  | 
1495  | 0  |       num_chunks = 0;  | 
1496  | 0  |       for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)  | 
1497  | 0  |       { | 
1498  | 0  |          if (inlist[4])  | 
1499  | 0  |          { | 
1500  | 0  |             if (outlist != inlist)  | 
1501  | 0  |                memcpy(outlist, inlist, 5);  | 
1502  | 0  |             outlist += 5;  | 
1503  | 0  |             ++num_chunks;  | 
1504  | 0  |          }  | 
1505  | 0  |       }  | 
1506  |  |  | 
1507  |  |       /* This means the application has removed all the specialized handling. */  | 
1508  | 0  |       if (num_chunks == 0)  | 
1509  | 0  |       { | 
1510  | 0  |          if (png_ptr->chunk_list != new_list)  | 
1511  | 0  |             png_free(png_ptr, new_list);  | 
1512  |  | 
  | 
1513  | 0  |          new_list = NULL;  | 
1514  | 0  |       }  | 
1515  | 0  |    }  | 
1516  |  |  | 
1517  | 0  |    else  | 
1518  | 0  |       num_chunks = 0;  | 
1519  |  | 
  | 
1520  | 0  |    png_ptr->num_chunk_list = num_chunks;  | 
1521  |  | 
  | 
1522  | 0  |    if (png_ptr->chunk_list != new_list)  | 
1523  | 0  |    { | 
1524  | 0  |       if (png_ptr->chunk_list != NULL)  | 
1525  | 0  |          png_free(png_ptr, png_ptr->chunk_list);  | 
1526  |  | 
  | 
1527  | 0  |       png_ptr->chunk_list = new_list;  | 
1528  | 0  |    }  | 
1529  | 0  | }  | 
1530  |  | #endif  | 
1531  |  |  | 
1532  |  | #ifdef PNG_READ_USER_CHUNKS_SUPPORTED  | 
1533  |  | void PNGAPI  | 
1534  |  | png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,  | 
1535  |  |     png_user_chunk_ptr read_user_chunk_fn)  | 
1536  | 0  | { | 
1537  | 0  |    png_debug(1, "in png_set_read_user_chunk_fn");  | 
1538  |  | 
  | 
1539  | 0  |    if (png_ptr == NULL)  | 
1540  | 0  |       return;  | 
1541  |  |  | 
1542  | 0  |    png_ptr->read_user_chunk_fn = read_user_chunk_fn;  | 
1543  | 0  |    png_ptr->user_chunk_ptr = user_chunk_ptr;  | 
1544  | 0  | }  | 
1545  |  | #endif  | 
1546  |  |  | 
1547  |  | #ifdef PNG_INFO_IMAGE_SUPPORTED  | 
1548  |  | void PNGAPI  | 
1549  |  | png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,  | 
1550  |  |     png_bytepp row_pointers)  | 
1551  | 0  | { | 
1552  | 0  |    png_debug1(1, "in %s storage function", "rows");  | 
1553  |  | 
  | 
1554  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
1555  | 0  |       return;  | 
1556  |  |  | 
1557  | 0  |    if (info_ptr->row_pointers != NULL &&  | 
1558  | 0  |        (info_ptr->row_pointers != row_pointers))  | 
1559  | 0  |       png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);  | 
1560  |  | 
  | 
1561  | 0  |    info_ptr->row_pointers = row_pointers;  | 
1562  |  | 
  | 
1563  | 0  |    if (row_pointers != NULL)  | 
1564  | 0  |       info_ptr->valid |= PNG_INFO_IDAT;  | 
1565  | 0  | }  | 
1566  |  | #endif  | 
1567  |  |  | 
1568  |  | void PNGAPI  | 
1569  |  | png_set_compression_buffer_size(png_structrp png_ptr, size_t size)  | 
1570  | 0  | { | 
1571  | 0  |    if (png_ptr == NULL)  | 
1572  | 0  |       return;  | 
1573  |  |  | 
1574  | 0  |    if (size == 0 || size > PNG_UINT_31_MAX)  | 
1575  | 0  |       png_error(png_ptr, "invalid compression buffer size");  | 
1576  |  |  | 
1577  | 0  | #  ifdef PNG_SEQUENTIAL_READ_SUPPORTED  | 
1578  | 0  |    if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)  | 
1579  | 0  |    { | 
1580  | 0  |       png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */  | 
1581  | 0  |       return;  | 
1582  | 0  |    }  | 
1583  | 0  | #  endif  | 
1584  |  |  | 
1585  | 0  | #  ifdef PNG_WRITE_SUPPORTED  | 
1586  | 0  |    if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)  | 
1587  | 0  |    { | 
1588  | 0  |       if (png_ptr->zowner != 0)  | 
1589  | 0  |       { | 
1590  | 0  |          png_warning(png_ptr,  | 
1591  | 0  |              "Compression buffer size cannot be changed because it is in use");  | 
1592  |  | 
  | 
1593  | 0  |          return;  | 
1594  | 0  |       }  | 
1595  |  |  | 
1596  | 0  | #ifndef __COVERITY__  | 
1597  |  |       /* Some compilers complain that this is always false.  However, it  | 
1598  |  |        * can be true when integer overflow happens.  | 
1599  |  |        */  | 
1600  | 0  |       if (size > ZLIB_IO_MAX)  | 
1601  | 0  |       { | 
1602  | 0  |          png_warning(png_ptr,  | 
1603  | 0  |              "Compression buffer size limited to system maximum");  | 
1604  | 0  |          size = ZLIB_IO_MAX; /* must fit */  | 
1605  | 0  |       }  | 
1606  | 0  | #endif  | 
1607  |  | 
  | 
1608  | 0  |       if (size < 6)  | 
1609  | 0  |       { | 
1610  |  |          /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH  | 
1611  |  |           * if this is permitted.  | 
1612  |  |           */  | 
1613  | 0  |          png_warning(png_ptr,  | 
1614  | 0  |              "Compression buffer size cannot be reduced below 6");  | 
1615  |  | 
  | 
1616  | 0  |          return;  | 
1617  | 0  |       }  | 
1618  |  |  | 
1619  | 0  |       if (png_ptr->zbuffer_size != size)  | 
1620  | 0  |       { | 
1621  | 0  |          png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);  | 
1622  | 0  |          png_ptr->zbuffer_size = (uInt)size;  | 
1623  | 0  |       }  | 
1624  | 0  |    }  | 
1625  | 0  | #  endif  | 
1626  | 0  | }  | 
1627  |  |  | 
1628  |  | void PNGAPI  | 
1629  |  | png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)  | 
1630  | 0  | { | 
1631  | 0  |    if (png_ptr != NULL && info_ptr != NULL)  | 
1632  | 0  |       info_ptr->valid &= (unsigned int)(~mask);  | 
1633  | 0  | }  | 
1634  |  |  | 
1635  |  |  | 
1636  |  | #ifdef PNG_SET_USER_LIMITS_SUPPORTED  | 
1637  |  | /* This function was added to libpng 1.2.6 */  | 
1638  |  | void PNGAPI  | 
1639  |  | png_set_user_limits(png_structrp png_ptr, png_uint_32 user_width_max,  | 
1640  |  |     png_uint_32 user_height_max)  | 
1641  | 0  | { | 
1642  |  |    /* Images with dimensions larger than these limits will be  | 
1643  |  |     * rejected by png_set_IHDR().  To accept any PNG datastream  | 
1644  |  |     * regardless of dimensions, set both limits to 0x7fffffff.  | 
1645  |  |     */  | 
1646  | 0  |    if (png_ptr == NULL)  | 
1647  | 0  |       return;  | 
1648  |  |  | 
1649  | 0  |    png_ptr->user_width_max = user_width_max;  | 
1650  | 0  |    png_ptr->user_height_max = user_height_max;  | 
1651  | 0  | }  | 
1652  |  |  | 
1653  |  | /* This function was added to libpng 1.4.0 */  | 
1654  |  | void PNGAPI  | 
1655  |  | png_set_chunk_cache_max(png_structrp png_ptr, png_uint_32 user_chunk_cache_max)  | 
1656  | 0  | { | 
1657  | 0  |    if (png_ptr != NULL)  | 
1658  | 0  |       png_ptr->user_chunk_cache_max = user_chunk_cache_max;  | 
1659  | 0  | }  | 
1660  |  |  | 
1661  |  | /* This function was added to libpng 1.4.1 */  | 
1662  |  | void PNGAPI  | 
1663  |  | png_set_chunk_malloc_max(png_structrp png_ptr,  | 
1664  |  |     png_alloc_size_t user_chunk_malloc_max)  | 
1665  | 0  | { | 
1666  | 0  |    if (png_ptr != NULL)  | 
1667  | 0  |       png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;  | 
1668  | 0  | }  | 
1669  |  | #endif /* ?SET_USER_LIMITS */  | 
1670  |  |  | 
1671  |  |  | 
1672  |  | #ifdef PNG_BENIGN_ERRORS_SUPPORTED  | 
1673  |  | void PNGAPI  | 
1674  |  | png_set_benign_errors(png_structrp png_ptr, int allowed)  | 
1675  | 0  | { | 
1676  | 0  |    png_debug(1, "in png_set_benign_errors");  | 
1677  |  |  | 
1678  |  |    /* If allowed is 1, png_benign_error() is treated as a warning.  | 
1679  |  |     *  | 
1680  |  |     * If allowed is 0, png_benign_error() is treated as an error (which  | 
1681  |  |     * is the default behavior if png_set_benign_errors() is not called).  | 
1682  |  |     */  | 
1683  |  | 
  | 
1684  | 0  |    if (allowed != 0)  | 
1685  | 0  |       png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |  | 
1686  | 0  |          PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;  | 
1687  |  |  | 
1688  | 0  |    else  | 
1689  | 0  |       png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |  | 
1690  | 0  |          PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);  | 
1691  | 0  | }  | 
1692  |  | #endif /* BENIGN_ERRORS */  | 
1693  |  |  | 
1694  |  | #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED  | 
1695  |  |    /* Whether to report invalid palette index; added at libng-1.5.10.  | 
1696  |  |     * It is possible for an indexed (color-type==3) PNG file to contain  | 
1697  |  |     * pixels with invalid (out-of-range) indexes if the PLTE chunk has  | 
1698  |  |     * fewer entries than the image's bit-depth would allow. We recover  | 
1699  |  |     * from this gracefully by filling any incomplete palette with zeros  | 
1700  |  |     * (opaque black).  By default, when this occurs libpng will issue  | 
1701  |  |     * a benign error.  This API can be used to override that behavior.  | 
1702  |  |     */  | 
1703  |  | void PNGAPI  | 
1704  |  | png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)  | 
1705  | 0  | { | 
1706  | 0  |    png_debug(1, "in png_set_check_for_invalid_index");  | 
1707  |  | 
  | 
1708  | 0  |    if (allowed > 0)  | 
1709  | 0  |       png_ptr->num_palette_max = 0;  | 
1710  |  |  | 
1711  | 0  |    else  | 
1712  | 0  |       png_ptr->num_palette_max = -1;  | 
1713  | 0  | }  | 
1714  |  | #endif  | 
1715  |  |  | 
1716  |  | #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \  | 
1717  |  |     defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)  | 
1718  |  | /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,  | 
1719  |  |  * and if invalid, correct the keyword rather than discarding the entire  | 
1720  |  |  * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in  | 
1721  |  |  * length, forbids leading or trailing whitespace, multiple internal spaces,  | 
1722  |  |  * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.  | 
1723  |  |  *  | 
1724  |  |  * The 'new_key' buffer must be 80 characters in size (for the keyword plus a  | 
1725  |  |  * trailing '\0').  If this routine returns 0 then there was no keyword, or a  | 
1726  |  |  * valid one could not be generated, and the caller must png_error.  | 
1727  |  |  */  | 
1728  |  | png_uint_32 /* PRIVATE */  | 
1729  |  | png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)  | 
1730  | 0  | { | 
1731  | 0  | #ifdef PNG_WARNINGS_SUPPORTED  | 
1732  | 0  |    png_const_charp orig_key = key;  | 
1733  | 0  | #endif  | 
1734  | 0  |    png_uint_32 key_len = 0;  | 
1735  | 0  |    int bad_character = 0;  | 
1736  | 0  |    int space = 1;  | 
1737  |  | 
  | 
1738  | 0  |    png_debug(1, "in png_check_keyword");  | 
1739  |  | 
  | 
1740  | 0  |    if (key == NULL)  | 
1741  | 0  |    { | 
1742  | 0  |       *new_key = 0;  | 
1743  | 0  |       return 0;  | 
1744  | 0  |    }  | 
1745  |  |  | 
1746  | 0  |    while (*key && key_len < 79)  | 
1747  | 0  |    { | 
1748  | 0  |       png_byte ch = (png_byte)*key++;  | 
1749  |  | 
  | 
1750  | 0  |       if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))  | 
1751  | 0  |       { | 
1752  | 0  |          *new_key++ = ch; ++key_len; space = 0;  | 
1753  | 0  |       }  | 
1754  |  |  | 
1755  | 0  |       else if (space == 0)  | 
1756  | 0  |       { | 
1757  |  |          /* A space or an invalid character when one wasn't seen immediately  | 
1758  |  |           * before; output just a space.  | 
1759  |  |           */  | 
1760  | 0  |          *new_key++ = 32; ++key_len; space = 1;  | 
1761  |  |  | 
1762  |  |          /* If the character was not a space then it is invalid. */  | 
1763  | 0  |          if (ch != 32)  | 
1764  | 0  |             bad_character = ch;  | 
1765  | 0  |       }  | 
1766  |  |  | 
1767  | 0  |       else if (bad_character == 0)  | 
1768  | 0  |          bad_character = ch; /* just skip it, record the first error */  | 
1769  | 0  |    }  | 
1770  |  | 
  | 
1771  | 0  |    if (key_len > 0 && space != 0) /* trailing space */  | 
1772  | 0  |    { | 
1773  | 0  |       --key_len; --new_key;  | 
1774  | 0  |       if (bad_character == 0)  | 
1775  | 0  |          bad_character = 32;  | 
1776  | 0  |    }  | 
1777  |  |  | 
1778  |  |    /* Terminate the keyword */  | 
1779  | 0  |    *new_key = 0;  | 
1780  |  | 
  | 
1781  | 0  |    if (key_len == 0)  | 
1782  | 0  |       return 0;  | 
1783  |  |  | 
1784  | 0  | #ifdef PNG_WARNINGS_SUPPORTED  | 
1785  |  |    /* Try to only output one warning per keyword: */  | 
1786  | 0  |    if (*key != 0) /* keyword too long */  | 
1787  | 0  |       png_warning(png_ptr, "keyword truncated");  | 
1788  |  |  | 
1789  | 0  |    else if (bad_character != 0)  | 
1790  | 0  |    { | 
1791  | 0  |       PNG_WARNING_PARAMETERS(p)  | 
1792  |  | 
  | 
1793  | 0  |       png_warning_parameter(p, 1, orig_key);  | 
1794  | 0  |       png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);  | 
1795  |  | 
  | 
1796  | 0  |       png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");  | 
1797  | 0  |    }  | 
1798  |  | #else /* !WARNINGS */  | 
1799  |  |    PNG_UNUSED(png_ptr)  | 
1800  |  | #endif /* !WARNINGS */  | 
1801  |  | 
  | 
1802  | 0  |    return key_len;  | 
1803  | 0  | }  | 
1804  |  | #endif /* TEXT || pCAL || iCCP || sPLT */  | 
1805  |  | #endif /* READ || WRITE */  |