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 /* pngset.c - storage of image information into info struct 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 * The functions here are used during reads to store data from the file 42 * into the info struct, and during writes to store application data 43 * into the info struct for writing into the file. This abstracts the 44 * info struct and allows us to change the structure in the future. 45 */ 46 47 #include "pngpriv.h" 48 49 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) 50 51 #ifdef PNG_bKGD_SUPPORTED 52 void PNGAPI 53 png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, 963 { 964 png_debug1(1, "in %s storage function", "tRNS"); 965 966 if (png_ptr == NULL || info_ptr == NULL) 967 968 return; 969 970 if (trans_alpha != NULL) 971 { 972 /* It may not actually be necessary to set png_ptr->trans_alpha here; 973 * we do it for backward compatibility with the way the png_handle_tRNS 974 * function used to do the allocation. 975 * 976 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively 977 * relies on png_set_tRNS storing the information in png_struct 978 * (otherwise it won't be there for the code in pngrtran.c). 979 */ 980 981 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); 982 983 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */ 984 png_ptr->trans_alpha = info_ptr->trans_alpha = png_voidcast(png_bytep, 985 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH)); 986 987 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) 988 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans); 989 } 990 991 if (trans_color != NULL) 992 { 993 #ifdef PNG_WARNINGS_SUPPORTED 994 if (info_ptr->bit_depth < 16) 995 { 996 int sample_max = (1 << info_ptr->bit_depth) - 1; 997 998 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && 999 trans_color->gray > sample_max) || 1000 (info_ptr->color_type == PNG_COLOR_TYPE_RGB && 1001 (trans_color->red > sample_max || 1002 trans_color->green > sample_max || 1003 trans_color->blue > sample_max))) 1004 png_warning(png_ptr, 1005 "tRNS chunk has out-of-range samples for bit_depth"); 1006 } 1007 #endif 1008 1009 info_ptr->trans_color = *trans_color; 1655 /* Whether to report invalid palette index; added at libng-1.5.10. 1656 * It is possible for an indexed (color-type==3) PNG file to contain 1657 * pixels with invalid (out-of-range) indexes if the PLTE chunk has 1658 * fewer entries than the image's bit-depth would allow. We recover 1659 * from this gracefully by filling any incomplete palette with zeros 1660 * (opaque black). By default, when this occurs libpng will issue 1661 * a benign error. This API can be used to override that behavior. 1662 */ 1663 void PNGAPI 1664 png_set_check_for_invalid_index(png_structrp png_ptr, int allowed) 1665 { 1666 png_debug(1, "in png_set_check_for_invalid_index"); 1667 1668 if (allowed > 0) 1669 png_ptr->num_palette_max = 0; 1670 1671 else 1672 png_ptr->num_palette_max = -1; 1673 } 1674 #endif 1675 #endif /* READ || WRITE */ | 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 /* pngset.c - storage of image information into info struct 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-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 * The functions here are used during reads to store data from the file 42 * into the info struct, and during writes to store application data 43 * into the info struct for writing into the file. This abstracts the 44 * info struct and allows us to change the structure in the future. 45 */ 46 47 #include "pngpriv.h" 48 49 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) 50 51 #ifdef PNG_bKGD_SUPPORTED 52 void PNGAPI 53 png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, 963 { 964 png_debug1(1, "in %s storage function", "tRNS"); 965 966 if (png_ptr == NULL || info_ptr == NULL) 967 968 return; 969 970 if (trans_alpha != NULL) 971 { 972 /* It may not actually be necessary to set png_ptr->trans_alpha here; 973 * we do it for backward compatibility with the way the png_handle_tRNS 974 * function used to do the allocation. 975 * 976 * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively 977 * relies on png_set_tRNS storing the information in png_struct 978 * (otherwise it won't be there for the code in pngrtran.c). 979 */ 980 981 png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); 982 983 if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) 984 { 985 /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */ 986 info_ptr->trans_alpha = png_voidcast(png_bytep, 987 png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH)); 988 memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans); 989 } 990 png_ptr->trans_alpha = info_ptr->trans_alpha; 991 } 992 993 if (trans_color != NULL) 994 { 995 #ifdef PNG_WARNINGS_SUPPORTED 996 if (info_ptr->bit_depth < 16) 997 { 998 int sample_max = (1 << info_ptr->bit_depth) - 1; 999 1000 if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && 1001 trans_color->gray > sample_max) || 1002 (info_ptr->color_type == PNG_COLOR_TYPE_RGB && 1003 (trans_color->red > sample_max || 1004 trans_color->green > sample_max || 1005 trans_color->blue > sample_max))) 1006 png_warning(png_ptr, 1007 "tRNS chunk has out-of-range samples for bit_depth"); 1008 } 1009 #endif 1010 1011 info_ptr->trans_color = *trans_color; 1657 /* Whether to report invalid palette index; added at libng-1.5.10. 1658 * It is possible for an indexed (color-type==3) PNG file to contain 1659 * pixels with invalid (out-of-range) indexes if the PLTE chunk has 1660 * fewer entries than the image's bit-depth would allow. We recover 1661 * from this gracefully by filling any incomplete palette with zeros 1662 * (opaque black). By default, when this occurs libpng will issue 1663 * a benign error. This API can be used to override that behavior. 1664 */ 1665 void PNGAPI 1666 png_set_check_for_invalid_index(png_structrp png_ptr, int allowed) 1667 { 1668 png_debug(1, "in png_set_check_for_invalid_index"); 1669 1670 if (allowed > 0) 1671 png_ptr->num_palette_max = 0; 1672 1673 else 1674 png_ptr->num_palette_max = -1; 1675 } 1676 #endif 1677 1678 #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \ 1679 defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) 1680 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, 1681 * and if invalid, correct the keyword rather than discarding the entire 1682 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in 1683 * length, forbids leading or trailing whitespace, multiple internal spaces, 1684 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. 1685 * 1686 * The 'new_key' buffer must be 80 characters in size (for the keyword plus a 1687 * trailing '\0'). If this routine returns 0 then there was no keyword, or a 1688 * valid one could not be generated, and the caller must png_error. 1689 */ 1690 png_uint_32 /* PRIVATE */ 1691 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key) 1692 { 1693 png_const_charp orig_key = key; 1694 png_uint_32 key_len = 0; 1695 int bad_character = 0; 1696 int space = 1; 1697 1698 png_debug(1, "in png_check_keyword"); 1699 1700 if (key == NULL) 1701 { 1702 *new_key = 0; 1703 return 0; 1704 } 1705 1706 while (*key && key_len < 79) 1707 { 1708 png_byte ch = (png_byte)*key++; 1709 1710 if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) 1711 *new_key++ = ch, ++key_len, space = 0; 1712 1713 else if (space == 0) 1714 { 1715 /* A space or an invalid character when one wasn't seen immediately 1716 * before; output just a space. 1717 */ 1718 *new_key++ = 32, ++key_len, space = 1; 1719 1720 /* If the character was not a space then it is invalid. */ 1721 if (ch != 32) 1722 bad_character = ch; 1723 } 1724 1725 else if (bad_character == 0) 1726 bad_character = ch; /* just skip it, record the first error */ 1727 } 1728 1729 if (key_len > 0 && space != 0) /* trailing space */ 1730 { 1731 --key_len, --new_key; 1732 if (bad_character == 0) 1733 bad_character = 32; 1734 } 1735 1736 /* Terminate the keyword */ 1737 *new_key = 0; 1738 1739 if (key_len == 0) 1740 return 0; 1741 1742 #ifdef PNG_WARNINGS_SUPPORTED 1743 /* Try to only output one warning per keyword: */ 1744 if (*key != 0) /* keyword too long */ 1745 png_warning(png_ptr, "keyword truncated"); 1746 1747 else if (bad_character != 0) 1748 { 1749 PNG_WARNING_PARAMETERS(p) 1750 1751 png_warning_parameter(p, 1, orig_key); 1752 png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); 1753 1754 png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); 1755 } 1756 #endif /* WARNINGS */ 1757 1758 return key_len; 1759 } 1760 #endif /* TEXT || pCAL || iCCP || sPLT */ 1761 #endif /* READ || WRITE */ |