Path: blob/master/src/java.desktop/share/native/libsplashscreen/libpng/pngset.c
41154 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* pngset.c - storage of image information into info struct25*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file and, per its terms, should not be removed:30*31* Copyright (c) 2018 Cosmin Truta32* Copyright (c) 1998-2018 Glenn Randers-Pehrson33* Copyright (c) 1996-1997 Andreas Dilger34* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.35*36* This code is released under the libpng license.37* For conditions of distribution and use, see the disclaimer38* and license in png.h39*40* The functions here are used during reads to store data from the file41* into the info struct, and during writes to store application data42* into the info struct for writing into the file. This abstracts the43* info struct and allows us to change the structure in the future.44*/4546#include "pngpriv.h"4748#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)4950#ifdef PNG_bKGD_SUPPORTED51void PNGAPI52png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,53png_const_color_16p background)54{55png_debug1(1, "in %s storage function", "bKGD");5657if (png_ptr == NULL || info_ptr == NULL || background == NULL)58return;5960info_ptr->background = *background;61info_ptr->valid |= PNG_INFO_bKGD;62}63#endif6465#ifdef PNG_cHRM_SUPPORTED66void PNGFAPI67png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,68png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,69png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,70png_fixed_point blue_x, png_fixed_point blue_y)71{72png_xy xy;7374png_debug1(1, "in %s storage function", "cHRM fixed");7576if (png_ptr == NULL || info_ptr == NULL)77return;7879xy.redx = red_x;80xy.redy = red_y;81xy.greenx = green_x;82xy.greeny = green_y;83xy.bluex = blue_x;84xy.bluey = blue_y;85xy.whitex = white_x;86xy.whitey = white_y;8788if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,892/* override with app values*/) != 0)90info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;9192png_colorspace_sync_info(png_ptr, info_ptr);93}9495void PNGFAPI96png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,97png_fixed_point int_red_X, png_fixed_point int_red_Y,98png_fixed_point int_red_Z, png_fixed_point int_green_X,99png_fixed_point int_green_Y, png_fixed_point int_green_Z,100png_fixed_point int_blue_X, png_fixed_point int_blue_Y,101png_fixed_point int_blue_Z)102{103png_XYZ XYZ;104105png_debug1(1, "in %s storage function", "cHRM XYZ fixed");106107if (png_ptr == NULL || info_ptr == NULL)108return;109110XYZ.red_X = int_red_X;111XYZ.red_Y = int_red_Y;112XYZ.red_Z = int_red_Z;113XYZ.green_X = int_green_X;114XYZ.green_Y = int_green_Y;115XYZ.green_Z = int_green_Z;116XYZ.blue_X = int_blue_X;117XYZ.blue_Y = int_blue_Y;118XYZ.blue_Z = int_blue_Z;119120if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,121&XYZ, 2) != 0)122info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;123124png_colorspace_sync_info(png_ptr, info_ptr);125}126127# ifdef PNG_FLOATING_POINT_SUPPORTED128void PNGAPI129png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,130double white_x, double white_y, double red_x, double red_y,131double green_x, double green_y, double blue_x, double blue_y)132{133png_set_cHRM_fixed(png_ptr, info_ptr,134png_fixed(png_ptr, white_x, "cHRM White X"),135png_fixed(png_ptr, white_y, "cHRM White Y"),136png_fixed(png_ptr, red_x, "cHRM Red X"),137png_fixed(png_ptr, red_y, "cHRM Red Y"),138png_fixed(png_ptr, green_x, "cHRM Green X"),139png_fixed(png_ptr, green_y, "cHRM Green Y"),140png_fixed(png_ptr, blue_x, "cHRM Blue X"),141png_fixed(png_ptr, blue_y, "cHRM Blue Y"));142}143144void PNGAPI145png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,146double red_Y, double red_Z, double green_X, double green_Y, double green_Z,147double blue_X, double blue_Y, double blue_Z)148{149png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,150png_fixed(png_ptr, red_X, "cHRM Red X"),151png_fixed(png_ptr, red_Y, "cHRM Red Y"),152png_fixed(png_ptr, red_Z, "cHRM Red Z"),153png_fixed(png_ptr, green_X, "cHRM Green X"),154png_fixed(png_ptr, green_Y, "cHRM Green Y"),155png_fixed(png_ptr, green_Z, "cHRM Green Z"),156png_fixed(png_ptr, blue_X, "cHRM Blue X"),157png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),158png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));159}160# endif /* FLOATING_POINT */161162#endif /* cHRM */163164#ifdef PNG_eXIf_SUPPORTED165void PNGAPI166png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr,167png_bytep eXIf_buf)168{169png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1");170PNG_UNUSED(info_ptr)171PNG_UNUSED(eXIf_buf)172}173174void PNGAPI175png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr,176png_uint_32 num_exif, png_bytep eXIf_buf)177{178int i;179180png_debug1(1, "in %s storage function", "eXIf");181182if (png_ptr == NULL || info_ptr == NULL)183return;184185if (info_ptr->exif)186{187png_free(png_ptr, info_ptr->exif);188info_ptr->exif = NULL;189}190191info_ptr->num_exif = num_exif;192193info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr,194info_ptr->num_exif));195196if (info_ptr->exif == NULL)197{198png_warning(png_ptr, "Insufficient memory for eXIf chunk data");199return;200}201202info_ptr->free_me |= PNG_FREE_EXIF;203204for (i = 0; i < (int) info_ptr->num_exif; i++)205info_ptr->exif[i] = eXIf_buf[i];206207info_ptr->valid |= PNG_INFO_eXIf;208}209#endif /* eXIf */210211#ifdef PNG_gAMA_SUPPORTED212void PNGFAPI213png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,214png_fixed_point file_gamma)215{216png_debug1(1, "in %s storage function", "gAMA");217218if (png_ptr == NULL || info_ptr == NULL)219return;220221png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);222png_colorspace_sync_info(png_ptr, info_ptr);223}224225# ifdef PNG_FLOATING_POINT_SUPPORTED226void PNGAPI227png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)228{229png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,230"png_set_gAMA"));231}232# endif233#endif234235#ifdef PNG_hIST_SUPPORTED236void PNGAPI237png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,238png_const_uint_16p hist)239{240int i;241242png_debug1(1, "in %s storage function", "hIST");243244if (png_ptr == NULL || info_ptr == NULL)245return;246247if (info_ptr->num_palette == 0 || info_ptr->num_palette248> PNG_MAX_PALETTE_LENGTH)249{250png_warning(png_ptr,251"Invalid palette size, hIST allocation skipped");252253return;254}255256png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);257258/* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in259* version 1.2.1260*/261info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,262PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));263264if (info_ptr->hist == NULL)265{266png_warning(png_ptr, "Insufficient memory for hIST chunk data");267268return;269}270271info_ptr->free_me |= PNG_FREE_HIST;272273for (i = 0; i < info_ptr->num_palette; i++)274info_ptr->hist[i] = hist[i];275276info_ptr->valid |= PNG_INFO_hIST;277}278#endif279280void PNGAPI281png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,282png_uint_32 width, png_uint_32 height, int bit_depth,283int color_type, int interlace_type, int compression_type,284int filter_type)285{286png_debug1(1, "in %s storage function", "IHDR");287288if (png_ptr == NULL || info_ptr == NULL)289return;290291info_ptr->width = width;292info_ptr->height = height;293info_ptr->bit_depth = (png_byte)bit_depth;294info_ptr->color_type = (png_byte)color_type;295info_ptr->compression_type = (png_byte)compression_type;296info_ptr->filter_type = (png_byte)filter_type;297info_ptr->interlace_type = (png_byte)interlace_type;298299png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,300info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,301info_ptr->compression_type, info_ptr->filter_type);302303if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)304info_ptr->channels = 1;305306else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)307info_ptr->channels = 3;308309else310info_ptr->channels = 1;311312if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)313info_ptr->channels++;314315info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);316317info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);318}319320#ifdef PNG_oFFs_SUPPORTED321void PNGAPI322png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,323png_int_32 offset_x, png_int_32 offset_y, int unit_type)324{325png_debug1(1, "in %s storage function", "oFFs");326327if (png_ptr == NULL || info_ptr == NULL)328return;329330info_ptr->x_offset = offset_x;331info_ptr->y_offset = offset_y;332info_ptr->offset_unit_type = (png_byte)unit_type;333info_ptr->valid |= PNG_INFO_oFFs;334}335#endif336337#ifdef PNG_pCAL_SUPPORTED338void PNGAPI339png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,340png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,341int nparams, png_const_charp units, png_charpp params)342{343size_t length;344int i;345346png_debug1(1, "in %s storage function", "pCAL");347348if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL349|| (nparams > 0 && params == NULL))350return;351352length = strlen(purpose) + 1;353png_debug1(3, "allocating purpose for info (%lu bytes)",354(unsigned long)length);355356/* TODO: validate format of calibration name and unit name */357358/* Check that the type matches the specification. */359if (type < 0 || type > 3)360{361png_chunk_report(png_ptr, "Invalid pCAL equation type",362PNG_CHUNK_WRITE_ERROR);363return;364}365366if (nparams < 0 || nparams > 255)367{368png_chunk_report(png_ptr, "Invalid pCAL parameter count",369PNG_CHUNK_WRITE_ERROR);370return;371}372373/* Validate params[nparams] */374for (i=0; i<nparams; ++i)375{376if (params[i] == NULL ||377!png_check_fp_string(params[i], strlen(params[i])))378{379png_chunk_report(png_ptr, "Invalid format for pCAL parameter",380PNG_CHUNK_WRITE_ERROR);381return;382}383}384385info_ptr->pcal_purpose = png_voidcast(png_charp,386png_malloc_warn(png_ptr, length));387388if (info_ptr->pcal_purpose == NULL)389{390png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",391PNG_CHUNK_WRITE_ERROR);392return;393}394395memcpy(info_ptr->pcal_purpose, purpose, length);396397png_debug(3, "storing X0, X1, type, and nparams in info");398info_ptr->pcal_X0 = X0;399info_ptr->pcal_X1 = X1;400info_ptr->pcal_type = (png_byte)type;401info_ptr->pcal_nparams = (png_byte)nparams;402403length = strlen(units) + 1;404png_debug1(3, "allocating units for info (%lu bytes)",405(unsigned long)length);406407info_ptr->pcal_units = png_voidcast(png_charp,408png_malloc_warn(png_ptr, length));409410if (info_ptr->pcal_units == NULL)411{412png_warning(png_ptr, "Insufficient memory for pCAL units");413414return;415}416417memcpy(info_ptr->pcal_units, units, length);418419info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,420(size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));421422if (info_ptr->pcal_params == NULL)423{424png_warning(png_ptr, "Insufficient memory for pCAL params");425426return;427}428429memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *430(sizeof (png_charp)));431432for (i = 0; i < nparams; i++)433{434length = strlen(params[i]) + 1;435png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,436(unsigned long)length);437438info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);439440if (info_ptr->pcal_params[i] == NULL)441{442png_warning(png_ptr, "Insufficient memory for pCAL parameter");443444return;445}446447memcpy(info_ptr->pcal_params[i], params[i], length);448}449450info_ptr->valid |= PNG_INFO_pCAL;451info_ptr->free_me |= PNG_FREE_PCAL;452}453#endif454455#ifdef PNG_sCAL_SUPPORTED456void PNGAPI457png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,458int unit, png_const_charp swidth, png_const_charp sheight)459{460size_t lengthw = 0, lengthh = 0;461462png_debug1(1, "in %s storage function", "sCAL");463464if (png_ptr == NULL || info_ptr == NULL)465return;466467/* Double check the unit (should never get here with an invalid468* unit unless this is an API call.)469*/470if (unit != 1 && unit != 2)471png_error(png_ptr, "Invalid sCAL unit");472473if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||474swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))475png_error(png_ptr, "Invalid sCAL width");476477if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||478sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))479png_error(png_ptr, "Invalid sCAL height");480481info_ptr->scal_unit = (png_byte)unit;482483++lengthw;484485png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);486487info_ptr->scal_s_width = png_voidcast(png_charp,488png_malloc_warn(png_ptr, lengthw));489490if (info_ptr->scal_s_width == NULL)491{492png_warning(png_ptr, "Memory allocation failed while processing sCAL");493494return;495}496497memcpy(info_ptr->scal_s_width, swidth, lengthw);498499++lengthh;500501png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);502503info_ptr->scal_s_height = png_voidcast(png_charp,504png_malloc_warn(png_ptr, lengthh));505506if (info_ptr->scal_s_height == NULL)507{508png_free (png_ptr, info_ptr->scal_s_width);509info_ptr->scal_s_width = NULL;510511png_warning(png_ptr, "Memory allocation failed while processing sCAL");512513return;514}515516memcpy(info_ptr->scal_s_height, sheight, lengthh);517518info_ptr->valid |= PNG_INFO_sCAL;519info_ptr->free_me |= PNG_FREE_SCAL;520}521522# ifdef PNG_FLOATING_POINT_SUPPORTED523void PNGAPI524png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,525double width, double height)526{527png_debug1(1, "in %s storage function", "sCAL");528529/* Check the arguments. */530if (width <= 0)531png_warning(png_ptr, "Invalid sCAL width ignored");532533else if (height <= 0)534png_warning(png_ptr, "Invalid sCAL height ignored");535536else537{538/* Convert 'width' and 'height' to ASCII. */539char swidth[PNG_sCAL_MAX_DIGITS+1];540char sheight[PNG_sCAL_MAX_DIGITS+1];541542png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,543PNG_sCAL_PRECISION);544png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,545PNG_sCAL_PRECISION);546547png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);548}549}550# endif551552# ifdef PNG_FIXED_POINT_SUPPORTED553void PNGAPI554png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,555png_fixed_point width, png_fixed_point height)556{557png_debug1(1, "in %s storage function", "sCAL");558559/* Check the arguments. */560if (width <= 0)561png_warning(png_ptr, "Invalid sCAL width ignored");562563else if (height <= 0)564png_warning(png_ptr, "Invalid sCAL height ignored");565566else567{568/* Convert 'width' and 'height' to ASCII. */569char swidth[PNG_sCAL_MAX_DIGITS+1];570char sheight[PNG_sCAL_MAX_DIGITS+1];571572png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);573png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);574575png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);576}577}578# endif579#endif580581#ifdef PNG_pHYs_SUPPORTED582void PNGAPI583png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,584png_uint_32 res_x, png_uint_32 res_y, int unit_type)585{586png_debug1(1, "in %s storage function", "pHYs");587588if (png_ptr == NULL || info_ptr == NULL)589return;590591info_ptr->x_pixels_per_unit = res_x;592info_ptr->y_pixels_per_unit = res_y;593info_ptr->phys_unit_type = (png_byte)unit_type;594info_ptr->valid |= PNG_INFO_pHYs;595}596#endif597598void PNGAPI599png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,600png_const_colorp palette, int num_palette)601{602603png_uint_32 max_palette_length;604605png_debug1(1, "in %s storage function", "PLTE");606607if (png_ptr == NULL || info_ptr == NULL)608return;609610max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?611(1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;612613if (num_palette < 0 || num_palette > (int) max_palette_length)614{615if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)616png_error(png_ptr, "Invalid palette length");617618else619{620png_warning(png_ptr, "Invalid palette length");621622return;623}624}625626if ((num_palette > 0 && palette == NULL) ||627(num_palette == 0628# ifdef PNG_MNG_FEATURES_SUPPORTED629&& (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0630# endif631))632{633png_error(png_ptr, "Invalid palette");634}635636/* It may not actually be necessary to set png_ptr->palette here;637* we do it for backward compatibility with the way the png_handle_tRNS638* function used to do the allocation.639*640* 1.6.0: the above statement appears to be incorrect; something has to set641* the palette inside png_struct on read.642*/643png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);644645/* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead646* of num_palette entries, in case of an invalid PNG file or incorrect647* call to png_set_PLTE() with too-large sample values.648*/649png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,650PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));651652if (num_palette > 0)653memcpy(png_ptr->palette, palette, (unsigned int)num_palette *654(sizeof (png_color)));655info_ptr->palette = png_ptr->palette;656info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;657658info_ptr->free_me |= PNG_FREE_PLTE;659660info_ptr->valid |= PNG_INFO_PLTE;661}662663#ifdef PNG_sBIT_SUPPORTED664void PNGAPI665png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,666png_const_color_8p sig_bit)667{668png_debug1(1, "in %s storage function", "sBIT");669670if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)671return;672673info_ptr->sig_bit = *sig_bit;674info_ptr->valid |= PNG_INFO_sBIT;675}676#endif677678#ifdef PNG_sRGB_SUPPORTED679void PNGAPI680png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)681{682png_debug1(1, "in %s storage function", "sRGB");683684if (png_ptr == NULL || info_ptr == NULL)685return;686687(void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);688png_colorspace_sync_info(png_ptr, info_ptr);689}690691void PNGAPI692png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,693int srgb_intent)694{695png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");696697if (png_ptr == NULL || info_ptr == NULL)698return;699700if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,701srgb_intent) != 0)702{703/* This causes the gAMA and cHRM to be written too */704info_ptr->colorspace.flags |=705PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;706}707708png_colorspace_sync_info(png_ptr, info_ptr);709}710#endif /* sRGB */711712713#ifdef PNG_iCCP_SUPPORTED714void PNGAPI715png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,716png_const_charp name, int compression_type,717png_const_bytep profile, png_uint_32 proflen)718{719png_charp new_iccp_name;720png_bytep new_iccp_profile;721size_t length;722723png_debug1(1, "in %s storage function", "iCCP");724725if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)726return;727728if (compression_type != PNG_COMPRESSION_TYPE_BASE)729png_app_error(png_ptr, "Invalid iCCP compression method");730731/* Set the colorspace first because this validates the profile; do not732* override previously set app cHRM or gAMA here (because likely as not the733* application knows better than libpng what the correct values are.) Pass734* the info_ptr color_type field to png_colorspace_set_ICC because in the735* write case it has not yet been stored in png_ptr.736*/737{738int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,739proflen, profile, info_ptr->color_type);740741png_colorspace_sync_info(png_ptr, info_ptr);742743/* Don't do any of the copying if the profile was bad, or inconsistent. */744if (result == 0)745return;746747/* But do write the gAMA and cHRM chunks from the profile. */748info_ptr->colorspace.flags |=749PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;750}751752length = strlen(name)+1;753new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));754755if (new_iccp_name == NULL)756{757png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");758759return;760}761762memcpy(new_iccp_name, name, length);763new_iccp_profile = png_voidcast(png_bytep,764png_malloc_warn(png_ptr, proflen));765766if (new_iccp_profile == NULL)767{768png_free(png_ptr, new_iccp_name);769png_benign_error(png_ptr,770"Insufficient memory to process iCCP profile");771772return;773}774775memcpy(new_iccp_profile, profile, proflen);776777png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);778779info_ptr->iccp_proflen = proflen;780info_ptr->iccp_name = new_iccp_name;781info_ptr->iccp_profile = new_iccp_profile;782info_ptr->free_me |= PNG_FREE_ICCP;783info_ptr->valid |= PNG_INFO_iCCP;784}785#endif786787#ifdef PNG_TEXT_SUPPORTED788void PNGAPI789png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,790png_const_textp text_ptr, int num_text)791{792int ret;793ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);794795if (ret != 0)796png_error(png_ptr, "Insufficient memory to store text");797}798799int /* PRIVATE */800png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,801png_const_textp text_ptr, int num_text)802{803int i;804805png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :806(unsigned long)png_ptr->chunk_name);807808if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)809return(0);810811/* Make sure we have enough space in the "text" array in info_struct812* to hold all of the incoming text_ptr objects. This compare can't overflow813* because max_text >= num_text (anyway, subtract of two positive integers814* can't overflow in any case.)815*/816if (num_text > info_ptr->max_text - info_ptr->num_text)817{818int old_num_text = info_ptr->num_text;819int max_text;820png_textp new_text = NULL;821822/* Calculate an appropriate max_text, checking for overflow. */823max_text = old_num_text;824if (num_text <= INT_MAX - max_text)825{826max_text += num_text;827828/* Round up to a multiple of 8 */829if (max_text < INT_MAX-8)830max_text = (max_text + 8) & ~0x7;831832else833max_text = INT_MAX;834835/* Now allocate a new array and copy the old members in; this does all836* the overflow checks.837*/838new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,839info_ptr->text, old_num_text, max_text-old_num_text,840sizeof *new_text));841}842843if (new_text == NULL)844{845png_chunk_report(png_ptr, "too many text chunks",846PNG_CHUNK_WRITE_ERROR);847848return 1;849}850851png_free(png_ptr, info_ptr->text);852853info_ptr->text = new_text;854info_ptr->free_me |= PNG_FREE_TEXT;855info_ptr->max_text = max_text;856/* num_text is adjusted below as the entries are copied in */857858png_debug1(3, "allocated %d entries for info_ptr->text", max_text);859}860861for (i = 0; i < num_text; i++)862{863size_t text_length, key_len;864size_t lang_len, lang_key_len;865png_textp textp = &(info_ptr->text[info_ptr->num_text]);866867if (text_ptr[i].key == NULL)868continue;869870if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||871text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)872{873png_chunk_report(png_ptr, "text compression mode is out of range",874PNG_CHUNK_WRITE_ERROR);875continue;876}877878key_len = strlen(text_ptr[i].key);879880if (text_ptr[i].compression <= 0)881{882lang_len = 0;883lang_key_len = 0;884}885886else887# ifdef PNG_iTXt_SUPPORTED888{889/* Set iTXt data */890891if (text_ptr[i].lang != NULL)892lang_len = strlen(text_ptr[i].lang);893894else895lang_len = 0;896897if (text_ptr[i].lang_key != NULL)898lang_key_len = strlen(text_ptr[i].lang_key);899900else901lang_key_len = 0;902}903# else /* iTXt */904{905png_chunk_report(png_ptr, "iTXt chunk not supported",906PNG_CHUNK_WRITE_ERROR);907continue;908}909# endif910911if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')912{913text_length = 0;914# ifdef PNG_iTXt_SUPPORTED915if (text_ptr[i].compression > 0)916textp->compression = PNG_ITXT_COMPRESSION_NONE;917918else919# endif920textp->compression = PNG_TEXT_COMPRESSION_NONE;921}922923else924{925text_length = strlen(text_ptr[i].text);926textp->compression = text_ptr[i].compression;927}928929textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,930key_len + text_length + lang_len + lang_key_len + 4));931932if (textp->key == NULL)933{934png_chunk_report(png_ptr, "text chunk: out of memory",935PNG_CHUNK_WRITE_ERROR);936937return 1;938}939940png_debug2(2, "Allocated %lu bytes at %p in png_set_text",941(unsigned long)(png_uint_32)942(key_len + lang_len + lang_key_len + text_length + 4),943textp->key);944945memcpy(textp->key, text_ptr[i].key, key_len);946*(textp->key + key_len) = '\0';947948if (text_ptr[i].compression > 0)949{950textp->lang = textp->key + key_len + 1;951memcpy(textp->lang, text_ptr[i].lang, lang_len);952*(textp->lang + lang_len) = '\0';953textp->lang_key = textp->lang + lang_len + 1;954memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);955*(textp->lang_key + lang_key_len) = '\0';956textp->text = textp->lang_key + lang_key_len + 1;957}958959else960{961textp->lang=NULL;962textp->lang_key=NULL;963textp->text = textp->key + key_len + 1;964}965966if (text_length != 0)967memcpy(textp->text, text_ptr[i].text, text_length);968969*(textp->text + text_length) = '\0';970971# ifdef PNG_iTXt_SUPPORTED972if (textp->compression > 0)973{974textp->text_length = 0;975textp->itxt_length = text_length;976}977978else979# endif980{981textp->text_length = text_length;982textp->itxt_length = 0;983}984985info_ptr->num_text++;986png_debug1(3, "transferred text chunk %d", info_ptr->num_text);987}988989return(0);990}991#endif992993#ifdef PNG_tIME_SUPPORTED994void PNGAPI995png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,996png_const_timep mod_time)997{998png_debug1(1, "in %s storage function", "tIME");9991000if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||1001(png_ptr->mode & PNG_WROTE_tIME) != 0)1002return;10031004if (mod_time->month == 0 || mod_time->month > 12 ||1005mod_time->day == 0 || mod_time->day > 31 ||1006mod_time->hour > 23 || mod_time->minute > 59 ||1007mod_time->second > 60)1008{1009png_warning(png_ptr, "Ignoring invalid time value");10101011return;1012}10131014info_ptr->mod_time = *mod_time;1015info_ptr->valid |= PNG_INFO_tIME;1016}1017#endif10181019#ifdef PNG_tRNS_SUPPORTED1020void PNGAPI1021png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,1022png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)1023{1024png_debug1(1, "in %s storage function", "tRNS");10251026if (png_ptr == NULL || info_ptr == NULL)10271028return;10291030if (trans_alpha != NULL)1031{1032/* It may not actually be necessary to set png_ptr->trans_alpha here;1033* we do it for backward compatibility with the way the png_handle_tRNS1034* function used to do the allocation.1035*1036* 1.6.0: The above statement is incorrect; png_handle_tRNS effectively1037* relies on png_set_tRNS storing the information in png_struct1038* (otherwise it won't be there for the code in pngrtran.c).1039*/10401041png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);10421043if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)1044{1045/* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */1046info_ptr->trans_alpha = png_voidcast(png_bytep,1047png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));1048memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans);1049}1050png_ptr->trans_alpha = info_ptr->trans_alpha;1051}10521053if (trans_color != NULL)1054{1055#ifdef PNG_WARNINGS_SUPPORTED1056if (info_ptr->bit_depth < 16)1057{1058int sample_max = (1 << info_ptr->bit_depth) - 1;10591060if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&1061trans_color->gray > sample_max) ||1062(info_ptr->color_type == PNG_COLOR_TYPE_RGB &&1063(trans_color->red > sample_max ||1064trans_color->green > sample_max ||1065trans_color->blue > sample_max)))1066png_warning(png_ptr,1067"tRNS chunk has out-of-range samples for bit_depth");1068}1069#endif10701071info_ptr->trans_color = *trans_color;10721073if (num_trans == 0)1074num_trans = 1;1075}10761077info_ptr->num_trans = (png_uint_16)num_trans;10781079if (num_trans != 0)1080{1081info_ptr->valid |= PNG_INFO_tRNS;1082info_ptr->free_me |= PNG_FREE_TRNS;1083}1084}1085#endif10861087#ifdef PNG_sPLT_SUPPORTED1088void PNGAPI1089png_set_sPLT(png_const_structrp png_ptr,1090png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)1091/*1092* entries - array of png_sPLT_t structures1093* to be added to the list of palettes1094* in the info structure.1095*1096* nentries - number of palette structures to be1097* added.1098*/1099{1100png_sPLT_tp np;11011102if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)1103return;11041105/* Use the internal realloc function, which checks for all the possible1106* overflows. Notice that the parameters are (int) and (size_t)1107*/1108np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,1109info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,1110sizeof *np));11111112if (np == NULL)1113{1114/* Out of memory or too many chunks */1115png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);11161117return;1118}11191120png_free(png_ptr, info_ptr->splt_palettes);1121info_ptr->splt_palettes = np;1122info_ptr->free_me |= PNG_FREE_SPLT;11231124np += info_ptr->splt_palettes_num;11251126do1127{1128size_t length;11291130/* Skip invalid input entries */1131if (entries->name == NULL || entries->entries == NULL)1132{1133/* png_handle_sPLT doesn't do this, so this is an app error */1134png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");1135/* Just skip the invalid entry */1136continue;1137}11381139np->depth = entries->depth;11401141/* In the event of out-of-memory just return - there's no point keeping1142* on trying to add sPLT chunks.1143*/1144length = strlen(entries->name) + 1;1145np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));11461147if (np->name == NULL)1148break;11491150memcpy(np->name, entries->name, length);11511152/* IMPORTANT: we have memory now that won't get freed if something else1153* goes wrong; this code must free it. png_malloc_array produces no1154* warnings; use a png_chunk_report (below) if there is an error.1155*/1156np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,1157entries->nentries, sizeof (png_sPLT_entry)));11581159if (np->entries == NULL)1160{1161png_free(png_ptr, np->name);1162np->name = NULL;1163break;1164}11651166np->nentries = entries->nentries;1167/* This multiply can't overflow because png_malloc_array has already1168* checked it when doing the allocation.1169*/1170memcpy(np->entries, entries->entries,1171(unsigned int)entries->nentries * sizeof (png_sPLT_entry));11721173/* Note that 'continue' skips the advance of the out pointer and out1174* count, so an invalid entry is not added.1175*/1176info_ptr->valid |= PNG_INFO_sPLT;1177++(info_ptr->splt_palettes_num);1178++np;1179++entries;1180}1181while (--nentries);11821183if (nentries > 0)1184png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);1185}1186#endif /* sPLT */11871188#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED1189static png_byte1190check_location(png_const_structrp png_ptr, int location)1191{1192location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);11931194/* New in 1.6.0; copy the location and check it. This is an API1195* change; previously the app had to use the1196* png_set_unknown_chunk_location API below for each chunk.1197*/1198if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)1199{1200/* Write struct, so unknown chunks come from the app */1201png_app_warning(png_ptr,1202"png_set_unknown_chunks now expects a valid location");1203/* Use the old behavior */1204location = (png_byte)(png_ptr->mode &1205(PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));1206}12071208/* This need not be an internal error - if the app calls1209* png_set_unknown_chunks on a read pointer it must get the location right.1210*/1211if (location == 0)1212png_error(png_ptr, "invalid location in png_set_unknown_chunks");12131214/* Now reduce the location to the top-most set bit by removing each least1215* significant bit in turn.1216*/1217while (location != (location & -location))1218location &= ~(location & -location);12191220/* The cast is safe because 'location' is a bit mask and only the low four1221* bits are significant.1222*/1223return (png_byte)location;1224}12251226void PNGAPI1227png_set_unknown_chunks(png_const_structrp png_ptr,1228png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)1229{1230png_unknown_chunkp np;12311232if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||1233unknowns == NULL)1234return;12351236/* Check for the failure cases where support has been disabled at compile1237* time. This code is hardly ever compiled - it's here because1238* STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this1239* code) but may be meaningless if the read or write handling of unknown1240* chunks is not compiled in.1241*/1242# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \1243defined(PNG_READ_SUPPORTED)1244if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)1245{1246png_app_error(png_ptr, "no unknown chunk support on read");12471248return;1249}1250# endif1251# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \1252defined(PNG_WRITE_SUPPORTED)1253if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)1254{1255png_app_error(png_ptr, "no unknown chunk support on write");12561257return;1258}1259# endif12601261/* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that1262* unknown critical chunks could be lost with just a warning resulting in1263* undefined behavior. Now png_chunk_report is used to provide behavior1264* appropriate to read or write.1265*/1266np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,1267info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,1268sizeof *np));12691270if (np == NULL)1271{1272png_chunk_report(png_ptr, "too many unknown chunks",1273PNG_CHUNK_WRITE_ERROR);12741275return;1276}12771278png_free(png_ptr, info_ptr->unknown_chunks);1279info_ptr->unknown_chunks = np; /* safe because it is initialized */1280info_ptr->free_me |= PNG_FREE_UNKN;12811282np += info_ptr->unknown_chunks_num;12831284/* Increment unknown_chunks_num each time round the loop to protect the1285* just-allocated chunk data.1286*/1287for (; num_unknowns > 0; --num_unknowns, ++unknowns)1288{1289memcpy(np->name, unknowns->name, (sizeof np->name));1290np->name[(sizeof np->name)-1] = '\0';1291np->location = check_location(png_ptr, unknowns->location);12921293if (unknowns->size == 0)1294{1295np->data = NULL;1296np->size = 0;1297}12981299else1300{1301np->data = png_voidcast(png_bytep,1302png_malloc_base(png_ptr, unknowns->size));13031304if (np->data == NULL)1305{1306png_chunk_report(png_ptr, "unknown chunk: out of memory",1307PNG_CHUNK_WRITE_ERROR);1308/* But just skip storing the unknown chunk */1309continue;1310}13111312memcpy(np->data, unknowns->data, unknowns->size);1313np->size = unknowns->size;1314}13151316/* These increments are skipped on out-of-memory for the data - the1317* unknown chunk entry gets overwritten if the png_chunk_report returns.1318* This is correct in the read case (the chunk is just dropped.)1319*/1320++np;1321++(info_ptr->unknown_chunks_num);1322}1323}13241325void PNGAPI1326png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,1327int chunk, int location)1328{1329/* This API is pretty pointless in 1.6.0 because the location can be set1330* before the call to png_set_unknown_chunks.1331*1332* TODO: add a png_app_warning in 1.71333*/1334if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&1335chunk < info_ptr->unknown_chunks_num)1336{1337if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)1338{1339png_app_error(png_ptr, "invalid unknown chunk location");1340/* Fake out the pre 1.6.0 behavior: */1341if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */1342location = PNG_AFTER_IDAT;13431344else1345location = PNG_HAVE_IHDR; /* also undocumented */1346}13471348info_ptr->unknown_chunks[chunk].location =1349check_location(png_ptr, location);1350}1351}1352#endif /* STORE_UNKNOWN_CHUNKS */13531354#ifdef PNG_MNG_FEATURES_SUPPORTED1355png_uint_32 PNGAPI1356png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)1357{1358png_debug(1, "in png_permit_mng_features");13591360if (png_ptr == NULL)1361return 0;13621363png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;13641365return png_ptr->mng_features_permitted;1366}1367#endif13681369#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED1370static unsigned int1371add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)1372{1373unsigned int i;13741375/* Utility function: update the 'keep' state of a chunk if it is already in1376* the list, otherwise add it to the list.1377*/1378for (i=0; i<count; ++i, list += 5)1379{1380if (memcmp(list, add, 4) == 0)1381{1382list[4] = (png_byte)keep;13831384return count;1385}1386}13871388if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)1389{1390++count;1391memcpy(list, add, 4);1392list[4] = (png_byte)keep;1393}13941395return count;1396}13971398void PNGAPI1399png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,1400png_const_bytep chunk_list, int num_chunks_in)1401{1402png_bytep new_list;1403unsigned int num_chunks, old_num_chunks;14041405if (png_ptr == NULL)1406return;14071408if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)1409{1410png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");14111412return;1413}14141415if (num_chunks_in <= 0)1416{1417png_ptr->unknown_default = keep;14181419/* '0' means just set the flags, so stop here */1420if (num_chunks_in == 0)1421return;1422}14231424if (num_chunks_in < 0)1425{1426/* Ignore all unknown chunks and all chunks recognized by1427* libpng except for IHDR, PLTE, tRNS, IDAT, and IEND1428*/1429static const png_byte chunks_to_ignore[] = {143098, 75, 71, 68, '\0', /* bKGD */143199, 72, 82, 77, '\0', /* cHRM */1432101, 88, 73, 102, '\0', /* eXIf */1433103, 65, 77, 65, '\0', /* gAMA */1434104, 73, 83, 84, '\0', /* hIST */1435105, 67, 67, 80, '\0', /* iCCP */1436105, 84, 88, 116, '\0', /* iTXt */1437111, 70, 70, 115, '\0', /* oFFs */1438112, 67, 65, 76, '\0', /* pCAL */1439112, 72, 89, 115, '\0', /* pHYs */1440115, 66, 73, 84, '\0', /* sBIT */1441115, 67, 65, 76, '\0', /* sCAL */1442115, 80, 76, 84, '\0', /* sPLT */1443115, 84, 69, 82, '\0', /* sTER */1444115, 82, 71, 66, '\0', /* sRGB */1445116, 69, 88, 116, '\0', /* tEXt */1446116, 73, 77, 69, '\0', /* tIME */1447122, 84, 88, 116, '\0' /* zTXt */1448};14491450chunk_list = chunks_to_ignore;1451num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;1452}14531454else /* num_chunks_in > 0 */1455{1456if (chunk_list == NULL)1457{1458/* Prior to 1.6.0 this was silently ignored, now it is an app_error1459* which can be switched off.1460*/1461png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");14621463return;1464}14651466num_chunks = (unsigned int)num_chunks_in;1467}14681469old_num_chunks = png_ptr->num_chunk_list;1470if (png_ptr->chunk_list == NULL)1471old_num_chunks = 0;14721473/* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.1474*/1475if (num_chunks + old_num_chunks > UINT_MAX/5)1476{1477png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");14781479return;1480}14811482/* If these chunks are being reset to the default then no more memory is1483* required because add_one_chunk above doesn't extend the list if the 'keep'1484* parameter is the default.1485*/1486if (keep != 0)1487{1488new_list = png_voidcast(png_bytep, png_malloc(png_ptr,14895 * (num_chunks + old_num_chunks)));14901491if (old_num_chunks > 0)1492memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);1493}14941495else if (old_num_chunks > 0)1496new_list = png_ptr->chunk_list;14971498else1499new_list = NULL;15001501/* Add the new chunks together with each one's handling code. If the chunk1502* already exists the code is updated, otherwise the chunk is added to the1503* end. (In libpng 1.6.0 order no longer matters because this code enforces1504* the earlier convention that the last setting is the one that is used.)1505*/1506if (new_list != NULL)1507{1508png_const_bytep inlist;1509png_bytep outlist;1510unsigned int i;15111512for (i=0; i<num_chunks; ++i)1513{1514old_num_chunks = add_one_chunk(new_list, old_num_chunks,1515chunk_list+5*i, keep);1516}15171518/* Now remove any spurious 'default' entries. */1519num_chunks = 0;1520for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)1521{1522if (inlist[4])1523{1524if (outlist != inlist)1525memcpy(outlist, inlist, 5);1526outlist += 5;1527++num_chunks;1528}1529}15301531/* This means the application has removed all the specialized handling. */1532if (num_chunks == 0)1533{1534if (png_ptr->chunk_list != new_list)1535png_free(png_ptr, new_list);15361537new_list = NULL;1538}1539}15401541else1542num_chunks = 0;15431544png_ptr->num_chunk_list = num_chunks;15451546if (png_ptr->chunk_list != new_list)1547{1548if (png_ptr->chunk_list != NULL)1549png_free(png_ptr, png_ptr->chunk_list);15501551png_ptr->chunk_list = new_list;1552}1553}1554#endif15551556#ifdef PNG_READ_USER_CHUNKS_SUPPORTED1557void PNGAPI1558png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,1559png_user_chunk_ptr read_user_chunk_fn)1560{1561png_debug(1, "in png_set_read_user_chunk_fn");15621563if (png_ptr == NULL)1564return;15651566png_ptr->read_user_chunk_fn = read_user_chunk_fn;1567png_ptr->user_chunk_ptr = user_chunk_ptr;1568}1569#endif15701571#ifdef PNG_INFO_IMAGE_SUPPORTED1572void PNGAPI1573png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,1574png_bytepp row_pointers)1575{1576png_debug1(1, "in %s storage function", "rows");15771578if (png_ptr == NULL || info_ptr == NULL)1579return;15801581if (info_ptr->row_pointers != NULL &&1582(info_ptr->row_pointers != row_pointers))1583png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);15841585info_ptr->row_pointers = row_pointers;15861587if (row_pointers != NULL)1588info_ptr->valid |= PNG_INFO_IDAT;1589}1590#endif15911592void PNGAPI1593png_set_compression_buffer_size(png_structrp png_ptr, size_t size)1594{1595if (png_ptr == NULL)1596return;15971598if (size == 0 || size > PNG_UINT_31_MAX)1599png_error(png_ptr, "invalid compression buffer size");16001601# ifdef PNG_SEQUENTIAL_READ_SUPPORTED1602if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)1603{1604png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */1605return;1606}1607# endif16081609# ifdef PNG_WRITE_SUPPORTED1610if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)1611{1612if (png_ptr->zowner != 0)1613{1614png_warning(png_ptr,1615"Compression buffer size cannot be changed because it is in use");16161617return;1618}16191620#ifndef __COVERITY__1621/* Some compilers complain that this is always false. However, it1622* can be true when integer overflow happens.1623*/1624if (size > ZLIB_IO_MAX)1625{1626png_warning(png_ptr,1627"Compression buffer size limited to system maximum");1628size = ZLIB_IO_MAX; /* must fit */1629}1630#endif16311632if (size < 6)1633{1634/* Deflate will potentially go into an infinite loop on a SYNC_FLUSH1635* if this is permitted.1636*/1637png_warning(png_ptr,1638"Compression buffer size cannot be reduced below 6");16391640return;1641}16421643if (png_ptr->zbuffer_size != size)1644{1645png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);1646png_ptr->zbuffer_size = (uInt)size;1647}1648}1649# endif1650}16511652void PNGAPI1653png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)1654{1655if (png_ptr != NULL && info_ptr != NULL)1656info_ptr->valid &= (unsigned int)(~mask);1657}165816591660#ifdef PNG_SET_USER_LIMITS_SUPPORTED1661/* This function was added to libpng 1.2.6 */1662void PNGAPI1663png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,1664png_uint_32 user_height_max)1665{1666/* Images with dimensions larger than these limits will be1667* rejected by png_set_IHDR(). To accept any PNG datastream1668* regardless of dimensions, set both limits to 0x7fffffff.1669*/1670if (png_ptr == NULL)1671return;16721673png_ptr->user_width_max = user_width_max;1674png_ptr->user_height_max = user_height_max;1675}16761677/* This function was added to libpng 1.4.0 */1678void PNGAPI1679png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)1680{1681if (png_ptr != NULL)1682png_ptr->user_chunk_cache_max = user_chunk_cache_max;1683}16841685/* This function was added to libpng 1.4.1 */1686void PNGAPI1687png_set_chunk_malloc_max (png_structrp png_ptr,1688png_alloc_size_t user_chunk_malloc_max)1689{1690if (png_ptr != NULL)1691png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;1692}1693#endif /* ?SET_USER_LIMITS */169416951696#ifdef PNG_BENIGN_ERRORS_SUPPORTED1697void PNGAPI1698png_set_benign_errors(png_structrp png_ptr, int allowed)1699{1700png_debug(1, "in png_set_benign_errors");17011702/* If allowed is 1, png_benign_error() is treated as a warning.1703*1704* If allowed is 0, png_benign_error() is treated as an error (which1705* is the default behavior if png_set_benign_errors() is not called).1706*/17071708if (allowed != 0)1709png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |1710PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;17111712else1713png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |1714PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);1715}1716#endif /* BENIGN_ERRORS */17171718#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED1719/* Whether to report invalid palette index; added at libng-1.5.10.1720* It is possible for an indexed (color-type==3) PNG file to contain1721* pixels with invalid (out-of-range) indexes if the PLTE chunk has1722* fewer entries than the image's bit-depth would allow. We recover1723* from this gracefully by filling any incomplete palette with zeros1724* (opaque black). By default, when this occurs libpng will issue1725* a benign error. This API can be used to override that behavior.1726*/1727void PNGAPI1728png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)1729{1730png_debug(1, "in png_set_check_for_invalid_index");17311732if (allowed > 0)1733png_ptr->num_palette_max = 0;17341735else1736png_ptr->num_palette_max = -1;1737}1738#endif17391740#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \1741defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)1742/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,1743* and if invalid, correct the keyword rather than discarding the entire1744* chunk. The PNG 1.0 specification requires keywords 1-79 characters in1745* length, forbids leading or trailing whitespace, multiple internal spaces,1746* and the non-break space (0x80) from ISO 8859-1. Returns keyword length.1747*1748* The 'new_key' buffer must be 80 characters in size (for the keyword plus a1749* trailing '\0'). If this routine returns 0 then there was no keyword, or a1750* valid one could not be generated, and the caller must png_error.1751*/1752png_uint_32 /* PRIVATE */1753png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)1754{1755#ifdef PNG_WARNINGS_SUPPORTED1756png_const_charp orig_key = key;1757#endif1758png_uint_32 key_len = 0;1759int bad_character = 0;1760int space = 1;17611762png_debug(1, "in png_check_keyword");17631764if (key == NULL)1765{1766*new_key = 0;1767return 0;1768}17691770while (*key && key_len < 79)1771{1772png_byte ch = (png_byte)*key++;17731774if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))1775{1776*new_key++ = ch; ++key_len; space = 0;1777}17781779else if (space == 0)1780{1781/* A space or an invalid character when one wasn't seen immediately1782* before; output just a space.1783*/1784*new_key++ = 32; ++key_len; space = 1;17851786/* If the character was not a space then it is invalid. */1787if (ch != 32)1788bad_character = ch;1789}17901791else if (bad_character == 0)1792bad_character = ch; /* just skip it, record the first error */1793}17941795if (key_len > 0 && space != 0) /* trailing space */1796{1797--key_len; --new_key;1798if (bad_character == 0)1799bad_character = 32;1800}18011802/* Terminate the keyword */1803*new_key = 0;18041805if (key_len == 0)1806return 0;18071808#ifdef PNG_WARNINGS_SUPPORTED1809/* Try to only output one warning per keyword: */1810if (*key != 0) /* keyword too long */1811png_warning(png_ptr, "keyword truncated");18121813else if (bad_character != 0)1814{1815PNG_WARNING_PARAMETERS(p)18161817png_warning_parameter(p, 1, orig_key);1818png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);18191820png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");1821}1822#else /* !WARNINGS */1823PNG_UNUSED(png_ptr)1824#endif /* !WARNINGS */18251826return key_len;1827}1828#endif /* TEXT || pCAL || iCCP || sPLT */1829#endif /* READ || WRITE */183018311832