CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libpng17/pngset.c
Views: 1401
1/* pngset.c - storage of image information into info struct2*3* Last changed in libpng 1.7.0 [(PENDING RELEASE)]4* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12* The functions here are used during reads to store data from the file13* into the info struct, and during writes to store application data14* into the info struct for writing into the file. This abstracts the15* info struct and allows us to change the structure in the future.16*/1718#include "pngpriv.h"19#define PNG_SRC_FILE PNG_SRC_FILE_pngset2021#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)2223#ifdef PNG_bKGD_SUPPORTED24void PNGAPI25png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,26png_const_color_16p background)27{28png_debug1(1, "in %s storage function", "bKGD");2930if (png_ptr == NULL || info_ptr == NULL || background == NULL)31return;3233info_ptr->background = *background;34info_ptr->valid |= PNG_INFO_bKGD;35}36#endif3738#ifdef PNG_cHRM_SUPPORTED39void PNGFAPI40png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,41png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,42png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,43png_fixed_point blue_x, png_fixed_point blue_y)44{45png_xy xy;4647png_debug1(1, "in %s storage function", "cHRM fixed");4849if (png_ptr == NULL || info_ptr == NULL)50return;5152xy.redx = red_x;53xy.redy = red_y;54xy.greenx = green_x;55xy.greeny = green_y;56xy.bluex = blue_x;57xy.bluey = blue_y;58xy.whitex = white_x;59xy.whitey = white_y;6061if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,622/* override with app values*/) != 0)63info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;6465png_colorspace_sync_info(png_ptr, info_ptr);66}6768void PNGFAPI69png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,70png_fixed_point int_red_X, png_fixed_point int_red_Y,71png_fixed_point int_red_Z, png_fixed_point int_green_X,72png_fixed_point int_green_Y, png_fixed_point int_green_Z,73png_fixed_point int_blue_X, png_fixed_point int_blue_Y,74png_fixed_point int_blue_Z)75{76png_XYZ XYZ;7778png_debug1(1, "in %s storage function", "cHRM XYZ fixed");7980if (png_ptr == NULL || info_ptr == NULL)81return;8283XYZ.red_X = int_red_X;84XYZ.red_Y = int_red_Y;85XYZ.red_Z = int_red_Z;86XYZ.green_X = int_green_X;87XYZ.green_Y = int_green_Y;88XYZ.green_Z = int_green_Z;89XYZ.blue_X = int_blue_X;90XYZ.blue_Y = int_blue_Y;91XYZ.blue_Z = int_blue_Z;9293if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,94&XYZ, 2) != 0)95info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;9697png_colorspace_sync_info(png_ptr, info_ptr);98}99100# ifdef PNG_FLOATING_POINT_SUPPORTED101void PNGAPI102png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,103double white_x, double white_y, double red_x, double red_y,104double green_x, double green_y, double blue_x, double blue_y)105{106png_set_cHRM_fixed(png_ptr, info_ptr,107png_fixed(png_ptr, white_x, "cHRM White X"),108png_fixed(png_ptr, white_y, "cHRM White Y"),109png_fixed(png_ptr, red_x, "cHRM Red X"),110png_fixed(png_ptr, red_y, "cHRM Red Y"),111png_fixed(png_ptr, green_x, "cHRM Green X"),112png_fixed(png_ptr, green_y, "cHRM Green Y"),113png_fixed(png_ptr, blue_x, "cHRM Blue X"),114png_fixed(png_ptr, blue_y, "cHRM Blue Y"));115}116117void PNGAPI118png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,119double red_Y, double red_Z, double green_X, double green_Y, double green_Z,120double blue_X, double blue_Y, double blue_Z)121{122png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,123png_fixed(png_ptr, red_X, "cHRM Red X"),124png_fixed(png_ptr, red_Y, "cHRM Red Y"),125png_fixed(png_ptr, red_Z, "cHRM Red Z"),126png_fixed(png_ptr, green_X, "cHRM Green X"),127png_fixed(png_ptr, green_Y, "cHRM Green Y"),128png_fixed(png_ptr, green_Z, "cHRM Green Z"),129png_fixed(png_ptr, blue_X, "cHRM Blue X"),130png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),131png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));132}133# endif /* FLOATING_POINT */134135#endif /* cHRM */136137#ifdef PNG_gAMA_SUPPORTED138void PNGFAPI139png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,140png_fixed_point file_gamma)141{142png_debug1(1, "in %s storage function", "gAMA");143144if (png_ptr == NULL || info_ptr == NULL)145return;146147png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);148png_colorspace_sync_info(png_ptr, info_ptr);149}150151# ifdef PNG_FLOATING_POINT_SUPPORTED152void PNGAPI153png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)154{155png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,156"png_set_gAMA"));157}158# endif159#endif160161#ifdef PNG_hIST_SUPPORTED162void PNGAPI163png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,164png_const_uint_16p hist)165{166png_debug1(1, "in %s storage function", "hIST");167168if (png_ptr == NULL || info_ptr == NULL)169return;170171if (info_ptr->num_palette == 0 || info_ptr->num_palette172> PNG_MAX_PALETTE_LENGTH)173{174png_warning(png_ptr,175"Invalid palette size, hIST allocation skipped");176177return;178}179180png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);181182/* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in183* version 1.2.1184*/185info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,186PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));187188if (info_ptr->hist == NULL)189{190png_warning(png_ptr, "Insufficient memory for hIST chunk data");191192return;193}194195info_ptr->free_me |= PNG_FREE_HIST;196197{198unsigned int i;199200for (i = 0; i < info_ptr->num_palette; i++)201info_ptr->hist[i] = hist[i];202}203204info_ptr->valid |= PNG_INFO_hIST;205}206#endif207208void PNGAPI209png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,210png_uint_32 width, png_uint_32 height, int bit_depth,211int color_type, int interlace_type, int compression_type,212int filter_type)213{214png_debug1(1, "in %s storage function", "IHDR");215216if (png_ptr == NULL || info_ptr == NULL)217return;218219info_ptr->width = width;220info_ptr->height = height;221info_ptr->bit_depth = png_check_bits(png_ptr, bit_depth, 6);222info_ptr->format = png_check_bits(png_ptr,223PNG_FORMAT_FROM_COLOR_TYPE(color_type), PNG_RF_BITS);224info_ptr->compression_type = png_check_byte(png_ptr, compression_type);225info_ptr->filter_type = png_check_byte(png_ptr, filter_type);226info_ptr->interlace_type = png_check_byte(png_ptr, interlace_type);227228png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,229info_ptr->bit_depth, color_type, info_ptr->interlace_type,230info_ptr->compression_type, info_ptr->filter_type);231}232233#ifdef PNG_oFFs_SUPPORTED234void PNGAPI235png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,236png_int_32 offset_x, png_int_32 offset_y, int unit_type)237{238png_debug1(1, "in %s storage function", "oFFs");239240if (png_ptr == NULL || info_ptr == NULL)241return;242243info_ptr->x_offset = offset_x;244info_ptr->y_offset = offset_y;245info_ptr->offset_unit_type = png_check_byte(png_ptr, unit_type);246info_ptr->valid |= PNG_INFO_oFFs;247}248#endif249250#ifdef PNG_pCAL_SUPPORTED251void PNGAPI252png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,253png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,254int nparams, png_const_charp units, png_charpp params)255{256png_size_t length;257int i;258259png_debug1(1, "in %s storage function", "pCAL");260261if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL262|| (nparams > 0 && params == NULL))263return;264265length = strlen(purpose) + 1;266png_debug1(3, "allocating purpose for info (%lu bytes)",267(unsigned long)length);268269/* TODO: validate format of calibration name and unit name */270271/* Check that the type matches the specification. */272if (type < 0 || type > 3)273png_error(png_ptr, "Invalid pCAL equation type");274275if (nparams < 0 || nparams > 255)276png_error(png_ptr, "Invalid pCAL parameter count");277278/* Validate params[nparams] */279for (i=0; i<nparams; ++i)280{281if (params[i] == NULL ||282!png_check_fp_string(params[i], strlen(params[i])))283png_error(png_ptr, "Invalid format for pCAL parameter");284}285286info_ptr->pcal_purpose = png_voidcast(png_charp,287png_malloc_warn(png_ptr, length));288289if (info_ptr->pcal_purpose == NULL)290{291png_warning(png_ptr, "Insufficient memory for pCAL purpose");292293return;294}295296memcpy(info_ptr->pcal_purpose, purpose, length);297298png_debug(3, "storing X0, X1, type, and nparams in info");299info_ptr->pcal_X0 = X0;300info_ptr->pcal_X1 = X1;301info_ptr->pcal_type = png_check_byte(png_ptr, type);302info_ptr->pcal_nparams = png_check_byte(png_ptr, nparams);303304length = strlen(units) + 1;305png_debug1(3, "allocating units for info (%lu bytes)",306(unsigned long)length);307308info_ptr->pcal_units = png_voidcast(png_charp,309png_malloc_warn(png_ptr, length));310311if (info_ptr->pcal_units == NULL)312{313png_warning(png_ptr, "Insufficient memory for pCAL units");314315return;316}317318memcpy(info_ptr->pcal_units, units, length);319320info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,321(png_size_t)((nparams + 1) * (sizeof (png_charp)))));322323if (info_ptr->pcal_params == NULL)324{325png_warning(png_ptr, "Insufficient memory for pCAL params");326327return;328}329330memset(info_ptr->pcal_params, 0, (nparams + 1) * (sizeof (png_charp)));331332for (i = 0; i < nparams; i++)333{334length = strlen(params[i]) + 1;335png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,336(unsigned long)length);337338info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);339340if (info_ptr->pcal_params[i] == NULL)341{342png_warning(png_ptr, "Insufficient memory for pCAL parameter");343344return;345}346347memcpy(info_ptr->pcal_params[i], params[i], length);348}349350info_ptr->valid |= PNG_INFO_pCAL;351info_ptr->free_me |= PNG_FREE_PCAL;352}353#endif354355#ifdef PNG_sCAL_SUPPORTED356void PNGAPI357png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,358int unit, png_const_charp swidth, png_const_charp sheight)359{360png_size_t lengthw = 0, lengthh = 0;361362png_debug1(1, "in %s storage function", "sCAL");363364if (png_ptr == NULL || info_ptr == NULL)365return;366367/* Double check the unit (should never get here with an invalid368* unit unless this is an API call.)369*/370if (unit != 1 && unit != 2)371png_error(png_ptr, "Invalid sCAL unit");372373if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||374swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))375png_error(png_ptr, "Invalid sCAL width");376377if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||378sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))379png_error(png_ptr, "Invalid sCAL height");380381info_ptr->scal_unit = png_check_byte(png_ptr, unit);382383++lengthw;384385png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);386387info_ptr->scal_s_width = png_voidcast(png_charp,388png_malloc_warn(png_ptr, lengthw));389390if (info_ptr->scal_s_width == NULL)391{392png_warning(png_ptr, "Memory allocation failed while processing sCAL");393394return;395}396397memcpy(info_ptr->scal_s_width, swidth, lengthw);398399++lengthh;400401png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);402403info_ptr->scal_s_height = png_voidcast(png_charp,404png_malloc_warn(png_ptr, lengthh));405406if (info_ptr->scal_s_height == NULL)407{408png_free (png_ptr, info_ptr->scal_s_width);409info_ptr->scal_s_width = NULL;410411png_warning(png_ptr, "Memory allocation failed while processing sCAL");412413return;414}415416memcpy(info_ptr->scal_s_height, sheight, lengthh);417418info_ptr->valid |= PNG_INFO_sCAL;419info_ptr->free_me |= PNG_FREE_SCAL;420}421422# if defined(PNG_FLOATING_POINT_SUPPORTED) &&\423defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)424void PNGAPI425png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,426double width, double height)427{428png_debug1(1, "in %s storage function", "sCAL");429430/* Check the arguments. */431if (width <= 0)432png_warning(png_ptr, "Invalid sCAL width ignored");433434else if (height <= 0)435png_warning(png_ptr, "Invalid sCAL height ignored");436437else438{439/* Convert 'width' and 'height' to ASCII. */440char swidth[PNG_sCAL_MAX_DIGITS+1];441char sheight[PNG_sCAL_MAX_DIGITS+1];442443png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,444PNG_sCAL_PRECISION);445png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,446PNG_sCAL_PRECISION);447448png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);449}450}451# endif452453# ifdef PNG_FIXED_POINT_SUPPORTED454void PNGAPI455png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,456png_fixed_point width, png_fixed_point height)457{458png_debug1(1, "in %s storage function", "sCAL");459460/* Check the arguments. */461if (width <= 0)462png_warning(png_ptr, "Invalid sCAL width ignored");463464else if (height <= 0)465png_warning(png_ptr, "Invalid sCAL height ignored");466467else468{469/* Convert 'width' and 'height' to ASCII. */470char swidth[PNG_sCAL_MAX_DIGITS+1];471char sheight[PNG_sCAL_MAX_DIGITS+1];472473png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);474png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);475476png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);477}478}479# endif480#endif481482#ifdef PNG_pHYs_SUPPORTED483void PNGAPI484png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,485png_uint_32 res_x, png_uint_32 res_y, int unit_type)486{487png_debug1(1, "in %s storage function", "pHYs");488489if (png_ptr == NULL || info_ptr == NULL)490return;491492info_ptr->x_pixels_per_unit = res_x;493info_ptr->y_pixels_per_unit = res_y;494info_ptr->phys_unit_type = png_check_byte(png_ptr, unit_type);495info_ptr->valid |= PNG_INFO_pHYs;496}497#endif498499void PNGAPI500png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,501png_const_colorp palette, int num_palette)502{503504png_uint_32 max_palette_length;505506png_debug1(1, "in %s storage function", "PLTE");507508if (png_ptr == NULL || info_ptr == NULL)509return;510511max_palette_length = (info_ptr->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ?512(1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;513514if (num_palette < 0 || num_palette > (int) max_palette_length)515{516if ((info_ptr->format & PNG_FORMAT_FLAG_COLORMAP) != 0)517png_chunk_error(png_ptr, "Invalid palette length");518519else520{521png_chunk_report(png_ptr, "Invalid palette length", PNG_CHUNK_ERROR);522return;523}524}525526#ifdef PNG_MNG_FEATURES_SUPPORTED527if ((num_palette > 0 && palette == NULL) || (num_palette == 0 &&528(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0))529png_error(png_ptr, "Invalid palette");530#else531if ((num_palette > 0 && palette == NULL) || num_palette == 0)532png_error(png_ptr, "Invalid palette");533#endif /* MNG_FEATURES */534535png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);536537/* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead538* of num_palette entries, in case of an invalid PNG file or incorrect539* call to png_set_PLTE() with too-large sample values.540*/541info_ptr->palette = png_voidcast(png_colorp, png_malloc(png_ptr,542PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));543544if (num_palette > 0)545memcpy(info_ptr->palette, palette, num_palette * (sizeof (png_color)));546547/* Set the remainder of the palette entries to something recognizable; the548* code used to leave them set to 0, which made seeing palette index errors549* difficult.550*/551if (num_palette < PNG_MAX_PALETTE_LENGTH)552{553int i;554png_color c;555556memset(&c, 0x42, sizeof c); /* fill in any padding */557c.red = 0xbe;558c.green = 0xad;559c.blue = 0xed; /* Visible in memory as 'beaded' */560561for (i=num_palette; i<PNG_MAX_PALETTE_LENGTH; ++i)562info_ptr->palette[i] = c;563}564565info_ptr->num_palette = png_check_bits(png_ptr, num_palette, 9);566info_ptr->free_me |= PNG_FREE_PLTE;567info_ptr->valid |= PNG_INFO_PLTE;568}569570#ifdef PNG_sBIT_SUPPORTED571void PNGAPI572png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,573png_const_color_8p sig_bit)574{575png_debug1(1, "in %s storage function", "sBIT");576577if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)578return;579580info_ptr->sig_bit = *sig_bit;581info_ptr->valid |= PNG_INFO_sBIT;582}583#endif584585#ifdef PNG_sRGB_SUPPORTED586void PNGAPI587png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)588{589png_debug1(1, "in %s storage function", "sRGB");590591if (png_ptr == NULL || info_ptr == NULL)592return;593594(void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);595png_colorspace_sync_info(png_ptr, info_ptr);596}597598void PNGAPI599png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,600int srgb_intent)601{602png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");603604if (png_ptr == NULL || info_ptr == NULL)605return;606607if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,608srgb_intent) != 0)609{610/* This causes the gAMA and cHRM to be written too */611info_ptr->colorspace.flags |=612PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;613}614615png_colorspace_sync_info(png_ptr, info_ptr);616}617#endif /* sRGB */618619620#ifdef PNG_iCCP_SUPPORTED621void PNGAPI622png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,623png_const_charp name, int compression_type,624png_const_bytep profile, png_uint_32 proflen)625{626png_charp new_iccp_name;627png_bytep new_iccp_profile;628png_size_t length;629630png_debug1(1, "in %s storage function", "iCCP");631632if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)633return;634635if (compression_type != PNG_COMPRESSION_TYPE_BASE)636png_app_error(png_ptr, "Invalid iCCP compression method");637638/* Set the colorspace first because this validates the profile; do not639* override previously set app cHRM or gAMA here (because likely as not the640* application knows better than libpng what the correct values are.) Pass641* the info_ptr color_type field to png_colorspace_set_ICC because in the642* write case it has not yet been stored in png_ptr.643*/644{645int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,646proflen, profile, (info_ptr->format & PNG_FORMAT_FLAG_COLOR) != 0);647648png_colorspace_sync_info(png_ptr, info_ptr);649650/* Don't do any of the copying if the profile was bad, or inconsistent. */651if (result == 0)652return;653654/* But do write the gAMA and cHRM chunks from the profile. */655info_ptr->colorspace.flags |=656PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;657}658659length = strlen(name)+1;660new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));661662if (new_iccp_name == NULL)663{664png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");665666return;667}668669memcpy(new_iccp_name, name, length);670new_iccp_profile = png_voidcast(png_bytep,671png_malloc_warn(png_ptr, proflen));672673if (new_iccp_profile == NULL)674{675png_free(png_ptr, new_iccp_name);676png_benign_error(png_ptr,677"Insufficient memory to process iCCP profile");678679return;680}681682memcpy(new_iccp_profile, profile, proflen);683684png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);685686info_ptr->iccp_name = new_iccp_name;687info_ptr->iccp_profile = new_iccp_profile;688info_ptr->free_me |= PNG_FREE_ICCP;689info_ptr->valid |= PNG_INFO_iCCP;690}691#endif692693#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_tIME_SUPPORTED)694static png_byte695get_location(png_const_structrp png_ptr)696/* Return the correct location flag for a chunk. For a png_set_<chunk>697* called during read this is the current read location, for a698* png_set_<chunk> called during write it is the following write location699* (because the chunks at the current location have already been written.)700* For a png_set_<chunk> called before read starts (none of the 'position'701* mode bits are set) the position is set to the start (PNG_HAVE_IHDR). For702* a png_set_chunk> called before write starts PNG_HAVE_PLTE|PNG_AFTER_IDAT703* are set because we don't know whether this is the main png_info or the one704* for use after the IDAT from png_write_end.705*706* The latter behavior gives compatibility with the old behavior of707* png_set_text; it only wrote text chunks after the PLTE or IDAT and it just708* wrote them once.709*/710{711if ((png_ptr->mode & PNG_AFTER_IDAT) != 0U)712return PNG_AFTER_IDAT;713714else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0U)715{716/* In a write operation PNG_HAVE_PLTE is set when the chunks before the717* IDAT are written (in png_write_info), so the location needs to be after718* IDAT. In a read the chunk was read after the PLTE but before the IDAT.719*/720if (png_ptr->read_struct)721return PNG_HAVE_PLTE;722723else /* write struct */724return PNG_AFTER_IDAT;725}726727else if ((png_ptr->mode & PNG_HAVE_IHDR) != 0U)728{729/* For read this means the chunk is between the IHDR and any PLTE; there730* may be none but then there is no order to preserve.731*732* For write png_write_IHDR has been called and that means that the info733* before PLTE has been written, so the chunk goes after.734*/735if (png_ptr->read_struct)736return PNG_HAVE_IHDR;737738else /* write struct */739return PNG_HAVE_PLTE;740}741742else if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0U)743{744/* This should not happen on read, on write it means that the app has745* started writing so assume the text is meant to go before PLTE.746*/747return PNG_HAVE_IHDR;748}749750/* Either a png_set_<chunk> from the application during read before reading751* starts (so mode is 0) or the same for write.752*/753else754{755if (png_ptr->read_struct)756return PNG_HAVE_IHDR;757758else759return PNG_HAVE_PLTE|PNG_AFTER_IDAT;760}761}762#endif /* TEXT || tIME */763764#ifdef PNG_TEXT_SUPPORTED765void PNGAPI766png_set_text(png_structrp png_ptr, png_inforp info_ptr,767png_const_textp text_ptr, int num_text)768{769int ret;770ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);771772if (ret != 0)773png_error(png_ptr, "Insufficient memory to store text");774}775776int /* PRIVATE */777png_set_text_2(png_structrp png_ptr, png_inforp info_ptr,778png_const_textp text_ptr, int num_text)779{780int i;781782png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11 :783(unsigned long)png_ptr->chunk_name);784785if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)786return(0);787788/* Make sure we have enough space in the "text" array in info_struct789* to hold all of the incoming text_ptr objects. This compare can't overflow790* because max_text >= num_text (anyway, subtract of two positive integers791* can't overflow in any case.)792*/793if (num_text > info_ptr->max_text - info_ptr->num_text)794{795int old_num_text = info_ptr->num_text;796int max_text;797png_textp new_text = NULL;798799/* The code below goes horribly wrong if old_num_text ever ends up800* negative, so:801*/802affirm(old_num_text >= 0);803804/* Calculate an appropriate max_text, checking for overflow. */805max_text = old_num_text;806if (num_text <= INT_MAX - max_text)807{808max_text += num_text;809810/* Round up to a multiple of 8 */811if (max_text < INT_MAX-8)812max_text = (max_text + 8) & ~0x7;813814else815max_text = INT_MAX;816817/* Now allocate a new array and copy the old members in; this does all818* the overflow checks.819*/820new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,821info_ptr->text, old_num_text, max_text-old_num_text,822sizeof *new_text));823}824825if (new_text == NULL)826{827png_chunk_report(png_ptr, "too many text chunks",828PNG_CHUNK_WRITE_ERROR);829830return 1;831}832833png_free(png_ptr, info_ptr->text);834835info_ptr->text = new_text;836info_ptr->free_me |= PNG_FREE_TEXT;837info_ptr->max_text = max_text;838/* num_text is adjusted below as the entries are copied in */839840png_debug1(3, "allocated %d entries for info_ptr->text", max_text);841}842843for (i = 0; i < num_text; i++)844{845size_t text_length, key_len;846size_t lang_len, lang_key_len;847png_textp textp = &(info_ptr->text[info_ptr->num_text]);848849if (text_ptr[i].key == NULL)850continue;851852if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||853text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)854{855png_chunk_report(png_ptr, "text compression mode is out of range",856PNG_CHUNK_WRITE_ERROR);857continue;858}859860key_len = strlen(text_ptr[i].key);861862if (text_ptr[i].compression <= 0)863{864lang_len = 0;865lang_key_len = 0;866}867868else869# ifdef PNG_iTXt_SUPPORTED870{871/* Set iTXt data */872873if (text_ptr[i].lang != NULL)874lang_len = strlen(text_ptr[i].lang);875876else877lang_len = 0;878879if (text_ptr[i].lang_key != NULL)880lang_key_len = strlen(text_ptr[i].lang_key);881882else883lang_key_len = 0;884}885# else /* iTXt */886{887png_chunk_report(png_ptr, "iTXt chunk not supported",888PNG_CHUNK_WRITE_ERROR);889continue;890}891# endif892893/* Record the location for posterity. On the read side this just says894* where the chunk was (approximately). On the write side it says how far895* through the write process libpng was before this API was called.896*/897textp->location = get_location(png_ptr);898899if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')900{901text_length = 0;902# ifdef PNG_iTXt_SUPPORTED903if (text_ptr[i].compression > 0)904textp->compression = PNG_ITXT_COMPRESSION_NONE;905906else907# endif908textp->compression = PNG_TEXT_COMPRESSION_NONE;909}910911else912{913text_length = strlen(text_ptr[i].text);914textp->compression = text_ptr[i].compression;915}916917textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,918key_len + text_length + lang_len + lang_key_len + 4));919920if (textp->key == NULL)921{922png_chunk_report(png_ptr, "text chunk: out of memory",923PNG_CHUNK_WRITE_ERROR);924925return 1;926}927928png_debug2(2, "Allocated %lu bytes at %p in png_set_text",929(unsigned long)(png_uint_32)930(key_len + lang_len + lang_key_len + text_length + 4),931textp->key);932933memcpy(textp->key, text_ptr[i].key, key_len);934*(textp->key + key_len) = '\0';935936if (text_ptr[i].compression > 0)937{938textp->lang = textp->key + key_len + 1;939memcpy(textp->lang, text_ptr[i].lang, lang_len);940*(textp->lang + lang_len) = '\0';941textp->lang_key = textp->lang + lang_len + 1;942memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);943*(textp->lang_key + lang_key_len) = '\0';944textp->text = textp->lang_key + lang_key_len + 1;945}946947else948{949textp->lang=NULL;950textp->lang_key=NULL;951textp->text = textp->key + key_len + 1;952}953954if (text_length != 0)955memcpy(textp->text, text_ptr[i].text, text_length);956957*(textp->text + text_length) = '\0';958959# ifdef PNG_iTXt_SUPPORTED960if (textp->compression > 0)961{962textp->text_length = 0;963textp->itxt_length = text_length;964}965966else967# endif968{969textp->text_length = text_length;970textp->itxt_length = 0;971}972973info_ptr->num_text++;974png_debug1(3, "transferred text chunk %d", info_ptr->num_text);975}976977return(0);978}979#endif980981#ifdef PNG_tIME_SUPPORTED982void PNGAPI983png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,984png_const_timep mod_time)985{986png_debug1(1, "in %s storage function", "tIME");987988if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL)989return;990991/* It is valid to do set the tIME chunk after the IDAT has been written, but992* not if one has already been written. This was ignored before - the993* previous time was used - this is a bad thing.994*/995if ((info_ptr->valid & PNG_INFO_tIME) != 0 /* Changing a time chunk */ &&996(png_ptr->mode & PNG_HAVE_IHDR) != 0 /* after writing started */)997{998/* So it can be *set* but it can't be *changed* after the info before PLTE999* has been written. (Note that putting tIME into an unknown chunk1000* currently gets round this; to be fixed.)1001*/1002png_app_error(png_ptr, "cannot change tIME after writing starts");1003return;1004}10051006if (mod_time->month == 0 || mod_time->month > 12 ||1007mod_time->day == 0 || mod_time->day > 31 ||1008mod_time->hour > 23 || mod_time->minute > 59 ||1009mod_time->second > 60)1010{1011png_app_error(png_ptr, "Ignoring invalid time value");1012return;1013}10141015info_ptr->mod_time = *mod_time;1016info_ptr->time_location = get_location(png_ptr);1017info_ptr->valid |= PNG_INFO_tIME;1018}1019#endif /* tIME */10201021#ifdef PNG_tRNS_SUPPORTED1022void PNGAPI1023png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,1024png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)1025{1026png_debug1(1, "in %s storage function", "tRNS");10271028if (png_ptr == NULL || info_ptr == NULL)1029return;10301031if ((info_ptr->format & PNG_FORMAT_FLAG_ALPHA) != 0)1032png_chunk_report(png_ptr,1033"png_set_tRNS: invalid on PNG with alpha channel", PNG_CHUNK_ERROR);10341035else if ((info_ptr->format & PNG_FORMAT_FLAG_COLORMAP) != 0)1036{1037int max_num;10381039/* Free the old data; num_trans 0 can be used to kill the tRNS */1040png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);10411042/* Do this just in case the old data was not owned by libpng: */1043info_ptr->valid &= PNG_BIC_MASK(PNG_INFO_tRNS);1044info_ptr->trans_alpha = NULL;1045info_ptr->num_trans = 0;10461047/* Expect png_set_PLTE to happen before png_set_tRNS, so num_palette will1048* be set, but this is not a requirement of the API.1049*/1050if (info_ptr->num_palette)1051max_num = info_ptr->num_palette;10521053else1054max_num = 1 << info_ptr->bit_depth;10551056if (num_trans > max_num)1057{1058png_chunk_report(png_ptr, "png_set_tRNS: num_trans too large",1059PNG_CHUNK_ERROR);1060/* If control returns simply limit it; the behavior prior to 1.7 was to1061* issue a warning and skip the palette in png_write_tRNS.1062*/1063num_trans = max_num;1064}10651066/* But only attempt a malloc if there is something to do; so the app can1067* set a tRNS array then later delete it.1068*/1069if (num_trans > 0 && trans_alpha != NULL)1070{1071/* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1,1072* this avoids issues where a palette image contains out of range1073* indices.1074*/1075info_ptr->trans_alpha = png_voidcast(png_bytep, png_malloc(png_ptr,1076PNG_MAX_PALETTE_LENGTH));1077info_ptr->free_me |= PNG_FREE_TRNS;10781079memcpy(info_ptr->trans_alpha, trans_alpha,1080(unsigned)/*SAFE*/num_trans);1081info_ptr->valid |= PNG_INFO_tRNS;1082info_ptr->num_trans = png_check_bits(png_ptr, num_trans, 9);1083}1084}10851086else /* not a PALETTE image */1087{1088/* Invalidate any prior transparent color and set num_trans. It is not1089* used internally in this case but png_get_tRNS still returns it.1090*/1091info_ptr->valid &= PNG_BIC_MASK(PNG_INFO_tRNS);1092info_ptr->num_trans = 0; /* for png_get_tRNS */10931094if (trans_color != NULL && info_ptr->bit_depth < 16)1095{1096unsigned int sample_max = (1U << info_ptr->bit_depth) - 1U;10971098if (!(info_ptr->format & PNG_FORMAT_FLAG_COLOR) ?1099trans_color->gray <= sample_max :1100trans_color->red <= sample_max &&1101trans_color->green <= sample_max &&1102trans_color->blue <= sample_max)1103{1104info_ptr->trans_color = *trans_color;1105info_ptr->valid |= PNG_INFO_tRNS;1106info_ptr->num_trans = 1; /* for png_get_tRNS */1107}11081109else1110png_chunk_report(png_ptr,1111"tRNS chunk has out-of-range samples for bit_depth",1112PNG_CHUNK_ERROR);1113}1114}1115}1116#endif11171118#ifdef PNG_sPLT_SUPPORTED1119void PNGAPI1120png_set_sPLT(png_structrp png_ptr,1121png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)1122/*1123* entries - array of png_sPLT_t structures1124* to be added to the list of palettes1125* in the info structure.1126*1127* nentries - number of palette structures to be1128* added.1129*/1130{1131png_sPLT_tp np;11321133if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)1134return;11351136/* Use the internal realloc function, which checks for all the possible1137* overflows. Notice that the parameters are (int) and (size_t)1138*/1139np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,1140info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,1141sizeof *np));11421143if (np == NULL)1144{1145/* Out of memory or too many chunks */1146png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);11471148return;1149}11501151png_free(png_ptr, info_ptr->splt_palettes);1152info_ptr->splt_palettes = np;1153info_ptr->free_me |= PNG_FREE_SPLT;11541155np += info_ptr->splt_palettes_num;11561157do1158{1159png_size_t length;11601161/* Skip invalid input entries */1162if (entries->name == NULL || entries->entries == NULL)1163{1164/* png_handle_sPLT doesn't do this, so this is an app error */1165png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");1166/* Just skip the invalid entry */1167continue;1168}11691170np->depth = entries->depth;11711172/* In the event of out-of-memory just return - there's no point keeping1173* on trying to add sPLT chunks.1174*/1175length = strlen(entries->name) + 1;1176np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));11771178if (np->name == NULL)1179break;11801181memcpy(np->name, entries->name, length);11821183/* IMPORTANT: we have memory now that won't get freed if something else1184* goes wrong; this code must free it. png_malloc_array produces no1185* warnings; use a png_chunk_report (below) if there is an error.1186*/1187np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,1188entries->nentries, sizeof (png_sPLT_entry)));11891190if (np->entries == NULL)1191{1192png_free(png_ptr, np->name);1193np->name = NULL;1194break;1195}11961197np->nentries = entries->nentries;1198/* This multiply can't overflow because png_malloc_array has already1199* checked it when doing the allocation.1200*/1201memcpy(np->entries, entries->entries,1202entries->nentries * sizeof (png_sPLT_entry));12031204/* Note that 'continue' skips the advance of the out pointer and out1205* count, so an invalid entry is not added.1206*/1207info_ptr->valid |= PNG_INFO_sPLT;1208++(info_ptr->splt_palettes_num);1209++np;1210++entries;1211}1212while (--nentries);12131214if (nentries > 0)1215png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);1216}1217#endif /* sPLT */12181219#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED1220static png_byte1221check_location(png_const_structrp png_ptr, int location)1222{1223location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);12241225/* New in 1.6.0; copy the location and check it. This is an API1226* change; previously the app had to use the1227* png_set_unknown_chunk_location API below for each chunk.1228*/1229if (location == 0 && !png_ptr->read_struct)1230{1231/* Write struct, so unknown chunks come from the app */1232png_app_warning(png_ptr,1233"png_set_unknown_chunks now expects a valid location");1234/* Use the old behavior */1235location = png_check_byte(png_ptr, png_ptr->mode &1236(PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));1237}12381239/* This need not be an internal error - if the app calls1240* png_set_unknown_chunks on a read pointer it must get the location right.1241*/1242if (location == 0)1243png_error(png_ptr, "invalid location in png_set_unknown_chunks");12441245/* Now reduce the location to the top-most set bit by removing each least1246* significant bit in turn.1247*/1248while (location != (location & -location))1249location &= ~(location & -location);12501251/* The cast is safe because 'location' is a bit mask and only the low four1252* bits are significant.1253*/1254return png_check_byte(png_ptr, location);1255}12561257void PNGAPI1258png_set_unknown_chunks(png_structrp png_ptr,1259png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)1260{1261png_unknown_chunkp np;12621263if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||1264unknowns == NULL)1265return;12661267/* Check for the failure cases where support has been disabled at compile1268* time. This code is hardly ever compiled - it's here because1269* STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this1270* code) but may be meaningless if the read or write handling of unknown1271* chunks is not compiled in.1272*/1273# ifndef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED1274if (png_ptr->read_struct)1275{1276png_app_error(png_ptr, "no unknown chunk support on read");12771278return;1279}1280# endif1281# ifndef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED1282if (!png_ptr->read_struct)1283{1284png_app_error(png_ptr, "no unknown chunk support on write");12851286return;1287}1288# endif12891290/* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that1291* unknown critical chunks could be lost with just a warning resulting in1292* undefined behavior. Now png_chunk_report is used to provide behavior1293* appropriate to read or write.1294*/1295np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,1296info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,1297sizeof *np));12981299if (np == NULL)1300{1301png_chunk_report(png_ptr, "too many unknown chunks", PNG_CHUNK_ERROR);1302return;1303}13041305png_free(png_ptr, info_ptr->unknown_chunks);1306info_ptr->unknown_chunks = np; /* safe because it is initialized */1307info_ptr->free_me |= PNG_FREE_UNKN;13081309np += info_ptr->unknown_chunks_num;13101311/* Increment unknown_chunks_num each time round the loop to protect the1312* just-allocated chunk data.1313*/1314for (; num_unknowns > 0; --num_unknowns, ++unknowns)1315{1316memcpy(np->name, unknowns->name, (sizeof np->name));1317np->name[(sizeof np->name)-1] = '\0';1318np->location = check_location(png_ptr, unknowns->location);13191320if (unknowns->size == 0)1321{1322np->data = NULL;1323np->size = 0;1324}13251326else1327{1328np->data = png_voidcast(png_bytep,1329png_malloc_base(png_ptr, unknowns->size));13301331if (np->data == NULL)1332{1333png_chunk_report(png_ptr, "unknown chunk: out of memory",1334PNG_CHUNK_ERROR);1335/* But just skip storing the unknown chunk */1336continue;1337}13381339memcpy(np->data, unknowns->data, unknowns->size);1340np->size = unknowns->size;1341}13421343/* These increments are skipped on out-of-memory for the data - the1344* unknown chunk entry gets overwritten if the png_chunk_report returns.1345* This is correct in the read case (the chunk is just dropped.)1346*/1347++np;1348++(info_ptr->unknown_chunks_num);1349}1350}13511352void PNGAPI1353png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,1354int chunk, int location)1355{1356/* This API is pretty pointless in 1.6.0 because the location can be set1357* before the call to png_set_unknown_chunks.1358*/1359if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&1360chunk < info_ptr->unknown_chunks_num)1361{1362if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)1363{1364png_app_error(png_ptr, "invalid unknown chunk location");1365/* Fake out the pre 1.6.0 behavior: */1366if ((location & PNG_HAVE_IDAT) != 0) /* undocumented! */1367location = PNG_AFTER_IDAT;13681369else1370location = PNG_HAVE_IHDR; /* also undocumented */1371}13721373info_ptr->unknown_chunks[chunk].location =1374check_location(png_ptr, location);1375}13761377/* TODO: make this an error in 1.8 (or maybe it will become one in 1.7!) */1378else if (png_ptr != NULL)1379png_app_warning(png_ptr, "unknown chunk index out of range");1380}1381#endif /* STORE_UNKNOWN_CHUNKS */13821383#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED1384static unsigned int1385add_one_chunk(png_structrp png_ptr, png_bytep list, unsigned int count,1386png_const_bytep add, int keep)1387{1388unsigned int i;13891390/* Utility function: update the 'keep' state of a chunk if it is already in1391* the list, otherwise add it to the list.1392*/1393for (i=0; i<count; ++i, list += 5)1394{1395if (memcmp(list, add, 4) == 0)1396{1397list[4] = keep & 0x3;1398png_cache_known_unknown(png_ptr, add, keep);1399return count;1400}1401}14021403if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)1404{1405++count;1406memcpy(list, add, 4);1407list[4] = png_check_byte(png_ptr, keep);1408png_cache_known_unknown(png_ptr, add, keep);1409}14101411return count;1412}14131414void PNGAPI1415png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,1416png_const_bytep chunk_list, int num_chunks_in)1417{1418png_bytep new_list;1419unsigned int num_chunks, old_num_chunks;14201421if (png_ptr == NULL)1422return;14231424/* To actually use IF_SAFE or ALWAYS on read it is necessary to have the1425* read SAVE code enabled.1426*/1427if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)1428{1429png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");1430return;1431}14321433# ifndef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED1434/* This is only a warning; if the application handles the chunk with a1435* read callback it may work fine.1436*/1437if (png_ptr->read_struct && keep >= PNG_HANDLE_CHUNK_IF_SAFE)1438png_app_warning(png_ptr,1439"png_set_keep_unknown_chunks: unsupported keep");1440# endif14411442if (num_chunks_in <= 0)1443{1444png_ptr->unknown_default = (unsigned)keep & 0x3;14451446/* '0' means just set the flags, so stop here */1447if (num_chunks_in == 0)1448return;1449}14501451if (num_chunks_in < 0)1452{1453/* Ignore all unknown chunks and all chunks recognized by1454* libpng except for IHDR, PLTE, tRNS, IDAT, and IEND1455*/1456static PNG_CONST png_byte chunks_to_ignore[] = {145798, 75, 71, 68, '\0', /* bKGD */145899, 72, 82, 77, '\0', /* cHRM */1459103, 65, 77, 65, '\0', /* gAMA */1460104, 73, 83, 84, '\0', /* hIST */1461105, 67, 67, 80, '\0', /* iCCP */1462105, 84, 88, 116, '\0', /* iTXt */1463111, 70, 70, 115, '\0', /* oFFs */1464112, 67, 65, 76, '\0', /* pCAL */1465112, 72, 89, 115, '\0', /* pHYs */1466115, 66, 73, 84, '\0', /* sBIT */1467115, 67, 65, 76, '\0', /* sCAL */1468115, 80, 76, 84, '\0', /* sPLT */1469115, 84, 69, 82, '\0', /* sTER */1470115, 82, 71, 66, '\0', /* sRGB */1471116, 69, 88, 116, '\0', /* tEXt */1472116, 73, 77, 69, '\0', /* tIME */1473122, 84, 88, 116, '\0' /* zTXt */1474};14751476chunk_list = chunks_to_ignore;1477num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;1478}14791480else /* num_chunks_in > 0 */1481{1482if (chunk_list == NULL)1483{1484/* Prior to 1.6.0 this was silently ignored, now it is an app_error1485* which can be switched off.1486*/1487png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");1488return;1489}14901491num_chunks = num_chunks_in;1492}14931494old_num_chunks = png_ptr->num_chunk_list;1495if (png_ptr->chunk_list == NULL)1496old_num_chunks = 0;14971498/* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.1499*/1500if (num_chunks + old_num_chunks > UINT_MAX/5)1501{1502png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");1503return;1504}15051506/* If these chunks are being reset to the default then no more memory is1507* required because add_one_chunk above doesn't extend the list if the 'keep'1508* parameter is the default.1509*/1510if (keep != 0)1511{1512new_list = png_voidcast(png_bytep, png_malloc(png_ptr,15135 * (num_chunks + old_num_chunks)));15141515if (old_num_chunks > 0)1516memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);1517}15181519else if (old_num_chunks > 0)1520new_list = png_ptr->chunk_list;15211522else1523new_list = NULL;15241525/* Add the new chunks together with each one's handling code. If the chunk1526* already exists the code is updated, otherwise the chunk is added to the1527* end. (In libpng 1.6.0 order no longer matters because this code enforces1528* the earlier convention that the last setting is the one that is used.)1529*/1530if (new_list != NULL)1531{1532png_const_bytep inlist;1533png_bytep outlist;1534unsigned int i;15351536for (i=0; i<num_chunks; ++i)1537{1538old_num_chunks = add_one_chunk(png_ptr, new_list, old_num_chunks,1539chunk_list+5*i, keep);1540}15411542/* Now remove any spurious 'default' entries. */1543num_chunks = 0;1544for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)1545{1546if (inlist[4])1547{1548if (outlist != inlist)1549memcpy(outlist, inlist, 5);1550outlist += 5;1551++num_chunks;1552}1553}15541555/* This means the application has removed all the specialized handling. */1556if (num_chunks == 0)1557{1558if (png_ptr->chunk_list != new_list)1559png_free(png_ptr, new_list);15601561new_list = NULL;1562}1563}15641565else1566num_chunks = 0;15671568png_ptr->num_chunk_list = num_chunks;15691570if (png_ptr->chunk_list != new_list)1571{1572if (png_ptr->chunk_list != NULL)1573png_free(png_ptr, png_ptr->chunk_list);15741575png_ptr->chunk_list = new_list;1576}1577}1578#endif15791580#ifdef PNG_READ_USER_CHUNKS_SUPPORTED1581void PNGAPI1582png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,1583png_user_chunk_ptr read_user_chunk_fn)1584{1585png_debug(1, "in png_set_read_user_chunk_fn");15861587if (png_ptr == NULL)1588return;15891590png_ptr->read_user_chunk_fn = read_user_chunk_fn;1591png_ptr->user_chunk_ptr = user_chunk_ptr;1592}1593#endif15941595#ifdef PNG_WRITE_PNG_SUPPORTED1596void PNGAPI1597png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,1598png_bytepp row_pointers)1599{1600png_debug1(1, "in %s storage function", "rows");16011602if (png_ptr == NULL || info_ptr == NULL)1603return;16041605if (info_ptr->row_pointers != NULL &&1606(info_ptr->row_pointers != row_pointers))1607png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);16081609info_ptr->row_pointers = row_pointers;16101611if (row_pointers != NULL)1612info_ptr->valid |= PNG_INFO_IDAT;1613}1614#endif16151616void PNGAPI1617png_set_compression_buffer_size(png_structrp png_ptr, png_alloc_size_t size)1618{1619if (png_ptr == NULL)1620return;16211622if (size == 0 ||1623size > (png_ptr->read_struct ? ZLIB_IO_MAX : PNG_UINT_31_MAX))1624png_error(png_ptr, "invalid compression buffer size");16251626# if (defined PNG_SEQUENTIAL_READ_SUPPORTED) || defined PNG_WRITE_SUPPORTED1627png_ptr->IDAT_size = (png_uint_32)/*SAFE*/size;1628# endif /* SEQUENTIAL_READ || WRITE */1629}16301631void PNGAPI1632png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)1633{1634if (png_ptr != NULL && info_ptr != NULL)1635info_ptr->valid &= PNG_BIC_MASK(mask);1636}163716381639#ifdef PNG_SET_USER_LIMITS_SUPPORTED1640/* This function was added to libpng 1.2.6 */1641void PNGAPI1642png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,1643png_uint_32 user_height_max)1644{1645/* Images with dimensions larger than these limits will be1646* rejected by png_set_IHDR(). To accept any PNG datastream1647* regardless of dimensions, set both limits to 0x7fffffffL.1648*/1649if (png_ptr == NULL)1650return;16511652png_ptr->user_width_max = user_width_max;1653png_ptr->user_height_max = user_height_max;1654}16551656/* This function was added to libpng 1.4.0 */1657void PNGAPI1658png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)1659{1660if (png_ptr != NULL)1661png_ptr->user_chunk_cache_max = user_chunk_cache_max;1662}16631664/* This function was added to libpng 1.4.1 */1665void PNGAPI1666png_set_chunk_malloc_max (png_structrp png_ptr,1667png_alloc_size_t user_chunk_malloc_max)1668{1669if (png_ptr != NULL)1670png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;1671}1672#endif /* ?SET_USER_LIMITS */1673#endif /* READ || WRITE */167416751676