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 /* pngwutil.c - utilities to write a PNG file 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.19 [November 12, 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_WRITE_SUPPORTED 45 46 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED 47 /* Place a 32-bit number into a buffer in PNG byte order. We work 48 * with unsigned numbers for convenience, although one supported 49 * ancillary chunk uses signed (two's complement) numbers. 50 */ 51 void PNGAPI 52 png_save_uint_32(png_bytep buf, png_uint_32 i) 53 { 54 buf[0] = (png_byte)(i >> 24); 55 buf[1] = (png_byte)(i >> 16); 56 buf[2] = (png_byte)(i >> 8); 57 buf[3] = (png_byte)(i ); 58 } 59 60 /* Place a 16-bit number into a buffer in PNG byte order. 61 * The parameter is declared unsigned int, not png_uint_16, 62 * just to avoid potential problems on pre-ANSI C compilers. 63 */ 64 void PNGAPI 65 png_save_uint_16(png_bytep buf, unsigned int i) 66 { 67 buf[0] = (png_byte)(i >> 8); 68 buf[1] = (png_byte)(i ); 69 } 70 #endif 71 72 /* Simple function to write the signature. If we have already written 73 * the magic bytes of the signature, or more likely, the PNG stream is 74 * being embedded into another stream and doesn't need its own signature, 75 * we should call png_set_sig_bytes() to tell libpng how many of the 76 * bytes have already been written. 77 */ 78 void PNGAPI 79 png_write_sig(png_structrp png_ptr) 80 { 81 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; 82 83 #ifdef PNG_IO_STATE_SUPPORTED 84 /* Inform the I/O callback that the signature is being written */ 85 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE; 86 #endif 87 88 /* Write the rest of the 8 byte signature */ 676 avail = output_len; 677 678 png_write_chunk_data(png_ptr, output, avail); 679 680 output_len -= avail; 681 682 if (output_len == 0 || next == NULL) 683 break; 684 685 avail = png_ptr->zbuffer_size; 686 output = next->output; 687 next = next->next; 688 } 689 690 /* This is an internal error; 'next' must have been NULL! */ 691 if (output_len > 0) 692 png_error(png_ptr, "error writing ancillary chunked compressed data"); 693 } 694 #endif /* WRITE_COMPRESSED_TEXT */ 695 696 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ 697 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) 698 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, 699 * and if invalid, correct the keyword rather than discarding the entire 700 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in 701 * length, forbids leading or trailing whitespace, multiple internal spaces, 702 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. 703 * 704 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a 705 * trailing '\0'). If this routine returns 0 then there was no keyword, or a 706 * valid one could not be generated, and the caller must png_error. 707 */ 708 static png_uint_32 709 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key) 710 { 711 png_const_charp orig_key = key; 712 png_uint_32 key_len = 0; 713 int bad_character = 0; 714 int space = 1; 715 716 png_debug(1, "in png_check_keyword"); 717 718 if (key == NULL) 719 { 720 *new_key = 0; 721 return 0; 722 } 723 724 while (*key && key_len < 79) 725 { 726 png_byte ch = (png_byte)*key++; 727 728 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) 729 *new_key++ = ch, ++key_len, space = 0; 730 731 else if (space == 0) 732 { 733 /* A space or an invalid character when one wasn't seen immediately 734 * before; output just a space. 735 */ 736 *new_key++ = 32, ++key_len, space = 1; 737 738 /* If the character was not a space then it is invalid. */ 739 if (ch != 32) 740 bad_character = ch; 741 } 742 743 else if (bad_character == 0) 744 bad_character = ch; /* just skip it, record the first error */ 745 } 746 747 if (key_len > 0 && space != 0) /* trailing space */ 748 { 749 --key_len, --new_key; 750 if (bad_character == 0) 751 bad_character = 32; 752 } 753 754 /* Terminate the keyword */ 755 *new_key = 0; 756 757 if (key_len == 0) 758 return 0; 759 760 #ifdef PNG_WARNINGS_SUPPORTED 761 /* Try to only output one warning per keyword: */ 762 if (*key != 0) /* keyword too long */ 763 png_warning(png_ptr, "keyword truncated"); 764 765 else if (bad_character != 0) 766 { 767 PNG_WARNING_PARAMETERS(p) 768 769 png_warning_parameter(p, 1, orig_key); 770 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); 771 772 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); 773 } 774 #endif /* WARNINGS */ 775 776 return key_len; 777 } 778 #endif /* WRITE_TEXT || WRITE_pCAL || WRITE_iCCP || WRITE_sPLT */ 779 780 /* Write the IHDR chunk, and update the png_struct with the necessary 781 * information. Note that the rest of this code depends upon this 782 * information being correct. 783 */ 784 void /* PRIVATE */ 785 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, 786 int bit_depth, int color_type, int compression_type, int filter_type, 787 int interlace_type) 788 { 789 png_byte buf[13]; /* Buffer to store the IHDR info */ 790 791 png_debug(1, "in png_write_IHDR"); 792 793 /* Check that we have valid input data from the application info */ 794 switch (color_type) 795 { 796 case PNG_COLOR_TYPE_GRAY: 797 switch (bit_depth) 798 { 799 case 1: | 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 /* pngwutil.c - utilities to write a PNG file 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.22 [May 26, 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_WRITE_SUPPORTED 45 46 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED 47 /* Place a 32-bit number into a buffer in PNG byte order. We work 48 * with unsigned numbers for convenience, although one supported 49 * ancillary chunk uses signed (two's complement) numbers. 50 */ 51 void PNGAPI 52 png_save_uint_32(png_bytep buf, png_uint_32 i) 53 { 54 buf[0] = (png_byte)((i >> 24) & 0xffU); 55 buf[1] = (png_byte)((i >> 16) & 0xffU); 56 buf[2] = (png_byte)((i >> 8) & 0xffU); 57 buf[3] = (png_byte)( i & 0xffU); 58 } 59 60 /* Place a 16-bit number into a buffer in PNG byte order. 61 * The parameter is declared unsigned int, not png_uint_16, 62 * just to avoid potential problems on pre-ANSI C compilers. 63 */ 64 void PNGAPI 65 png_save_uint_16(png_bytep buf, unsigned int i) 66 { 67 buf[0] = (png_byte)((i >> 8) & 0xffU); 68 buf[1] = (png_byte)( i & 0xffU); 69 } 70 #endif 71 72 /* Simple function to write the signature. If we have already written 73 * the magic bytes of the signature, or more likely, the PNG stream is 74 * being embedded into another stream and doesn't need its own signature, 75 * we should call png_set_sig_bytes() to tell libpng how many of the 76 * bytes have already been written. 77 */ 78 void PNGAPI 79 png_write_sig(png_structrp png_ptr) 80 { 81 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; 82 83 #ifdef PNG_IO_STATE_SUPPORTED 84 /* Inform the I/O callback that the signature is being written */ 85 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE; 86 #endif 87 88 /* Write the rest of the 8 byte signature */ 676 avail = output_len; 677 678 png_write_chunk_data(png_ptr, output, avail); 679 680 output_len -= avail; 681 682 if (output_len == 0 || next == NULL) 683 break; 684 685 avail = png_ptr->zbuffer_size; 686 output = next->output; 687 next = next->next; 688 } 689 690 /* This is an internal error; 'next' must have been NULL! */ 691 if (output_len > 0) 692 png_error(png_ptr, "error writing ancillary chunked compressed data"); 693 } 694 #endif /* WRITE_COMPRESSED_TEXT */ 695 696 /* Write the IHDR chunk, and update the png_struct with the necessary 697 * information. Note that the rest of this code depends upon this 698 * information being correct. 699 */ 700 void /* PRIVATE */ 701 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, 702 int bit_depth, int color_type, int compression_type, int filter_type, 703 int interlace_type) 704 { 705 png_byte buf[13]; /* Buffer to store the IHDR info */ 706 707 png_debug(1, "in png_write_IHDR"); 708 709 /* Check that we have valid input data from the application info */ 710 switch (color_type) 711 { 712 case PNG_COLOR_TYPE_GRAY: 713 switch (bit_depth) 714 { 715 case 1: |