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