OLD | NEW |
1 | 1 |
2 /* pngpread.c - read a png file in push mode | 2 /* pngpread.c - read a png file in push mode |
3 * | 3 * |
4 * Last changed in libpng 1.2.43 [February 25, 2010] | 4 * Last changed in libpng 1.2.44 [June 26, 2010] |
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson | 5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson |
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
8 * | 8 * |
9 * This code is released under the libpng license. | 9 * This code is released under the libpng license. |
10 * For conditions of distribution and use, see the disclaimer | 10 * For conditions of distribution and use, see the disclaimer |
11 * and license in png.h | 11 * and license in png.h |
12 */ | 12 */ |
13 | 13 |
14 #define PNG_INTERNAL | 14 #define PNG_INTERNAL |
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 | 776 |
777 /* Check for overflow */ | 777 /* Check for overflow */ |
778 if ((png_uint_32)save_size != png_ptr->idat_size) | 778 if ((png_uint_32)save_size != png_ptr->idat_size) |
779 png_error(png_ptr, "save_size overflowed in pngpread"); | 779 png_error(png_ptr, "save_size overflowed in pngpread"); |
780 } | 780 } |
781 else | 781 else |
782 save_size = png_ptr->save_buffer_size; | 782 save_size = png_ptr->save_buffer_size; |
783 | 783 |
784 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); | 784 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); |
785 | 785 |
786 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) | 786 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); |
787 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); | |
788 | 787 |
789 png_ptr->idat_size -= save_size; | 788 png_ptr->idat_size -= save_size; |
790 png_ptr->buffer_size -= save_size; | 789 png_ptr->buffer_size -= save_size; |
791 png_ptr->save_buffer_size -= save_size; | 790 png_ptr->save_buffer_size -= save_size; |
792 png_ptr->save_buffer_ptr += save_size; | 791 png_ptr->save_buffer_ptr += save_size; |
793 } | 792 } |
794 if (png_ptr->idat_size && png_ptr->current_buffer_size) | 793 if (png_ptr->idat_size && png_ptr->current_buffer_size) |
795 { | 794 { |
796 png_size_t save_size; | 795 png_size_t save_size; |
797 | 796 |
798 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) | 797 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) |
799 { | 798 { |
800 save_size = (png_size_t)png_ptr->idat_size; | 799 save_size = (png_size_t)png_ptr->idat_size; |
801 | 800 |
802 /* Check for overflow */ | 801 /* Check for overflow */ |
803 if ((png_uint_32)save_size != png_ptr->idat_size) | 802 if ((png_uint_32)save_size != png_ptr->idat_size) |
804 png_error(png_ptr, "save_size overflowed in pngpread"); | 803 png_error(png_ptr, "save_size overflowed in pngpread"); |
805 } | 804 } |
806 else | 805 else |
807 save_size = png_ptr->current_buffer_size; | 806 save_size = png_ptr->current_buffer_size; |
808 | 807 |
809 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); | 808 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); |
810 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) | 809 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); |
811 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); | |
812 | 810 |
813 png_ptr->idat_size -= save_size; | 811 png_ptr->idat_size -= save_size; |
814 png_ptr->buffer_size -= save_size; | 812 png_ptr->buffer_size -= save_size; |
815 png_ptr->current_buffer_size -= save_size; | 813 png_ptr->current_buffer_size -= save_size; |
816 png_ptr->current_buffer_ptr += save_size; | 814 png_ptr->current_buffer_ptr += save_size; |
817 } | 815 } |
818 if (!png_ptr->idat_size) | 816 if (!png_ptr->idat_size) |
819 { | 817 { |
820 if (png_ptr->buffer_size < 4) | 818 if (png_ptr->buffer_size < 4) |
821 { | 819 { |
822 png_push_save_buffer(png_ptr); | 820 png_push_save_buffer(png_ptr); |
823 return; | 821 return; |
824 } | 822 } |
825 | 823 |
826 png_crc_finish(png_ptr, 0); | 824 png_crc_finish(png_ptr, 0); |
827 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; | 825 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; |
828 png_ptr->mode |= PNG_AFTER_IDAT; | 826 png_ptr->mode |= PNG_AFTER_IDAT; |
829 } | 827 } |
830 } | 828 } |
831 | 829 |
832 void /* PRIVATE */ | 830 void /* PRIVATE */ |
833 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, | 831 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, |
834 png_size_t buffer_length) | 832 png_size_t buffer_length) |
835 { | 833 { |
836 int ret; | 834 /* The caller checks for a non-zero buffer length. */ |
| 835 if (!(buffer_length > 0) || buffer == NULL) |
| 836 png_error(png_ptr, "No IDAT data (internal error)"); |
837 | 837 |
838 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length) | 838 /* This routine must process all the data it has been given |
839 png_error(png_ptr, "Extra compression data"); | 839 * before returning, calling the row callback as required to |
840 | 840 * handle the uncompressed results. |
| 841 */ |
841 png_ptr->zstream.next_in = buffer; | 842 png_ptr->zstream.next_in = buffer; |
842 png_ptr->zstream.avail_in = (uInt)buffer_length; | 843 png_ptr->zstream.avail_in = (uInt)buffer_length; |
843 for (;;) | 844 |
| 845 /* Keep going until the decompressed data is all processed |
| 846 * or the stream marked as finished. |
| 847 */ |
| 848 while (png_ptr->zstream.avail_in > 0 && |
| 849 » !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) |
844 { | 850 { |
845 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); | 851 int ret; |
846 if (ret != Z_OK) | 852 /* We have data for zlib, but we must check that zlib |
| 853 * has somewhere to put the results. It doesn't matter |
| 854 * if we don't expect any results -- it may be the input |
| 855 * data is just the LZ end code. |
| 856 */ |
| 857 if (!(png_ptr->zstream.avail_out > 0)) |
847 { | 858 { |
848 if (ret == Z_STREAM_END) | |
849 { | |
850 if (png_ptr->zstream.avail_in) | |
851 png_error(png_ptr, "Extra compressed data"); | |
852 | |
853 if (!(png_ptr->zstream.avail_out)) | |
854 { | |
855 png_push_process_row(png_ptr); | |
856 } | |
857 | |
858 png_ptr->mode |= PNG_AFTER_IDAT; | |
859 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; | |
860 break; | |
861 } | |
862 else if (ret == Z_BUF_ERROR) | |
863 break; | |
864 | |
865 else | |
866 png_error(png_ptr, "Decompression Error"); | |
867 } | |
868 if (!(png_ptr->zstream.avail_out)) | |
869 { | |
870 if (( | |
871 #ifdef PNG_READ_INTERLACING_SUPPORTED | |
872 png_ptr->interlaced && png_ptr->pass > 6) || | |
873 (!png_ptr->interlaced && | |
874 #endif | |
875 png_ptr->row_number == png_ptr->num_rows)) | |
876 { | |
877 if (png_ptr->zstream.avail_in) | |
878 png_warning(png_ptr, "Too much data in IDAT chunks"); | |
879 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; | |
880 break; | |
881 } | |
882 png_push_process_row(png_ptr); | |
883 png_ptr->zstream.avail_out = | 859 png_ptr->zstream.avail_out = |
884 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, | 860 (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, |
885 png_ptr->iwidth) + 1; | 861 png_ptr->iwidth) + 1; |
886 png_ptr->zstream.next_out = png_ptr->row_buf; | 862 png_ptr->zstream.next_out = png_ptr->row_buf; |
887 } | 863 } |
888 | 864 |
| 865 /* Using Z_SYNC_FLUSH here means that an unterminated |
| 866 * LZ stream can still be handled (a stream with a missing |
| 867 * end code), otherwise (Z_NO_FLUSH) a future zlib |
| 868 * implementation might defer output and, therefore, |
| 869 * change the current behavior. (See comments in inflate.c |
| 870 * for why this doesn't happen at present with zlib 1.2.5.) |
| 871 */ |
| 872 ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH); |
| 873 |
| 874 /* Check for any failure before proceeding. */ |
| 875 if (ret != Z_OK && ret != Z_STREAM_END) |
| 876 { |
| 877 /* Terminate the decompression. */ |
| 878 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
| 879 /* This may be a truncated stream (missing or |
| 880 * damaged end code). Treat that as a warning. |
| 881 */ |
| 882 if (png_ptr->row_number >= png_ptr->num_rows || |
| 883 png_ptr->pass > 6) |
| 884 png_warning(png_ptr, "Truncated compressed data in IDAT"); |
| 885 else |
| 886 png_error(png_ptr, "Decompression error in IDAT"); |
| 887 |
| 888 /* Skip the check on unprocessed input */ |
| 889 return; |
| 890 } |
| 891 |
| 892 /* Did inflate output any data? */ |
| 893 if (png_ptr->zstream.next_out != png_ptr->row_buf) |
| 894 { |
| 895 /* Is this unexpected data after the last row? |
| 896 * If it is, artificially terminate the LZ output |
| 897 * here. |
| 898 */ |
| 899 if (png_ptr->row_number >= png_ptr->num_rows || |
| 900 png_ptr->pass > 6) |
| 901 { |
| 902 /* Extra data. */ |
| 903 png_warning(png_ptr, "Extra compressed data in IDAT"); |
| 904 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
| 905 /* Do no more processing; skip the unprocessed |
| 906 * input check below. |
| 907 */ |
| 908 return; |
| 909 } |
| 910 |
| 911 /* Do we have a complete row? */ |
| 912 if (png_ptr->zstream.avail_out == 0) |
| 913 png_push_process_row(png_ptr); |
| 914 } |
| 915 |
889 else | 916 else |
890 break; | 917 break; |
| 918 /* And check for the end of the stream. */ |
| 919 if (ret == Z_STREAM_END) |
| 920 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; |
891 } | 921 } |
| 922 |
| 923 /* All the data should have been processed, if anything |
| 924 * is left at this point we have bytes of IDAT data |
| 925 * after the zlib end code. |
| 926 */ |
| 927 if (png_ptr->zstream.avail_in > 0) |
| 928 png_warning(png_ptr, "Extra compression data"); |
892 } | 929 } |
893 | 930 |
894 void /* PRIVATE */ | 931 void /* PRIVATE */ |
895 png_push_process_row(png_structp png_ptr) | 932 png_push_process_row(png_structp png_ptr) |
896 { | 933 { |
897 png_ptr->row_info.color_type = png_ptr->color_type; | 934 png_ptr->row_info.color_type = png_ptr->color_type; |
898 png_ptr->row_info.width = png_ptr->iwidth; | 935 png_ptr->row_info.width = png_ptr->iwidth; |
899 png_ptr->row_info.channels = png_ptr->channels; | 936 png_ptr->row_info.channels = png_ptr->channels; |
900 png_ptr->row_info.bit_depth = png_ptr->bit_depth; | 937 png_ptr->row_info.bit_depth = png_ptr->bit_depth; |
901 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; | 938 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; |
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1727 | 1764 |
1728 png_voidp PNGAPI | 1765 png_voidp PNGAPI |
1729 png_get_progressive_ptr(png_structp png_ptr) | 1766 png_get_progressive_ptr(png_structp png_ptr) |
1730 { | 1767 { |
1731 if (png_ptr == NULL) | 1768 if (png_ptr == NULL) |
1732 return (NULL); | 1769 return (NULL); |
1733 | 1770 |
1734 return png_ptr->io_ptr; | 1771 return png_ptr->io_ptr; |
1735 } | 1772 } |
1736 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ | 1773 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ |
OLD | NEW |