12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /* pngpread.c - read a png file in push mode
26 *
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file and, per its terms, should not be removed:
31 *
32 * Last changed in libpng 1.6.18 [July 23, 2015]
33 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
34 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
35 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
36 *
37 * This code is released under the libpng license.
38 * For conditions of distribution and use, see the disclaimer
39 * and license in png.h
40 */
41
42 #include "pngpriv.h"
43
44 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
45
46 /* Push model modes */
47 #define PNG_READ_SIG_MODE 0
48 #define PNG_READ_CHUNK_MODE 1
49 #define PNG_READ_IDAT_MODE 2
50 #define PNG_READ_tEXt_MODE 4
51 #define PNG_READ_zTXt_MODE 5
52 #define PNG_READ_DONE_MODE 6
53 #define PNG_READ_iTXt_MODE 7
221 }
222
223 chunk_name = png_ptr->chunk_name;
224
225 if (chunk_name == png_IDAT)
226 {
227 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
228 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
229
230 /* If we reach an IDAT chunk, this means we have read all of the
231 * header chunks, and we can start reading the image (or if this
232 * is called after the image has been read - we have an error).
233 */
234 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
235 png_error(png_ptr, "Missing IHDR before IDAT");
236
237 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
238 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
239 png_error(png_ptr, "Missing PLTE before IDAT");
240
241 png_ptr->mode |= PNG_HAVE_IDAT;
242 png_ptr->process_mode = PNG_READ_IDAT_MODE;
243
244 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
245 if (png_ptr->push_length == 0)
246 return;
247
248 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
249 png_benign_error(png_ptr, "Too many IDATs found");
250 }
251
252 if (chunk_name == png_IHDR)
253 {
254 if (png_ptr->push_length != 13)
255 png_error(png_ptr, "Invalid IHDR length");
256
257 PNG_PUSH_SAVE_BUFFER_IF_FULL
258 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
259 }
260
261 else if (chunk_name == png_IEND)
262 {
263 PNG_PUSH_SAVE_BUFFER_IF_FULL
264 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
265
266 png_ptr->process_mode = PNG_READ_DONE_MODE;
267 png_push_have_end(png_ptr, info_ptr);
510 png_size_t new_max;
511 png_bytep old_buffer;
512
513 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
514 (png_ptr->current_buffer_size + 256))
515 {
516 png_error(png_ptr, "Potential overflow of save_buffer");
517 }
518
519 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
520 old_buffer = png_ptr->save_buffer;
521 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
522 (png_size_t)new_max);
523
524 if (png_ptr->save_buffer == NULL)
525 {
526 png_free(png_ptr, old_buffer);
527 png_error(png_ptr, "Insufficient memory for save_buffer");
528 }
529
530 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
531 png_free(png_ptr, old_buffer);
532 png_ptr->save_buffer_max = new_max;
533 }
534 if (png_ptr->current_buffer_size)
535 {
536 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
537 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
538 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
539 png_ptr->current_buffer_size = 0;
540 }
541 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
542 png_ptr->buffer_size = 0;
543 }
544
545 void /* PRIVATE */
546 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
547 png_size_t buffer_length)
548 {
549 png_ptr->current_buffer = buffer;
550 png_ptr->current_buffer_size = buffer_length;
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /* pngpread.c - read a png file in push mode
26 *
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file and, per its terms, should not be removed:
31 *
32 * Last changed in libpng 1.6.23 [June 9, 2016]
33 * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
34 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
35 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
36 *
37 * This code is released under the libpng license.
38 * For conditions of distribution and use, see the disclaimer
39 * and license in png.h
40 */
41
42 #include "pngpriv.h"
43
44 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
45
46 /* Push model modes */
47 #define PNG_READ_SIG_MODE 0
48 #define PNG_READ_CHUNK_MODE 1
49 #define PNG_READ_IDAT_MODE 2
50 #define PNG_READ_tEXt_MODE 4
51 #define PNG_READ_zTXt_MODE 5
52 #define PNG_READ_DONE_MODE 6
53 #define PNG_READ_iTXt_MODE 7
221 }
222
223 chunk_name = png_ptr->chunk_name;
224
225 if (chunk_name == png_IDAT)
226 {
227 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
228 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
229
230 /* If we reach an IDAT chunk, this means we have read all of the
231 * header chunks, and we can start reading the image (or if this
232 * is called after the image has been read - we have an error).
233 */
234 if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
235 png_error(png_ptr, "Missing IHDR before IDAT");
236
237 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
238 (png_ptr->mode & PNG_HAVE_PLTE) == 0)
239 png_error(png_ptr, "Missing PLTE before IDAT");
240
241 png_ptr->process_mode = PNG_READ_IDAT_MODE;
242
243 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
244 if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
245 if (png_ptr->push_length == 0)
246 return;
247
248 png_ptr->mode |= PNG_HAVE_IDAT;
249
250 if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
251 png_benign_error(png_ptr, "Too many IDATs found");
252 }
253
254 if (chunk_name == png_IHDR)
255 {
256 if (png_ptr->push_length != 13)
257 png_error(png_ptr, "Invalid IHDR length");
258
259 PNG_PUSH_SAVE_BUFFER_IF_FULL
260 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
261 }
262
263 else if (chunk_name == png_IEND)
264 {
265 PNG_PUSH_SAVE_BUFFER_IF_FULL
266 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
267
268 png_ptr->process_mode = PNG_READ_DONE_MODE;
269 png_push_have_end(png_ptr, info_ptr);
512 png_size_t new_max;
513 png_bytep old_buffer;
514
515 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
516 (png_ptr->current_buffer_size + 256))
517 {
518 png_error(png_ptr, "Potential overflow of save_buffer");
519 }
520
521 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
522 old_buffer = png_ptr->save_buffer;
523 png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
524 (png_size_t)new_max);
525
526 if (png_ptr->save_buffer == NULL)
527 {
528 png_free(png_ptr, old_buffer);
529 png_error(png_ptr, "Insufficient memory for save_buffer");
530 }
531
532 if (old_buffer)
533 memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
534 else if (png_ptr->save_buffer_size)
535 png_error(png_ptr, "save_buffer error");
536 png_free(png_ptr, old_buffer);
537 png_ptr->save_buffer_max = new_max;
538 }
539 if (png_ptr->current_buffer_size)
540 {
541 memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
542 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
543 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
544 png_ptr->current_buffer_size = 0;
545 }
546 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
547 png_ptr->buffer_size = 0;
548 }
549
550 void /* PRIVATE */
551 png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
552 png_size_t buffer_length)
553 {
554 png_ptr->current_buffer = buffer;
555 png_ptr->current_buffer_size = buffer_length;
|