LCOV - code coverage report
Current view: top level - media/libpng - pngset.c (source / functions) Hit Total Coverage
Test: output.info Lines: 97 286 33.9 %
Date: 2017-07-14 16:53:18 Functions: 8 23 34.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : 
       2             : /* pngset.c - storage of image information into info struct
       3             :  *
       4             :  * Last changed in libpng 1.6.26 [October 20, 2016]
       5             :  * Copyright (c) 1998-2016 Glenn Randers-Pehrson
       6             :  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
       7             :  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
       8             :  *
       9             :  * This code is released under the libpng license.
      10             :  * For conditions of distribution and use, see the disclaimer
      11             :  * and license in png.h
      12             :  *
      13             :  * The functions here are used during reads to store data from the file
      14             :  * into the info struct, and during writes to store application data
      15             :  * into the info struct for writing into the file.  This abstracts the
      16             :  * info struct and allows us to change the structure in the future.
      17             :  */
      18             : 
      19             : #include "pngpriv.h"
      20             : 
      21             : #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
      22             : 
      23             : #ifdef PNG_bKGD_SUPPORTED
      24             : void PNGAPI
      25             : png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,
      26             :     png_const_color_16p background)
      27             : {
      28             :    png_debug1(1, "in %s storage function", "bKGD");
      29             : 
      30             :    if (png_ptr == NULL || info_ptr == NULL || background == NULL)
      31             :       return;
      32             : 
      33             :    info_ptr->background = *background;
      34             :    info_ptr->valid |= PNG_INFO_bKGD;
      35             : }
      36             : #endif
      37             : 
      38             : #ifdef PNG_cHRM_SUPPORTED
      39             : void PNGFAPI
      40           0 : png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
      41             :     png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
      42             :     png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
      43             :     png_fixed_point blue_x, png_fixed_point blue_y)
      44             : {
      45             :    png_xy xy;
      46             : 
      47             :    png_debug1(1, "in %s storage function", "cHRM fixed");
      48             : 
      49           0 :    if (png_ptr == NULL || info_ptr == NULL)
      50           0 :       return;
      51             : 
      52           0 :    xy.redx = red_x;
      53           0 :    xy.redy = red_y;
      54           0 :    xy.greenx = green_x;
      55           0 :    xy.greeny = green_y;
      56           0 :    xy.bluex = blue_x;
      57           0 :    xy.bluey = blue_y;
      58           0 :    xy.whitex = white_x;
      59           0 :    xy.whitey = white_y;
      60             : 
      61           0 :    if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy,
      62             :        2/* override with app values*/) != 0)
      63           0 :       info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
      64             : 
      65           0 :    png_colorspace_sync_info(png_ptr, info_ptr);
      66             : }
      67             : 
      68             : void PNGFAPI
      69           0 : png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
      70             :     png_fixed_point int_red_X, png_fixed_point int_red_Y,
      71             :     png_fixed_point int_red_Z, png_fixed_point int_green_X,
      72             :     png_fixed_point int_green_Y, png_fixed_point int_green_Z,
      73             :     png_fixed_point int_blue_X, png_fixed_point int_blue_Y,
      74             :     png_fixed_point int_blue_Z)
      75             : {
      76             :    png_XYZ XYZ;
      77             : 
      78             :    png_debug1(1, "in %s storage function", "cHRM XYZ fixed");
      79             : 
      80           0 :    if (png_ptr == NULL || info_ptr == NULL)
      81           0 :       return;
      82             : 
      83           0 :    XYZ.red_X = int_red_X;
      84           0 :    XYZ.red_Y = int_red_Y;
      85           0 :    XYZ.red_Z = int_red_Z;
      86           0 :    XYZ.green_X = int_green_X;
      87           0 :    XYZ.green_Y = int_green_Y;
      88           0 :    XYZ.green_Z = int_green_Z;
      89           0 :    XYZ.blue_X = int_blue_X;
      90           0 :    XYZ.blue_Y = int_blue_Y;
      91           0 :    XYZ.blue_Z = int_blue_Z;
      92             : 
      93           0 :    if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace,
      94             :        &XYZ, 2) != 0)
      95           0 :       info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
      96             : 
      97           0 :    png_colorspace_sync_info(png_ptr, info_ptr);
      98             : }
      99             : 
     100             : #  ifdef PNG_FLOATING_POINT_SUPPORTED
     101             : void PNGAPI
     102           0 : png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
     103             :     double white_x, double white_y, double red_x, double red_y,
     104             :     double green_x, double green_y, double blue_x, double blue_y)
     105             : {
     106           0 :    png_set_cHRM_fixed(png_ptr, info_ptr,
     107             :        png_fixed(png_ptr, white_x, "cHRM White X"),
     108             :        png_fixed(png_ptr, white_y, "cHRM White Y"),
     109             :        png_fixed(png_ptr, red_x, "cHRM Red X"),
     110             :        png_fixed(png_ptr, red_y, "cHRM Red Y"),
     111             :        png_fixed(png_ptr, green_x, "cHRM Green X"),
     112             :        png_fixed(png_ptr, green_y, "cHRM Green Y"),
     113             :        png_fixed(png_ptr, blue_x, "cHRM Blue X"),
     114             :        png_fixed(png_ptr, blue_y, "cHRM Blue Y"));
     115           0 : }
     116             : 
     117             : void PNGAPI
     118           0 : png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X,
     119             :     double red_Y, double red_Z, double green_X, double green_Y, double green_Z,
     120             :     double blue_X, double blue_Y, double blue_Z)
     121             : {
     122           0 :    png_set_cHRM_XYZ_fixed(png_ptr, info_ptr,
     123             :        png_fixed(png_ptr, red_X, "cHRM Red X"),
     124             :        png_fixed(png_ptr, red_Y, "cHRM Red Y"),
     125             :        png_fixed(png_ptr, red_Z, "cHRM Red Z"),
     126             :        png_fixed(png_ptr, green_X, "cHRM Green X"),
     127             :        png_fixed(png_ptr, green_Y, "cHRM Green Y"),
     128             :        png_fixed(png_ptr, green_Z, "cHRM Green Z"),
     129             :        png_fixed(png_ptr, blue_X, "cHRM Blue X"),
     130             :        png_fixed(png_ptr, blue_Y, "cHRM Blue Y"),
     131             :        png_fixed(png_ptr, blue_Z, "cHRM Blue Z"));
     132           0 : }
     133             : #  endif /* FLOATING_POINT */
     134             : 
     135             : #endif /* cHRM */
     136             : 
     137             : #ifdef PNG_gAMA_SUPPORTED
     138             : void PNGFAPI
     139           0 : png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr,
     140             :     png_fixed_point file_gamma)
     141             : {
     142             :    png_debug1(1, "in %s storage function", "gAMA");
     143             : 
     144           0 :    if (png_ptr == NULL || info_ptr == NULL)
     145           0 :       return;
     146             : 
     147           0 :    png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma);
     148           0 :    png_colorspace_sync_info(png_ptr, info_ptr);
     149             : }
     150             : 
     151             : #  ifdef PNG_FLOATING_POINT_SUPPORTED
     152             : void PNGAPI
     153           0 : png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma)
     154             : {
     155           0 :    png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma,
     156             :        "png_set_gAMA"));
     157           0 : }
     158             : #  endif
     159             : #endif
     160             : 
     161             : #ifdef PNG_hIST_SUPPORTED
     162             : void PNGAPI
     163             : png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr,
     164             :     png_const_uint_16p hist)
     165             : {
     166             :    int i;
     167             : 
     168             :    png_debug1(1, "in %s storage function", "hIST");
     169             : 
     170             :    if (png_ptr == NULL || info_ptr == NULL)
     171             :       return;
     172             : 
     173             :    if (info_ptr->num_palette == 0 || info_ptr->num_palette
     174             :        > PNG_MAX_PALETTE_LENGTH)
     175             :    {
     176             :       png_warning(png_ptr,
     177             :           "Invalid palette size, hIST allocation skipped");
     178             : 
     179             :       return;
     180             :    }
     181             : 
     182             :    png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0);
     183             : 
     184             :    /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in
     185             :     * version 1.2.1
     186             :     */
     187             :    info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr,
     188             :        PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16))));
     189             : 
     190             :    if (info_ptr->hist == NULL)
     191             :    {
     192             :       png_warning(png_ptr, "Insufficient memory for hIST chunk data");
     193             : 
     194             :       return;
     195             :    }
     196             : 
     197             :    info_ptr->free_me |= PNG_FREE_HIST;
     198             : 
     199             :    for (i = 0; i < info_ptr->num_palette; i++)
     200             :       info_ptr->hist[i] = hist[i];
     201             : 
     202             :    info_ptr->valid |= PNG_INFO_hIST;
     203             : }
     204             : #endif
     205             : 
     206             : void PNGAPI
     207          31 : png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr,
     208             :     png_uint_32 width, png_uint_32 height, int bit_depth,
     209             :     int color_type, int interlace_type, int compression_type,
     210             :     int filter_type)
     211             : {
     212             :    png_debug1(1, "in %s storage function", "IHDR");
     213             : 
     214          31 :    if (png_ptr == NULL || info_ptr == NULL)
     215           0 :       return;
     216             : 
     217          31 :    info_ptr->width = width;
     218          31 :    info_ptr->height = height;
     219          31 :    info_ptr->bit_depth = (png_byte)bit_depth;
     220          31 :    info_ptr->color_type = (png_byte)color_type;
     221          31 :    info_ptr->compression_type = (png_byte)compression_type;
     222          31 :    info_ptr->filter_type = (png_byte)filter_type;
     223          31 :    info_ptr->interlace_type = (png_byte)interlace_type;
     224             : 
     225         155 :    png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height,
     226          93 :        info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type,
     227          62 :        info_ptr->compression_type, info_ptr->filter_type);
     228             : 
     229          31 :    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
     230           2 :       info_ptr->channels = 1;
     231             : 
     232          29 :    else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
     233          25 :       info_ptr->channels = 3;
     234             : 
     235             :    else
     236           4 :       info_ptr->channels = 1;
     237             : 
     238          31 :    if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
     239          29 :       info_ptr->channels++;
     240             : 
     241          31 :    info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth);
     242             : 
     243          31 :    info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width);
     244             : 
     245             : #ifdef PNG_APNG_SUPPORTED
     246             :    /* for non-animated png. this may be overwritten from an acTL chunk later */
     247          31 :    info_ptr->num_frames = 1;
     248             : #endif
     249             : }
     250             : 
     251             : #ifdef PNG_oFFs_SUPPORTED
     252             : void PNGAPI
     253             : png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr,
     254             :     png_int_32 offset_x, png_int_32 offset_y, int unit_type)
     255             : {
     256             :    png_debug1(1, "in %s storage function", "oFFs");
     257             : 
     258             :    if (png_ptr == NULL || info_ptr == NULL)
     259             :       return;
     260             : 
     261             :    info_ptr->x_offset = offset_x;
     262             :    info_ptr->y_offset = offset_y;
     263             :    info_ptr->offset_unit_type = (png_byte)unit_type;
     264             :    info_ptr->valid |= PNG_INFO_oFFs;
     265             : }
     266             : #endif
     267             : 
     268             : #ifdef PNG_pCAL_SUPPORTED
     269             : void PNGAPI
     270             : png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,
     271             :     png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type,
     272             :     int nparams, png_const_charp units, png_charpp params)
     273             : {
     274             :    png_size_t length;
     275             :    int i;
     276             : 
     277             :    png_debug1(1, "in %s storage function", "pCAL");
     278             : 
     279             :    if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL
     280             :        || (nparams > 0 && params == NULL))
     281             :       return;
     282             : 
     283             :    length = strlen(purpose) + 1;
     284             :    png_debug1(3, "allocating purpose for info (%lu bytes)",
     285             :        (unsigned long)length);
     286             : 
     287             :    /* TODO: validate format of calibration name and unit name */
     288             : 
     289             :    /* Check that the type matches the specification. */
     290             :    if (type < 0 || type > 3)
     291             :    {
     292             :       png_chunk_report(png_ptr, "Invalid pCAL equation type",
     293             :             PNG_CHUNK_WRITE_ERROR);
     294             :       return;
     295             :    }
     296             : 
     297             :    if (nparams < 0 || nparams > 255)
     298             :    {
     299             :       png_chunk_report(png_ptr, "Invalid pCAL parameter count",
     300             :             PNG_CHUNK_WRITE_ERROR);
     301             :       return;
     302             :    }
     303             : 
     304             :    /* Validate params[nparams] */
     305             :    for (i=0; i<nparams; ++i)
     306             :    {
     307             :       if (params[i] == NULL ||
     308             :           !png_check_fp_string(params[i], strlen(params[i])))
     309             :       {
     310             :          png_chunk_report(png_ptr, "Invalid format for pCAL parameter",
     311             :                PNG_CHUNK_WRITE_ERROR);
     312             :          return;
     313             :       }
     314             :    }
     315             : 
     316             :    info_ptr->pcal_purpose = png_voidcast(png_charp,
     317             :        png_malloc_warn(png_ptr, length));
     318             : 
     319             :    if (info_ptr->pcal_purpose == NULL)
     320             :    {
     321             :       png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose",
     322             :             PNG_CHUNK_WRITE_ERROR);
     323             :       return;
     324             :    }
     325             : 
     326             :    memcpy(info_ptr->pcal_purpose, purpose, length);
     327             : 
     328             :    png_debug(3, "storing X0, X1, type, and nparams in info");
     329             :    info_ptr->pcal_X0 = X0;
     330             :    info_ptr->pcal_X1 = X1;
     331             :    info_ptr->pcal_type = (png_byte)type;
     332             :    info_ptr->pcal_nparams = (png_byte)nparams;
     333             : 
     334             :    length = strlen(units) + 1;
     335             :    png_debug1(3, "allocating units for info (%lu bytes)",
     336             :        (unsigned long)length);
     337             : 
     338             :    info_ptr->pcal_units = png_voidcast(png_charp,
     339             :        png_malloc_warn(png_ptr, length));
     340             : 
     341             :    if (info_ptr->pcal_units == NULL)
     342             :    {
     343             :       png_warning(png_ptr, "Insufficient memory for pCAL units");
     344             : 
     345             :       return;
     346             :    }
     347             : 
     348             :    memcpy(info_ptr->pcal_units, units, length);
     349             : 
     350             :    info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
     351             :        (png_size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp)))));
     352             : 
     353             :    if (info_ptr->pcal_params == NULL)
     354             :    {
     355             :       png_warning(png_ptr, "Insufficient memory for pCAL params");
     356             : 
     357             :       return;
     358             :    }
     359             : 
     360             :    memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) *
     361             :        (sizeof (png_charp)));
     362             : 
     363             :    for (i = 0; i < nparams; i++)
     364             :    {
     365             :       length = strlen(params[i]) + 1;
     366             :       png_debug2(3, "allocating parameter %d for info (%lu bytes)", i,
     367             :           (unsigned long)length);
     368             : 
     369             :       info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length);
     370             : 
     371             :       if (info_ptr->pcal_params[i] == NULL)
     372             :       {
     373             :          png_warning(png_ptr, "Insufficient memory for pCAL parameter");
     374             : 
     375             :          return;
     376             :       }
     377             : 
     378             :       memcpy(info_ptr->pcal_params[i], params[i], length);
     379             :    }
     380             : 
     381             :    info_ptr->valid |= PNG_INFO_pCAL;
     382             :    info_ptr->free_me |= PNG_FREE_PCAL;
     383             : }
     384             : #endif
     385             : 
     386             : #ifdef PNG_sCAL_SUPPORTED
     387             : void PNGAPI
     388             : png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr,
     389             :     int unit, png_const_charp swidth, png_const_charp sheight)
     390             : {
     391             :    png_size_t lengthw = 0, lengthh = 0;
     392             : 
     393             :    png_debug1(1, "in %s storage function", "sCAL");
     394             : 
     395             :    if (png_ptr == NULL || info_ptr == NULL)
     396             :       return;
     397             : 
     398             :    /* Double check the unit (should never get here with an invalid
     399             :     * unit unless this is an API call.)
     400             :     */
     401             :    if (unit != 1 && unit != 2)
     402             :       png_error(png_ptr, "Invalid sCAL unit");
     403             : 
     404             :    if (swidth == NULL || (lengthw = strlen(swidth)) == 0 ||
     405             :        swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw))
     406             :       png_error(png_ptr, "Invalid sCAL width");
     407             : 
     408             :    if (sheight == NULL || (lengthh = strlen(sheight)) == 0 ||
     409             :        sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh))
     410             :       png_error(png_ptr, "Invalid sCAL height");
     411             : 
     412             :    info_ptr->scal_unit = (png_byte)unit;
     413             : 
     414             :    ++lengthw;
     415             : 
     416             :    png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw);
     417             : 
     418             :    info_ptr->scal_s_width = png_voidcast(png_charp,
     419             :        png_malloc_warn(png_ptr, lengthw));
     420             : 
     421             :    if (info_ptr->scal_s_width == NULL)
     422             :    {
     423             :       png_warning(png_ptr, "Memory allocation failed while processing sCAL");
     424             : 
     425             :       return;
     426             :    }
     427             : 
     428             :    memcpy(info_ptr->scal_s_width, swidth, lengthw);
     429             : 
     430             :    ++lengthh;
     431             : 
     432             :    png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh);
     433             : 
     434             :    info_ptr->scal_s_height = png_voidcast(png_charp,
     435             :        png_malloc_warn(png_ptr, lengthh));
     436             : 
     437             :    if (info_ptr->scal_s_height == NULL)
     438             :    {
     439             :       png_free (png_ptr, info_ptr->scal_s_width);
     440             :       info_ptr->scal_s_width = NULL;
     441             : 
     442             :       png_warning(png_ptr, "Memory allocation failed while processing sCAL");
     443             : 
     444             :       return;
     445             :    }
     446             : 
     447             :    memcpy(info_ptr->scal_s_height, sheight, lengthh);
     448             : 
     449             :    info_ptr->valid |= PNG_INFO_sCAL;
     450             :    info_ptr->free_me |= PNG_FREE_SCAL;
     451             : }
     452             : 
     453             : #  ifdef PNG_FLOATING_POINT_SUPPORTED
     454             : void PNGAPI
     455             : png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
     456             :     double width, double height)
     457             : {
     458             :    png_debug1(1, "in %s storage function", "sCAL");
     459             : 
     460             :    /* Check the arguments. */
     461             :    if (width <= 0)
     462             :       png_warning(png_ptr, "Invalid sCAL width ignored");
     463             : 
     464             :    else if (height <= 0)
     465             :       png_warning(png_ptr, "Invalid sCAL height ignored");
     466             : 
     467             :    else
     468             :    {
     469             :       /* Convert 'width' and 'height' to ASCII. */
     470             :       char swidth[PNG_sCAL_MAX_DIGITS+1];
     471             :       char sheight[PNG_sCAL_MAX_DIGITS+1];
     472             : 
     473             :       png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width,
     474             :           PNG_sCAL_PRECISION);
     475             :       png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height,
     476             :           PNG_sCAL_PRECISION);
     477             : 
     478             :       png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
     479             :    }
     480             : }
     481             : #  endif
     482             : 
     483             : #  ifdef PNG_FIXED_POINT_SUPPORTED
     484             : void PNGAPI
     485             : png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit,
     486             :     png_fixed_point width, png_fixed_point height)
     487             : {
     488             :    png_debug1(1, "in %s storage function", "sCAL");
     489             : 
     490             :    /* Check the arguments. */
     491             :    if (width <= 0)
     492             :       png_warning(png_ptr, "Invalid sCAL width ignored");
     493             : 
     494             :    else if (height <= 0)
     495             :       png_warning(png_ptr, "Invalid sCAL height ignored");
     496             : 
     497             :    else
     498             :    {
     499             :       /* Convert 'width' and 'height' to ASCII. */
     500             :       char swidth[PNG_sCAL_MAX_DIGITS+1];
     501             :       char sheight[PNG_sCAL_MAX_DIGITS+1];
     502             : 
     503             :       png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width);
     504             :       png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height);
     505             : 
     506             :       png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight);
     507             :    }
     508             : }
     509             : #  endif
     510             : #endif
     511             : 
     512             : #ifdef PNG_pHYs_SUPPORTED
     513             : void PNGAPI
     514             : png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr,
     515             :     png_uint_32 res_x, png_uint_32 res_y, int unit_type)
     516             : {
     517             :    png_debug1(1, "in %s storage function", "pHYs");
     518             : 
     519             :    if (png_ptr == NULL || info_ptr == NULL)
     520             :       return;
     521             : 
     522             :    info_ptr->x_pixels_per_unit = res_x;
     523             :    info_ptr->y_pixels_per_unit = res_y;
     524             :    info_ptr->phys_unit_type = (png_byte)unit_type;
     525             :    info_ptr->valid |= PNG_INFO_pHYs;
     526             : }
     527             : #endif
     528             : 
     529             : void PNGAPI
     530           2 : png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr,
     531             :     png_const_colorp palette, int num_palette)
     532             : {
     533             : 
     534             :    png_uint_32 max_palette_length;
     535             : 
     536             :    png_debug1(1, "in %s storage function", "PLTE");
     537             : 
     538           2 :    if (png_ptr == NULL || info_ptr == NULL)
     539           0 :       return;
     540             : 
     541           4 :    max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
     542           2 :       (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
     543             : 
     544           2 :    if (num_palette < 0 || num_palette > (int) max_palette_length)
     545             :    {
     546           0 :       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
     547           0 :          png_error(png_ptr, "Invalid palette length");
     548             : 
     549             :       else
     550             :       {
     551           0 :          png_warning(png_ptr, "Invalid palette length");
     552             : 
     553           0 :          return;
     554             :       }
     555             :    }
     556             : 
     557           2 :    if ((num_palette > 0 && palette == NULL) ||
     558             :       (num_palette == 0
     559             : #        ifdef PNG_MNG_FEATURES_SUPPORTED
     560             :             && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
     561             : #        endif
     562             :       ))
     563             :    {
     564           0 :       png_error(png_ptr, "Invalid palette");
     565             :    }
     566             : 
     567             :    /* It may not actually be necessary to set png_ptr->palette here;
     568             :     * we do it for backward compatibility with the way the png_handle_tRNS
     569             :     * function used to do the allocation.
     570             :     *
     571             :     * 1.6.0: the above statement appears to be incorrect; something has to set
     572             :     * the palette inside png_struct on read.
     573             :     */
     574           2 :    png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0);
     575             : 
     576             :    /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead
     577             :     * of num_palette entries, in case of an invalid PNG file or incorrect
     578             :     * call to png_set_PLTE() with too-large sample values.
     579             :     */
     580           2 :    png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
     581             :        PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
     582             : 
     583           2 :    if (num_palette > 0)
     584           2 :       memcpy(png_ptr->palette, palette, (unsigned int)num_palette *
     585             :           (sizeof (png_color)));
     586           2 :    info_ptr->palette = png_ptr->palette;
     587           2 :    info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
     588             : 
     589           2 :    info_ptr->free_me |= PNG_FREE_PLTE;
     590             : 
     591           2 :    info_ptr->valid |= PNG_INFO_PLTE;
     592             : }
     593             : 
     594             : #ifdef PNG_sBIT_SUPPORTED
     595             : void PNGAPI
     596             : png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,
     597             :     png_const_color_8p sig_bit)
     598             : {
     599             :    png_debug1(1, "in %s storage function", "sBIT");
     600             : 
     601             :    if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL)
     602             :       return;
     603             : 
     604             :    info_ptr->sig_bit = *sig_bit;
     605             :    info_ptr->valid |= PNG_INFO_sBIT;
     606             : }
     607             : #endif
     608             : 
     609             : #ifdef PNG_sRGB_SUPPORTED
     610             : void PNGAPI
     611           0 : png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent)
     612             : {
     613             :    png_debug1(1, "in %s storage function", "sRGB");
     614             : 
     615           0 :    if (png_ptr == NULL || info_ptr == NULL)
     616           0 :       return;
     617             : 
     618           0 :    (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent);
     619           0 :    png_colorspace_sync_info(png_ptr, info_ptr);
     620             : }
     621             : 
     622             : void PNGAPI
     623           0 : png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr,
     624             :     int srgb_intent)
     625             : {
     626             :    png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM");
     627             : 
     628           0 :    if (png_ptr == NULL || info_ptr == NULL)
     629           0 :       return;
     630             : 
     631           0 :    if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace,
     632             :        srgb_intent) != 0)
     633             :    {
     634             :       /* This causes the gAMA and cHRM to be written too */
     635           0 :       info_ptr->colorspace.flags |=
     636             :          PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
     637             :    }
     638             : 
     639           0 :    png_colorspace_sync_info(png_ptr, info_ptr);
     640             : }
     641             : #endif /* sRGB */
     642             : 
     643             : 
     644             : #ifdef PNG_iCCP_SUPPORTED
     645             : void PNGAPI
     646           0 : png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,
     647             :     png_const_charp name, int compression_type,
     648             :     png_const_bytep profile, png_uint_32 proflen)
     649             : {
     650             :    png_charp new_iccp_name;
     651             :    png_bytep new_iccp_profile;
     652             :    png_size_t length;
     653             : 
     654             :    png_debug1(1, "in %s storage function", "iCCP");
     655             : 
     656           0 :    if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL)
     657           0 :       return;
     658             : 
     659           0 :    if (compression_type != PNG_COMPRESSION_TYPE_BASE)
     660           0 :       png_app_error(png_ptr, "Invalid iCCP compression method");
     661             : 
     662             :    /* Set the colorspace first because this validates the profile; do not
     663             :     * override previously set app cHRM or gAMA here (because likely as not the
     664             :     * application knows better than libpng what the correct values are.)  Pass
     665             :     * the info_ptr color_type field to png_colorspace_set_ICC because in the
     666             :     * write case it has not yet been stored in png_ptr.
     667             :     */
     668             :    {
     669           0 :       int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name,
     670           0 :           proflen, profile, info_ptr->color_type);
     671             : 
     672           0 :       png_colorspace_sync_info(png_ptr, info_ptr);
     673             : 
     674             :       /* Don't do any of the copying if the profile was bad, or inconsistent. */
     675           0 :       if (result == 0)
     676           0 :          return;
     677             : 
     678             :       /* But do write the gAMA and cHRM chunks from the profile. */
     679           0 :       info_ptr->colorspace.flags |=
     680             :          PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM;
     681             :    }
     682             : 
     683           0 :    length = strlen(name)+1;
     684           0 :    new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length));
     685             : 
     686           0 :    if (new_iccp_name == NULL)
     687             :    {
     688           0 :       png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk");
     689             : 
     690           0 :       return;
     691             :    }
     692             : 
     693           0 :    memcpy(new_iccp_name, name, length);
     694           0 :    new_iccp_profile = png_voidcast(png_bytep,
     695             :        png_malloc_warn(png_ptr, proflen));
     696             : 
     697           0 :    if (new_iccp_profile == NULL)
     698             :    {
     699           0 :       png_free(png_ptr, new_iccp_name);
     700           0 :       png_benign_error(png_ptr,
     701             :           "Insufficient memory to process iCCP profile");
     702             : 
     703           0 :       return;
     704             :    }
     705             : 
     706           0 :    memcpy(new_iccp_profile, profile, proflen);
     707             : 
     708           0 :    png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0);
     709             : 
     710           0 :    info_ptr->iccp_proflen = proflen;
     711           0 :    info_ptr->iccp_name = new_iccp_name;
     712           0 :    info_ptr->iccp_profile = new_iccp_profile;
     713           0 :    info_ptr->free_me |= PNG_FREE_ICCP;
     714           0 :    info_ptr->valid |= PNG_INFO_iCCP;
     715             : }
     716             : #endif
     717             : 
     718             : #ifdef PNG_TEXT_SUPPORTED
     719             : void PNGAPI
     720             : png_set_text(png_const_structrp png_ptr, png_inforp info_ptr,
     721             :     png_const_textp text_ptr, int num_text)
     722             : {
     723             :    int ret;
     724             :    ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text);
     725             : 
     726             :    if (ret != 0)
     727             :       png_error(png_ptr, "Insufficient memory to store text");
     728             : }
     729             : 
     730             : int /* PRIVATE */
     731             : png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr,
     732             :     png_const_textp text_ptr, int num_text)
     733             : {
     734             :    int i;
     735             : 
     736             :    png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U :
     737             :       (unsigned long)png_ptr->chunk_name);
     738             : 
     739             :    if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
     740             :       return(0);
     741             : 
     742             :    /* Make sure we have enough space in the "text" array in info_struct
     743             :     * to hold all of the incoming text_ptr objects.  This compare can't overflow
     744             :     * because max_text >= num_text (anyway, subtract of two positive integers
     745             :     * can't overflow in any case.)
     746             :     */
     747             :    if (num_text > info_ptr->max_text - info_ptr->num_text)
     748             :    {
     749             :       int old_num_text = info_ptr->num_text;
     750             :       int max_text;
     751             :       png_textp new_text = NULL;
     752             : 
     753             :       /* Calculate an appropriate max_text, checking for overflow. */
     754             :       max_text = old_num_text;
     755             :       if (num_text <= INT_MAX - max_text)
     756             :       {
     757             :          max_text += num_text;
     758             : 
     759             :          /* Round up to a multiple of 8 */
     760             :          if (max_text < INT_MAX-8)
     761             :             max_text = (max_text + 8) & ~0x7;
     762             : 
     763             :          else
     764             :             max_text = INT_MAX;
     765             : 
     766             :          /* Now allocate a new array and copy the old members in; this does all
     767             :           * the overflow checks.
     768             :           */
     769             :          new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
     770             :              info_ptr->text, old_num_text, max_text-old_num_text,
     771             :              sizeof *new_text));
     772             :       }
     773             : 
     774             :       if (new_text == NULL)
     775             :       {
     776             :          png_chunk_report(png_ptr, "too many text chunks",
     777             :              PNG_CHUNK_WRITE_ERROR);
     778             : 
     779             :          return 1;
     780             :       }
     781             : 
     782             :       png_free(png_ptr, info_ptr->text);
     783             : 
     784             :       info_ptr->text = new_text;
     785             :       info_ptr->free_me |= PNG_FREE_TEXT;
     786             :       info_ptr->max_text = max_text;
     787             :       /* num_text is adjusted below as the entries are copied in */
     788             : 
     789             :       png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
     790             :    }
     791             : 
     792             :    for (i = 0; i < num_text; i++)
     793             :    {
     794             :       size_t text_length, key_len;
     795             :       size_t lang_len, lang_key_len;
     796             :       png_textp textp = &(info_ptr->text[info_ptr->num_text]);
     797             : 
     798             :       if (text_ptr[i].key == NULL)
     799             :           continue;
     800             : 
     801             :       if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
     802             :           text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
     803             :       {
     804             :          png_chunk_report(png_ptr, "text compression mode is out of range",
     805             :              PNG_CHUNK_WRITE_ERROR);
     806             :          continue;
     807             :       }
     808             : 
     809             :       key_len = strlen(text_ptr[i].key);
     810             : 
     811             :       if (text_ptr[i].compression <= 0)
     812             :       {
     813             :          lang_len = 0;
     814             :          lang_key_len = 0;
     815             :       }
     816             : 
     817             :       else
     818             : #  ifdef PNG_iTXt_SUPPORTED
     819             :       {
     820             :          /* Set iTXt data */
     821             : 
     822             :          if (text_ptr[i].lang != NULL)
     823             :             lang_len = strlen(text_ptr[i].lang);
     824             : 
     825             :          else
     826             :             lang_len = 0;
     827             : 
     828             :          if (text_ptr[i].lang_key != NULL)
     829             :             lang_key_len = strlen(text_ptr[i].lang_key);
     830             : 
     831             :          else
     832             :             lang_key_len = 0;
     833             :       }
     834             : #  else /* iTXt */
     835             :       {
     836             :          png_chunk_report(png_ptr, "iTXt chunk not supported",
     837             :              PNG_CHUNK_WRITE_ERROR);
     838             :          continue;
     839             :       }
     840             : #  endif
     841             : 
     842             :       if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
     843             :       {
     844             :          text_length = 0;
     845             : #  ifdef PNG_iTXt_SUPPORTED
     846             :          if (text_ptr[i].compression > 0)
     847             :             textp->compression = PNG_ITXT_COMPRESSION_NONE;
     848             : 
     849             :          else
     850             : #  endif
     851             :             textp->compression = PNG_TEXT_COMPRESSION_NONE;
     852             :       }
     853             : 
     854             :       else
     855             :       {
     856             :          text_length = strlen(text_ptr[i].text);
     857             :          textp->compression = text_ptr[i].compression;
     858             :       }
     859             : 
     860             :       textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
     861             :           key_len + text_length + lang_len + lang_key_len + 4));
     862             : 
     863             :       if (textp->key == NULL)
     864             :       {
     865             :          png_chunk_report(png_ptr, "text chunk: out of memory",
     866             :              PNG_CHUNK_WRITE_ERROR);
     867             : 
     868             :          return 1;
     869             :       }
     870             : 
     871             :       png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
     872             :           (unsigned long)(png_uint_32)
     873             :           (key_len + lang_len + lang_key_len + text_length + 4),
     874             :           textp->key);
     875             : 
     876             :       memcpy(textp->key, text_ptr[i].key, key_len);
     877             :       *(textp->key + key_len) = '\0';
     878             : 
     879             :       if (text_ptr[i].compression > 0)
     880             :       {
     881             :          textp->lang = textp->key + key_len + 1;
     882             :          memcpy(textp->lang, text_ptr[i].lang, lang_len);
     883             :          *(textp->lang + lang_len) = '\0';
     884             :          textp->lang_key = textp->lang + lang_len + 1;
     885             :          memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
     886             :          *(textp->lang_key + lang_key_len) = '\0';
     887             :          textp->text = textp->lang_key + lang_key_len + 1;
     888             :       }
     889             : 
     890             :       else
     891             :       {
     892             :          textp->lang=NULL;
     893             :          textp->lang_key=NULL;
     894             :          textp->text = textp->key + key_len + 1;
     895             :       }
     896             : 
     897             :       if (text_length != 0)
     898             :          memcpy(textp->text, text_ptr[i].text, text_length);
     899             : 
     900             :       *(textp->text + text_length) = '\0';
     901             : 
     902             : #  ifdef PNG_iTXt_SUPPORTED
     903             :       if (textp->compression > 0)
     904             :       {
     905             :          textp->text_length = 0;
     906             :          textp->itxt_length = text_length;
     907             :       }
     908             : 
     909             :       else
     910             : #  endif
     911             :       {
     912             :          textp->text_length = text_length;
     913             :          textp->itxt_length = 0;
     914             :       }
     915             : 
     916             :       info_ptr->num_text++;
     917             :       png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
     918             :    }
     919             : 
     920             :    return(0);
     921             : }
     922             : #endif
     923             : 
     924             : #ifdef PNG_tIME_SUPPORTED
     925             : void PNGAPI
     926             : png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr,
     927             :     png_const_timep mod_time)
     928             : {
     929             :    png_debug1(1, "in %s storage function", "tIME");
     930             : 
     931             :    if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL ||
     932             :        (png_ptr->mode & PNG_WROTE_tIME) != 0)
     933             :       return;
     934             : 
     935             :    if (mod_time->month == 0   || mod_time->month > 12  ||
     936             :        mod_time->day   == 0   || mod_time->day   > 31  ||
     937             :        mod_time->hour  > 23   || mod_time->minute > 59 ||
     938             :        mod_time->second > 60)
     939             :    {
     940             :       png_warning(png_ptr, "Ignoring invalid time value");
     941             : 
     942             :       return;
     943             :    }
     944             : 
     945             :    info_ptr->mod_time = *mod_time;
     946             :    info_ptr->valid |= PNG_INFO_tIME;
     947             : }
     948             : #endif
     949             : 
     950             : #ifdef PNG_tRNS_SUPPORTED
     951             : void PNGAPI
     952           2 : png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr,
     953             :     png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
     954             : {
     955             :    png_debug1(1, "in %s storage function", "tRNS");
     956             : 
     957           2 :    if (png_ptr == NULL || info_ptr == NULL)
     958             : 
     959           0 :       return;
     960             : 
     961           2 :    if (trans_alpha != NULL)
     962             :    {
     963             :        /* It may not actually be necessary to set png_ptr->trans_alpha here;
     964             :         * we do it for backward compatibility with the way the png_handle_tRNS
     965             :         * function used to do the allocation.
     966             :         *
     967             :         * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively
     968             :         * relies on png_set_tRNS storing the information in png_struct
     969             :         * (otherwise it won't be there for the code in pngrtran.c).
     970             :         */
     971             : 
     972           2 :        png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
     973             : 
     974           2 :        if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH)
     975             :        {
     976             :          /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */
     977           2 :           info_ptr->trans_alpha = png_voidcast(png_bytep,
     978             :               png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH));
     979           2 :           memcpy(info_ptr->trans_alpha, trans_alpha, (png_size_t)num_trans);
     980             :        }
     981           2 :        png_ptr->trans_alpha = info_ptr->trans_alpha;
     982             :    }
     983             : 
     984           2 :    if (trans_color != NULL)
     985             :    {
     986             : #ifdef PNG_WARNINGS_SUPPORTED
     987           2 :       if (info_ptr->bit_depth < 16)
     988             :       {
     989           2 :          int sample_max = (1 << info_ptr->bit_depth) - 1;
     990             : 
     991           2 :          if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY &&
     992           2 :              trans_color->gray > sample_max) ||
     993           2 :              (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
     994           0 :              (trans_color->red > sample_max ||
     995           0 :              trans_color->green > sample_max ||
     996           0 :              trans_color->blue > sample_max)))
     997           0 :             png_warning(png_ptr,
     998             :                 "tRNS chunk has out-of-range samples for bit_depth");
     999             :       }
    1000             : #endif
    1001             : 
    1002           2 :       info_ptr->trans_color = *trans_color;
    1003             : 
    1004           2 :       if (num_trans == 0)
    1005           0 :          num_trans = 1;
    1006             :    }
    1007             : 
    1008           2 :    info_ptr->num_trans = (png_uint_16)num_trans;
    1009             : 
    1010           2 :    if (num_trans != 0)
    1011             :    {
    1012           2 :       info_ptr->valid |= PNG_INFO_tRNS;
    1013           2 :       info_ptr->free_me |= PNG_FREE_TRNS;
    1014             :    }
    1015             : }
    1016             : #endif
    1017             : 
    1018             : #ifdef PNG_sPLT_SUPPORTED
    1019             : void PNGAPI
    1020             : png_set_sPLT(png_const_structrp png_ptr,
    1021             :     png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
    1022             : /*
    1023             :  *  entries        - array of png_sPLT_t structures
    1024             :  *                   to be added to the list of palettes
    1025             :  *                   in the info structure.
    1026             :  *
    1027             :  *  nentries       - number of palette structures to be
    1028             :  *                   added.
    1029             :  */
    1030             : {
    1031             :    png_sPLT_tp np;
    1032             : 
    1033             :    if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL)
    1034             :       return;
    1035             : 
    1036             :    /* Use the internal realloc function, which checks for all the possible
    1037             :     * overflows.  Notice that the parameters are (int) and (size_t)
    1038             :     */
    1039             :    np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr,
    1040             :        info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries,
    1041             :        sizeof *np));
    1042             : 
    1043             :    if (np == NULL)
    1044             :    {
    1045             :       /* Out of memory or too many chunks */
    1046             :       png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR);
    1047             : 
    1048             :       return;
    1049             :    }
    1050             : 
    1051             :    png_free(png_ptr, info_ptr->splt_palettes);
    1052             :    info_ptr->splt_palettes = np;
    1053             :    info_ptr->free_me |= PNG_FREE_SPLT;
    1054             : 
    1055             :    np += info_ptr->splt_palettes_num;
    1056             : 
    1057             :    do
    1058             :    {
    1059             :       png_size_t length;
    1060             : 
    1061             :       /* Skip invalid input entries */
    1062             :       if (entries->name == NULL || entries->entries == NULL)
    1063             :       {
    1064             :          /* png_handle_sPLT doesn't do this, so this is an app error */
    1065             :          png_app_error(png_ptr, "png_set_sPLT: invalid sPLT");
    1066             :          /* Just skip the invalid entry */
    1067             :          continue;
    1068             :       }
    1069             : 
    1070             :       np->depth = entries->depth;
    1071             : 
    1072             :       /* In the event of out-of-memory just return - there's no point keeping
    1073             :        * on trying to add sPLT chunks.
    1074             :        */
    1075             :       length = strlen(entries->name) + 1;
    1076             :       np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length));
    1077             : 
    1078             :       if (np->name == NULL)
    1079             :          break;
    1080             : 
    1081             :       memcpy(np->name, entries->name, length);
    1082             : 
    1083             :       /* IMPORTANT: we have memory now that won't get freed if something else
    1084             :        * goes wrong; this code must free it.  png_malloc_array produces no
    1085             :        * warnings; use a png_chunk_report (below) if there is an error.
    1086             :        */
    1087             :       np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr,
    1088             :           entries->nentries, sizeof (png_sPLT_entry)));
    1089             : 
    1090             :       if (np->entries == NULL)
    1091             :       {
    1092             :          png_free(png_ptr, np->name);
    1093             :          np->name = NULL;
    1094             :          break;
    1095             :       }
    1096             : 
    1097             :       np->nentries = entries->nentries;
    1098             :       /* This multiply can't overflow because png_malloc_array has already
    1099             :        * checked it when doing the allocation.
    1100             :        */
    1101             :       memcpy(np->entries, entries->entries,
    1102             :           (unsigned int)entries->nentries * sizeof (png_sPLT_entry));
    1103             : 
    1104             :       /* Note that 'continue' skips the advance of the out pointer and out
    1105             :        * count, so an invalid entry is not added.
    1106             :        */
    1107             :       info_ptr->valid |= PNG_INFO_sPLT;
    1108             :       ++(info_ptr->splt_palettes_num);
    1109             :       ++np;
    1110             :    }
    1111             :    while (++entries, --nentries);
    1112             : 
    1113             :    if (nentries > 0)
    1114             :       png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR);
    1115             : }
    1116             : #endif /* sPLT */
    1117             : 
    1118             : #ifdef PNG_APNG_SUPPORTED
    1119             : png_uint_32 PNGAPI
    1120           4 : png_set_acTL(png_structp png_ptr, png_infop info_ptr,
    1121             :     png_uint_32 num_frames, png_uint_32 num_plays)
    1122             : {
    1123             :     png_debug1(1, "in %s storage function", "acTL");
    1124             : 
    1125           4 :     if (png_ptr == NULL || info_ptr == NULL)
    1126             :     {
    1127           0 :         png_warning(png_ptr,
    1128             :                     "Call to png_set_acTL() with NULL png_ptr "
    1129             :                     "or info_ptr ignored");
    1130           0 :         return (0);
    1131             :     }
    1132           4 :     if (num_frames == 0)
    1133             :     {
    1134           0 :         png_warning(png_ptr,
    1135             :                     "Ignoring attempt to set acTL with num_frames zero");
    1136           0 :         return (0);
    1137             :     }
    1138           4 :     if (num_frames > PNG_UINT_31_MAX)
    1139             :     {
    1140           0 :         png_warning(png_ptr,
    1141             :                     "Ignoring attempt to set acTL with num_frames > 2^31-1");
    1142           0 :         return (0);
    1143             :     }
    1144           4 :     if (num_plays > PNG_UINT_31_MAX)
    1145             :     {
    1146           0 :         png_warning(png_ptr,
    1147             :                     "Ignoring attempt to set acTL with num_plays > 2^31-1");
    1148           0 :         return (0);
    1149             :     }
    1150             : 
    1151           4 :     info_ptr->num_frames = num_frames;
    1152           4 :     info_ptr->num_plays = num_plays;
    1153             : 
    1154           4 :     info_ptr->valid |= PNG_INFO_acTL;
    1155             : 
    1156           4 :     return (1);
    1157             : }
    1158             : 
    1159             : /* delay_num and delay_den can hold any 16-bit values including zero */
    1160             : png_uint_32 PNGAPI
    1161          38 : png_set_next_frame_fcTL(png_structp png_ptr, png_infop info_ptr,
    1162             :     png_uint_32 width, png_uint_32 height,
    1163             :     png_uint_32 x_offset, png_uint_32 y_offset,
    1164             :     png_uint_16 delay_num, png_uint_16 delay_den,
    1165             :     png_byte dispose_op, png_byte blend_op)
    1166             : {
    1167             :     png_debug1(1, "in %s storage function", "fcTL");
    1168             : 
    1169          38 :     if (png_ptr == NULL || info_ptr == NULL)
    1170             :     {
    1171           0 :         png_warning(png_ptr,
    1172             :                     "Call to png_set_fcTL() with NULL png_ptr or info_ptr "
    1173             :                     "ignored");
    1174           0 :         return (0);
    1175             :     }
    1176             : 
    1177          38 :     png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
    1178             :                              delay_num, delay_den, dispose_op, blend_op);
    1179             : 
    1180          38 :     if (blend_op == PNG_BLEND_OP_OVER)
    1181             :     {
    1182           0 :         if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0 &&
    1183           0 :             png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) == 0)
    1184             :         {
    1185           0 :           png_warning(png_ptr, "PNG_BLEND_OP_OVER is meaningless "
    1186             :                                "and wasteful for opaque images, ignored");
    1187           0 :           blend_op = PNG_BLEND_OP_SOURCE;
    1188             :         }
    1189             :     }
    1190             : 
    1191          38 :     info_ptr->next_frame_width = width;
    1192          38 :     info_ptr->next_frame_height = height;
    1193          38 :     info_ptr->next_frame_x_offset = x_offset;
    1194          38 :     info_ptr->next_frame_y_offset = y_offset;
    1195          38 :     info_ptr->next_frame_delay_num = delay_num;
    1196          38 :     info_ptr->next_frame_delay_den = delay_den;
    1197          38 :     info_ptr->next_frame_dispose_op = dispose_op;
    1198          38 :     info_ptr->next_frame_blend_op = blend_op;
    1199             : 
    1200          38 :     info_ptr->valid |= PNG_INFO_fcTL;
    1201             : 
    1202          38 :     return (1);
    1203             : }
    1204             : 
    1205             : void /* PRIVATE */
    1206          38 : png_ensure_fcTL_is_valid(png_structp png_ptr,
    1207             :     png_uint_32 width, png_uint_32 height,
    1208             :     png_uint_32 x_offset, png_uint_32 y_offset,
    1209             :     png_uint_16 delay_num, png_uint_16 delay_den,
    1210             :     png_byte dispose_op, png_byte blend_op)
    1211             : {
    1212          38 :     if (width == 0 || width > PNG_UINT_31_MAX)
    1213           0 :         png_error(png_ptr, "invalid width in fcTL (0 or > 2^31-1)");
    1214          38 :     if (height == 0 || height > PNG_UINT_31_MAX)
    1215           0 :         png_error(png_ptr, "invalid height in fcTL (0 or > 2^31-1)");
    1216          38 :     if (x_offset > PNG_UINT_31_MAX)
    1217           0 :         png_error(png_ptr, "invalid x_offset in fcTL (> 2^31-1)");
    1218          38 :     if (y_offset > PNG_UINT_31_MAX)
    1219           0 :         png_error(png_ptr, "invalid y_offset in fcTL (> 2^31-1)");
    1220          76 :     if (width + x_offset > png_ptr->first_frame_width ||
    1221          38 :         height + y_offset > png_ptr->first_frame_height)
    1222           0 :         png_error(png_ptr, "dimensions of a frame are greater than "
    1223             :                            "the ones in IHDR");
    1224             : 
    1225          38 :     if (dispose_op != PNG_DISPOSE_OP_NONE &&
    1226           0 :         dispose_op != PNG_DISPOSE_OP_BACKGROUND &&
    1227             :         dispose_op != PNG_DISPOSE_OP_PREVIOUS)
    1228           0 :         png_error(png_ptr, "invalid dispose_op in fcTL");
    1229             : 
    1230          38 :     if (blend_op != PNG_BLEND_OP_SOURCE &&
    1231             :         blend_op != PNG_BLEND_OP_OVER)
    1232           0 :         png_error(png_ptr, "invalid blend_op in fcTL");
    1233             : 
    1234             :     PNG_UNUSED(delay_num)
    1235             :     PNG_UNUSED(delay_den)
    1236          38 : }
    1237             : 
    1238             : png_uint_32 PNGAPI
    1239           0 : png_set_first_frame_is_hidden(png_structp png_ptr, png_infop info_ptr,
    1240             :                               png_byte is_hidden)
    1241             : {
    1242             :     png_debug(1, "in png_first_frame_is_hidden()");
    1243             : 
    1244           0 :     if (png_ptr == NULL)
    1245           0 :         return 0;
    1246             : 
    1247           0 :     if (is_hidden != 0)
    1248           0 :         png_ptr->apng_flags |= PNG_FIRST_FRAME_HIDDEN;
    1249             :     else
    1250           0 :         png_ptr->apng_flags &= ~PNG_FIRST_FRAME_HIDDEN;
    1251             : 
    1252             :     PNG_UNUSED(info_ptr)
    1253             : 
    1254           0 :     return 1;
    1255             : }
    1256             : #endif /* APNG */
    1257             : 
    1258             : #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
    1259             : static png_byte
    1260             : check_location(png_const_structrp png_ptr, int location)
    1261             : {
    1262             :    location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT);
    1263             : 
    1264             :    /* New in 1.6.0; copy the location and check it.  This is an API
    1265             :     * change; previously the app had to use the
    1266             :     * png_set_unknown_chunk_location API below for each chunk.
    1267             :     */
    1268             :    if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
    1269             :    {
    1270             :       /* Write struct, so unknown chunks come from the app */
    1271             :       png_app_warning(png_ptr,
    1272             :           "png_set_unknown_chunks now expects a valid location");
    1273             :       /* Use the old behavior */
    1274             :       location = (png_byte)(png_ptr->mode &
    1275             :           (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT));
    1276             :    }
    1277             : 
    1278             :    /* This need not be an internal error - if the app calls
    1279             :     * png_set_unknown_chunks on a read pointer it must get the location right.
    1280             :     */
    1281             :    if (location == 0)
    1282             :       png_error(png_ptr, "invalid location in png_set_unknown_chunks");
    1283             : 
    1284             :    /* Now reduce the location to the top-most set bit by removing each least
    1285             :     * significant bit in turn.
    1286             :     */
    1287             :    while (location != (location & -location))
    1288             :       location &= ~(location & -location);
    1289             : 
    1290             :    /* The cast is safe because 'location' is a bit mask and only the low four
    1291             :     * bits are significant.
    1292             :     */
    1293             :    return (png_byte)location;
    1294             : }
    1295             : 
    1296             : void PNGAPI
    1297             : png_set_unknown_chunks(png_const_structrp png_ptr,
    1298             :     png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
    1299             : {
    1300             :    png_unknown_chunkp np;
    1301             : 
    1302             :    if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 ||
    1303             :        unknowns == NULL)
    1304             :       return;
    1305             : 
    1306             :    /* Check for the failure cases where support has been disabled at compile
    1307             :     * time.  This code is hardly ever compiled - it's here because
    1308             :     * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this
    1309             :     * code) but may be meaningless if the read or write handling of unknown
    1310             :     * chunks is not compiled in.
    1311             :     */
    1312             : #  if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
    1313             :       defined(PNG_READ_SUPPORTED)
    1314             :       if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
    1315             :       {
    1316             :          png_app_error(png_ptr, "no unknown chunk support on read");
    1317             : 
    1318             :          return;
    1319             :       }
    1320             : #  endif
    1321             : #  if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
    1322             :       defined(PNG_WRITE_SUPPORTED)
    1323             :       if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
    1324             :       {
    1325             :          png_app_error(png_ptr, "no unknown chunk support on write");
    1326             : 
    1327             :          return;
    1328             :       }
    1329             : #  endif
    1330             : 
    1331             :    /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that
    1332             :     * unknown critical chunks could be lost with just a warning resulting in
    1333             :     * undefined behavior.  Now png_chunk_report is used to provide behavior
    1334             :     * appropriate to read or write.
    1335             :     */
    1336             :    np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr,
    1337             :        info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns,
    1338             :        sizeof *np));
    1339             : 
    1340             :    if (np == NULL)
    1341             :    {
    1342             :       png_chunk_report(png_ptr, "too many unknown chunks",
    1343             :           PNG_CHUNK_WRITE_ERROR);
    1344             : 
    1345             :       return;
    1346             :    }
    1347             : 
    1348             :    png_free(png_ptr, info_ptr->unknown_chunks);
    1349             :    info_ptr->unknown_chunks = np; /* safe because it is initialized */
    1350             :    info_ptr->free_me |= PNG_FREE_UNKN;
    1351             : 
    1352             :    np += info_ptr->unknown_chunks_num;
    1353             : 
    1354             :    /* Increment unknown_chunks_num each time round the loop to protect the
    1355             :     * just-allocated chunk data.
    1356             :     */
    1357             :    for (; num_unknowns > 0; --num_unknowns, ++unknowns)
    1358             :    {
    1359             :       memcpy(np->name, unknowns->name, (sizeof np->name));
    1360             :       np->name[(sizeof np->name)-1] = '\0';
    1361             :       np->location = check_location(png_ptr, unknowns->location);
    1362             : 
    1363             :       if (unknowns->size == 0)
    1364             :       {
    1365             :          np->data = NULL;
    1366             :          np->size = 0;
    1367             :       }
    1368             : 
    1369             :       else
    1370             :       {
    1371             :          np->data = png_voidcast(png_bytep,
    1372             :              png_malloc_base(png_ptr, unknowns->size));
    1373             : 
    1374             :          if (np->data == NULL)
    1375             :          {
    1376             :             png_chunk_report(png_ptr, "unknown chunk: out of memory",
    1377             :                 PNG_CHUNK_WRITE_ERROR);
    1378             :             /* But just skip storing the unknown chunk */
    1379             :             continue;
    1380             :          }
    1381             : 
    1382             :          memcpy(np->data, unknowns->data, unknowns->size);
    1383             :          np->size = unknowns->size;
    1384             :       }
    1385             : 
    1386             :       /* These increments are skipped on out-of-memory for the data - the
    1387             :        * unknown chunk entry gets overwritten if the png_chunk_report returns.
    1388             :        * This is correct in the read case (the chunk is just dropped.)
    1389             :        */
    1390             :       ++np;
    1391             :       ++(info_ptr->unknown_chunks_num);
    1392             :    }
    1393             : }
    1394             : 
    1395             : void PNGAPI
    1396             : png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr,
    1397             :     int chunk, int location)
    1398             : {
    1399             :    /* This API is pretty pointless in 1.6.0 because the location can be set
    1400             :     * before the call to png_set_unknown_chunks.
    1401             :     *
    1402             :     * TODO: add a png_app_warning in 1.7
    1403             :     */
    1404             :    if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 &&
    1405             :       chunk < info_ptr->unknown_chunks_num)
    1406             :    {
    1407             :       if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0)
    1408             :       {
    1409             :          png_app_error(png_ptr, "invalid unknown chunk location");
    1410             :          /* Fake out the pre 1.6.0 behavior: */
    1411             :          if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */
    1412             :             location = PNG_AFTER_IDAT;
    1413             : 
    1414             :          else
    1415             :             location = PNG_HAVE_IHDR; /* also undocumented */
    1416             :       }
    1417             : 
    1418             :       info_ptr->unknown_chunks[chunk].location =
    1419             :          check_location(png_ptr, location);
    1420             :    }
    1421             : }
    1422             : #endif /* STORE_UNKNOWN_CHUNKS */
    1423             : 
    1424             : #ifdef PNG_MNG_FEATURES_SUPPORTED
    1425             : png_uint_32 PNGAPI
    1426             : png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features)
    1427             : {
    1428             :    png_debug(1, "in png_permit_mng_features");
    1429             : 
    1430             :    if (png_ptr == NULL)
    1431             :       return 0;
    1432             : 
    1433             :    png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES;
    1434             : 
    1435             :    return png_ptr->mng_features_permitted;
    1436             : }
    1437             : #endif
    1438             : 
    1439             : #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
    1440             : static unsigned int
    1441             : add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep)
    1442             : {
    1443             :    unsigned int i;
    1444             : 
    1445             :    /* Utility function: update the 'keep' state of a chunk if it is already in
    1446             :     * the list, otherwise add it to the list.
    1447             :     */
    1448             :    for (i=0; i<count; ++i, list += 5)
    1449             :    {
    1450             :       if (memcmp(list, add, 4) == 0)
    1451             :       {
    1452             :          list[4] = (png_byte)keep;
    1453             : 
    1454             :          return count;
    1455             :       }
    1456             :    }
    1457             : 
    1458             :    if (keep != PNG_HANDLE_CHUNK_AS_DEFAULT)
    1459             :    {
    1460             :       ++count;
    1461             :       memcpy(list, add, 4);
    1462             :       list[4] = (png_byte)keep;
    1463             :    }
    1464             : 
    1465             :    return count;
    1466             : }
    1467             : 
    1468             : void PNGAPI
    1469             : png_set_keep_unknown_chunks(png_structrp png_ptr, int keep,
    1470             :     png_const_bytep chunk_list, int num_chunks_in)
    1471             : {
    1472             :    png_bytep new_list;
    1473             :    unsigned int num_chunks, old_num_chunks;
    1474             : 
    1475             :    if (png_ptr == NULL)
    1476             :       return;
    1477             : 
    1478             :    if (keep < 0 || keep >= PNG_HANDLE_CHUNK_LAST)
    1479             :    {
    1480             :       png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep");
    1481             : 
    1482             :       return;
    1483             :    }
    1484             : 
    1485             :    if (num_chunks_in <= 0)
    1486             :    {
    1487             :       png_ptr->unknown_default = keep;
    1488             : 
    1489             :       /* '0' means just set the flags, so stop here */
    1490             :       if (num_chunks_in == 0)
    1491             :         return;
    1492             :    }
    1493             : 
    1494             :    if (num_chunks_in < 0)
    1495             :    {
    1496             :       /* Ignore all unknown chunks and all chunks recognized by
    1497             :        * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND
    1498             :        */
    1499             :       static PNG_CONST png_byte chunks_to_ignore[] = {
    1500             :          98,  75,  71,  68, '\0',  /* bKGD */
    1501             :          99,  72,  82,  77, '\0',  /* cHRM */
    1502             :         103,  65,  77,  65, '\0',  /* gAMA */
    1503             :         104,  73,  83,  84, '\0',  /* hIST */
    1504             :         105,  67,  67,  80, '\0',  /* iCCP */
    1505             :         105,  84,  88, 116, '\0',  /* iTXt */
    1506             :         111,  70,  70, 115, '\0',  /* oFFs */
    1507             :         112,  67,  65,  76, '\0',  /* pCAL */
    1508             :         112,  72,  89, 115, '\0',  /* pHYs */
    1509             :         115,  66,  73,  84, '\0',  /* sBIT */
    1510             :         115,  67,  65,  76, '\0',  /* sCAL */
    1511             :         115,  80,  76,  84, '\0',  /* sPLT */
    1512             :         115,  84,  69,  82, '\0',  /* sTER */
    1513             :         115,  82,  71,  66, '\0',  /* sRGB */
    1514             :         116,  69,  88, 116, '\0',  /* tEXt */
    1515             :         116,  73,  77,  69, '\0',  /* tIME */
    1516             :         122,  84,  88, 116, '\0'   /* zTXt */
    1517             :       };
    1518             : 
    1519             :       chunk_list = chunks_to_ignore;
    1520             :       num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U;
    1521             :    }
    1522             : 
    1523             :    else /* num_chunks_in > 0 */
    1524             :    {
    1525             :       if (chunk_list == NULL)
    1526             :       {
    1527             :          /* Prior to 1.6.0 this was silently ignored, now it is an app_error
    1528             :           * which can be switched off.
    1529             :           */
    1530             :          png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list");
    1531             : 
    1532             :          return;
    1533             :       }
    1534             : 
    1535             :       num_chunks = (unsigned int)num_chunks_in;
    1536             :    }
    1537             : 
    1538             :    old_num_chunks = png_ptr->num_chunk_list;
    1539             :    if (png_ptr->chunk_list == NULL)
    1540             :       old_num_chunks = 0;
    1541             : 
    1542             :    /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow.
    1543             :     */
    1544             :    if (num_chunks + old_num_chunks > UINT_MAX/5)
    1545             :    {
    1546             :       png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks");
    1547             : 
    1548             :       return;
    1549             :    }
    1550             : 
    1551             :    /* If these chunks are being reset to the default then no more memory is
    1552             :     * required because add_one_chunk above doesn't extend the list if the 'keep'
    1553             :     * parameter is the default.
    1554             :     */
    1555             :    if (keep != 0)
    1556             :    {
    1557             :       new_list = png_voidcast(png_bytep, png_malloc(png_ptr,
    1558             :           5 * (num_chunks + old_num_chunks)));
    1559             : 
    1560             :       if (old_num_chunks > 0)
    1561             :          memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks);
    1562             :    }
    1563             : 
    1564             :    else if (old_num_chunks > 0)
    1565             :       new_list = png_ptr->chunk_list;
    1566             : 
    1567             :    else
    1568             :       new_list = NULL;
    1569             : 
    1570             :    /* Add the new chunks together with each one's handling code.  If the chunk
    1571             :     * already exists the code is updated, otherwise the chunk is added to the
    1572             :     * end.  (In libpng 1.6.0 order no longer matters because this code enforces
    1573             :     * the earlier convention that the last setting is the one that is used.)
    1574             :     */
    1575             :    if (new_list != NULL)
    1576             :    {
    1577             :       png_const_bytep inlist;
    1578             :       png_bytep outlist;
    1579             :       unsigned int i;
    1580             : 
    1581             :       for (i=0; i<num_chunks; ++i)
    1582             :       {
    1583             :          old_num_chunks = add_one_chunk(new_list, old_num_chunks,
    1584             :              chunk_list+5*i, keep);
    1585             :       }
    1586             : 
    1587             :       /* Now remove any spurious 'default' entries. */
    1588             :       num_chunks = 0;
    1589             :       for (i=0, inlist=outlist=new_list; i<old_num_chunks; ++i, inlist += 5)
    1590             :       {
    1591             :          if (inlist[4])
    1592             :          {
    1593             :             if (outlist != inlist)
    1594             :                memcpy(outlist, inlist, 5);
    1595             :             outlist += 5;
    1596             :             ++num_chunks;
    1597             :          }
    1598             :       }
    1599             : 
    1600             :       /* This means the application has removed all the specialized handling. */
    1601             :       if (num_chunks == 0)
    1602             :       {
    1603             :          if (png_ptr->chunk_list != new_list)
    1604             :             png_free(png_ptr, new_list);
    1605             : 
    1606             :          new_list = NULL;
    1607             :       }
    1608             :    }
    1609             : 
    1610             :    else
    1611             :       num_chunks = 0;
    1612             : 
    1613             :    png_ptr->num_chunk_list = num_chunks;
    1614             : 
    1615             :    if (png_ptr->chunk_list != new_list)
    1616             :    {
    1617             :       if (png_ptr->chunk_list != NULL)
    1618             :          png_free(png_ptr, png_ptr->chunk_list);
    1619             : 
    1620             :       png_ptr->chunk_list = new_list;
    1621             :    }
    1622             : }
    1623             : #endif
    1624             : 
    1625             : #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
    1626             : void PNGAPI
    1627             : png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr,
    1628             :     png_user_chunk_ptr read_user_chunk_fn)
    1629             : {
    1630             :    png_debug(1, "in png_set_read_user_chunk_fn");
    1631             : 
    1632             :    if (png_ptr == NULL)
    1633             :       return;
    1634             : 
    1635             :    png_ptr->read_user_chunk_fn = read_user_chunk_fn;
    1636             :    png_ptr->user_chunk_ptr = user_chunk_ptr;
    1637             : }
    1638             : #endif
    1639             : 
    1640             : #ifdef PNG_INFO_IMAGE_SUPPORTED
    1641             : void PNGAPI
    1642             : png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr,
    1643             :     png_bytepp row_pointers)
    1644             : {
    1645             :    png_debug1(1, "in %s storage function", "rows");
    1646             : 
    1647             :    if (png_ptr == NULL || info_ptr == NULL)
    1648             :       return;
    1649             : 
    1650             :    if (info_ptr->row_pointers != NULL &&
    1651             :        (info_ptr->row_pointers != row_pointers))
    1652             :       png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
    1653             : 
    1654             :    info_ptr->row_pointers = row_pointers;
    1655             : 
    1656             :    if (row_pointers != NULL)
    1657             :       info_ptr->valid |= PNG_INFO_IDAT;
    1658             : }
    1659             : #endif
    1660             : 
    1661             : void PNGAPI
    1662           0 : png_set_compression_buffer_size(png_structrp png_ptr, png_size_t size)
    1663             : {
    1664           0 :    if (png_ptr == NULL)
    1665           0 :       return;
    1666             : 
    1667           0 :    if (size == 0 || size > PNG_UINT_31_MAX)
    1668           0 :       png_error(png_ptr, "invalid compression buffer size");
    1669             : 
    1670             : #  ifdef PNG_SEQUENTIAL_READ_SUPPORTED
    1671             :    if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
    1672             :    {
    1673             :       png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */
    1674             :       return;
    1675             :    }
    1676             : #  endif
    1677             : 
    1678             : #  ifdef PNG_WRITE_SUPPORTED
    1679           0 :    if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
    1680             :    {
    1681           0 :       if (png_ptr->zowner != 0)
    1682             :       {
    1683           0 :          png_warning(png_ptr,
    1684             :              "Compression buffer size cannot be changed because it is in use");
    1685             : 
    1686           0 :          return;
    1687             :       }
    1688             : 
    1689             : #ifndef __COVERITY__
    1690             :       /* Some compilers complain that this is always false.  However, it
    1691             :        * can be true when integer overflow happens.
    1692             :        */
    1693           0 :       if (size > ZLIB_IO_MAX)
    1694             :       {
    1695           0 :          png_warning(png_ptr,
    1696             :              "Compression buffer size limited to system maximum");
    1697           0 :          size = ZLIB_IO_MAX; /* must fit */
    1698             :       }
    1699             : #endif
    1700             : 
    1701           0 :       if (size < 6)
    1702             :       {
    1703             :          /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH
    1704             :           * if this is permitted.
    1705             :           */
    1706           0 :          png_warning(png_ptr,
    1707             :              "Compression buffer size cannot be reduced below 6");
    1708             : 
    1709           0 :          return;
    1710             :       }
    1711             : 
    1712           0 :       if (png_ptr->zbuffer_size != size)
    1713             :       {
    1714           0 :          png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
    1715           0 :          png_ptr->zbuffer_size = (uInt)size;
    1716             :       }
    1717             :    }
    1718             : #  endif
    1719             : }
    1720             : 
    1721             : void PNGAPI
    1722           0 : png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask)
    1723             : {
    1724           0 :    if (png_ptr != NULL && info_ptr != NULL)
    1725           0 :       info_ptr->valid &= (unsigned int)(~mask);
    1726           0 : }
    1727             : 
    1728             : 
    1729             : #ifdef PNG_SET_USER_LIMITS_SUPPORTED
    1730             : /* This function was added to libpng 1.2.6 */
    1731             : void PNGAPI
    1732          31 : png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max,
    1733             :     png_uint_32 user_height_max)
    1734             : {
    1735             :    /* Images with dimensions larger than these limits will be
    1736             :     * rejected by png_set_IHDR().  To accept any PNG datastream
    1737             :     * regardless of dimensions, set both limits to 0x7fffffff.
    1738             :     */
    1739          31 :    if (png_ptr == NULL)
    1740           0 :       return;
    1741             : 
    1742          31 :    png_ptr->user_width_max = user_width_max;
    1743          31 :    png_ptr->user_height_max = user_height_max;
    1744             : }
    1745             : 
    1746             : /* This function was added to libpng 1.4.0 */
    1747             : void PNGAPI
    1748           0 : png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max)
    1749             : {
    1750           0 :    if (png_ptr != NULL)
    1751           0 :       png_ptr->user_chunk_cache_max = user_chunk_cache_max;
    1752           0 : }
    1753             : 
    1754             : /* This function was added to libpng 1.4.1 */
    1755             : void PNGAPI
    1756          31 : png_set_chunk_malloc_max (png_structrp png_ptr,
    1757             :     png_alloc_size_t user_chunk_malloc_max)
    1758             : {
    1759          31 :    if (png_ptr != NULL)
    1760          31 :       png_ptr->user_chunk_malloc_max = user_chunk_malloc_max;
    1761          31 : }
    1762             : #endif /* ?SET_USER_LIMITS */
    1763             : 
    1764             : 
    1765             : #ifdef PNG_BENIGN_ERRORS_SUPPORTED
    1766             : void PNGAPI
    1767           0 : png_set_benign_errors(png_structrp png_ptr, int allowed)
    1768             : {
    1769             :    png_debug(1, "in png_set_benign_errors");
    1770             : 
    1771             :    /* If allowed is 1, png_benign_error() is treated as a warning.
    1772             :     *
    1773             :     * If allowed is 0, png_benign_error() is treated as an error (which
    1774             :     * is the default behavior if png_set_benign_errors() is not called).
    1775             :     */
    1776             : 
    1777           0 :    if (allowed != 0)
    1778           0 :       png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN |
    1779             :          PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN;
    1780             : 
    1781             :    else
    1782           0 :       png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN |
    1783             :          PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN);
    1784           0 : }
    1785             : #endif /* BENIGN_ERRORS */
    1786             : 
    1787             : #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
    1788             :    /* Whether to report invalid palette index; added at libng-1.5.10.
    1789             :     * It is possible for an indexed (color-type==3) PNG file to contain
    1790             :     * pixels with invalid (out-of-range) indexes if the PLTE chunk has
    1791             :     * fewer entries than the image's bit-depth would allow. We recover
    1792             :     * from this gracefully by filling any incomplete palette with zeros
    1793             :     * (opaque black).  By default, when this occurs libpng will issue
    1794             :     * a benign error.  This API can be used to override that behavior.
    1795             :     */
    1796             : void PNGAPI
    1797             : png_set_check_for_invalid_index(png_structrp png_ptr, int allowed)
    1798             : {
    1799             :    png_debug(1, "in png_set_check_for_invalid_index");
    1800             : 
    1801             :    if (allowed > 0)
    1802             :       png_ptr->num_palette_max = 0;
    1803             : 
    1804             :    else
    1805             :       png_ptr->num_palette_max = -1;
    1806             : }
    1807             : #endif
    1808             : 
    1809             : #if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \
    1810             :     defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED)
    1811             : /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
    1812             :  * and if invalid, correct the keyword rather than discarding the entire
    1813             :  * chunk.  The PNG 1.0 specification requires keywords 1-79 characters in
    1814             :  * length, forbids leading or trailing whitespace, multiple internal spaces,
    1815             :  * and the non-break space (0x80) from ISO 8859-1.  Returns keyword length.
    1816             :  *
    1817             :  * The 'new_key' buffer must be 80 characters in size (for the keyword plus a
    1818             :  * trailing '\0').  If this routine returns 0 then there was no keyword, or a
    1819             :  * valid one could not be generated, and the caller must png_error.
    1820             :  */
    1821             : png_uint_32 /* PRIVATE */
    1822           0 : png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
    1823             : {
    1824             : #ifdef PNG_WARNINGS_SUPPORTED
    1825           0 :    png_const_charp orig_key = key;
    1826             : #endif
    1827           0 :    png_uint_32 key_len = 0;
    1828           0 :    int bad_character = 0;
    1829           0 :    int space = 1;
    1830             : 
    1831             :    png_debug(1, "in png_check_keyword");
    1832             : 
    1833           0 :    if (key == NULL)
    1834             :    {
    1835           0 :       *new_key = 0;
    1836           0 :       return 0;
    1837             :    }
    1838             : 
    1839           0 :    while (*key && key_len < 79)
    1840             :    {
    1841           0 :       png_byte ch = (png_byte)*key++;
    1842             : 
    1843           0 :       if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/))
    1844           0 :          *new_key++ = ch, ++key_len, space = 0;
    1845             : 
    1846           0 :       else if (space == 0)
    1847             :       {
    1848             :          /* A space or an invalid character when one wasn't seen immediately
    1849             :           * before; output just a space.
    1850             :           */
    1851           0 :          *new_key++ = 32, ++key_len, space = 1;
    1852             : 
    1853             :          /* If the character was not a space then it is invalid. */
    1854           0 :          if (ch != 32)
    1855           0 :             bad_character = ch;
    1856             :       }
    1857             : 
    1858           0 :       else if (bad_character == 0)
    1859           0 :          bad_character = ch; /* just skip it, record the first error */
    1860             :    }
    1861             : 
    1862           0 :    if (key_len > 0 && space != 0) /* trailing space */
    1863             :    {
    1864           0 :       --key_len, --new_key;
    1865           0 :       if (bad_character == 0)
    1866           0 :          bad_character = 32;
    1867             :    }
    1868             : 
    1869             :    /* Terminate the keyword */
    1870           0 :    *new_key = 0;
    1871             : 
    1872           0 :    if (key_len == 0)
    1873           0 :       return 0;
    1874             : 
    1875             : #ifdef PNG_WARNINGS_SUPPORTED
    1876             :    /* Try to only output one warning per keyword: */
    1877           0 :    if (*key != 0) /* keyword too long */
    1878           0 :       png_warning(png_ptr, "keyword truncated");
    1879             : 
    1880           0 :    else if (bad_character != 0)
    1881             :    {
    1882             :       PNG_WARNING_PARAMETERS(p)
    1883             : 
    1884           0 :       png_warning_parameter(p, 1, orig_key);
    1885           0 :       png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character);
    1886             : 
    1887           0 :       png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'");
    1888             :    }
    1889             : #else /* !WARNINGS */
    1890             :    PNG_UNUSED(png_ptr)
    1891             : #endif /* !WARNINGS */
    1892             : 
    1893           0 :    return key_len;
    1894             : }
    1895             : #endif /* TEXT || pCAL || iCCP || sPLT */
    1896             : #endif /* READ || WRITE */

Generated by: LCOV version 1.13