Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: third_party/libpng/pngrutil.c

Issue 15041: Update libpng to 1.2.33. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 12 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libpng/pngrtran.c ('k') | third_party/libpng/pngset.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* pngrutil.c - utilities to read a PNG file 2 /* pngrutil.c - utilities to read a PNG file
3 * 3 *
4 * Last changed in libpng 1.2.27 [April 29, 2008] 4 * Last changed in libpng 1.2.33 [October 31, 2008]
5 * For conditions of distribution and use, see copyright notice in png.h 5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * 9 *
10 * This file contains routines that are only called from within 10 * This file contains routines that are only called from within
11 * libpng itself during the course of reading an image. 11 * libpng itself during the course of reading an image.
12 */ 12 */
13 13
14 #define PNG_INTERNAL 14 #define PNG_INTERNAL
15 #include "png.h" 15 #include "png.h"
16
17 #if defined(PNG_READ_SUPPORTED) 16 #if defined(PNG_READ_SUPPORTED)
18 17
19 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500) 18 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
20 # define WIN32_WCE_OLD 19 # define WIN32_WCE_OLD
21 #endif 20 #endif
22 21
23 #ifdef PNG_FLOATING_POINT_SUPPORTED 22 #ifdef PNG_FLOATING_POINT_SUPPORTED
24 # if defined(WIN32_WCE_OLD) 23 # if defined(WIN32_WCE_OLD)
25 /* strtod() function is not supported on WindowsCE */ 24 /* strtod() function is not supported on WindowsCE */
26 __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **end ptr) 25 __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **end ptr)
27 { 26 {
28 double result = 0; 27 double result = 0;
29 int len; 28 int len;
30 wchar_t *str, *end; 29 wchar_t *str, *end;
31 30
32 len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0); 31 len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
33 str = (wchar_t *)png_malloc(png_ptr, len * sizeof(wchar_t)); 32 str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
34 if ( NULL != str ) 33 if ( NULL != str )
35 { 34 {
36 MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len); 35 MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
37 result = wcstod(str, &end); 36 result = wcstod(str, &end);
38 len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL); 37 len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
39 *endptr = (char *)nptr + (png_strlen(nptr) - len + 1); 38 *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
40 png_free(png_ptr, str); 39 png_free(png_ptr, str);
41 } 40 }
42 return result; 41 return result;
43 } 42 }
44 # else 43 # else
45 # define png_strtod(p,a,b) strtod(a,b) 44 # define png_strtod(p,a,b) strtod(a,b)
46 # endif 45 # endif
47 #endif 46 #endif
48 47
49 png_uint_32 PNGAPI 48 png_uint_32 PNGAPI
50 png_get_uint_31(png_structp png_ptr, png_bytep buf) 49 png_get_uint_31(png_structp png_ptr, png_bytep buf)
51 { 50 {
51 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
52 png_uint_32 i = png_get_uint_32(buf); 52 png_uint_32 i = png_get_uint_32(buf);
53 #else
54 /* Avoid an extra function call by inlining the result. */
55 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
56 ((png_uint_32)(*(buf + 1)) << 16) +
57 ((png_uint_32)(*(buf + 2)) << 8) +
58 (png_uint_32)(*(buf + 3));
59 #endif
53 if (i > PNG_UINT_31_MAX) 60 if (i > PNG_UINT_31_MAX)
54 png_error(png_ptr, "PNG unsigned integer out of range."); 61 png_error(png_ptr, "PNG unsigned integer out of range.");
55 return (i); 62 return (i);
56 } 63 }
57 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED 64 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
58 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ 65 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
59 png_uint_32 PNGAPI 66 png_uint_32 PNGAPI
60 png_get_uint_32(png_bytep buf) 67 png_get_uint_32(png_bytep buf)
61 { 68 {
62 png_uint_32 i = ((png_uint_32)(*buf) << 24) + 69 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
(...skipping 22 matching lines...) Expand all
85 png_uint_16 PNGAPI 92 png_uint_16 PNGAPI
86 png_get_uint_16(png_bytep buf) 93 png_get_uint_16(png_bytep buf)
87 { 94 {
88 png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) + 95 png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
89 (png_uint_16)(*(buf + 1))); 96 (png_uint_16)(*(buf + 1)));
90 97
91 return (i); 98 return (i);
92 } 99 }
93 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */ 100 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
94 101
102 /* Read the chunk header (length + type name).
103 * Put the type name into png_ptr->chunk_name, and return the length.
104 */
105 png_uint_32 /* PRIVATE */
106 png_read_chunk_header(png_structp png_ptr)
107 {
108 png_byte buf[8];
109 png_uint_32 length;
110
111 /* read the length and the chunk name */
112 png_read_data(png_ptr, buf, 8);
113 length = png_get_uint_31(png_ptr, buf);
114
115 /* put the chunk name into png_ptr->chunk_name */
116 png_memcpy(png_ptr->chunk_name, buf + 4, 4);
117
118 png_debug2(0, "Reading %s chunk, length = %lu\n",
119 png_ptr->chunk_name, length);
120
121 /* reset the crc and run it over the chunk name */
122 png_reset_crc(png_ptr);
123 png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
124
125 /* check to see if chunk name is valid */
126 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
127
128 return length;
129 }
130
95 /* Read data, and (optionally) run it through the CRC. */ 131 /* Read data, and (optionally) run it through the CRC. */
96 void /* PRIVATE */ 132 void /* PRIVATE */
97 png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length) 133 png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
98 { 134 {
99 if(png_ptr == NULL) return; 135 if (png_ptr == NULL) return;
100 png_read_data(png_ptr, buf, length); 136 png_read_data(png_ptr, buf, length);
101 png_calculate_crc(png_ptr, buf, length); 137 png_calculate_crc(png_ptr, buf, length);
102 } 138 }
103 139
104 /* Optionally skip data and then check the CRC. Depending on whether we 140 /* Optionally skip data and then check the CRC. Depending on whether we
105 are reading a ancillary or critical chunk, and how the program has set 141 are reading a ancillary or critical chunk, and how the program has set
106 things up, we may calculate the CRC on the data and print a message. 142 things up, we may calculate the CRC on the data and print a message.
107 Returns '1' if there was a CRC error, '0' otherwise. */ 143 Returns '1' if there was a CRC error, '0' otherwise. */
108 int /* PRIVATE */ 144 int /* PRIVATE */
109 png_crc_finish(png_structp png_ptr, png_uint_32 skip) 145 png_crc_finish(png_structp png_ptr, png_uint_32 skip)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 209
174 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \ 210 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
175 defined(PNG_READ_iCCP_SUPPORTED) 211 defined(PNG_READ_iCCP_SUPPORTED)
176 /* 212 /*
177 * Decompress trailing data in a chunk. The assumption is that chunkdata 213 * Decompress trailing data in a chunk. The assumption is that chunkdata
178 * points at an allocated area holding the contents of a chunk with a 214 * points at an allocated area holding the contents of a chunk with a
179 * trailing compressed part. What we get back is an allocated area 215 * trailing compressed part. What we get back is an allocated area
180 * holding the original prefix part and an uncompressed version of the 216 * holding the original prefix part and an uncompressed version of the
181 * trailing part (the malloc area passed in is freed). 217 * trailing part (the malloc area passed in is freed).
182 */ 218 */
183 png_charp /* PRIVATE */ 219 void /* PRIVATE */
184 png_decompress_chunk(png_structp png_ptr, int comp_type, 220 png_decompress_chunk(png_structp png_ptr, int comp_type,
185 png_charp chunkdata, png_size_t chunklength, 221 png_size_t chunklength,
186 png_size_t prefix_size, png_size_t *newlength) 222 png_size_t prefix_size, png_size_t *newlength)
187 { 223 {
188 static PNG_CONST char msg[] = "Error decoding compressed text"; 224 static PNG_CONST char msg[] = "Error decoding compressed text";
189 png_charp text; 225 png_charp text;
190 png_size_t text_size; 226 png_size_t text_size;
191 227
192 if (comp_type == PNG_COMPRESSION_TYPE_BASE) 228 if (comp_type == PNG_COMPRESSION_TYPE_BASE)
193 { 229 {
194 int ret = Z_OK; 230 int ret = Z_OK;
195 png_ptr->zstream.next_in = (png_bytep)(chunkdata + prefix_size); 231 png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
196 png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size); 232 png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
197 png_ptr->zstream.next_out = png_ptr->zbuf; 233 png_ptr->zstream.next_out = png_ptr->zbuf;
198 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; 234 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
199 235
200 text_size = 0; 236 text_size = 0;
201 text = NULL; 237 text = NULL;
202 238
203 while (png_ptr->zstream.avail_in) 239 while (png_ptr->zstream.avail_in)
204 { 240 {
205 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); 241 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
206 if (ret != Z_OK && ret != Z_STREAM_END) 242 if (ret != Z_OK && ret != Z_STREAM_END)
207 { 243 {
208 if (png_ptr->zstream.msg != NULL) 244 if (png_ptr->zstream.msg != NULL)
209 png_warning(png_ptr, png_ptr->zstream.msg); 245 png_warning(png_ptr, png_ptr->zstream.msg);
210 else 246 else
211 png_warning(png_ptr, msg); 247 png_warning(png_ptr, msg);
212 inflateReset(&png_ptr->zstream); 248 inflateReset(&png_ptr->zstream);
213 png_ptr->zstream.avail_in = 0; 249 png_ptr->zstream.avail_in = 0;
214 250
215 if (text == NULL) 251 if (text == NULL)
216 { 252 {
217 text_size = prefix_size + png_sizeof(msg) + 1; 253 text_size = prefix_size + png_sizeof(msg) + 1;
218 text = (png_charp)png_malloc_warn(png_ptr, text_size); 254 text = (png_charp)png_malloc_warn(png_ptr, text_size);
219 if (text == NULL) 255 if (text == NULL)
220 { 256 {
221 png_free(png_ptr,chunkdata); 257 png_free(png_ptr, png_ptr->chunkdata);
222 png_error(png_ptr,"Not enough memory to decompress chunk"); 258 png_ptr->chunkdata = NULL;
259 png_error(png_ptr, "Not enough memory to decompress chunk");
223 } 260 }
224 png_memcpy(text, chunkdata, prefix_size); 261 png_memcpy(text, png_ptr->chunkdata, prefix_size);
225 } 262 }
226 263
227 text[text_size - 1] = 0x00; 264 text[text_size - 1] = 0x00;
228 265
229 /* Copy what we can of the error message into the text chunk */ 266 /* Copy what we can of the error message into the text chunk */
230 text_size = (png_size_t)(chunklength - (text - chunkdata) - 1); 267 text_size = (png_size_t)(chunklength -
231 text_size = png_sizeof(msg) > text_size ? text_size : 268 (text - png_ptr->chunkdata) - 1);
232 png_sizeof(msg); 269 if (text_size > png_sizeof(msg))
270 text_size = png_sizeof(msg);
233 png_memcpy(text + prefix_size, msg, text_size); 271 png_memcpy(text + prefix_size, msg, text_size);
234 break; 272 break;
235 } 273 }
236 if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) 274 if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
237 { 275 {
238 if (text == NULL) 276 if (text == NULL)
239 { 277 {
240 text_size = prefix_size + 278 text_size = prefix_size +
241 png_ptr->zbuf_size - png_ptr->zstream.avail_out; 279 png_ptr->zbuf_size - png_ptr->zstream.avail_out;
242 text = (png_charp)png_malloc_warn(png_ptr, text_size + 1); 280 text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
243 if (text == NULL) 281 if (text == NULL)
244 { 282 {
245 png_free(png_ptr,chunkdata); 283 png_free(png_ptr, png_ptr->chunkdata);
246 png_error(png_ptr,"Not enough memory to decompress chunk."); 284 png_ptr->chunkdata = NULL;
247 } 285 png_error(png_ptr,
286 "Not enough memory to decompress chunk.");
287 }
248 png_memcpy(text + prefix_size, png_ptr->zbuf, 288 png_memcpy(text + prefix_size, png_ptr->zbuf,
249 text_size - prefix_size); 289 text_size - prefix_size);
250 png_memcpy(text, chunkdata, prefix_size); 290 png_memcpy(text, png_ptr->chunkdata, prefix_size);
251 *(text + text_size) = 0x00; 291 *(text + text_size) = 0x00;
252 } 292 }
253 else 293 else
254 { 294 {
255 png_charp tmp; 295 png_charp tmp;
256 296
257 tmp = text; 297 tmp = text;
258 text = (png_charp)png_malloc_warn(png_ptr, 298 text = (png_charp)png_malloc_warn(png_ptr,
259 (png_uint_32)(text_size + 299 (png_uint_32)(text_size +
260 png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1)); 300 png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
261 if (text == NULL) 301 if (text == NULL)
262 { 302 {
263 png_free(png_ptr, tmp); 303 png_free(png_ptr, tmp);
264 png_free(png_ptr, chunkdata); 304 png_free(png_ptr, png_ptr->chunkdata);
265 png_error(png_ptr,"Not enough memory to decompress chunk.."); 305 png_ptr->chunkdata = NULL;
306 png_error(png_ptr,
307 "Not enough memory to decompress chunk..");
266 } 308 }
267 png_memcpy(text, tmp, text_size); 309 png_memcpy(text, tmp, text_size);
268 png_free(png_ptr, tmp); 310 png_free(png_ptr, tmp);
269 png_memcpy(text + text_size, png_ptr->zbuf, 311 png_memcpy(text + text_size, png_ptr->zbuf,
270 (png_ptr->zbuf_size - png_ptr->zstream.avail_out)); 312 (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
271 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; 313 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
272 *(text + text_size) = 0x00; 314 *(text + text_size) = 0x00;
273 } 315 }
274 if (ret == Z_STREAM_END) 316 if (ret == Z_STREAM_END)
275 break; 317 break;
(...skipping 19 matching lines...) Expand all
295 png_ptr->chunk_name); 337 png_ptr->chunk_name);
296 else 338 else
297 png_snprintf(umsg, 52, 339 png_snprintf(umsg, 52,
298 "Incomplete compressed datastream in %s chunk", 340 "Incomplete compressed datastream in %s chunk",
299 png_ptr->chunk_name); 341 png_ptr->chunk_name);
300 png_warning(png_ptr, umsg); 342 png_warning(png_ptr, umsg);
301 #else 343 #else
302 png_warning(png_ptr, 344 png_warning(png_ptr,
303 "Incomplete compressed datastream in chunk other than IDAT"); 345 "Incomplete compressed datastream in chunk other than IDAT");
304 #endif 346 #endif
305 text_size=prefix_size; 347 text_size = prefix_size;
306 if (text == NULL) 348 if (text == NULL)
307 { 349 {
308 text = (png_charp)png_malloc_warn(png_ptr, text_size+1); 350 text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
309 if (text == NULL) 351 if (text == NULL)
310 { 352 {
311 png_free(png_ptr, chunkdata); 353 png_free(png_ptr, png_ptr->chunkdata);
312 png_error(png_ptr,"Not enough memory for text."); 354 png_ptr->chunkdata = NULL;
355 png_error(png_ptr, "Not enough memory for text.");
313 } 356 }
314 png_memcpy(text, chunkdata, prefix_size); 357 png_memcpy(text, png_ptr->chunkdata, prefix_size);
315 } 358 }
316 *(text + text_size) = 0x00; 359 *(text + text_size) = 0x00;
317 } 360 }
318 361
319 inflateReset(&png_ptr->zstream); 362 inflateReset(&png_ptr->zstream);
320 png_ptr->zstream.avail_in = 0; 363 png_ptr->zstream.avail_in = 0;
321 364
322 png_free(png_ptr, chunkdata); 365 png_free(png_ptr, png_ptr->chunkdata);
323 chunkdata = text; 366 png_ptr->chunkdata = text;
324 *newlength=text_size; 367 *newlength=text_size;
325 } 368 }
326 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */ 369 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
327 { 370 {
328 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) 371 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
329 char umsg[50]; 372 char umsg[50];
330 373
331 png_snprintf(umsg, 50, 374 png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
332 "Unknown zTXt compression type %d", comp_type);
333 png_warning(png_ptr, umsg); 375 png_warning(png_ptr, umsg);
334 #else 376 #else
335 png_warning(png_ptr, "Unknown zTXt compression type"); 377 png_warning(png_ptr, "Unknown zTXt compression type");
336 #endif 378 #endif
337 379
338 *(chunkdata + prefix_size) = 0x00; 380 *(png_ptr->chunkdata + prefix_size) = 0x00;
339 *newlength=prefix_size; 381 *newlength = prefix_size;
340 } 382 }
341
342 return chunkdata;
343 } 383 }
344 #endif 384 #endif
345 385
346 /* read and check the IDHR chunk */ 386 /* read and check the IDHR chunk */
347 void /* PRIVATE */ 387 void /* PRIVATE */
348 png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 388 png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
349 { 389 {
350 png_byte buf[13]; 390 png_byte buf[13];
351 png_uint_32 width, height; 391 png_uint_32 width, height;
352 int bit_depth, color_type, compression_type, filter_type; 392 int bit_depth, color_type, compression_type, filter_type;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 png_ptr->channels = 2; 439 png_ptr->channels = 2;
400 break; 440 break;
401 case PNG_COLOR_TYPE_RGB_ALPHA: 441 case PNG_COLOR_TYPE_RGB_ALPHA:
402 png_ptr->channels = 4; 442 png_ptr->channels = 4;
403 break; 443 break;
404 } 444 }
405 445
406 /* set up other useful info */ 446 /* set up other useful info */
407 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * 447 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
408 png_ptr->channels); 448 png_ptr->channels);
409 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->width); 449 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
410 png_debug1(3,"bit_depth = %d\n", png_ptr->bit_depth); 450 png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
411 png_debug1(3,"channels = %d\n", png_ptr->channels); 451 png_debug1(3, "channels = %d\n", png_ptr->channels);
412 png_debug1(3,"rowbytes = %lu\n", png_ptr->rowbytes); 452 png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
413 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, 453 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
414 color_type, interlace_type, compression_type, filter_type); 454 color_type, interlace_type, compression_type, filter_type);
415 } 455 }
416 456
417 /* read and check the palette */ 457 /* read and check the palette */
418 void /* PRIVATE */ 458 void /* PRIVATE */
419 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 459 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
420 { 460 {
421 png_color palette[PNG_MAX_PALETTE_LENGTH]; 461 png_color palette[PNG_MAX_PALETTE_LENGTH];
422 int num, i; 462 int num, i;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 } 604 }
565 605
566 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); 606 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
567 607
568 if (length != 0) 608 if (length != 0)
569 { 609 {
570 png_warning(png_ptr, "Incorrect IEND chunk length"); 610 png_warning(png_ptr, "Incorrect IEND chunk length");
571 } 611 }
572 png_crc_finish(png_ptr, length); 612 png_crc_finish(png_ptr, length);
573 613
574 info_ptr =info_ptr; /* quiet compiler warnings about unused info_ptr */ 614 info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
575 } 615 }
576 616
577 #if defined(PNG_READ_gAMA_SUPPORTED) 617 #if defined(PNG_READ_gAMA_SUPPORTED)
578 void /* PRIVATE */ 618 void /* PRIVATE */
579 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 619 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
580 { 620 {
581 png_fixed_point igamma; 621 png_fixed_point igamma;
582 #ifdef PNG_FLOATING_POINT_SUPPORTED 622 #ifdef PNG_FLOATING_POINT_SUPPORTED
583 float file_gamma; 623 float file_gamma;
584 #endif 624 #endif
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 png_ptr->sig_bit.alpha = buf[1]; 758 png_ptr->sig_bit.alpha = buf[1];
719 } 759 }
720 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); 760 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
721 } 761 }
722 #endif 762 #endif
723 763
724 #if defined(PNG_READ_cHRM_SUPPORTED) 764 #if defined(PNG_READ_cHRM_SUPPORTED)
725 void /* PRIVATE */ 765 void /* PRIVATE */
726 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 766 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
727 { 767 {
728 png_byte buf[4]; 768 png_byte buf[32];
729 #ifdef PNG_FLOATING_POINT_SUPPORTED 769 #ifdef PNG_FLOATING_POINT_SUPPORTED
730 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; 770 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
731 #endif 771 #endif
732 png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, 772 png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
733 int_y_green, int_x_blue, int_y_blue; 773 int_y_green, int_x_blue, int_y_blue;
734 774
735 png_uint_32 uint_x, uint_y; 775 png_uint_32 uint_x, uint_y;
736 776
737 png_debug(1, "in png_handle_cHRM\n"); 777 png_debug(1, "in png_handle_cHRM\n");
738 778
(...skipping 20 matching lines...) Expand all
759 return; 799 return;
760 } 800 }
761 801
762 if (length != 32) 802 if (length != 32)
763 { 803 {
764 png_warning(png_ptr, "Incorrect cHRM chunk length"); 804 png_warning(png_ptr, "Incorrect cHRM chunk length");
765 png_crc_finish(png_ptr, length); 805 png_crc_finish(png_ptr, length);
766 return; 806 return;
767 } 807 }
768 808
769 png_crc_read(png_ptr, buf, 4); 809 png_crc_read(png_ptr, buf, 32);
810 if (png_crc_finish(png_ptr, 0))
811 return;
812
770 uint_x = png_get_uint_32(buf); 813 uint_x = png_get_uint_32(buf);
771 814 uint_y = png_get_uint_32(buf + 4);
772 png_crc_read(png_ptr, buf, 4);
773 uint_y = png_get_uint_32(buf);
774
775 if (uint_x > 80000L || uint_y > 80000L || 815 if (uint_x > 80000L || uint_y > 80000L ||
776 uint_x + uint_y > 100000L) 816 uint_x + uint_y > 100000L)
777 { 817 {
778 png_warning(png_ptr, "Invalid cHRM white point"); 818 png_warning(png_ptr, "Invalid cHRM white point");
779 png_crc_finish(png_ptr, 24);
780 return; 819 return;
781 } 820 }
782 int_x_white = (png_fixed_point)uint_x; 821 int_x_white = (png_fixed_point)uint_x;
783 int_y_white = (png_fixed_point)uint_y; 822 int_y_white = (png_fixed_point)uint_y;
784 823
785 png_crc_read(png_ptr, buf, 4); 824 uint_x = png_get_uint_32(buf + 8);
786 uint_x = png_get_uint_32(buf); 825 uint_y = png_get_uint_32(buf + 12);
787
788 png_crc_read(png_ptr, buf, 4);
789 uint_y = png_get_uint_32(buf);
790
791 if (uint_x + uint_y > 100000L) 826 if (uint_x + uint_y > 100000L)
792 { 827 {
793 png_warning(png_ptr, "Invalid cHRM red point"); 828 png_warning(png_ptr, "Invalid cHRM red point");
794 png_crc_finish(png_ptr, 16);
795 return; 829 return;
796 } 830 }
797 int_x_red = (png_fixed_point)uint_x; 831 int_x_red = (png_fixed_point)uint_x;
798 int_y_red = (png_fixed_point)uint_y; 832 int_y_red = (png_fixed_point)uint_y;
799 833
800 png_crc_read(png_ptr, buf, 4); 834 uint_x = png_get_uint_32(buf + 16);
801 uint_x = png_get_uint_32(buf); 835 uint_y = png_get_uint_32(buf + 20);
802
803 png_crc_read(png_ptr, buf, 4);
804 uint_y = png_get_uint_32(buf);
805
806 if (uint_x + uint_y > 100000L) 836 if (uint_x + uint_y > 100000L)
807 { 837 {
808 png_warning(png_ptr, "Invalid cHRM green point"); 838 png_warning(png_ptr, "Invalid cHRM green point");
809 png_crc_finish(png_ptr, 8);
810 return; 839 return;
811 } 840 }
812 int_x_green = (png_fixed_point)uint_x; 841 int_x_green = (png_fixed_point)uint_x;
813 int_y_green = (png_fixed_point)uint_y; 842 int_y_green = (png_fixed_point)uint_y;
814 843
815 png_crc_read(png_ptr, buf, 4); 844 uint_x = png_get_uint_32(buf + 24);
816 uint_x = png_get_uint_32(buf); 845 uint_y = png_get_uint_32(buf + 28);
817
818 png_crc_read(png_ptr, buf, 4);
819 uint_y = png_get_uint_32(buf);
820
821 if (uint_x + uint_y > 100000L) 846 if (uint_x + uint_y > 100000L)
822 { 847 {
823 png_warning(png_ptr, "Invalid cHRM blue point"); 848 png_warning(png_ptr, "Invalid cHRM blue point");
824 png_crc_finish(png_ptr, 0);
825 return; 849 return;
826 } 850 }
827 int_x_blue = (png_fixed_point)uint_x; 851 int_x_blue = (png_fixed_point)uint_x;
828 int_y_blue = (png_fixed_point)uint_y; 852 int_y_blue = (png_fixed_point)uint_y;
829 853
830 #ifdef PNG_FLOATING_POINT_SUPPORTED 854 #ifdef PNG_FLOATING_POINT_SUPPORTED
831 white_x = (float)int_x_white / (float)100000.0; 855 white_x = (float)int_x_white / (float)100000.0;
832 white_y = (float)int_y_white / (float)100000.0; 856 white_y = (float)int_y_white / (float)100000.0;
833 red_x = (float)int_x_red / (float)100000.0; 857 red_x = (float)int_x_red / (float)100000.0;
834 red_y = (float)int_y_red / (float)100000.0; 858 red_y = (float)int_y_red / (float)100000.0;
(...skipping 12 matching lines...) Expand all
847 PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) || 871 PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) ||
848 PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) || 872 PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) ||
849 PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) || 873 PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
850 PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) || 874 PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) ||
851 PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000)) 875 PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000))
852 { 876 {
853 png_warning(png_ptr, 877 png_warning(png_ptr,
854 "Ignoring incorrect cHRM value when sRGB is also present"); 878 "Ignoring incorrect cHRM value when sRGB is also present");
855 #ifndef PNG_NO_CONSOLE_IO 879 #ifndef PNG_NO_CONSOLE_IO
856 #ifdef PNG_FLOATING_POINT_SUPPORTED 880 #ifdef PNG_FLOATING_POINT_SUPPORTED
857 fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n", 881 fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
858 white_x, white_y, red_x, red_y); 882 white_x, white_y, red_x, red_y);
859 fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n", 883 fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
860 green_x, green_y, blue_x, blue_y); 884 green_x, green_y, blue_x, blue_y);
861 #else 885 #else
862 fprintf(stderr,"wx=%ld, wy=%ld, rx=%ld, ry=%ld\n", 886 fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
863 int_x_white, int_y_white, int_x_red, int_y_red); 887 int_x_white, int_y_white, int_x_red, int_y_red);
864 fprintf(stderr,"gx=%ld, gy=%ld, bx=%ld, by=%ld\n", 888 fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
865 int_x_green, int_y_green, int_x_blue, int_y_blue); 889 int_x_green, int_y_green, int_x_blue, int_y_blue);
866 #endif 890 #endif
867 #endif /* PNG_NO_CONSOLE_IO */ 891 #endif /* PNG_NO_CONSOLE_IO */
868 } 892 }
869 png_crc_finish(png_ptr, 0);
870 return; 893 return;
871 } 894 }
872 #endif /* PNG_READ_sRGB_SUPPORTED */ 895 #endif /* PNG_READ_sRGB_SUPPORTED */
873 896
874 #ifdef PNG_FLOATING_POINT_SUPPORTED 897 #ifdef PNG_FLOATING_POINT_SUPPORTED
875 png_set_cHRM(png_ptr, info_ptr, 898 png_set_cHRM(png_ptr, info_ptr,
876 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); 899 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
877 #endif 900 #endif
878 #ifdef PNG_FIXED_POINT_SUPPORTED 901 #ifdef PNG_FIXED_POINT_SUPPORTED
879 png_set_cHRM_fixed(png_ptr, info_ptr, 902 png_set_cHRM_fixed(png_ptr, info_ptr,
880 int_x_white, int_y_white, int_x_red, int_y_red, int_x_green, 903 int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
881 int_y_green, int_x_blue, int_y_blue); 904 int_y_green, int_x_blue, int_y_blue);
882 #endif 905 #endif
883 if (png_crc_finish(png_ptr, 0))
884 return;
885 } 906 }
886 #endif 907 #endif
887 908
888 #if defined(PNG_READ_sRGB_SUPPORTED) 909 #if defined(PNG_READ_sRGB_SUPPORTED)
889 void /* PRIVATE */ 910 void /* PRIVATE */
890 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 911 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
891 { 912 {
892 int intent; 913 int intent;
893 png_byte buf[1]; 914 png_byte buf[1];
894 915
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 # ifdef PNG_FLOATING_POINT_SUPPORTED 963 # ifdef PNG_FLOATING_POINT_SUPPORTED
943 igamma=(png_fixed_point)(info_ptr->gamma * 100000.); 964 igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
944 # endif 965 # endif
945 #endif 966 #endif
946 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500)) 967 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
947 { 968 {
948 png_warning(png_ptr, 969 png_warning(png_ptr,
949 "Ignoring incorrect gAMA value when sRGB is also present"); 970 "Ignoring incorrect gAMA value when sRGB is also present");
950 #ifndef PNG_NO_CONSOLE_IO 971 #ifndef PNG_NO_CONSOLE_IO
951 # ifdef PNG_FIXED_POINT_SUPPORTED 972 # ifdef PNG_FIXED_POINT_SUPPORTED
952 fprintf(stderr,"incorrect gamma=(%d/100000)\n",(int)png_ptr->int_gamma) ; 973 fprintf(stderr, "incorrect gamma=(%d/100000)\n",
974 (int)png_ptr->int_gamma);
953 # else 975 # else
954 # ifdef PNG_FLOATING_POINT_SUPPORTED 976 # ifdef PNG_FLOATING_POINT_SUPPORTED
955 fprintf(stderr,"incorrect gamma=%f\n",png_ptr->gamma); 977 fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
956 # endif 978 # endif
957 # endif 979 # endif
958 #endif 980 #endif
959 } 981 }
960 } 982 }
961 #endif /* PNG_READ_gAMA_SUPPORTED */ 983 #endif /* PNG_READ_gAMA_SUPPORTED */
962 984
963 #ifdef PNG_READ_cHRM_SUPPORTED 985 #ifdef PNG_READ_cHRM_SUPPORTED
964 #ifdef PNG_FIXED_POINT_SUPPORTED 986 #ifdef PNG_FIXED_POINT_SUPPORTED
965 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)) 987 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
(...skipping 14 matching lines...) Expand all
980 1002
981 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent); 1003 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
982 } 1004 }
983 #endif /* PNG_READ_sRGB_SUPPORTED */ 1005 #endif /* PNG_READ_sRGB_SUPPORTED */
984 1006
985 #if defined(PNG_READ_iCCP_SUPPORTED) 1007 #if defined(PNG_READ_iCCP_SUPPORTED)
986 void /* PRIVATE */ 1008 void /* PRIVATE */
987 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1009 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
988 /* Note: this does not properly handle chunks that are > 64K under DOS */ 1010 /* Note: this does not properly handle chunks that are > 64K under DOS */
989 { 1011 {
990 png_charp chunkdata;
991 png_byte compression_type; 1012 png_byte compression_type;
992 png_bytep pC; 1013 png_bytep pC;
993 png_charp profile; 1014 png_charp profile;
994 png_uint_32 skip = 0; 1015 png_uint_32 skip = 0;
995 png_uint_32 profile_size, profile_length; 1016 png_uint_32 profile_size, profile_length;
996 png_size_t slength, prefix_length, data_length; 1017 png_size_t slength, prefix_length, data_length;
997 1018
998 png_debug(1, "in png_handle_iCCP\n"); 1019 png_debug(1, "in png_handle_iCCP\n");
999 1020
1000 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1021 if (!(png_ptr->mode & PNG_HAVE_IHDR))
(...skipping 17 matching lines...) Expand all
1018 1039
1019 #ifdef PNG_MAX_MALLOC_64K 1040 #ifdef PNG_MAX_MALLOC_64K
1020 if (length > (png_uint_32)65535L) 1041 if (length > (png_uint_32)65535L)
1021 { 1042 {
1022 png_warning(png_ptr, "iCCP chunk too large to fit in memory"); 1043 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
1023 skip = length - (png_uint_32)65535L; 1044 skip = length - (png_uint_32)65535L;
1024 length = (png_uint_32)65535L; 1045 length = (png_uint_32)65535L;
1025 } 1046 }
1026 #endif 1047 #endif
1027 1048
1028 chunkdata = (png_charp)png_malloc(png_ptr, length + 1); 1049 png_free(png_ptr, png_ptr->chunkdata);
1050 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1029 slength = (png_size_t)length; 1051 slength = (png_size_t)length;
1030 png_crc_read(png_ptr, (png_bytep)chunkdata, slength); 1052 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1031 1053
1032 if (png_crc_finish(png_ptr, skip)) 1054 if (png_crc_finish(png_ptr, skip))
1033 { 1055 {
1034 png_free(png_ptr, chunkdata); 1056 png_free(png_ptr, png_ptr->chunkdata);
1057 png_ptr->chunkdata = NULL;
1035 return; 1058 return;
1036 } 1059 }
1037 1060
1038 chunkdata[slength] = 0x00; 1061 png_ptr->chunkdata[slength] = 0x00;
1039 1062
1040 for (profile = chunkdata; *profile; profile++) 1063 for (profile = png_ptr->chunkdata; *profile; profile++)
1041 /* empty loop to find end of name */ ; 1064 /* empty loop to find end of name */ ;
1042 1065
1043 ++profile; 1066 ++profile;
1044 1067
1045 /* there should be at least one zero (the compression type byte) 1068 /* there should be at least one zero (the compression type byte)
1046 following the separator, and we should be on it */ 1069 following the separator, and we should be on it */
1047 if ( profile >= chunkdata + slength - 1) 1070 if ( profile >= png_ptr->chunkdata + slength - 1)
1048 { 1071 {
1049 png_free(png_ptr, chunkdata); 1072 png_free(png_ptr, png_ptr->chunkdata);
1073 png_ptr->chunkdata = NULL;
1050 png_warning(png_ptr, "Malformed iCCP chunk"); 1074 png_warning(png_ptr, "Malformed iCCP chunk");
1051 return; 1075 return;
1052 } 1076 }
1053 1077
1054 /* compression_type should always be zero */ 1078 /* compression_type should always be zero */
1055 compression_type = *profile++; 1079 compression_type = *profile++;
1056 if (compression_type) 1080 if (compression_type)
1057 { 1081 {
1058 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk"); 1082 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
1059 compression_type=0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8 1083 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
1060 wrote nonzero) */ 1084 wrote nonzero) */
1061 } 1085 }
1062 1086
1063 prefix_length = profile - chunkdata; 1087 prefix_length = profile - png_ptr->chunkdata;
1064 chunkdata = png_decompress_chunk(png_ptr, compression_type, chunkdata, 1088 png_decompress_chunk(png_ptr, compression_type,
1065 slength, prefix_length, &data_length); 1089 slength, prefix_length, &data_length);
1066 1090
1067 profile_length = data_length - prefix_length; 1091 profile_length = data_length - prefix_length;
1068 1092
1069 if ( prefix_length > data_length || profile_length < 4) 1093 if ( prefix_length > data_length || profile_length < 4)
1070 { 1094 {
1071 png_free(png_ptr, chunkdata); 1095 png_free(png_ptr, png_ptr->chunkdata);
1096 png_ptr->chunkdata = NULL;
1072 png_warning(png_ptr, "Profile size field missing from iCCP chunk"); 1097 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
1073 return; 1098 return;
1074 } 1099 }
1075 1100
1076 /* Check the profile_size recorded in the first 32 bits of the ICC profile */ 1101 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
1077 pC = (png_bytep)(chunkdata+prefix_length); 1102 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
1078 profile_size = ((*(pC ))<<24) | 1103 profile_size = ((*(pC ))<<24) |
1079 ((*(pC+1))<<16) | 1104 ((*(pC + 1))<<16) |
1080 ((*(pC+2))<< 8) | 1105 ((*(pC + 2))<< 8) |
1081 ((*(pC+3)) ); 1106 ((*(pC + 3)) );
1082 1107
1083 if(profile_size < profile_length) 1108 if (profile_size < profile_length)
1084 profile_length = profile_size; 1109 profile_length = profile_size;
1085 1110
1086 if(profile_size > profile_length) 1111 if (profile_size > profile_length)
1087 { 1112 {
1088 png_free(png_ptr, chunkdata); 1113 png_free(png_ptr, png_ptr->chunkdata);
1114 png_ptr->chunkdata = NULL;
1089 png_warning(png_ptr, "Ignoring truncated iCCP profile."); 1115 png_warning(png_ptr, "Ignoring truncated iCCP profile.");
1090 return; 1116 return;
1091 } 1117 }
1092 1118
1093 png_set_iCCP(png_ptr, info_ptr, chunkdata, compression_type, 1119 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
1094 chunkdata + prefix_length, profile_length); 1120 compression_type, png_ptr->chunkdata + prefix_length, profile_length);
1095 png_free(png_ptr, chunkdata); 1121 png_free(png_ptr, png_ptr->chunkdata);
1122 png_ptr->chunkdata = NULL;
1096 } 1123 }
1097 #endif /* PNG_READ_iCCP_SUPPORTED */ 1124 #endif /* PNG_READ_iCCP_SUPPORTED */
1098 1125
1099 #if defined(PNG_READ_sPLT_SUPPORTED) 1126 #if defined(PNG_READ_sPLT_SUPPORTED)
1100 void /* PRIVATE */ 1127 void /* PRIVATE */
1101 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1128 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1102 /* Note: this does not properly handle chunks that are > 64K under DOS */ 1129 /* Note: this does not properly handle chunks that are > 64K under DOS */
1103 { 1130 {
1104 png_bytep chunkdata;
1105 png_bytep entry_start; 1131 png_bytep entry_start;
1106 png_sPLT_t new_palette; 1132 png_sPLT_t new_palette;
1107 #ifdef PNG_NO_POINTER_INDEXING 1133 #ifdef PNG_NO_POINTER_INDEXING
1108 png_sPLT_entryp pp; 1134 png_sPLT_entryp pp;
1109 #endif 1135 #endif
1110 int data_length, entry_size, i; 1136 int data_length, entry_size, i;
1111 png_uint_32 skip = 0; 1137 png_uint_32 skip = 0;
1112 png_size_t slength; 1138 png_size_t slength;
1113 1139
1114 png_debug(1, "in png_handle_sPLT\n"); 1140 png_debug(1, "in png_handle_sPLT\n");
1115 1141
1116 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1142 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1117 png_error(png_ptr, "Missing IHDR before sPLT"); 1143 png_error(png_ptr, "Missing IHDR before sPLT");
1118 else if (png_ptr->mode & PNG_HAVE_IDAT) 1144 else if (png_ptr->mode & PNG_HAVE_IDAT)
1119 { 1145 {
1120 png_warning(png_ptr, "Invalid sPLT after IDAT"); 1146 png_warning(png_ptr, "Invalid sPLT after IDAT");
1121 png_crc_finish(png_ptr, length); 1147 png_crc_finish(png_ptr, length);
1122 return; 1148 return;
1123 } 1149 }
1124 1150
1125 #ifdef PNG_MAX_MALLOC_64K 1151 #ifdef PNG_MAX_MALLOC_64K
1126 if (length > (png_uint_32)65535L) 1152 if (length > (png_uint_32)65535L)
1127 { 1153 {
1128 png_warning(png_ptr, "sPLT chunk too large to fit in memory"); 1154 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
1129 skip = length - (png_uint_32)65535L; 1155 skip = length - (png_uint_32)65535L;
1130 length = (png_uint_32)65535L; 1156 length = (png_uint_32)65535L;
1131 } 1157 }
1132 #endif 1158 #endif
1133 1159
1134 chunkdata = (png_bytep)png_malloc(png_ptr, length + 1); 1160 png_free(png_ptr, png_ptr->chunkdata);
1161 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
1135 slength = (png_size_t)length; 1162 slength = (png_size_t)length;
1136 png_crc_read(png_ptr, (png_bytep)chunkdata, slength); 1163 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1137 1164
1138 if (png_crc_finish(png_ptr, skip)) 1165 if (png_crc_finish(png_ptr, skip))
1139 { 1166 {
1140 png_free(png_ptr, chunkdata); 1167 png_free(png_ptr, png_ptr->chunkdata);
1168 png_ptr->chunkdata = NULL;
1141 return; 1169 return;
1142 } 1170 }
1143 1171
1144 chunkdata[slength] = 0x00; 1172 png_ptr->chunkdata[slength] = 0x00;
1145 1173
1146 for (entry_start = chunkdata; *entry_start; entry_start++) 1174 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++ )
1147 /* empty loop to find end of name */ ; 1175 /* empty loop to find end of name */ ;
1148 ++entry_start; 1176 ++entry_start;
1149 1177
1150 /* a sample depth should follow the separator, and we should be on it */ 1178 /* a sample depth should follow the separator, and we should be on it */
1151 if (entry_start > chunkdata + slength - 2) 1179 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
1152 { 1180 {
1153 png_free(png_ptr, chunkdata); 1181 png_free(png_ptr, png_ptr->chunkdata);
1182 png_ptr->chunkdata = NULL;
1154 png_warning(png_ptr, "malformed sPLT chunk"); 1183 png_warning(png_ptr, "malformed sPLT chunk");
1155 return; 1184 return;
1156 } 1185 }
1157 1186
1158 new_palette.depth = *entry_start++; 1187 new_palette.depth = *entry_start++;
1159 entry_size = (new_palette.depth == 8 ? 6 : 10); 1188 entry_size = (new_palette.depth == 8 ? 6 : 10);
1160 data_length = (slength - (entry_start - chunkdata)); 1189 data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
1161 1190
1162 /* integrity-check the data length */ 1191 /* integrity-check the data length */
1163 if (data_length % entry_size) 1192 if (data_length % entry_size)
1164 { 1193 {
1165 png_free(png_ptr, chunkdata); 1194 png_free(png_ptr, png_ptr->chunkdata);
1195 png_ptr->chunkdata = NULL;
1166 png_warning(png_ptr, "sPLT chunk has bad length"); 1196 png_warning(png_ptr, "sPLT chunk has bad length");
1167 return; 1197 return;
1168 } 1198 }
1169 1199
1170 new_palette.nentries = (png_int_32) ( data_length / entry_size); 1200 new_palette.nentries = (png_int_32) ( data_length / entry_size);
1171 if ((png_uint_32) new_palette.nentries > (png_uint_32) (PNG_SIZE_MAX / 1201 if ((png_uint_32) new_palette.nentries >
1172 png_sizeof(png_sPLT_entry))) 1202 (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
1173 { 1203 {
1174 png_warning(png_ptr, "sPLT chunk too long"); 1204 png_warning(png_ptr, "sPLT chunk too long");
1175 return; 1205 return;
1176 } 1206 }
1177 new_palette.entries = (png_sPLT_entryp)png_malloc_warn( 1207 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1178 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry)); 1208 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
1179 if (new_palette.entries == NULL) 1209 if (new_palette.entries == NULL)
1180 { 1210 {
1181 png_warning(png_ptr, "sPLT chunk requires too much memory"); 1211 png_warning(png_ptr, "sPLT chunk requires too much memory");
1182 return; 1212 return;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 pp[i].red = png_get_uint_16(entry_start); entry_start += 2; 1250 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1221 pp[i].green = png_get_uint_16(entry_start); entry_start += 2; 1251 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1222 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; 1252 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1223 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; 1253 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1224 } 1254 }
1225 pp->frequency = png_get_uint_16(entry_start); entry_start += 2; 1255 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1226 } 1256 }
1227 #endif 1257 #endif
1228 1258
1229 /* discard all chunk data except the name and stash that */ 1259 /* discard all chunk data except the name and stash that */
1230 new_palette.name = (png_charp)chunkdata; 1260 new_palette.name = png_ptr->chunkdata;
1231 1261
1232 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); 1262 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1233 1263
1234 png_free(png_ptr, chunkdata); 1264 png_free(png_ptr, png_ptr->chunkdata);
1265 png_ptr->chunkdata = NULL;
1235 png_free(png_ptr, new_palette.entries); 1266 png_free(png_ptr, new_palette.entries);
1236 } 1267 }
1237 #endif /* PNG_READ_sPLT_SUPPORTED */ 1268 #endif /* PNG_READ_sPLT_SUPPORTED */
1238 1269
1239 #if defined(PNG_READ_tRNS_SUPPORTED) 1270 #if defined(PNG_READ_tRNS_SUPPORTED)
1240 void /* PRIVATE */ 1271 void /* PRIVATE */
1241 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1272 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1242 { 1273 {
1243 png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; 1274 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1244 1275
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 1413
1383 /* We convert the index value into RGB components so that we can allow 1414 /* We convert the index value into RGB components so that we can allow
1384 * arbitrary RGB values for background when we have transparency, and 1415 * arbitrary RGB values for background when we have transparency, and
1385 * so it is easy to determine the RGB values of the background color 1416 * so it is easy to determine the RGB values of the background color
1386 * from the info_ptr struct. */ 1417 * from the info_ptr struct. */
1387 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 1418 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1388 { 1419 {
1389 png_ptr->background.index = buf[0]; 1420 png_ptr->background.index = buf[0];
1390 if (info_ptr && info_ptr->num_palette) 1421 if (info_ptr && info_ptr->num_palette)
1391 { 1422 {
1392 if(buf[0] > info_ptr->num_palette) 1423 if (buf[0] > info_ptr->num_palette)
1393 { 1424 {
1394 png_warning(png_ptr, "Incorrect bKGD chunk index value"); 1425 png_warning(png_ptr, "Incorrect bKGD chunk index value");
1395 return; 1426 return;
1396 } 1427 }
1397 png_ptr->background.red = 1428 png_ptr->background.red =
1398 (png_uint_16)png_ptr->palette[buf[0]].red; 1429 (png_uint_16)png_ptr->palette[buf[0]].red;
1399 png_ptr->background.green = 1430 png_ptr->background.green =
1400 (png_uint_16)png_ptr->palette[buf[0]].green; 1431 (png_uint_16)png_ptr->palette[buf[0]].green;
1401 png_ptr->background.blue = 1432 png_ptr->background.blue =
1402 (png_uint_16)png_ptr->palette[buf[0]].blue; 1433 (png_uint_16)png_ptr->palette[buf[0]].blue;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1558 unit_type = buf[8]; 1589 unit_type = buf[8];
1559 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); 1590 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
1560 } 1591 }
1561 #endif 1592 #endif
1562 1593
1563 #if defined(PNG_READ_pCAL_SUPPORTED) 1594 #if defined(PNG_READ_pCAL_SUPPORTED)
1564 /* read the pCAL chunk (described in the PNG Extensions document) */ 1595 /* read the pCAL chunk (described in the PNG Extensions document) */
1565 void /* PRIVATE */ 1596 void /* PRIVATE */
1566 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1597 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1567 { 1598 {
1568 png_charp purpose;
1569 png_int_32 X0, X1; 1599 png_int_32 X0, X1;
1570 png_byte type, nparams; 1600 png_byte type, nparams;
1571 png_charp buf, units, endptr; 1601 png_charp buf, units, endptr;
1572 png_charpp params; 1602 png_charpp params;
1573 png_size_t slength; 1603 png_size_t slength;
1574 int i; 1604 int i;
1575 1605
1576 png_debug(1, "in png_handle_pCAL\n"); 1606 png_debug(1, "in png_handle_pCAL\n");
1577 1607
1578 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1608 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1579 png_error(png_ptr, "Missing IHDR before pCAL"); 1609 png_error(png_ptr, "Missing IHDR before pCAL");
1580 else if (png_ptr->mode & PNG_HAVE_IDAT) 1610 else if (png_ptr->mode & PNG_HAVE_IDAT)
1581 { 1611 {
1582 png_warning(png_ptr, "Invalid pCAL after IDAT"); 1612 png_warning(png_ptr, "Invalid pCAL after IDAT");
1583 png_crc_finish(png_ptr, length); 1613 png_crc_finish(png_ptr, length);
1584 return; 1614 return;
1585 } 1615 }
1586 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL)) 1616 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
1587 { 1617 {
1588 png_warning(png_ptr, "Duplicate pCAL chunk"); 1618 png_warning(png_ptr, "Duplicate pCAL chunk");
1589 png_crc_finish(png_ptr, length); 1619 png_crc_finish(png_ptr, length);
1590 return; 1620 return;
1591 } 1621 }
1592 1622
1593 png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n", 1623 png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
1594 length + 1); 1624 length + 1);
1595 purpose = (png_charp)png_malloc_warn(png_ptr, length + 1); 1625 png_free(png_ptr, png_ptr->chunkdata);
1596 if (purpose == NULL) 1626 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1627 if (png_ptr->chunkdata == NULL)
1597 { 1628 {
1598 png_warning(png_ptr, "No memory for pCAL purpose."); 1629 png_warning(png_ptr, "No memory for pCAL purpose.");
1599 return; 1630 return;
1600 } 1631 }
1601 slength = (png_size_t)length; 1632 slength = (png_size_t)length;
1602 png_crc_read(png_ptr, (png_bytep)purpose, slength); 1633 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1603 1634
1604 if (png_crc_finish(png_ptr, 0)) 1635 if (png_crc_finish(png_ptr, 0))
1605 { 1636 {
1606 png_free(png_ptr, purpose); 1637 png_free(png_ptr, png_ptr->chunkdata);
1638 png_ptr->chunkdata = NULL;
1607 return; 1639 return;
1608 } 1640 }
1609 1641
1610 purpose[slength] = 0x00; /* null terminate the last string */ 1642 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
1611 1643
1612 png_debug(3, "Finding end of pCAL purpose string\n"); 1644 png_debug(3, "Finding end of pCAL purpose string\n");
1613 for (buf = purpose; *buf; buf++) 1645 for (buf = png_ptr->chunkdata; *buf; buf++)
1614 /* empty loop */ ; 1646 /* empty loop */ ;
1615 1647
1616 endptr = purpose + slength; 1648 endptr = png_ptr->chunkdata + slength;
1617 1649
1618 /* We need to have at least 12 bytes after the purpose string 1650 /* We need to have at least 12 bytes after the purpose string
1619 in order to get the parameter information. */ 1651 in order to get the parameter information. */
1620 if (endptr <= buf + 12) 1652 if (endptr <= buf + 12)
1621 { 1653 {
1622 png_warning(png_ptr, "Invalid pCAL data"); 1654 png_warning(png_ptr, "Invalid pCAL data");
1623 png_free(png_ptr, purpose); 1655 png_free(png_ptr, png_ptr->chunkdata);
1656 png_ptr->chunkdata = NULL;
1624 return; 1657 return;
1625 } 1658 }
1626 1659
1627 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n"); 1660 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
1628 X0 = png_get_int_32((png_bytep)buf+1); 1661 X0 = png_get_int_32((png_bytep)buf+1);
1629 X1 = png_get_int_32((png_bytep)buf+5); 1662 X1 = png_get_int_32((png_bytep)buf+5);
1630 type = buf[9]; 1663 type = buf[9];
1631 nparams = buf[10]; 1664 nparams = buf[10];
1632 units = buf + 11; 1665 units = buf + 11;
1633 1666
1634 png_debug(3, "Checking pCAL equation type and number of parameters\n"); 1667 png_debug(3, "Checking pCAL equation type and number of parameters\n");
1635 /* Check that we have the right number of parameters for known 1668 /* Check that we have the right number of parameters for known
1636 equation types. */ 1669 equation types. */
1637 if ((type == PNG_EQUATION_LINEAR && nparams != 2) || 1670 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
1638 (type == PNG_EQUATION_BASE_E && nparams != 3) || 1671 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
1639 (type == PNG_EQUATION_ARBITRARY && nparams != 3) || 1672 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
1640 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) 1673 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
1641 { 1674 {
1642 png_warning(png_ptr, "Invalid pCAL parameters for equation type"); 1675 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
1643 png_free(png_ptr, purpose); 1676 png_free(png_ptr, png_ptr->chunkdata);
1677 png_ptr->chunkdata = NULL;
1644 return; 1678 return;
1645 } 1679 }
1646 else if (type >= PNG_EQUATION_LAST) 1680 else if (type >= PNG_EQUATION_LAST)
1647 { 1681 {
1648 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); 1682 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1649 } 1683 }
1650 1684
1651 for (buf = units; *buf; buf++) 1685 for (buf = units; *buf; buf++)
1652 /* Empty loop to move past the units string. */ ; 1686 /* Empty loop to move past the units string. */ ;
1653 1687
1654 png_debug(3, "Allocating pCAL parameters array\n"); 1688 png_debug(3, "Allocating pCAL parameters array\n");
1655 params = (png_charpp)png_malloc_warn(png_ptr, (png_uint_32)(nparams 1689 params = (png_charpp)png_malloc_warn(png_ptr,
1656 *png_sizeof(png_charp))) ; 1690 (png_uint_32)(nparams * png_sizeof(png_charp))) ;
1657 if (params == NULL) 1691 if (params == NULL)
1658 { 1692 {
1659 png_free(png_ptr, purpose); 1693 png_free(png_ptr, png_ptr->chunkdata);
1694 png_ptr->chunkdata = NULL;
1660 png_warning(png_ptr, "No memory for pCAL params."); 1695 png_warning(png_ptr, "No memory for pCAL params.");
1661 return; 1696 return;
1662 } 1697 }
1663 1698
1664 /* Get pointers to the start of each parameter string. */ 1699 /* Get pointers to the start of each parameter string. */
1665 for (i = 0; i < (int)nparams; i++) 1700 for (i = 0; i < (int)nparams; i++)
1666 { 1701 {
1667 buf++; /* Skip the null string terminator from previous parameter. */ 1702 buf++; /* Skip the null string terminator from previous parameter. */
1668 1703
1669 png_debug1(3, "Reading pCAL parameter %d\n", i); 1704 png_debug1(3, "Reading pCAL parameter %d\n", i);
1670 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++) 1705 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
1671 /* Empty loop to move past each parameter string */ ; 1706 /* Empty loop to move past each parameter string */ ;
1672 1707
1673 /* Make sure we haven't run out of data yet */ 1708 /* Make sure we haven't run out of data yet */
1674 if (buf > endptr) 1709 if (buf > endptr)
1675 { 1710 {
1676 png_warning(png_ptr, "Invalid pCAL data"); 1711 png_warning(png_ptr, "Invalid pCAL data");
1677 png_free(png_ptr, purpose); 1712 png_free(png_ptr, png_ptr->chunkdata);
1713 png_ptr->chunkdata = NULL;
1678 png_free(png_ptr, params); 1714 png_free(png_ptr, params);
1679 return; 1715 return;
1680 } 1716 }
1681 } 1717 }
1682 1718
1683 png_set_pCAL(png_ptr, info_ptr, purpose, X0, X1, type, nparams, 1719 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
1684 units, params); 1720 units, params);
1685 1721
1686 png_free(png_ptr, purpose); 1722 png_free(png_ptr, png_ptr->chunkdata);
1723 png_ptr->chunkdata = NULL;
1687 png_free(png_ptr, params); 1724 png_free(png_ptr, params);
1688 } 1725 }
1689 #endif 1726 #endif
1690 1727
1691 #if defined(PNG_READ_sCAL_SUPPORTED) 1728 #if defined(PNG_READ_sCAL_SUPPORTED)
1692 /* read the sCAL chunk */ 1729 /* read the sCAL chunk */
1693 void /* PRIVATE */ 1730 void /* PRIVATE */
1694 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1731 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1695 { 1732 {
1696 png_charp buffer, ep; 1733 png_charp ep;
1697 #ifdef PNG_FLOATING_POINT_SUPPORTED 1734 #ifdef PNG_FLOATING_POINT_SUPPORTED
1698 double width, height; 1735 double width, height;
1699 png_charp vp; 1736 png_charp vp;
1700 #else 1737 #else
1701 #ifdef PNG_FIXED_POINT_SUPPORTED 1738 #ifdef PNG_FIXED_POINT_SUPPORTED
1702 png_charp swidth, sheight; 1739 png_charp swidth, sheight;
1703 #endif 1740 #endif
1704 #endif 1741 #endif
1705 png_size_t slength; 1742 png_size_t slength;
1706 1743
1707 png_debug(1, "in png_handle_sCAL\n"); 1744 png_debug(1, "in png_handle_sCAL\n");
1708 1745
1709 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 1746 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1710 png_error(png_ptr, "Missing IHDR before sCAL"); 1747 png_error(png_ptr, "Missing IHDR before sCAL");
1711 else if (png_ptr->mode & PNG_HAVE_IDAT) 1748 else if (png_ptr->mode & PNG_HAVE_IDAT)
1712 { 1749 {
1713 png_warning(png_ptr, "Invalid sCAL after IDAT"); 1750 png_warning(png_ptr, "Invalid sCAL after IDAT");
1714 png_crc_finish(png_ptr, length); 1751 png_crc_finish(png_ptr, length);
1715 return; 1752 return;
1716 } 1753 }
1717 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL)) 1754 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
1718 { 1755 {
1719 png_warning(png_ptr, "Duplicate sCAL chunk"); 1756 png_warning(png_ptr, "Duplicate sCAL chunk");
1720 png_crc_finish(png_ptr, length); 1757 png_crc_finish(png_ptr, length);
1721 return; 1758 return;
1722 } 1759 }
1723 1760
1724 png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n", 1761 png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
1725 length + 1); 1762 length + 1);
1726 buffer = (png_charp)png_malloc_warn(png_ptr, length + 1); 1763 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1727 if (buffer == NULL) 1764 if (png_ptr->chunkdata == NULL)
1728 { 1765 {
1729 png_warning(png_ptr, "Out of memory while processing sCAL chunk"); 1766 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
1730 return; 1767 return;
1731 } 1768 }
1732 slength = (png_size_t)length; 1769 slength = (png_size_t)length;
1733 png_crc_read(png_ptr, (png_bytep)buffer, slength); 1770 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1734 1771
1735 if (png_crc_finish(png_ptr, 0)) 1772 if (png_crc_finish(png_ptr, 0))
1736 { 1773 {
1737 png_free(png_ptr, buffer); 1774 png_free(png_ptr, png_ptr->chunkdata);
1775 png_ptr->chunkdata = NULL;
1738 return; 1776 return;
1739 } 1777 }
1740 1778
1741 buffer[slength] = 0x00; /* null terminate the last string */ 1779 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
1742 1780
1743 ep = buffer + 1; /* skip unit byte */ 1781 ep = png_ptr->chunkdata + 1; /* skip unit byte */
1744 1782
1745 #ifdef PNG_FLOATING_POINT_SUPPORTED 1783 #ifdef PNG_FLOATING_POINT_SUPPORTED
1746 width = png_strtod(png_ptr, ep, &vp); 1784 width = png_strtod(png_ptr, ep, &vp);
1747 if (*vp) 1785 if (*vp)
1748 { 1786 {
1749 png_warning(png_ptr, "malformed width string in sCAL chunk"); 1787 png_warning(png_ptr, "malformed width string in sCAL chunk");
1750 return; 1788 return;
1751 } 1789 }
1752 #else 1790 #else
1753 #ifdef PNG_FIXED_POINT_SUPPORTED 1791 #ifdef PNG_FIXED_POINT_SUPPORTED
1754 swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); 1792 swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
1755 if (swidth == NULL) 1793 if (swidth == NULL)
1756 { 1794 {
1757 png_warning(png_ptr, "Out of memory while processing sCAL chunk width"); 1795 png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
1758 return; 1796 return;
1759 } 1797 }
1760 png_memcpy(swidth, ep, (png_size_t)png_strlen(ep)); 1798 png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
1761 #endif 1799 #endif
1762 #endif 1800 #endif
1763 1801
1764 for (ep = buffer; *ep; ep++) 1802 for (ep = png_ptr->chunkdata; *ep; ep++)
1765 /* empty loop */ ; 1803 /* empty loop */ ;
1766 ep++; 1804 ep++;
1767 1805
1768 if (buffer + slength < ep) 1806 if (png_ptr->chunkdata + slength < ep)
1769 { 1807 {
1770 png_warning(png_ptr, "Truncated sCAL chunk"); 1808 png_warning(png_ptr, "Truncated sCAL chunk");
1771 #if defined(PNG_FIXED_POINT_SUPPORTED) && \ 1809 #if defined(PNG_FIXED_POINT_SUPPORTED) && \
1772 !defined(PNG_FLOATING_POINT_SUPPORTED) 1810 !defined(PNG_FLOATING_POINT_SUPPORTED)
1773 png_free(png_ptr, swidth); 1811 png_free(png_ptr, swidth);
1774 #endif 1812 #endif
1775 png_free(png_ptr, buffer); 1813 png_free(png_ptr, png_ptr->chunkdata);
1776 return; 1814 png_ptr->chunkdata = NULL;
1815 return;
1777 } 1816 }
1778 1817
1779 #ifdef PNG_FLOATING_POINT_SUPPORTED 1818 #ifdef PNG_FLOATING_POINT_SUPPORTED
1780 height = png_strtod(png_ptr, ep, &vp); 1819 height = png_strtod(png_ptr, ep, &vp);
1781 if (*vp) 1820 if (*vp)
1782 { 1821 {
1783 png_warning(png_ptr, "malformed height string in sCAL chunk"); 1822 png_warning(png_ptr, "malformed height string in sCAL chunk");
1784 return; 1823 return;
1785 } 1824 }
1786 #else 1825 #else
1787 #ifdef PNG_FIXED_POINT_SUPPORTED 1826 #ifdef PNG_FIXED_POINT_SUPPORTED
1788 sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1); 1827 sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
1789 if (sheight == NULL) 1828 if (sheight == NULL)
1790 { 1829 {
1791 png_warning(png_ptr, "Out of memory while processing sCAL chunk height"); 1830 png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
1792 return; 1831 return;
1793 } 1832 }
1794 png_memcpy(sheight, ep, (png_size_t)png_strlen(ep)); 1833 png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
1795 #endif 1834 #endif
1796 #endif 1835 #endif
1797 1836
1798 if (buffer + slength < ep 1837 if (png_ptr->chunkdata + slength < ep
1799 #ifdef PNG_FLOATING_POINT_SUPPORTED 1838 #ifdef PNG_FLOATING_POINT_SUPPORTED
1800 || width <= 0. || height <= 0. 1839 || width <= 0. || height <= 0.
1801 #endif 1840 #endif
1802 ) 1841 )
1803 { 1842 {
1804 png_warning(png_ptr, "Invalid sCAL data"); 1843 png_warning(png_ptr, "Invalid sCAL data");
1805 png_free(png_ptr, buffer); 1844 png_free(png_ptr, png_ptr->chunkdata);
1845 png_ptr->chunkdata = NULL;
1806 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) 1846 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1807 png_free(png_ptr, swidth); 1847 png_free(png_ptr, swidth);
1808 png_free(png_ptr, sheight); 1848 png_free(png_ptr, sheight);
1809 #endif 1849 #endif
1810 return; 1850 return;
1811 } 1851 }
1812 1852
1813 1853
1814 #ifdef PNG_FLOATING_POINT_SUPPORTED 1854 #ifdef PNG_FLOATING_POINT_SUPPORTED
1815 png_set_sCAL(png_ptr, info_ptr, buffer[0], width, height); 1855 png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
1816 #else 1856 #else
1817 #ifdef PNG_FIXED_POINT_SUPPORTED 1857 #ifdef PNG_FIXED_POINT_SUPPORTED
1818 png_set_sCAL_s(png_ptr, info_ptr, buffer[0], swidth, sheight); 1858 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
1819 #endif 1859 #endif
1820 #endif 1860 #endif
1821 1861
1822 png_free(png_ptr, buffer); 1862 png_free(png_ptr, png_ptr->chunkdata);
1863 png_ptr->chunkdata = NULL;
1823 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) 1864 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
1824 png_free(png_ptr, swidth); 1865 png_free(png_ptr, swidth);
1825 png_free(png_ptr, sheight); 1866 png_free(png_ptr, sheight);
1826 #endif 1867 #endif
1827 } 1868 }
1828 #endif 1869 #endif
1829 1870
1830 #if defined(PNG_READ_tIME_SUPPORTED) 1871 #if defined(PNG_READ_tIME_SUPPORTED)
1831 void /* PRIVATE */ 1872 void /* PRIVATE */
1832 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 1873 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1892 1933
1893 #ifdef PNG_MAX_MALLOC_64K 1934 #ifdef PNG_MAX_MALLOC_64K
1894 if (length > (png_uint_32)65535L) 1935 if (length > (png_uint_32)65535L)
1895 { 1936 {
1896 png_warning(png_ptr, "tEXt chunk too large to fit in memory"); 1937 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
1897 skip = length - (png_uint_32)65535L; 1938 skip = length - (png_uint_32)65535L;
1898 length = (png_uint_32)65535L; 1939 length = (png_uint_32)65535L;
1899 } 1940 }
1900 #endif 1941 #endif
1901 1942
1902 key = (png_charp)png_malloc_warn(png_ptr, length + 1); 1943 png_free(png_ptr, png_ptr->chunkdata);
1903 if (key == NULL) 1944 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
1945 if (png_ptr->chunkdata == NULL)
1904 { 1946 {
1905 png_warning(png_ptr, "No memory to process text chunk."); 1947 png_warning(png_ptr, "No memory to process text chunk.");
1906 return; 1948 return;
1907 } 1949 }
1908 slength = (png_size_t)length; 1950 slength = (png_size_t)length;
1909 png_crc_read(png_ptr, (png_bytep)key, slength); 1951 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1910 1952
1911 if (png_crc_finish(png_ptr, skip)) 1953 if (png_crc_finish(png_ptr, skip))
1912 { 1954 {
1913 png_free(png_ptr, key); 1955 png_free(png_ptr, png_ptr->chunkdata);
1956 png_ptr->chunkdata = NULL;
1914 return; 1957 return;
1915 } 1958 }
1916 1959
1960 key = png_ptr->chunkdata;
1917 key[slength] = 0x00; 1961 key[slength] = 0x00;
1918 1962
1919 for (text = key; *text; text++) 1963 for (text = key; *text; text++)
1920 /* empty loop to find end of key */ ; 1964 /* empty loop to find end of key */ ;
1921 1965
1922 if (text != key + slength) 1966 if (text != key + slength)
1923 text++; 1967 text++;
1924 1968
1925 text_ptr = (png_textp)png_malloc_warn(png_ptr, 1969 text_ptr = (png_textp)png_malloc_warn(png_ptr,
1926 (png_uint_32)png_sizeof(png_text)); 1970 (png_uint_32)png_sizeof(png_text));
1927 if (text_ptr == NULL) 1971 if (text_ptr == NULL)
1928 { 1972 {
1929 png_warning(png_ptr, "Not enough memory to process text chunk."); 1973 png_warning(png_ptr, "Not enough memory to process text chunk.");
1930 png_free(png_ptr, key); 1974 png_free(png_ptr, png_ptr->chunkdata);
1975 png_ptr->chunkdata = NULL;
1931 return; 1976 return;
1932 } 1977 }
1933 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; 1978 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
1934 text_ptr->key = key; 1979 text_ptr->key = key;
1935 #ifdef PNG_iTXt_SUPPORTED 1980 #ifdef PNG_iTXt_SUPPORTED
1936 text_ptr->lang = NULL; 1981 text_ptr->lang = NULL;
1937 text_ptr->lang_key = NULL; 1982 text_ptr->lang_key = NULL;
1938 text_ptr->itxt_length = 0; 1983 text_ptr->itxt_length = 0;
1939 #endif 1984 #endif
1940 text_ptr->text = text; 1985 text_ptr->text = text;
1941 text_ptr->text_length = png_strlen(text); 1986 text_ptr->text_length = png_strlen(text);
1942 1987
1943 ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); 1988 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1944 1989
1945 png_free(png_ptr, key); 1990 png_free(png_ptr, png_ptr->chunkdata);
1991 png_ptr->chunkdata = NULL;
1946 png_free(png_ptr, text_ptr); 1992 png_free(png_ptr, text_ptr);
1947 if (ret) 1993 if (ret)
1948 png_warning(png_ptr, "Insufficient memory to process text chunk."); 1994 png_warning(png_ptr, "Insufficient memory to process text chunk.");
1949 } 1995 }
1950 #endif 1996 #endif
1951 1997
1952 #if defined(PNG_READ_zTXt_SUPPORTED) 1998 #if defined(PNG_READ_zTXt_SUPPORTED)
1953 /* note: this does not correctly handle chunks that are > 64K under DOS */ 1999 /* note: this does not correctly handle chunks that are > 64K under DOS */
1954 void /* PRIVATE */ 2000 void /* PRIVATE */
1955 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 2001 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
1956 { 2002 {
1957 png_textp text_ptr; 2003 png_textp text_ptr;
1958 png_charp chunkdata;
1959 png_charp text; 2004 png_charp text;
1960 int comp_type; 2005 int comp_type;
1961 int ret; 2006 int ret;
1962 png_size_t slength, prefix_len, data_len; 2007 png_size_t slength, prefix_len, data_len;
1963 2008
1964 png_debug(1, "in png_handle_zTXt\n"); 2009 png_debug(1, "in png_handle_zTXt\n");
1965 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2010 if (!(png_ptr->mode & PNG_HAVE_IHDR))
1966 png_error(png_ptr, "Missing IHDR before zTXt"); 2011 png_error(png_ptr, "Missing IHDR before zTXt");
1967 2012
1968 if (png_ptr->mode & PNG_HAVE_IDAT) 2013 if (png_ptr->mode & PNG_HAVE_IDAT)
1969 png_ptr->mode |= PNG_AFTER_IDAT; 2014 png_ptr->mode |= PNG_AFTER_IDAT;
1970 2015
1971 #ifdef PNG_MAX_MALLOC_64K 2016 #ifdef PNG_MAX_MALLOC_64K
1972 /* We will no doubt have problems with chunks even half this size, but 2017 /* We will no doubt have problems with chunks even half this size, but
1973 there is no hard and fast rule to tell us where to stop. */ 2018 there is no hard and fast rule to tell us where to stop. */
1974 if (length > (png_uint_32)65535L) 2019 if (length > (png_uint_32)65535L)
1975 { 2020 {
1976 png_warning(png_ptr,"zTXt chunk too large to fit in memory"); 2021 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
1977 png_crc_finish(png_ptr, length); 2022 png_crc_finish(png_ptr, length);
1978 return; 2023 return;
1979 } 2024 }
1980 #endif 2025 #endif
1981 2026
1982 chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); 2027 png_free(png_ptr, png_ptr->chunkdata);
1983 if (chunkdata == NULL) 2028 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2029 if (png_ptr->chunkdata == NULL)
1984 { 2030 {
1985 png_warning(png_ptr,"Out of memory processing zTXt chunk."); 2031 png_warning(png_ptr, "Out of memory processing zTXt chunk.");
1986 return; 2032 return;
1987 } 2033 }
1988 slength = (png_size_t)length; 2034 slength = (png_size_t)length;
1989 png_crc_read(png_ptr, (png_bytep)chunkdata, slength); 2035 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
1990 if (png_crc_finish(png_ptr, 0)) 2036 if (png_crc_finish(png_ptr, 0))
1991 { 2037 {
1992 png_free(png_ptr, chunkdata); 2038 png_free(png_ptr, png_ptr->chunkdata);
2039 png_ptr->chunkdata = NULL;
1993 return; 2040 return;
1994 } 2041 }
1995 2042
1996 chunkdata[slength] = 0x00; 2043 png_ptr->chunkdata[slength] = 0x00;
1997 2044
1998 for (text = chunkdata; *text; text++) 2045 for (text = png_ptr->chunkdata; *text; text++)
1999 /* empty loop */ ; 2046 /* empty loop */ ;
2000 2047
2001 /* zTXt must have some text after the chunkdataword */ 2048 /* zTXt must have some text after the chunkdataword */
2002 if (text >= chunkdata + slength - 2) 2049 if (text >= png_ptr->chunkdata + slength - 2)
2003 { 2050 {
2004 png_warning(png_ptr, "Truncated zTXt chunk"); 2051 png_warning(png_ptr, "Truncated zTXt chunk");
2005 png_free(png_ptr, chunkdata); 2052 png_free(png_ptr, png_ptr->chunkdata);
2053 png_ptr->chunkdata = NULL;
2006 return; 2054 return;
2007 } 2055 }
2008 else 2056 else
2009 { 2057 {
2010 comp_type = *(++text); 2058 comp_type = *(++text);
2011 if (comp_type != PNG_TEXT_COMPRESSION_zTXt) 2059 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
2012 { 2060 {
2013 png_warning(png_ptr, "Unknown compression type in zTXt chunk"); 2061 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
2014 comp_type = PNG_TEXT_COMPRESSION_zTXt; 2062 comp_type = PNG_TEXT_COMPRESSION_zTXt;
2015 } 2063 }
2016 text++; /* skip the compression_method byte */ 2064 text++; /* skip the compression_method byte */
2017 } 2065 }
2018 prefix_len = text - chunkdata; 2066 prefix_len = text - png_ptr->chunkdata;
2019 2067
2020 chunkdata = (png_charp)png_decompress_chunk(png_ptr, comp_type, chunkdata, 2068 png_decompress_chunk(png_ptr, comp_type,
2021 (png_size_t)length, prefix_len, &data_len); 2069 (png_size_t)length, prefix_len, &data_len);
2022 2070
2023 text_ptr = (png_textp)png_malloc_warn(png_ptr, 2071 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2024 (png_uint_32)png_sizeof(png_text)); 2072 (png_uint_32)png_sizeof(png_text));
2025 if (text_ptr == NULL) 2073 if (text_ptr == NULL)
2026 { 2074 {
2027 png_warning(png_ptr,"Not enough memory to process zTXt chunk."); 2075 png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
2028 png_free(png_ptr, chunkdata); 2076 png_free(png_ptr, png_ptr->chunkdata);
2077 png_ptr->chunkdata = NULL;
2029 return; 2078 return;
2030 } 2079 }
2031 text_ptr->compression = comp_type; 2080 text_ptr->compression = comp_type;
2032 text_ptr->key = chunkdata; 2081 text_ptr->key = png_ptr->chunkdata;
2033 #ifdef PNG_iTXt_SUPPORTED 2082 #ifdef PNG_iTXt_SUPPORTED
2034 text_ptr->lang = NULL; 2083 text_ptr->lang = NULL;
2035 text_ptr->lang_key = NULL; 2084 text_ptr->lang_key = NULL;
2036 text_ptr->itxt_length = 0; 2085 text_ptr->itxt_length = 0;
2037 #endif 2086 #endif
2038 text_ptr->text = chunkdata + prefix_len; 2087 text_ptr->text = png_ptr->chunkdata + prefix_len;
2039 text_ptr->text_length = data_len; 2088 text_ptr->text_length = data_len;
2040 2089
2041 ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); 2090 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2042 2091
2043 png_free(png_ptr, text_ptr); 2092 png_free(png_ptr, text_ptr);
2044 png_free(png_ptr, chunkdata); 2093 png_free(png_ptr, png_ptr->chunkdata);
2094 png_ptr->chunkdata = NULL;
2045 if (ret) 2095 if (ret)
2046 png_error(png_ptr, "Insufficient memory to store zTXt chunk."); 2096 png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
2047 } 2097 }
2048 #endif 2098 #endif
2049 2099
2050 #if defined(PNG_READ_iTXt_SUPPORTED) 2100 #if defined(PNG_READ_iTXt_SUPPORTED)
2051 /* note: this does not correctly handle chunks that are > 64K under DOS */ 2101 /* note: this does not correctly handle chunks that are > 64K under DOS */
2052 void /* PRIVATE */ 2102 void /* PRIVATE */
2053 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 2103 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2054 { 2104 {
2055 png_textp text_ptr; 2105 png_textp text_ptr;
2056 png_charp chunkdata;
2057 png_charp key, lang, text, lang_key; 2106 png_charp key, lang, text, lang_key;
2058 int comp_flag; 2107 int comp_flag;
2059 int comp_type = 0; 2108 int comp_type = 0;
2060 int ret; 2109 int ret;
2061 png_size_t slength, prefix_len, data_len; 2110 png_size_t slength, prefix_len, data_len;
2062 2111
2063 png_debug(1, "in png_handle_iTXt\n"); 2112 png_debug(1, "in png_handle_iTXt\n");
2064 2113
2065 if (!(png_ptr->mode & PNG_HAVE_IHDR)) 2114 if (!(png_ptr->mode & PNG_HAVE_IHDR))
2066 png_error(png_ptr, "Missing IHDR before iTXt"); 2115 png_error(png_ptr, "Missing IHDR before iTXt");
2067 2116
2068 if (png_ptr->mode & PNG_HAVE_IDAT) 2117 if (png_ptr->mode & PNG_HAVE_IDAT)
2069 png_ptr->mode |= PNG_AFTER_IDAT; 2118 png_ptr->mode |= PNG_AFTER_IDAT;
2070 2119
2071 #ifdef PNG_MAX_MALLOC_64K 2120 #ifdef PNG_MAX_MALLOC_64K
2072 /* We will no doubt have problems with chunks even half this size, but 2121 /* We will no doubt have problems with chunks even half this size, but
2073 there is no hard and fast rule to tell us where to stop. */ 2122 there is no hard and fast rule to tell us where to stop. */
2074 if (length > (png_uint_32)65535L) 2123 if (length > (png_uint_32)65535L)
2075 { 2124 {
2076 png_warning(png_ptr,"iTXt chunk too large to fit in memory"); 2125 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
2077 png_crc_finish(png_ptr, length); 2126 png_crc_finish(png_ptr, length);
2078 return; 2127 return;
2079 } 2128 }
2080 #endif 2129 #endif
2081 2130
2082 chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1); 2131 png_free(png_ptr, png_ptr->chunkdata);
2083 if (chunkdata == NULL) 2132 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
2133 if (png_ptr->chunkdata == NULL)
2084 { 2134 {
2085 png_warning(png_ptr, "No memory to process iTXt chunk."); 2135 png_warning(png_ptr, "No memory to process iTXt chunk.");
2086 return; 2136 return;
2087 } 2137 }
2088 slength = (png_size_t)length; 2138 slength = (png_size_t)length;
2089 png_crc_read(png_ptr, (png_bytep)chunkdata, slength); 2139 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
2090 if (png_crc_finish(png_ptr, 0)) 2140 if (png_crc_finish(png_ptr, 0))
2091 { 2141 {
2092 png_free(png_ptr, chunkdata); 2142 png_free(png_ptr, png_ptr->chunkdata);
2143 png_ptr->chunkdata = NULL;
2093 return; 2144 return;
2094 } 2145 }
2095 2146
2096 chunkdata[slength] = 0x00; 2147 png_ptr->chunkdata[slength] = 0x00;
2097 2148
2098 for (lang = chunkdata; *lang; lang++) 2149 for (lang = png_ptr->chunkdata; *lang; lang++)
2099 /* empty loop */ ; 2150 /* empty loop */ ;
2100 lang++; /* skip NUL separator */ 2151 lang++; /* skip NUL separator */
2101 2152
2102 /* iTXt must have a language tag (possibly empty), two compression bytes, 2153 /* iTXt must have a language tag (possibly empty), two compression bytes,
2103 translated keyword (possibly empty), and possibly some text after the 2154 translated keyword (possibly empty), and possibly some text after the
2104 keyword */ 2155 keyword */
2105 2156
2106 if (lang >= chunkdata + slength - 3) 2157 if (lang >= png_ptr->chunkdata + slength - 3)
2107 { 2158 {
2108 png_warning(png_ptr, "Truncated iTXt chunk"); 2159 png_warning(png_ptr, "Truncated iTXt chunk");
2109 png_free(png_ptr, chunkdata); 2160 png_free(png_ptr, png_ptr->chunkdata);
2161 png_ptr->chunkdata = NULL;
2110 return; 2162 return;
2111 } 2163 }
2112 else 2164 else
2113 { 2165 {
2114 comp_flag = *lang++; 2166 comp_flag = *lang++;
2115 comp_type = *lang++; 2167 comp_type = *lang++;
2116 } 2168 }
2117 2169
2118 for (lang_key = lang; *lang_key; lang_key++) 2170 for (lang_key = lang; *lang_key; lang_key++)
2119 /* empty loop */ ; 2171 /* empty loop */ ;
2120 lang_key++; /* skip NUL separator */ 2172 lang_key++; /* skip NUL separator */
2121 2173
2122 if (lang_key >= chunkdata + slength) 2174 if (lang_key >= png_ptr->chunkdata + slength)
2123 { 2175 {
2124 png_warning(png_ptr, "Truncated iTXt chunk"); 2176 png_warning(png_ptr, "Truncated iTXt chunk");
2125 png_free(png_ptr, chunkdata); 2177 png_free(png_ptr, png_ptr->chunkdata);
2178 png_ptr->chunkdata = NULL;
2126 return; 2179 return;
2127 } 2180 }
2128 2181
2129 for (text = lang_key; *text; text++) 2182 for (text = lang_key; *text; text++)
2130 /* empty loop */ ; 2183 /* empty loop */ ;
2131 text++; /* skip NUL separator */ 2184 text++; /* skip NUL separator */
2132 if (text >= chunkdata + slength) 2185 if (text >= png_ptr->chunkdata + slength)
2133 { 2186 {
2134 png_warning(png_ptr, "Malformed iTXt chunk"); 2187 png_warning(png_ptr, "Malformed iTXt chunk");
2135 png_free(png_ptr, chunkdata); 2188 png_free(png_ptr, png_ptr->chunkdata);
2189 png_ptr->chunkdata = NULL;
2136 return; 2190 return;
2137 } 2191 }
2138 2192
2139 prefix_len = text - chunkdata; 2193 prefix_len = text - png_ptr->chunkdata;
2140 2194
2141 key=chunkdata; 2195 key=png_ptr->chunkdata;
2142 if (comp_flag) 2196 if (comp_flag)
2143 chunkdata = png_decompress_chunk(png_ptr, comp_type, chunkdata, 2197 png_decompress_chunk(png_ptr, comp_type,
2144 (size_t)length, prefix_len, &data_len); 2198 (size_t)length, prefix_len, &data_len);
2145 else 2199 else
2146 data_len=png_strlen(chunkdata + prefix_len); 2200 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
2147 text_ptr = (png_textp)png_malloc_warn(png_ptr, 2201 text_ptr = (png_textp)png_malloc_warn(png_ptr,
2148 (png_uint_32)png_sizeof(png_text)); 2202 (png_uint_32)png_sizeof(png_text));
2149 if (text_ptr == NULL) 2203 if (text_ptr == NULL)
2150 { 2204 {
2151 png_warning(png_ptr,"Not enough memory to process iTXt chunk."); 2205 png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
2152 png_free(png_ptr, chunkdata); 2206 png_free(png_ptr, png_ptr->chunkdata);
2207 png_ptr->chunkdata = NULL;
2153 return; 2208 return;
2154 } 2209 }
2155 text_ptr->compression = (int)comp_flag + 1; 2210 text_ptr->compression = (int)comp_flag + 1;
2156 text_ptr->lang_key = chunkdata+(lang_key-key); 2211 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
2157 text_ptr->lang = chunkdata+(lang-key); 2212 text_ptr->lang = png_ptr->chunkdata + (lang - key);
2158 text_ptr->itxt_length = data_len; 2213 text_ptr->itxt_length = data_len;
2159 text_ptr->text_length = 0; 2214 text_ptr->text_length = 0;
2160 text_ptr->key = chunkdata; 2215 text_ptr->key = png_ptr->chunkdata;
2161 text_ptr->text = chunkdata + prefix_len; 2216 text_ptr->text = png_ptr->chunkdata + prefix_len;
2162 2217
2163 ret=png_set_text_2(png_ptr, info_ptr, text_ptr, 1); 2218 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
2164 2219
2165 png_free(png_ptr, text_ptr); 2220 png_free(png_ptr, text_ptr);
2166 png_free(png_ptr, chunkdata); 2221 png_free(png_ptr, png_ptr->chunkdata);
2222 png_ptr->chunkdata = NULL;
2167 if (ret) 2223 if (ret)
2168 png_error(png_ptr, "Insufficient memory to store iTXt chunk."); 2224 png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
2169 } 2225 }
2170 #endif 2226 #endif
2171 2227
2172 /* This function is called when we haven't found a handler for a 2228 /* This function is called when we haven't found a handler for a
2173 chunk. If there isn't a problem with the chunk itself (ie bad 2229 chunk. If there isn't a problem with the chunk itself (ie bad
2174 chunk name, CRC, or a critical chunk), the chunk is silently ignored 2230 chunk name, CRC, or a critical chunk), the chunk is silently ignored
2175 -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which 2231 -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
2176 case it will be saved away to be written out later. */ 2232 case it will be saved away to be written out later. */
2177 void /* PRIVATE */ 2233 void /* PRIVATE */
2178 png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) 2234 png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
2179 { 2235 {
2180 png_uint_32 skip = 0; 2236 png_uint_32 skip = 0;
2181 2237
2182 png_debug(1, "in png_handle_unknown\n"); 2238 png_debug(1, "in png_handle_unknown\n");
2183 2239
2184 if (png_ptr->mode & PNG_HAVE_IDAT) 2240 if (png_ptr->mode & PNG_HAVE_IDAT)
2185 { 2241 {
2186 #ifdef PNG_USE_LOCAL_ARRAYS 2242 #ifdef PNG_USE_LOCAL_ARRAYS
2187 PNG_CONST PNG_IDAT; 2243 PNG_CONST PNG_IDAT;
2188 #endif 2244 #endif
2189 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */ 2245 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */
2190 png_ptr->mode |= PNG_AFTER_IDAT; 2246 png_ptr->mode |= PNG_AFTER_IDAT;
2191 } 2247 }
2192 2248
2193 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
2194
2195 if (!(png_ptr->chunk_name[0] & 0x20)) 2249 if (!(png_ptr->chunk_name[0] & 0x20))
2196 { 2250 {
2197 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) 2251 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2198 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != 2252 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2199 PNG_HANDLE_CHUNK_ALWAYS 2253 PNG_HANDLE_CHUNK_ALWAYS
2200 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) 2254 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2201 && png_ptr->read_user_chunk_fn == NULL 2255 && png_ptr->read_user_chunk_fn == NULL
2202 #endif 2256 #endif
2203 ) 2257 )
2204 #endif 2258 #endif
2205 png_chunk_error(png_ptr, "unknown critical chunk"); 2259 png_chunk_error(png_ptr, "unknown critical chunk");
2206 } 2260 }
2207 2261
2208 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) 2262 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
(...skipping 14 matching lines...) Expand all
2223 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0'; 2277 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
2224 png_ptr->unknown_chunk.size = (png_size_t)length; 2278 png_ptr->unknown_chunk.size = (png_size_t)length;
2225 if (length == 0) 2279 if (length == 0)
2226 png_ptr->unknown_chunk.data = NULL; 2280 png_ptr->unknown_chunk.data = NULL;
2227 else 2281 else
2228 { 2282 {
2229 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length); 2283 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
2230 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length); 2284 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
2231 } 2285 }
2232 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED) 2286 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
2233 if(png_ptr->read_user_chunk_fn != NULL) 2287 if (png_ptr->read_user_chunk_fn != NULL)
2234 { 2288 {
2235 /* callback to user unknown chunk handler */ 2289 /* callback to user unknown chunk handler */
2236 int ret; 2290 int ret;
2237 ret = (*(png_ptr->read_user_chunk_fn)) 2291 ret = (*(png_ptr->read_user_chunk_fn))
2238 (png_ptr, &png_ptr->unknown_chunk); 2292 (png_ptr, &png_ptr->unknown_chunk);
2239 if (ret < 0) 2293 if (ret < 0)
2240 png_chunk_error(png_ptr, "error in user chunk"); 2294 png_chunk_error(png_ptr, "error in user chunk");
2241 if (ret == 0) 2295 if (ret == 0)
2242 { 2296 {
2243 if (!(png_ptr->chunk_name[0] & 0x20)) 2297 if (!(png_ptr->chunk_name[0] & 0x20))
2244 if(png_handle_as_unknown(png_ptr, png_ptr->chunk_name) != 2298 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
2245 PNG_HANDLE_CHUNK_ALWAYS) 2299 PNG_HANDLE_CHUNK_ALWAYS)
2246 png_chunk_error(png_ptr, "unknown critical chunk"); 2300 png_chunk_error(png_ptr, "unknown critical chunk");
2247 png_set_unknown_chunks(png_ptr, info_ptr, 2301 png_set_unknown_chunks(png_ptr, info_ptr,
2248 &png_ptr->unknown_chunk, 1); 2302 &png_ptr->unknown_chunk, 1);
2249 } 2303 }
2250 } 2304 }
2251 else 2305 else
2252 #endif 2306 #endif
2253 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1); 2307 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
2254 png_free(png_ptr, png_ptr->unknown_chunk.data); 2308 png_free(png_ptr, png_ptr->unknown_chunk.data);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2291 The mask value describes which pixels are to be combined with 2345 The mask value describes which pixels are to be combined with
2292 the row. The pattern always repeats every 8 pixels, so just 8 2346 the row. The pattern always repeats every 8 pixels, so just 8
2293 bits are needed. A one indicates the pixel is to be combined, 2347 bits are needed. A one indicates the pixel is to be combined,
2294 a zero indicates the pixel is to be skipped. This is in addition 2348 a zero indicates the pixel is to be skipped. This is in addition
2295 to any alpha or transparency value associated with the pixel. If 2349 to any alpha or transparency value associated with the pixel. If
2296 you want all pixels to be combined, pass 0xff (255) in mask. */ 2350 you want all pixels to be combined, pass 0xff (255) in mask. */
2297 2351
2298 void /* PRIVATE */ 2352 void /* PRIVATE */
2299 png_combine_row(png_structp png_ptr, png_bytep row, int mask) 2353 png_combine_row(png_structp png_ptr, png_bytep row, int mask)
2300 { 2354 {
2301 png_debug(1,"in png_combine_row\n"); 2355 png_debug(1, "in png_combine_row\n");
2302 if (mask == 0xff) 2356 if (mask == 0xff)
2303 { 2357 {
2304 png_memcpy(row, png_ptr->row_buf + 1, 2358 png_memcpy(row, png_ptr->row_buf + 1,
2305 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width)); 2359 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
2306 } 2360 }
2307 else 2361 else
2308 { 2362 {
2309 switch (png_ptr->row_info.pixel_depth) 2363 switch (png_ptr->row_info.pixel_depth)
2310 { 2364 {
2311 case 1: 2365 case 1:
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 png_row_infop row_info = &(png_ptr->row_info); 2561 png_row_infop row_info = &(png_ptr->row_info);
2508 png_bytep row = png_ptr->row_buf + 1; 2562 png_bytep row = png_ptr->row_buf + 1;
2509 int pass = png_ptr->pass; 2563 int pass = png_ptr->pass;
2510 png_uint_32 transformations = png_ptr->transformations; 2564 png_uint_32 transformations = png_ptr->transformations;
2511 #ifdef PNG_USE_LOCAL_ARRAYS 2565 #ifdef PNG_USE_LOCAL_ARRAYS
2512 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 2566 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2513 /* offset to next interlace block */ 2567 /* offset to next interlace block */
2514 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 2568 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2515 #endif 2569 #endif
2516 2570
2517 png_debug(1,"in png_do_read_interlace\n"); 2571 png_debug(1, "in png_do_read_interlace\n");
2518 if (row != NULL && row_info != NULL) 2572 if (row != NULL && row_info != NULL)
2519 { 2573 {
2520 png_uint_32 final_width; 2574 png_uint_32 final_width;
2521 2575
2522 final_width = row_info->width * png_pass_inc[pass]; 2576 final_width = row_info->width * png_pass_inc[pass];
2523 2577
2524 switch (row_info->pixel_depth) 2578 switch (row_info->pixel_depth)
2525 { 2579 {
2526 case 1: 2580 case 1:
2527 { 2581 {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
2708 { 2762 {
2709 png_memcpy(dp, v, pixel_bytes); 2763 png_memcpy(dp, v, pixel_bytes);
2710 dp -= pixel_bytes; 2764 dp -= pixel_bytes;
2711 } 2765 }
2712 sp -= pixel_bytes; 2766 sp -= pixel_bytes;
2713 } 2767 }
2714 break; 2768 break;
2715 } 2769 }
2716 } 2770 }
2717 row_info->width = final_width; 2771 row_info->width = final_width;
2718 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,final_width); 2772 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
2719 } 2773 }
2720 #if !defined(PNG_READ_PACKSWAP_SUPPORTED) 2774 #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
2721 transformations = transformations; /* silence compiler warning */ 2775 transformations = transformations; /* silence compiler warning */
2722 #endif 2776 #endif
2723 } 2777 }
2724 #endif /* PNG_READ_INTERLACING_SUPPORTED */ 2778 #endif /* PNG_READ_INTERLACING_SUPPORTED */
2725 2779
2726 void /* PRIVATE */ 2780 void /* PRIVATE */
2727 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row, 2781 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
2728 png_bytep prev_row, int filter) 2782 png_bytep prev_row, int filter)
2729 { 2783 {
2730 png_debug(1, "in png_read_filter_row\n"); 2784 png_debug(1, "in png_read_filter_row\n");
2731 png_debug2(2,"row = %lu, filter = %d\n", png_ptr->row_number, filter); 2785 png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
2732 switch (filter) 2786 switch (filter)
2733 { 2787 {
2734 case PNG_FILTER_VALUE_NONE: 2788 case PNG_FILTER_VALUE_NONE:
2735 break; 2789 break;
2736 case PNG_FILTER_VALUE_SUB: 2790 case PNG_FILTER_VALUE_SUB:
2737 { 2791 {
2738 png_uint_32 i; 2792 png_uint_32 i;
2739 png_uint_32 istop = row_info->rowbytes; 2793 png_uint_32 istop = row_info->rowbytes;
2740 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3; 2794 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
2741 png_bytep rp = row + bpp; 2795 png_bytep rp = row + bpp;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
2825 2879
2826 /* 2880 /*
2827 if (pa <= pb && pa <= pc) 2881 if (pa <= pb && pa <= pc)
2828 p = a; 2882 p = a;
2829 else if (pb <= pc) 2883 else if (pb <= pc)
2830 p = b; 2884 p = b;
2831 else 2885 else
2832 p = c; 2886 p = c;
2833 */ 2887 */
2834 2888
2835 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; 2889 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
2836 2890
2837 *rp = (png_byte)(((int)(*rp) + p) & 0xff); 2891 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
2838 rp++; 2892 rp++;
2839 } 2893 }
2840 break; 2894 break;
2841 } 2895 }
2842 default: 2896 default:
2843 png_warning(png_ptr, "Ignoring bad adaptive filter type"); 2897 png_warning(png_ptr, "Ignoring bad adaptive filter type");
2844 *row=0; 2898 *row = 0;
2845 break; 2899 break;
2846 } 2900 }
2847 } 2901 }
2848 2902
2849 void /* PRIVATE */ 2903 void /* PRIVATE */
2850 png_read_finish_row(png_structp png_ptr) 2904 png_read_finish_row(png_structp png_ptr)
2851 { 2905 {
2852 #ifdef PNG_USE_LOCAL_ARRAYS 2906 #ifdef PNG_USE_LOCAL_ARRAYS
2853 #ifdef PNG_READ_INTERLACING_SUPPORTED 2907 #ifdef PNG_READ_INTERLACING_SUPPORTED
2854 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 2908 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2912 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) 2966 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
2913 { 2967 {
2914 #ifdef PNG_USE_LOCAL_ARRAYS 2968 #ifdef PNG_USE_LOCAL_ARRAYS
2915 PNG_CONST PNG_IDAT; 2969 PNG_CONST PNG_IDAT;
2916 #endif 2970 #endif
2917 char extra; 2971 char extra;
2918 int ret; 2972 int ret;
2919 2973
2920 png_ptr->zstream.next_out = (Byte *)&extra; 2974 png_ptr->zstream.next_out = (Byte *)&extra;
2921 png_ptr->zstream.avail_out = (uInt)1; 2975 png_ptr->zstream.avail_out = (uInt)1;
2922 for(;;) 2976 for (;;)
2923 { 2977 {
2924 if (!(png_ptr->zstream.avail_in)) 2978 if (!(png_ptr->zstream.avail_in))
2925 { 2979 {
2926 while (!png_ptr->idat_size) 2980 while (!png_ptr->idat_size)
2927 { 2981 {
2928 png_byte chunk_length[4]; 2982 png_byte chunk_length[4];
2929 2983
2930 png_crc_finish(png_ptr, 0); 2984 png_crc_finish(png_ptr, 0);
2931 2985
2932 png_read_data(png_ptr, chunk_length, 4); 2986 png_read_data(png_ptr, chunk_length, 4);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2993 3047
2994 /* start of interlace block in the y direction */ 3048 /* start of interlace block in the y direction */
2995 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; 3049 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2996 3050
2997 /* offset to next interlace block in the y direction */ 3051 /* offset to next interlace block in the y direction */
2998 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; 3052 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
2999 #endif 3053 #endif
3000 #endif 3054 #endif
3001 3055
3002 int max_pixel_depth; 3056 int max_pixel_depth;
3003 png_uint_32 row_bytes; 3057 png_size_t row_bytes;
3004 3058
3005 png_debug(1, "in png_read_start_row\n"); 3059 png_debug(1, "in png_read_start_row\n");
3006 png_ptr->zstream.avail_in = 0; 3060 png_ptr->zstream.avail_in = 0;
3007 png_init_read_transformations(png_ptr); 3061 png_init_read_transformations(png_ptr);
3008 #ifdef PNG_READ_INTERLACING_SUPPORTED 3062 #ifdef PNG_READ_INTERLACING_SUPPORTED
3009 if (png_ptr->interlaced) 3063 if (png_ptr->interlaced)
3010 { 3064 {
3011 if (!(png_ptr->transformations & PNG_INTERLACE)) 3065 if (!(png_ptr->transformations & PNG_INTERLACE))
3012 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - 3066 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
3013 png_pass_ystart[0]) / png_pass_yinc[0]; 3067 png_pass_ystart[0]) / png_pass_yinc[0];
3014 else 3068 else
3015 png_ptr->num_rows = png_ptr->height; 3069 png_ptr->num_rows = png_ptr->height;
3016 3070
3017 png_ptr->iwidth = (png_ptr->width + 3071 png_ptr->iwidth = (png_ptr->width +
3018 png_pass_inc[png_ptr->pass] - 1 - 3072 png_pass_inc[png_ptr->pass] - 1 -
3019 png_pass_start[png_ptr->pass]) / 3073 png_pass_start[png_ptr->pass]) /
3020 png_pass_inc[png_ptr->pass]; 3074 png_pass_inc[png_ptr->pass];
3021 3075
3022 row_bytes = PNG_ROWBYTES(png_ptr->pixel_depth,png_ptr->iwidth) + 1; 3076 png_ptr->irowbytes =
3023 3077 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
3024 png_ptr->irowbytes = (png_size_t)row_bytes;
3025 if((png_uint_32)png_ptr->irowbytes != row_bytes)
3026 png_error(png_ptr, "Rowbytes overflow in png_read_start_row");
3027 } 3078 }
3028 else 3079 else
3029 #endif /* PNG_READ_INTERLACING_SUPPORTED */ 3080 #endif /* PNG_READ_INTERLACING_SUPPORTED */
3030 { 3081 {
3031 png_ptr->num_rows = png_ptr->height; 3082 png_ptr->num_rows = png_ptr->height;
3032 png_ptr->iwidth = png_ptr->width; 3083 png_ptr->iwidth = png_ptr->width;
3033 png_ptr->irowbytes = png_ptr->rowbytes + 1; 3084 png_ptr->irowbytes = png_ptr->rowbytes + 1;
3034 } 3085 }
3035 max_pixel_depth = png_ptr->pixel_depth; 3086 max_pixel_depth = png_ptr->pixel_depth;
3036 3087
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
3118 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 3169 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
3119 max_pixel_depth = 64; 3170 max_pixel_depth = 64;
3120 else 3171 else
3121 max_pixel_depth = 48; 3172 max_pixel_depth = 48;
3122 } 3173 }
3123 } 3174 }
3124 #endif 3175 #endif
3125 3176
3126 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ 3177 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
3127 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) 3178 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
3128 if(png_ptr->transformations & PNG_USER_TRANSFORM) 3179 if (png_ptr->transformations & PNG_USER_TRANSFORM)
3129 { 3180 {
3130 int user_pixel_depth=png_ptr->user_transform_depth* 3181 int user_pixel_depth = png_ptr->user_transform_depth*
3131 png_ptr->user_transform_channels; 3182 png_ptr->user_transform_channels;
3132 if(user_pixel_depth > max_pixel_depth) 3183 if (user_pixel_depth > max_pixel_depth)
3133 max_pixel_depth=user_pixel_depth; 3184 max_pixel_depth=user_pixel_depth;
3134 } 3185 }
3135 #endif 3186 #endif
3136 3187
3137 /* align the width on the next larger 8 pixels. Mainly used 3188 /* align the width on the next larger 8 pixels. Mainly used
3138 for interlacing */ 3189 for interlacing */
3139 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); 3190 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
3140 /* calculate the maximum bytes needed, adding a byte and a pixel 3191 /* calculate the maximum bytes needed, adding a byte and a pixel
3141 for safety's sake */ 3192 for safety's sake */
3142 row_bytes = PNG_ROWBYTES(max_pixel_depth,row_bytes) + 3193 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
3143 1 + ((max_pixel_depth + 7) >> 3); 3194 1 + ((max_pixel_depth + 7) >> 3);
3144 #ifdef PNG_MAX_MALLOC_64K 3195 #ifdef PNG_MAX_MALLOC_64K
3145 if (row_bytes > (png_uint_32)65536L) 3196 if (row_bytes > (png_uint_32)65536L)
3146 png_error(png_ptr, "This image requires a row greater than 64KB"); 3197 png_error(png_ptr, "This image requires a row greater than 64KB");
3147 #endif 3198 #endif
3148 3199
3149 if(row_bytes + 64 > png_ptr->old_big_row_buf_size) 3200 if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
3150 { 3201 {
3151 png_free(png_ptr,png_ptr->big_row_buf); 3202 png_free(png_ptr, png_ptr->big_row_buf);
3152 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64); 3203 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
3153 png_ptr->row_buf = png_ptr->big_row_buf+32; 3204 png_ptr->row_buf = png_ptr->big_row_buf+32;
3154 png_ptr->old_big_row_buf_size = row_bytes+64; 3205 png_ptr->old_big_row_buf_size = row_bytes+64;
3155 } 3206 }
3156 3207
3157 #ifdef PNG_MAX_MALLOC_64K 3208 #ifdef PNG_MAX_MALLOC_64K
3158 if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L) 3209 if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
3159 png_error(png_ptr, "This image requires a row greater than 64KB"); 3210 png_error(png_ptr, "This image requires a row greater than 64KB");
3160 #endif 3211 #endif
3161 if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1)) 3212 if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
3162 png_error(png_ptr, "Row has too many bytes to allocate in memory."); 3213 png_error(png_ptr, "Row has too many bytes to allocate in memory.");
3163 3214
3164 if(png_ptr->rowbytes+1 > png_ptr->old_prev_row_size) 3215 if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
3165 { 3216 {
3166 png_free(png_ptr,png_ptr->prev_row); 3217 png_free(png_ptr, png_ptr->prev_row);
3167 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)( 3218 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
3168 png_ptr->rowbytes + 1)); 3219 png_ptr->rowbytes + 1));
3169 png_ptr->old_prev_row_size = png_ptr->rowbytes+1; 3220 png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
3170 } 3221 }
3171 3222
3172 png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1); 3223 png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
3173 3224
3174 png_debug1(3, "width = %lu,\n", png_ptr->width); 3225 png_debug1(3, "width = %lu,\n", png_ptr->width);
3175 png_debug1(3, "height = %lu,\n", png_ptr->height); 3226 png_debug1(3, "height = %lu,\n", png_ptr->height);
3176 png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth); 3227 png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
3177 png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows); 3228 png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
3178 png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes); 3229 png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
3179 png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes); 3230 png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
3180 3231
3181 png_ptr->flags |= PNG_FLAG_ROW_INIT; 3232 png_ptr->flags |= PNG_FLAG_ROW_INIT;
3182 } 3233 }
3183 #endif /* PNG_READ_SUPPORTED */ 3234 #endif /* PNG_READ_SUPPORTED */
OLDNEW
« no previous file with comments | « third_party/libpng/pngrtran.c ('k') | third_party/libpng/pngset.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698