Path: blob/master/src/java.desktop/share/native/libsplashscreen/libpng/pngrutil.c
41154 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* pngrutil.c - utilities to read a PNG file25*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file and, per its terms, should not be removed:30*31* Copyright (c) 2018 Cosmin Truta32* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson33* Copyright (c) 1996-1997 Andreas Dilger34* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.35*36* This code is released under the libpng license.37* For conditions of distribution and use, see the disclaimer38* and license in png.h39*40* This file contains routines that are only called from within41* libpng itself during the course of reading an image.42*/4344#include "pngpriv.h"4546#ifdef PNG_READ_SUPPORTED4748png_uint_32 PNGAPI49png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)50{51png_uint_32 uval = png_get_uint_32(buf);5253if (uval > PNG_UINT_31_MAX)54png_error(png_ptr, "PNG unsigned integer out of range");5556return (uval);57}5859#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)60/* The following is a variation on the above for use with the fixed61* point values used for gAMA and cHRM. Instead of png_error it62* issues a warning and returns (-1) - an invalid value because both63* gAMA and cHRM use *unsigned* integers for fixed point values.64*/65#define PNG_FIXED_ERROR (-1)6667static png_fixed_point /* PRIVATE */68png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)69{70png_uint_32 uval = png_get_uint_32(buf);7172if (uval <= PNG_UINT_31_MAX)73return (png_fixed_point)uval; /* known to be in range */7475/* The caller can turn off the warning by passing NULL. */76if (png_ptr != NULL)77png_warning(png_ptr, "PNG fixed point integer out of range");7879return PNG_FIXED_ERROR;80}81#endif8283#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED84/* NOTE: the read macros will obscure these definitions, so that if85* PNG_USE_READ_MACROS is set the library will not use them internally,86* but the APIs will still be available externally.87*88* The parentheses around "PNGAPI function_name" in the following three89* functions are necessary because they allow the macros to co-exist with90* these (unused but exported) functions.91*/9293/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */94png_uint_32 (PNGAPI95png_get_uint_32)(png_const_bytep buf)96{97png_uint_32 uval =98((png_uint_32)(*(buf )) << 24) +99((png_uint_32)(*(buf + 1)) << 16) +100((png_uint_32)(*(buf + 2)) << 8) +101((png_uint_32)(*(buf + 3)) ) ;102103return uval;104}105106/* Grab a signed 32-bit integer from a buffer in big-endian format. The107* data is stored in the PNG file in two's complement format and there108* is no guarantee that a 'png_int_32' is exactly 32 bits, therefore109* the following code does a two's complement to native conversion.110*/111png_int_32 (PNGAPI112png_get_int_32)(png_const_bytep buf)113{114png_uint_32 uval = png_get_uint_32(buf);115if ((uval & 0x80000000) == 0) /* non-negative */116return (png_int_32)uval;117118uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */119if ((uval & 0x80000000) == 0) /* no overflow */120return -(png_int_32)uval;121/* The following has to be safe; this function only gets called on PNG data122* and if we get here that data is invalid. 0 is the most safe value and123* if not then an attacker would surely just generate a PNG with 0 instead.124*/125return 0;126}127128/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */129png_uint_16 (PNGAPI130png_get_uint_16)(png_const_bytep buf)131{132/* ANSI-C requires an int value to accommodate at least 16 bits so this133* works and allows the compiler not to worry about possible narrowing134* on 32-bit systems. (Pre-ANSI systems did not make integers smaller135* than 16 bits either.)136*/137unsigned int val =138((unsigned int)(*buf) << 8) +139((unsigned int)(*(buf + 1)));140141return (png_uint_16)val;142}143144#endif /* READ_INT_FUNCTIONS */145146/* Read and check the PNG file signature */147void /* PRIVATE */148png_read_sig(png_structrp png_ptr, png_inforp info_ptr)149{150size_t num_checked, num_to_check;151152/* Exit if the user application does not expect a signature. */153if (png_ptr->sig_bytes >= 8)154return;155156num_checked = png_ptr->sig_bytes;157num_to_check = 8 - num_checked;158159#ifdef PNG_IO_STATE_SUPPORTED160png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;161#endif162163/* The signature must be serialized in a single I/O call. */164png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);165png_ptr->sig_bytes = 8;166167if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)168{169if (num_checked < 4 &&170png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))171png_error(png_ptr, "Not a PNG file");172else173png_error(png_ptr, "PNG file corrupted by ASCII conversion");174}175if (num_checked < 3)176png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;177}178179/* Read the chunk header (length + type name).180* Put the type name into png_ptr->chunk_name, and return the length.181*/182png_uint_32 /* PRIVATE */183png_read_chunk_header(png_structrp png_ptr)184{185png_byte buf[8];186png_uint_32 length;187188#ifdef PNG_IO_STATE_SUPPORTED189png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;190#endif191192/* Read the length and the chunk name.193* This must be performed in a single I/O call.194*/195png_read_data(png_ptr, buf, 8);196length = png_get_uint_31(png_ptr, buf);197198/* Put the chunk name into png_ptr->chunk_name. */199png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);200201png_debug2(0, "Reading %lx chunk, length = %lu",202(unsigned long)png_ptr->chunk_name, (unsigned long)length);203204/* Reset the crc and run it over the chunk name. */205png_reset_crc(png_ptr);206png_calculate_crc(png_ptr, buf + 4, 4);207208/* Check to see if chunk name is valid. */209png_check_chunk_name(png_ptr, png_ptr->chunk_name);210211/* Check for too-large chunk length */212png_check_chunk_length(png_ptr, length);213214#ifdef PNG_IO_STATE_SUPPORTED215png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;216#endif217218return length;219}220221/* Read data, and (optionally) run it through the CRC. */222void /* PRIVATE */223png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)224{225if (png_ptr == NULL)226return;227228png_read_data(png_ptr, buf, length);229png_calculate_crc(png_ptr, buf, length);230}231232/* Optionally skip data and then check the CRC. Depending on whether we233* are reading an ancillary or critical chunk, and how the program has set234* things up, we may calculate the CRC on the data and print a message.235* Returns '1' if there was a CRC error, '0' otherwise.236*/237int /* PRIVATE */238png_crc_finish(png_structrp png_ptr, png_uint_32 skip)239{240/* The size of the local buffer for inflate is a good guess as to a241* reasonable size to use for buffering reads from the application.242*/243while (skip > 0)244{245png_uint_32 len;246png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];247248len = (sizeof tmpbuf);249if (len > skip)250len = skip;251skip -= len;252253png_crc_read(png_ptr, tmpbuf, len);254}255256if (png_crc_error(png_ptr) != 0)257{258if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?259(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :260(png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)261{262png_chunk_warning(png_ptr, "CRC error");263}264265else266png_chunk_error(png_ptr, "CRC error");267268return (1);269}270271return (0);272}273274/* Compare the CRC stored in the PNG file with that calculated by libpng from275* the data it has read thus far.276*/277int /* PRIVATE */278png_crc_error(png_structrp png_ptr)279{280png_byte crc_bytes[4];281png_uint_32 crc;282int need_crc = 1;283284if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)285{286if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==287(PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))288need_crc = 0;289}290291else /* critical */292{293if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)294need_crc = 0;295}296297#ifdef PNG_IO_STATE_SUPPORTED298png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;299#endif300301/* The chunk CRC must be serialized in a single I/O call. */302png_read_data(png_ptr, crc_bytes, 4);303304if (need_crc != 0)305{306crc = png_get_uint_32(crc_bytes);307return ((int)(crc != png_ptr->crc));308}309310else311return (0);312}313314#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\315defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\316defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\317defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)318/* Manage the read buffer; this simply reallocates the buffer if it is not small319* enough (or if it is not allocated). The routine returns a pointer to the320* buffer; if an error occurs and 'warn' is set the routine returns NULL, else321* it will call png_error (via png_malloc) on failure. (warn == 2 means322* 'silent').323*/324static png_bytep325png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)326{327png_bytep buffer = png_ptr->read_buffer;328329if (buffer != NULL && new_size > png_ptr->read_buffer_size)330{331png_ptr->read_buffer = NULL;332png_ptr->read_buffer = NULL;333png_ptr->read_buffer_size = 0;334png_free(png_ptr, buffer);335buffer = NULL;336}337338if (buffer == NULL)339{340buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));341342if (buffer != NULL)343{344memset(buffer, 0, new_size); /* just in case */345png_ptr->read_buffer = buffer;346png_ptr->read_buffer_size = new_size;347}348349else if (warn < 2) /* else silent */350{351if (warn != 0)352png_chunk_warning(png_ptr, "insufficient memory to read chunk");353354else355png_chunk_error(png_ptr, "insufficient memory to read chunk");356}357}358359return buffer;360}361#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */362363/* png_inflate_claim: claim the zstream for some nefarious purpose that involves364* decompression. Returns Z_OK on success, else a zlib error code. It checks365* the owner but, in final release builds, just issues a warning if some other366* chunk apparently owns the stream. Prior to release it does a png_error.367*/368static int369png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)370{371if (png_ptr->zowner != 0)372{373char msg[64];374375PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);376/* So the message that results is "<chunk> using zstream"; this is an377* internal error, but is very useful for debugging. i18n requirements378* are minimal.379*/380(void)png_safecat(msg, (sizeof msg), 4, " using zstream");381#if PNG_RELEASE_BUILD382png_chunk_warning(png_ptr, msg);383png_ptr->zowner = 0;384#else385png_chunk_error(png_ptr, msg);386#endif387}388389/* Implementation note: unlike 'png_deflate_claim' this internal function390* does not take the size of the data as an argument. Some efficiency could391* be gained by using this when it is known *if* the zlib stream itself does392* not record the number; however, this is an illusion: the original writer393* of the PNG may have selected a lower window size, and we really must394* follow that because, for systems with with limited capabilities, we395* would otherwise reject the application's attempts to use a smaller window396* size (zlib doesn't have an interface to say "this or lower"!).397*398* inflateReset2 was added to zlib 1.2.4; before this the window could not be399* reset, therefore it is necessary to always allocate the maximum window400* size with earlier zlibs just in case later compressed chunks need it.401*/402{403int ret; /* zlib return code */404#if ZLIB_VERNUM >= 0x1240405int window_bits = 0;406407# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)408if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==409PNG_OPTION_ON)410{411window_bits = 15;412png_ptr->zstream_start = 0; /* fixed window size */413}414415else416{417png_ptr->zstream_start = 1;418}419# endif420421#endif /* ZLIB_VERNUM >= 0x1240 */422423/* Set this for safety, just in case the previous owner left pointers to424* memory allocations.425*/426png_ptr->zstream.next_in = NULL;427png_ptr->zstream.avail_in = 0;428png_ptr->zstream.next_out = NULL;429png_ptr->zstream.avail_out = 0;430431if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)432{433#if ZLIB_VERNUM >= 0x1240434ret = inflateReset2(&png_ptr->zstream, window_bits);435#else436ret = inflateReset(&png_ptr->zstream);437#endif438}439440else441{442#if ZLIB_VERNUM >= 0x1240443ret = inflateInit2(&png_ptr->zstream, window_bits);444#else445ret = inflateInit(&png_ptr->zstream);446#endif447448if (ret == Z_OK)449png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;450}451452#if ZLIB_VERNUM >= 0x1290 && \453defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32)454if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)455/* Turn off validation of the ADLER32 checksum in IDAT chunks */456ret = inflateValidate(&png_ptr->zstream, 0);457#endif458459if (ret == Z_OK)460png_ptr->zowner = owner;461462else463png_zstream_error(png_ptr, ret);464465return ret;466}467468#ifdef window_bits469# undef window_bits470#endif471}472473#if ZLIB_VERNUM >= 0x1240474/* Handle the start of the inflate stream if we called inflateInit2(strm,0);475* in this case some zlib versions skip validation of the CINFO field and, in476* certain circumstances, libpng may end up displaying an invalid image, in477* contrast to implementations that call zlib in the normal way (e.g. libpng478* 1.5).479*/480int /* PRIVATE */481png_zlib_inflate(png_structrp png_ptr, int flush)482{483if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)484{485if ((*png_ptr->zstream.next_in >> 4) > 7)486{487png_ptr->zstream.msg = "invalid window size (libpng)";488return Z_DATA_ERROR;489}490491png_ptr->zstream_start = 0;492}493494return inflate(&png_ptr->zstream, flush);495}496#endif /* Zlib >= 1.2.4 */497498#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED499#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)500/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to501* allow the caller to do multiple calls if required. If the 'finish' flag is502* set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must503* be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and504* Z_OK or Z_STREAM_END will be returned on success.505*506* The input and output sizes are updated to the actual amounts of data consumed507* or written, not the amount available (as in a z_stream). The data pointers508* are not changed, so the next input is (data+input_size) and the next509* available output is (output+output_size).510*/511static int512png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,513/* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,514/* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)515{516if (png_ptr->zowner == owner) /* Else not claimed */517{518int ret;519png_alloc_size_t avail_out = *output_size_ptr;520png_uint_32 avail_in = *input_size_ptr;521522/* zlib can't necessarily handle more than 65535 bytes at once (i.e. it523* can't even necessarily handle 65536 bytes) because the type uInt is524* "16 bits or more". Consequently it is necessary to chunk the input to525* zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the526* maximum value that can be stored in a uInt.) It is possible to set527* ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have528* a performance advantage, because it reduces the amount of data accessed529* at each step and that may give the OS more time to page it in.530*/531png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);532/* avail_in and avail_out are set below from 'size' */533png_ptr->zstream.avail_in = 0;534png_ptr->zstream.avail_out = 0;535536/* Read directly into the output if it is available (this is set to537* a local buffer below if output is NULL).538*/539if (output != NULL)540png_ptr->zstream.next_out = output;541542do543{544uInt avail;545Byte local_buffer[PNG_INFLATE_BUF_SIZE];546547/* zlib INPUT BUFFER */548/* The setting of 'avail_in' used to be outside the loop; by setting it549* inside it is possible to chunk the input to zlib and simply rely on550* zlib to advance the 'next_in' pointer. This allows arbitrary551* amounts of data to be passed through zlib at the unavoidable cost of552* requiring a window save (memcpy of up to 32768 output bytes)553* every ZLIB_IO_MAX input bytes.554*/555avail_in += png_ptr->zstream.avail_in; /* not consumed last time */556557avail = ZLIB_IO_MAX;558559if (avail_in < avail)560avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */561562avail_in -= avail;563png_ptr->zstream.avail_in = avail;564565/* zlib OUTPUT BUFFER */566avail_out += png_ptr->zstream.avail_out; /* not written last time */567568avail = ZLIB_IO_MAX; /* maximum zlib can process */569570if (output == NULL)571{572/* Reset the output buffer each time round if output is NULL and573* make available the full buffer, up to 'remaining_space'574*/575png_ptr->zstream.next_out = local_buffer;576if ((sizeof local_buffer) < avail)577avail = (sizeof local_buffer);578}579580if (avail_out < avail)581avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */582583png_ptr->zstream.avail_out = avail;584avail_out -= avail;585586/* zlib inflate call */587/* In fact 'avail_out' may be 0 at this point, that happens at the end588* of the read when the final LZ end code was not passed at the end of589* the previous chunk of input data. Tell zlib if we have reached the590* end of the output buffer.591*/592ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :593(finish ? Z_FINISH : Z_SYNC_FLUSH));594} while (ret == Z_OK);595596/* For safety kill the local buffer pointer now */597if (output == NULL)598png_ptr->zstream.next_out = NULL;599600/* Claw back the 'size' and 'remaining_space' byte counts. */601avail_in += png_ptr->zstream.avail_in;602avail_out += png_ptr->zstream.avail_out;603604/* Update the input and output sizes; the updated values are the amount605* consumed or written, effectively the inverse of what zlib uses.606*/607if (avail_out > 0)608*output_size_ptr -= avail_out;609610if (avail_in > 0)611*input_size_ptr -= avail_in;612613/* Ensure png_ptr->zstream.msg is set (even in the success case!) */614png_zstream_error(png_ptr, ret);615return ret;616}617618else619{620/* This is a bad internal error. The recovery assigns to the zstream msg621* pointer, which is not owned by the caller, but this is safe; it's only622* used on errors!623*/624png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");625return Z_STREAM_ERROR;626}627}628629/*630* Decompress trailing data in a chunk. The assumption is that read_buffer631* points at an allocated area holding the contents of a chunk with a632* trailing compressed part. What we get back is an allocated area633* holding the original prefix part and an uncompressed version of the634* trailing part (the malloc area passed in is freed).635*/636static int637png_decompress_chunk(png_structrp png_ptr,638png_uint_32 chunklength, png_uint_32 prefix_size,639png_alloc_size_t *newlength /* must be initialized to the maximum! */,640int terminate /*add a '\0' to the end of the uncompressed data*/)641{642/* TODO: implement different limits for different types of chunk.643*644* The caller supplies *newlength set to the maximum length of the645* uncompressed data, but this routine allocates space for the prefix and646* maybe a '\0' terminator too. We have to assume that 'prefix_size' is647* limited only by the maximum chunk size.648*/649png_alloc_size_t limit = PNG_SIZE_MAX;650651# ifdef PNG_SET_USER_LIMITS_SUPPORTED652if (png_ptr->user_chunk_malloc_max > 0 &&653png_ptr->user_chunk_malloc_max < limit)654limit = png_ptr->user_chunk_malloc_max;655# elif PNG_USER_CHUNK_MALLOC_MAX > 0656if (PNG_USER_CHUNK_MALLOC_MAX < limit)657limit = PNG_USER_CHUNK_MALLOC_MAX;658# endif659660if (limit >= prefix_size + (terminate != 0))661{662int ret;663664limit -= prefix_size + (terminate != 0);665666if (limit < *newlength)667*newlength = limit;668669/* Now try to claim the stream. */670ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);671672if (ret == Z_OK)673{674png_uint_32 lzsize = chunklength - prefix_size;675676ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,677/* input: */ png_ptr->read_buffer + prefix_size, &lzsize,678/* output: */ NULL, newlength);679680if (ret == Z_STREAM_END)681{682/* Use 'inflateReset' here, not 'inflateReset2' because this683* preserves the previously decided window size (otherwise it would684* be necessary to store the previous window size.) In practice685* this doesn't matter anyway, because png_inflate will call inflate686* with Z_FINISH in almost all cases, so the window will not be687* maintained.688*/689if (inflateReset(&png_ptr->zstream) == Z_OK)690{691/* Because of the limit checks above we know that the new,692* expanded, size will fit in a size_t (let alone an693* png_alloc_size_t). Use png_malloc_base here to avoid an694* extra OOM message.695*/696png_alloc_size_t new_size = *newlength;697png_alloc_size_t buffer_size = prefix_size + new_size +698(terminate != 0);699png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,700buffer_size));701702if (text != NULL)703{704memset(text, 0, buffer_size);705706ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,707png_ptr->read_buffer + prefix_size, &lzsize,708text + prefix_size, newlength);709710if (ret == Z_STREAM_END)711{712if (new_size == *newlength)713{714if (terminate != 0)715text[prefix_size + *newlength] = 0;716717if (prefix_size > 0)718memcpy(text, png_ptr->read_buffer, prefix_size);719720{721png_bytep old_ptr = png_ptr->read_buffer;722723png_ptr->read_buffer = text;724png_ptr->read_buffer_size = buffer_size;725text = old_ptr; /* freed below */726}727}728729else730{731/* The size changed on the second read, there can be no732* guarantee that anything is correct at this point.733* The 'msg' pointer has been set to "unexpected end of734* LZ stream", which is fine, but return an error code735* that the caller won't accept.736*/737ret = PNG_UNEXPECTED_ZLIB_RETURN;738}739}740741else if (ret == Z_OK)742ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */743744/* Free the text pointer (this is the old read_buffer on745* success)746*/747png_free(png_ptr, text);748749/* This really is very benign, but it's still an error because750* the extra space may otherwise be used as a Trojan Horse.751*/752if (ret == Z_STREAM_END &&753chunklength - prefix_size != lzsize)754png_chunk_benign_error(png_ptr, "extra compressed data");755}756757else758{759/* Out of memory allocating the buffer */760ret = Z_MEM_ERROR;761png_zstream_error(png_ptr, Z_MEM_ERROR);762}763}764765else766{767/* inflateReset failed, store the error message */768png_zstream_error(png_ptr, ret);769ret = PNG_UNEXPECTED_ZLIB_RETURN;770}771}772773else if (ret == Z_OK)774ret = PNG_UNEXPECTED_ZLIB_RETURN;775776/* Release the claimed stream */777png_ptr->zowner = 0;778}779780else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */781ret = PNG_UNEXPECTED_ZLIB_RETURN;782783return ret;784}785786else787{788/* Application/configuration limits exceeded */789png_zstream_error(png_ptr, Z_MEM_ERROR);790return Z_MEM_ERROR;791}792}793#endif /* READ_zTXt || READ_iTXt */794#endif /* READ_COMPRESSED_TEXT */795796#ifdef PNG_READ_iCCP_SUPPORTED797/* Perform a partial read and decompress, producing 'avail_out' bytes and798* reading from the current chunk as required.799*/800static int801png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,802png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,803int finish)804{805if (png_ptr->zowner == png_ptr->chunk_name)806{807int ret;808809/* next_in and avail_in must have been initialized by the caller. */810png_ptr->zstream.next_out = next_out;811png_ptr->zstream.avail_out = 0; /* set in the loop */812813do814{815if (png_ptr->zstream.avail_in == 0)816{817if (read_size > *chunk_bytes)818read_size = (uInt)*chunk_bytes;819*chunk_bytes -= read_size;820821if (read_size > 0)822png_crc_read(png_ptr, read_buffer, read_size);823824png_ptr->zstream.next_in = read_buffer;825png_ptr->zstream.avail_in = read_size;826}827828if (png_ptr->zstream.avail_out == 0)829{830uInt avail = ZLIB_IO_MAX;831if (avail > *out_size)832avail = (uInt)*out_size;833*out_size -= avail;834835png_ptr->zstream.avail_out = avail;836}837838/* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all839* the available output is produced; this allows reading of truncated840* streams.841*/842ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?843Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));844}845while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));846847*out_size += png_ptr->zstream.avail_out;848png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */849850/* Ensure the error message pointer is always set: */851png_zstream_error(png_ptr, ret);852return ret;853}854855else856{857png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");858return Z_STREAM_ERROR;859}860}861#endif /* READ_iCCP */862863/* Read and check the IDHR chunk */864865void /* PRIVATE */866png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)867{868png_byte buf[13];869png_uint_32 width, height;870int bit_depth, color_type, compression_type, filter_type;871int interlace_type;872873png_debug(1, "in png_handle_IHDR");874875if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)876png_chunk_error(png_ptr, "out of place");877878/* Check the length */879if (length != 13)880png_chunk_error(png_ptr, "invalid");881882png_ptr->mode |= PNG_HAVE_IHDR;883884png_crc_read(png_ptr, buf, 13);885png_crc_finish(png_ptr, 0);886887width = png_get_uint_31(png_ptr, buf);888height = png_get_uint_31(png_ptr, buf + 4);889bit_depth = buf[8];890color_type = buf[9];891compression_type = buf[10];892filter_type = buf[11];893interlace_type = buf[12];894895/* Set internal variables */896png_ptr->width = width;897png_ptr->height = height;898png_ptr->bit_depth = (png_byte)bit_depth;899png_ptr->interlaced = (png_byte)interlace_type;900png_ptr->color_type = (png_byte)color_type;901#ifdef PNG_MNG_FEATURES_SUPPORTED902png_ptr->filter_type = (png_byte)filter_type;903#endif904png_ptr->compression_type = (png_byte)compression_type;905906/* Find number of channels */907switch (png_ptr->color_type)908{909default: /* invalid, png_set_IHDR calls png_error */910case PNG_COLOR_TYPE_GRAY:911case PNG_COLOR_TYPE_PALETTE:912png_ptr->channels = 1;913break;914915case PNG_COLOR_TYPE_RGB:916png_ptr->channels = 3;917break;918919case PNG_COLOR_TYPE_GRAY_ALPHA:920png_ptr->channels = 2;921break;922923case PNG_COLOR_TYPE_RGB_ALPHA:924png_ptr->channels = 4;925break;926}927928/* Set up other useful info */929png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);930png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);931png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);932png_debug1(3, "channels = %d", png_ptr->channels);933png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);934png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,935color_type, interlace_type, compression_type, filter_type);936}937938/* Read and check the palette */939void /* PRIVATE */940png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)941{942png_color palette[PNG_MAX_PALETTE_LENGTH];943int max_palette_length, num, i;944#ifdef PNG_POINTER_INDEXING_SUPPORTED945png_colorp pal_ptr;946#endif947948png_debug(1, "in png_handle_PLTE");949950if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)951png_chunk_error(png_ptr, "missing IHDR");952953/* Moved to before the 'after IDAT' check below because otherwise duplicate954* PLTE chunks are potentially ignored (the spec says there shall not be more955* than one PLTE, the error is not treated as benign, so this check trumps956* the requirement that PLTE appears before IDAT.)957*/958else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)959png_chunk_error(png_ptr, "duplicate");960961else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)962{963/* This is benign because the non-benign error happened before, when an964* IDAT was encountered in a color-mapped image with no PLTE.965*/966png_crc_finish(png_ptr, length);967png_chunk_benign_error(png_ptr, "out of place");968return;969}970971png_ptr->mode |= PNG_HAVE_PLTE;972973if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)974{975png_crc_finish(png_ptr, length);976png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");977return;978}979980#ifndef PNG_READ_OPT_PLTE_SUPPORTED981if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)982{983png_crc_finish(png_ptr, length);984return;985}986#endif987988if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)989{990png_crc_finish(png_ptr, length);991992if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)993png_chunk_benign_error(png_ptr, "invalid");994995else996png_chunk_error(png_ptr, "invalid");997998return;999}10001001/* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */1002num = (int)length / 3;10031004/* If the palette has 256 or fewer entries but is too large for the bit1005* depth, we don't issue an error, to preserve the behavior of previous1006* libpng versions. We silently truncate the unused extra palette entries1007* here.1008*/1009if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)1010max_palette_length = (1 << png_ptr->bit_depth);1011else1012max_palette_length = PNG_MAX_PALETTE_LENGTH;10131014if (num > max_palette_length)1015num = max_palette_length;10161017#ifdef PNG_POINTER_INDEXING_SUPPORTED1018for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)1019{1020png_byte buf[3];10211022png_crc_read(png_ptr, buf, 3);1023pal_ptr->red = buf[0];1024pal_ptr->green = buf[1];1025pal_ptr->blue = buf[2];1026}1027#else1028for (i = 0; i < num; i++)1029{1030png_byte buf[3];10311032png_crc_read(png_ptr, buf, 3);1033/* Don't depend upon png_color being any order */1034palette[i].red = buf[0];1035palette[i].green = buf[1];1036palette[i].blue = buf[2];1037}1038#endif10391040/* If we actually need the PLTE chunk (ie for a paletted image), we do1041* whatever the normal CRC configuration tells us. However, if we1042* have an RGB image, the PLTE can be considered ancillary, so1043* we will act as though it is.1044*/1045#ifndef PNG_READ_OPT_PLTE_SUPPORTED1046if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)1047#endif1048{1049png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));1050}10511052#ifndef PNG_READ_OPT_PLTE_SUPPORTED1053else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */1054{1055/* If we don't want to use the data from an ancillary chunk,1056* we have two options: an error abort, or a warning and we1057* ignore the data in this chunk (which should be OK, since1058* it's considered ancillary for a RGB or RGBA image).1059*1060* IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the1061* chunk type to determine whether to check the ancillary or the critical1062* flags.1063*/1064if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)1065{1066if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)1067return;10681069else1070png_chunk_error(png_ptr, "CRC error");1071}10721073/* Otherwise, we (optionally) emit a warning and use the chunk. */1074else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)1075png_chunk_warning(png_ptr, "CRC error");1076}1077#endif10781079/* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its1080* own copy of the palette. This has the side effect that when png_start_row1081* is called (this happens after any call to png_read_update_info) the1082* info_ptr palette gets changed. This is extremely unexpected and1083* confusing.1084*1085* Fix this by not sharing the palette in this way.1086*/1087png_set_PLTE(png_ptr, info_ptr, palette, num);10881089/* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before1090* IDAT. Prior to 1.6.0 this was not checked; instead the code merely1091* checked the apparent validity of a tRNS chunk inserted before PLTE on a1092* palette PNG. 1.6.0 attempts to rigorously follow the standard and1093* therefore does a benign error if the erroneous condition is detected *and*1094* cancels the tRNS if the benign error returns. The alternative is to1095* amend the standard since it would be rather hypocritical of the standards1096* maintainers to ignore it.1097*/1098#ifdef PNG_READ_tRNS_SUPPORTED1099if (png_ptr->num_trans > 0 ||1100(info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))1101{1102/* Cancel this because otherwise it would be used if the transforms1103* require it. Don't cancel the 'valid' flag because this would prevent1104* detection of duplicate chunks.1105*/1106png_ptr->num_trans = 0;11071108if (info_ptr != NULL)1109info_ptr->num_trans = 0;11101111png_chunk_benign_error(png_ptr, "tRNS must be after");1112}1113#endif11141115#ifdef PNG_READ_hIST_SUPPORTED1116if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)1117png_chunk_benign_error(png_ptr, "hIST must be after");1118#endif11191120#ifdef PNG_READ_bKGD_SUPPORTED1121if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)1122png_chunk_benign_error(png_ptr, "bKGD must be after");1123#endif1124}11251126void /* PRIVATE */1127png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1128{1129png_debug(1, "in png_handle_IEND");11301131if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||1132(png_ptr->mode & PNG_HAVE_IDAT) == 0)1133png_chunk_error(png_ptr, "out of place");11341135png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);11361137png_crc_finish(png_ptr, length);11381139if (length != 0)1140png_chunk_benign_error(png_ptr, "invalid");11411142PNG_UNUSED(info_ptr)1143}11441145#ifdef PNG_READ_gAMA_SUPPORTED1146void /* PRIVATE */1147png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1148{1149png_fixed_point igamma;1150png_byte buf[4];11511152png_debug(1, "in png_handle_gAMA");11531154if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1155png_chunk_error(png_ptr, "missing IHDR");11561157else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)1158{1159png_crc_finish(png_ptr, length);1160png_chunk_benign_error(png_ptr, "out of place");1161return;1162}11631164if (length != 4)1165{1166png_crc_finish(png_ptr, length);1167png_chunk_benign_error(png_ptr, "invalid");1168return;1169}11701171png_crc_read(png_ptr, buf, 4);11721173if (png_crc_finish(png_ptr, 0) != 0)1174return;11751176igamma = png_get_fixed_point(NULL, buf);11771178png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);1179png_colorspace_sync(png_ptr, info_ptr);1180}1181#endif11821183#ifdef PNG_READ_sBIT_SUPPORTED1184void /* PRIVATE */1185png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1186{1187unsigned int truelen, i;1188png_byte sample_depth;1189png_byte buf[4];11901191png_debug(1, "in png_handle_sBIT");11921193if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1194png_chunk_error(png_ptr, "missing IHDR");11951196else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)1197{1198png_crc_finish(png_ptr, length);1199png_chunk_benign_error(png_ptr, "out of place");1200return;1201}12021203if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)1204{1205png_crc_finish(png_ptr, length);1206png_chunk_benign_error(png_ptr, "duplicate");1207return;1208}12091210if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)1211{1212truelen = 3;1213sample_depth = 8;1214}12151216else1217{1218truelen = png_ptr->channels;1219sample_depth = png_ptr->bit_depth;1220}12211222if (length != truelen || length > 4)1223{1224png_chunk_benign_error(png_ptr, "invalid");1225png_crc_finish(png_ptr, length);1226return;1227}12281229buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;1230png_crc_read(png_ptr, buf, truelen);12311232if (png_crc_finish(png_ptr, 0) != 0)1233return;12341235for (i=0; i<truelen; ++i)1236{1237if (buf[i] == 0 || buf[i] > sample_depth)1238{1239png_chunk_benign_error(png_ptr, "invalid");1240return;1241}1242}12431244if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)1245{1246png_ptr->sig_bit.red = buf[0];1247png_ptr->sig_bit.green = buf[1];1248png_ptr->sig_bit.blue = buf[2];1249png_ptr->sig_bit.alpha = buf[3];1250}12511252else1253{1254png_ptr->sig_bit.gray = buf[0];1255png_ptr->sig_bit.red = buf[0];1256png_ptr->sig_bit.green = buf[0];1257png_ptr->sig_bit.blue = buf[0];1258png_ptr->sig_bit.alpha = buf[1];1259}12601261png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));1262}1263#endif12641265#ifdef PNG_READ_cHRM_SUPPORTED1266void /* PRIVATE */1267png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1268{1269png_byte buf[32];1270png_xy xy;12711272png_debug(1, "in png_handle_cHRM");12731274if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1275png_chunk_error(png_ptr, "missing IHDR");12761277else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)1278{1279png_crc_finish(png_ptr, length);1280png_chunk_benign_error(png_ptr, "out of place");1281return;1282}12831284if (length != 32)1285{1286png_crc_finish(png_ptr, length);1287png_chunk_benign_error(png_ptr, "invalid");1288return;1289}12901291png_crc_read(png_ptr, buf, 32);12921293if (png_crc_finish(png_ptr, 0) != 0)1294return;12951296xy.whitex = png_get_fixed_point(NULL, buf);1297xy.whitey = png_get_fixed_point(NULL, buf + 4);1298xy.redx = png_get_fixed_point(NULL, buf + 8);1299xy.redy = png_get_fixed_point(NULL, buf + 12);1300xy.greenx = png_get_fixed_point(NULL, buf + 16);1301xy.greeny = png_get_fixed_point(NULL, buf + 20);1302xy.bluex = png_get_fixed_point(NULL, buf + 24);1303xy.bluey = png_get_fixed_point(NULL, buf + 28);13041305if (xy.whitex == PNG_FIXED_ERROR ||1306xy.whitey == PNG_FIXED_ERROR ||1307xy.redx == PNG_FIXED_ERROR ||1308xy.redy == PNG_FIXED_ERROR ||1309xy.greenx == PNG_FIXED_ERROR ||1310xy.greeny == PNG_FIXED_ERROR ||1311xy.bluex == PNG_FIXED_ERROR ||1312xy.bluey == PNG_FIXED_ERROR)1313{1314png_chunk_benign_error(png_ptr, "invalid values");1315return;1316}13171318/* If a colorspace error has already been output skip this chunk */1319if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)1320return;13211322if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)1323{1324png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;1325png_colorspace_sync(png_ptr, info_ptr);1326png_chunk_benign_error(png_ptr, "duplicate");1327return;1328}13291330png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;1331(void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,13321/*prefer cHRM values*/);1333png_colorspace_sync(png_ptr, info_ptr);1334}1335#endif13361337#ifdef PNG_READ_sRGB_SUPPORTED1338void /* PRIVATE */1339png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1340{1341png_byte intent;13421343png_debug(1, "in png_handle_sRGB");13441345if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1346png_chunk_error(png_ptr, "missing IHDR");13471348else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)1349{1350png_crc_finish(png_ptr, length);1351png_chunk_benign_error(png_ptr, "out of place");1352return;1353}13541355if (length != 1)1356{1357png_crc_finish(png_ptr, length);1358png_chunk_benign_error(png_ptr, "invalid");1359return;1360}13611362png_crc_read(png_ptr, &intent, 1);13631364if (png_crc_finish(png_ptr, 0) != 0)1365return;13661367/* If a colorspace error has already been output skip this chunk */1368if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)1369return;13701371/* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect1372* this.1373*/1374if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)1375{1376png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;1377png_colorspace_sync(png_ptr, info_ptr);1378png_chunk_benign_error(png_ptr, "too many profiles");1379return;1380}13811382(void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);1383png_colorspace_sync(png_ptr, info_ptr);1384}1385#endif /* READ_sRGB */13861387#ifdef PNG_READ_iCCP_SUPPORTED1388void /* PRIVATE */1389png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1390/* Note: this does not properly handle profiles that are > 64K under DOS */1391{1392png_const_charp errmsg = NULL; /* error message output, or no error */1393int finished = 0; /* crc checked */13941395png_debug(1, "in png_handle_iCCP");13961397if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1398png_chunk_error(png_ptr, "missing IHDR");13991400else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)1401{1402png_crc_finish(png_ptr, length);1403png_chunk_benign_error(png_ptr, "out of place");1404return;1405}14061407/* Consistent with all the above colorspace handling an obviously *invalid*1408* chunk is just ignored, so does not invalidate the color space. An1409* alternative is to set the 'invalid' flags at the start of this routine1410* and only clear them in they were not set before and all the tests pass.1411*/14121413/* The keyword must be at least one character and there is a1414* terminator (0) byte and the compression method byte, and the1415* 'zlib' datastream is at least 11 bytes.1416*/1417if (length < 14)1418{1419png_crc_finish(png_ptr, length);1420png_chunk_benign_error(png_ptr, "too short");1421return;1422}14231424/* If a colorspace error has already been output skip this chunk */1425if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)1426{1427png_crc_finish(png_ptr, length);1428return;1429}14301431/* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect1432* this.1433*/1434if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)1435{1436uInt read_length, keyword_length;1437char keyword[81];14381439/* Find the keyword; the keyword plus separator and compression method1440* bytes can be at most 81 characters long.1441*/1442read_length = 81; /* maximum */1443if (read_length > length)1444read_length = (uInt)length;14451446png_crc_read(png_ptr, (png_bytep)keyword, read_length);1447length -= read_length;14481449/* The minimum 'zlib' stream is assumed to be just the 2 byte header,1450* 5 bytes minimum 'deflate' stream, and the 4 byte checksum.1451*/1452if (length < 11)1453{1454png_crc_finish(png_ptr, length);1455png_chunk_benign_error(png_ptr, "too short");1456return;1457}14581459keyword_length = 0;1460while (keyword_length < 80 && keyword_length < read_length &&1461keyword[keyword_length] != 0)1462++keyword_length;14631464/* TODO: make the keyword checking common */1465if (keyword_length >= 1 && keyword_length <= 79)1466{1467/* We only understand '0' compression - deflate - so if we get a1468* different value we can't safely decode the chunk.1469*/1470if (keyword_length+1 < read_length &&1471keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)1472{1473read_length -= keyword_length+2;14741475if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)1476{1477Byte profile_header[132]={0};1478Byte local_buffer[PNG_INFLATE_BUF_SIZE];1479png_alloc_size_t size = (sizeof profile_header);14801481png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);1482png_ptr->zstream.avail_in = read_length;1483(void)png_inflate_read(png_ptr, local_buffer,1484(sizeof local_buffer), &length, profile_header, &size,14850/*finish: don't, because the output is too small*/);14861487if (size == 0)1488{1489/* We have the ICC profile header; do the basic header checks.1490*/1491png_uint_32 profile_length = png_get_uint_32(profile_header);14921493if (png_icc_check_length(png_ptr, &png_ptr->colorspace,1494keyword, profile_length) != 0)1495{1496/* The length is apparently ok, so we can check the 1321497* byte header.1498*/1499if (png_icc_check_header(png_ptr, &png_ptr->colorspace,1500keyword, profile_length, profile_header,1501png_ptr->color_type) != 0)1502{1503/* Now read the tag table; a variable size buffer is1504* needed at this point, allocate one for the whole1505* profile. The header check has already validated1506* that none of this stuff will overflow.1507*/1508png_uint_32 tag_count =1509png_get_uint_32(profile_header + 128);1510png_bytep profile = png_read_buffer(png_ptr,1511profile_length, 2/*silent*/);15121513if (profile != NULL)1514{1515memcpy(profile, profile_header,1516(sizeof profile_header));15171518size = 12 * tag_count;15191520(void)png_inflate_read(png_ptr, local_buffer,1521(sizeof local_buffer), &length,1522profile + (sizeof profile_header), &size, 0);15231524/* Still expect a buffer error because we expect1525* there to be some tag data!1526*/1527if (size == 0)1528{1529if (png_icc_check_tag_table(png_ptr,1530&png_ptr->colorspace, keyword, profile_length,1531profile) != 0)1532{1533/* The profile has been validated for basic1534* security issues, so read the whole thing in.1535*/1536size = profile_length - (sizeof profile_header)1537- 12 * tag_count;15381539(void)png_inflate_read(png_ptr, local_buffer,1540(sizeof local_buffer), &length,1541profile + (sizeof profile_header) +154212 * tag_count, &size, 1/*finish*/);15431544if (length > 0 && !(png_ptr->flags &1545PNG_FLAG_BENIGN_ERRORS_WARN))1546errmsg = "extra compressed data";15471548/* But otherwise allow extra data: */1549else if (size == 0)1550{1551if (length > 0)1552{1553/* This can be handled completely, so1554* keep going.1555*/1556png_chunk_warning(png_ptr,1557"extra compressed data");1558}15591560png_crc_finish(png_ptr, length);1561finished = 1;15621563# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 01564/* Check for a match against sRGB */1565png_icc_set_sRGB(png_ptr,1566&png_ptr->colorspace, profile,1567png_ptr->zstream.adler);1568# endif15691570/* Steal the profile for info_ptr. */1571if (info_ptr != NULL)1572{1573png_free_data(png_ptr, info_ptr,1574PNG_FREE_ICCP, 0);15751576info_ptr->iccp_name = png_voidcast(char*,1577png_malloc_base(png_ptr,1578keyword_length+1));1579if (info_ptr->iccp_name != NULL)1580{1581memcpy(info_ptr->iccp_name, keyword,1582keyword_length+1);1583info_ptr->iccp_proflen =1584profile_length;1585info_ptr->iccp_profile = profile;1586png_ptr->read_buffer = NULL; /*steal*/1587info_ptr->free_me |= PNG_FREE_ICCP;1588info_ptr->valid |= PNG_INFO_iCCP;1589}15901591else1592{1593png_ptr->colorspace.flags |=1594PNG_COLORSPACE_INVALID;1595errmsg = "out of memory";1596}1597}15981599/* else the profile remains in the read1600* buffer which gets reused for subsequent1601* chunks.1602*/16031604if (info_ptr != NULL)1605png_colorspace_sync(png_ptr, info_ptr);16061607if (errmsg == NULL)1608{1609png_ptr->zowner = 0;1610return;1611}1612}1613if (errmsg == NULL)1614errmsg = png_ptr->zstream.msg;1615}1616/* else png_icc_check_tag_table output an error */1617}1618else /* profile truncated */1619errmsg = png_ptr->zstream.msg;1620}16211622else1623errmsg = "out of memory";1624}16251626/* else png_icc_check_header output an error */1627}16281629/* else png_icc_check_length output an error */1630}16311632else /* profile truncated */1633errmsg = png_ptr->zstream.msg;16341635/* Release the stream */1636png_ptr->zowner = 0;1637}16381639else /* png_inflate_claim failed */1640errmsg = png_ptr->zstream.msg;1641}16421643else1644errmsg = "bad compression method"; /* or missing */1645}16461647else1648errmsg = "bad keyword";1649}16501651else1652errmsg = "too many profiles";16531654/* Failure: the reason is in 'errmsg' */1655if (finished == 0)1656png_crc_finish(png_ptr, length);16571658png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;1659png_colorspace_sync(png_ptr, info_ptr);1660if (errmsg != NULL) /* else already output */1661png_chunk_benign_error(png_ptr, errmsg);1662}1663#endif /* READ_iCCP */16641665#ifdef PNG_READ_sPLT_SUPPORTED1666void /* PRIVATE */1667png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1668/* Note: this does not properly handle chunks that are > 64K under DOS */1669{1670png_bytep entry_start, buffer;1671png_sPLT_t new_palette;1672png_sPLT_entryp pp;1673png_uint_32 data_length;1674int entry_size, i;1675png_uint_32 skip = 0;1676png_uint_32 dl;1677size_t max_dl;16781679png_debug(1, "in png_handle_sPLT");16801681#ifdef PNG_USER_LIMITS_SUPPORTED1682if (png_ptr->user_chunk_cache_max != 0)1683{1684if (png_ptr->user_chunk_cache_max == 1)1685{1686png_crc_finish(png_ptr, length);1687return;1688}16891690if (--png_ptr->user_chunk_cache_max == 1)1691{1692png_warning(png_ptr, "No space in chunk cache for sPLT");1693png_crc_finish(png_ptr, length);1694return;1695}1696}1697#endif16981699if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1700png_chunk_error(png_ptr, "missing IHDR");17011702else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)1703{1704png_crc_finish(png_ptr, length);1705png_chunk_benign_error(png_ptr, "out of place");1706return;1707}17081709#ifdef PNG_MAX_MALLOC_64K1710if (length > 65535U)1711{1712png_crc_finish(png_ptr, length);1713png_chunk_benign_error(png_ptr, "too large to fit in memory");1714return;1715}1716#endif17171718buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);1719if (buffer == NULL)1720{1721png_crc_finish(png_ptr, length);1722png_chunk_benign_error(png_ptr, "out of memory");1723return;1724}172517261727/* WARNING: this may break if size_t is less than 32 bits; it is assumed1728* that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a1729* potential breakage point if the types in pngconf.h aren't exactly right.1730*/1731png_crc_read(png_ptr, buffer, length);17321733if (png_crc_finish(png_ptr, skip) != 0)1734return;17351736buffer[length] = 0;17371738for (entry_start = buffer; *entry_start; entry_start++)1739/* Empty loop to find end of name */ ;17401741++entry_start;17421743/* A sample depth should follow the separator, and we should be on it */1744if (length < 2U || entry_start > buffer + (length - 2U))1745{1746png_warning(png_ptr, "malformed sPLT chunk");1747return;1748}17491750new_palette.depth = *entry_start++;1751entry_size = (new_palette.depth == 8 ? 6 : 10);1752/* This must fit in a png_uint_32 because it is derived from the original1753* chunk data length.1754*/1755data_length = length - (png_uint_32)(entry_start - buffer);17561757/* Integrity-check the data length */1758if ((data_length % (unsigned int)entry_size) != 0)1759{1760png_warning(png_ptr, "sPLT chunk has bad length");1761return;1762}17631764dl = (png_uint_32)(data_length / (unsigned int)entry_size);1765max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));17661767if (dl > max_dl)1768{1769png_warning(png_ptr, "sPLT chunk too long");1770return;1771}17721773new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);17741775new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,1776(png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));17771778if (new_palette.entries == NULL)1779{1780png_warning(png_ptr, "sPLT chunk requires too much memory");1781return;1782}17831784#ifdef PNG_POINTER_INDEXING_SUPPORTED1785for (i = 0; i < new_palette.nentries; i++)1786{1787pp = new_palette.entries + i;17881789if (new_palette.depth == 8)1790{1791pp->red = *entry_start++;1792pp->green = *entry_start++;1793pp->blue = *entry_start++;1794pp->alpha = *entry_start++;1795}17961797else1798{1799pp->red = png_get_uint_16(entry_start); entry_start += 2;1800pp->green = png_get_uint_16(entry_start); entry_start += 2;1801pp->blue = png_get_uint_16(entry_start); entry_start += 2;1802pp->alpha = png_get_uint_16(entry_start); entry_start += 2;1803}18041805pp->frequency = png_get_uint_16(entry_start); entry_start += 2;1806}1807#else1808pp = new_palette.entries;18091810for (i = 0; i < new_palette.nentries; i++)1811{18121813if (new_palette.depth == 8)1814{1815pp[i].red = *entry_start++;1816pp[i].green = *entry_start++;1817pp[i].blue = *entry_start++;1818pp[i].alpha = *entry_start++;1819}18201821else1822{1823pp[i].red = png_get_uint_16(entry_start); entry_start += 2;1824pp[i].green = png_get_uint_16(entry_start); entry_start += 2;1825pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;1826pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;1827}18281829pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;1830}1831#endif18321833/* Discard all chunk data except the name and stash that */1834new_palette.name = (png_charp)buffer;18351836png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);18371838png_free(png_ptr, new_palette.entries);1839}1840#endif /* READ_sPLT */18411842#ifdef PNG_READ_tRNS_SUPPORTED1843void /* PRIVATE */1844png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1845{1846png_byte readbuf[PNG_MAX_PALETTE_LENGTH];18471848png_debug(1, "in png_handle_tRNS");18491850if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1851png_chunk_error(png_ptr, "missing IHDR");18521853else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)1854{1855png_crc_finish(png_ptr, length);1856png_chunk_benign_error(png_ptr, "out of place");1857return;1858}18591860else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)1861{1862png_crc_finish(png_ptr, length);1863png_chunk_benign_error(png_ptr, "duplicate");1864return;1865}18661867if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)1868{1869png_byte buf[2];18701871if (length != 2)1872{1873png_crc_finish(png_ptr, length);1874png_chunk_benign_error(png_ptr, "invalid");1875return;1876}18771878png_crc_read(png_ptr, buf, 2);1879png_ptr->num_trans = 1;1880png_ptr->trans_color.gray = png_get_uint_16(buf);1881}18821883else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)1884{1885png_byte buf[6];18861887if (length != 6)1888{1889png_crc_finish(png_ptr, length);1890png_chunk_benign_error(png_ptr, "invalid");1891return;1892}18931894png_crc_read(png_ptr, buf, length);1895png_ptr->num_trans = 1;1896png_ptr->trans_color.red = png_get_uint_16(buf);1897png_ptr->trans_color.green = png_get_uint_16(buf + 2);1898png_ptr->trans_color.blue = png_get_uint_16(buf + 4);1899}19001901else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)1902{1903if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)1904{1905/* TODO: is this actually an error in the ISO spec? */1906png_crc_finish(png_ptr, length);1907png_chunk_benign_error(png_ptr, "out of place");1908return;1909}19101911if (length > (unsigned int) png_ptr->num_palette ||1912length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||1913length == 0)1914{1915png_crc_finish(png_ptr, length);1916png_chunk_benign_error(png_ptr, "invalid");1917return;1918}19191920png_crc_read(png_ptr, readbuf, length);1921png_ptr->num_trans = (png_uint_16)length;1922}19231924else1925{1926png_crc_finish(png_ptr, length);1927png_chunk_benign_error(png_ptr, "invalid with alpha channel");1928return;1929}19301931if (png_crc_finish(png_ptr, 0) != 0)1932{1933png_ptr->num_trans = 0;1934return;1935}19361937/* TODO: this is a horrible side effect in the palette case because the1938* png_struct ends up with a pointer to the tRNS buffer owned by the1939* png_info. Fix this.1940*/1941png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,1942&(png_ptr->trans_color));1943}1944#endif19451946#ifdef PNG_READ_bKGD_SUPPORTED1947void /* PRIVATE */1948png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)1949{1950unsigned int truelen;1951png_byte buf[6];1952png_color_16 background;19531954png_debug(1, "in png_handle_bKGD");19551956if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)1957png_chunk_error(png_ptr, "missing IHDR");19581959else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||1960(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&1961(png_ptr->mode & PNG_HAVE_PLTE) == 0))1962{1963png_crc_finish(png_ptr, length);1964png_chunk_benign_error(png_ptr, "out of place");1965return;1966}19671968else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)1969{1970png_crc_finish(png_ptr, length);1971png_chunk_benign_error(png_ptr, "duplicate");1972return;1973}19741975if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)1976truelen = 1;19771978else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)1979truelen = 6;19801981else1982truelen = 2;19831984if (length != truelen)1985{1986png_crc_finish(png_ptr, length);1987png_chunk_benign_error(png_ptr, "invalid");1988return;1989}19901991png_crc_read(png_ptr, buf, truelen);19921993if (png_crc_finish(png_ptr, 0) != 0)1994return;19951996/* We convert the index value into RGB components so that we can allow1997* arbitrary RGB values for background when we have transparency, and1998* so it is easy to determine the RGB values of the background color1999* from the info_ptr struct.2000*/2001if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)2002{2003background.index = buf[0];20042005if (info_ptr != NULL && info_ptr->num_palette != 0)2006{2007if (buf[0] >= info_ptr->num_palette)2008{2009png_chunk_benign_error(png_ptr, "invalid index");2010return;2011}20122013background.red = (png_uint_16)png_ptr->palette[buf[0]].red;2014background.green = (png_uint_16)png_ptr->palette[buf[0]].green;2015background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;2016}20172018else2019background.red = background.green = background.blue = 0;20202021background.gray = 0;2022}20232024else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */2025{2026if (png_ptr->bit_depth <= 8)2027{2028if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))2029{2030png_chunk_benign_error(png_ptr, "invalid gray level");2031return;2032}2033}20342035background.index = 0;2036background.red =2037background.green =2038background.blue =2039background.gray = png_get_uint_16(buf);2040}20412042else2043{2044if (png_ptr->bit_depth <= 8)2045{2046if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)2047{2048png_chunk_benign_error(png_ptr, "invalid color");2049return;2050}2051}20522053background.index = 0;2054background.red = png_get_uint_16(buf);2055background.green = png_get_uint_16(buf + 2);2056background.blue = png_get_uint_16(buf + 4);2057background.gray = 0;2058}20592060png_set_bKGD(png_ptr, info_ptr, &background);2061}2062#endif20632064#ifdef PNG_READ_eXIf_SUPPORTED2065void /* PRIVATE */2066png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2067{2068unsigned int i;20692070png_debug(1, "in png_handle_eXIf");20712072if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2073png_chunk_error(png_ptr, "missing IHDR");20742075if (length < 2)2076{2077png_crc_finish(png_ptr, length);2078png_chunk_benign_error(png_ptr, "too short");2079return;2080}20812082else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)2083{2084png_crc_finish(png_ptr, length);2085png_chunk_benign_error(png_ptr, "duplicate");2086return;2087}20882089info_ptr->free_me |= PNG_FREE_EXIF;20902091info_ptr->eXIf_buf = png_voidcast(png_bytep,2092png_malloc_warn(png_ptr, length));20932094if (info_ptr->eXIf_buf == NULL)2095{2096png_crc_finish(png_ptr, length);2097png_chunk_benign_error(png_ptr, "out of memory");2098return;2099}21002101for (i = 0; i < length; i++)2102{2103png_byte buf[1];2104png_crc_read(png_ptr, buf, 1);2105info_ptr->eXIf_buf[i] = buf[0];2106if (i == 1 && buf[0] != 'M' && buf[0] != 'I'2107&& info_ptr->eXIf_buf[0] != buf[0])2108{2109png_crc_finish(png_ptr, length);2110png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");2111png_free(png_ptr, info_ptr->eXIf_buf);2112info_ptr->eXIf_buf = NULL;2113return;2114}2115}21162117if (png_crc_finish(png_ptr, 0) != 0)2118return;21192120png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);21212122png_free(png_ptr, info_ptr->eXIf_buf);2123info_ptr->eXIf_buf = NULL;2124}2125#endif21262127#ifdef PNG_READ_hIST_SUPPORTED2128void /* PRIVATE */2129png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2130{2131unsigned int num, i;2132png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];21332134png_debug(1, "in png_handle_hIST");21352136if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2137png_chunk_error(png_ptr, "missing IHDR");21382139else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||2140(png_ptr->mode & PNG_HAVE_PLTE) == 0)2141{2142png_crc_finish(png_ptr, length);2143png_chunk_benign_error(png_ptr, "out of place");2144return;2145}21462147else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)2148{2149png_crc_finish(png_ptr, length);2150png_chunk_benign_error(png_ptr, "duplicate");2151return;2152}21532154num = length / 2 ;21552156if (num != (unsigned int) png_ptr->num_palette ||2157num > (unsigned int) PNG_MAX_PALETTE_LENGTH)2158{2159png_crc_finish(png_ptr, length);2160png_chunk_benign_error(png_ptr, "invalid");2161return;2162}21632164for (i = 0; i < num; i++)2165{2166png_byte buf[2];21672168png_crc_read(png_ptr, buf, 2);2169readbuf[i] = png_get_uint_16(buf);2170}21712172if (png_crc_finish(png_ptr, 0) != 0)2173return;21742175png_set_hIST(png_ptr, info_ptr, readbuf);2176}2177#endif21782179#ifdef PNG_READ_pHYs_SUPPORTED2180void /* PRIVATE */2181png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2182{2183png_byte buf[9];2184png_uint_32 res_x, res_y;2185int unit_type;21862187png_debug(1, "in png_handle_pHYs");21882189if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2190png_chunk_error(png_ptr, "missing IHDR");21912192else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2193{2194png_crc_finish(png_ptr, length);2195png_chunk_benign_error(png_ptr, "out of place");2196return;2197}21982199else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)2200{2201png_crc_finish(png_ptr, length);2202png_chunk_benign_error(png_ptr, "duplicate");2203return;2204}22052206if (length != 9)2207{2208png_crc_finish(png_ptr, length);2209png_chunk_benign_error(png_ptr, "invalid");2210return;2211}22122213png_crc_read(png_ptr, buf, 9);22142215if (png_crc_finish(png_ptr, 0) != 0)2216return;22172218res_x = png_get_uint_32(buf);2219res_y = png_get_uint_32(buf + 4);2220unit_type = buf[8];2221png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);2222}2223#endif22242225#ifdef PNG_READ_oFFs_SUPPORTED2226void /* PRIVATE */2227png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2228{2229png_byte buf[9];2230png_int_32 offset_x, offset_y;2231int unit_type;22322233png_debug(1, "in png_handle_oFFs");22342235if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2236png_chunk_error(png_ptr, "missing IHDR");22372238else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2239{2240png_crc_finish(png_ptr, length);2241png_chunk_benign_error(png_ptr, "out of place");2242return;2243}22442245else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)2246{2247png_crc_finish(png_ptr, length);2248png_chunk_benign_error(png_ptr, "duplicate");2249return;2250}22512252if (length != 9)2253{2254png_crc_finish(png_ptr, length);2255png_chunk_benign_error(png_ptr, "invalid");2256return;2257}22582259png_crc_read(png_ptr, buf, 9);22602261if (png_crc_finish(png_ptr, 0) != 0)2262return;22632264offset_x = png_get_int_32(buf);2265offset_y = png_get_int_32(buf + 4);2266unit_type = buf[8];2267png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);2268}2269#endif22702271#ifdef PNG_READ_pCAL_SUPPORTED2272/* Read the pCAL chunk (described in the PNG Extensions document) */2273void /* PRIVATE */2274png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2275{2276png_int_32 X0, X1;2277png_byte type, nparams;2278png_bytep buffer, buf, units, endptr;2279png_charpp params;2280int i;22812282png_debug(1, "in png_handle_pCAL");22832284if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2285png_chunk_error(png_ptr, "missing IHDR");22862287else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2288{2289png_crc_finish(png_ptr, length);2290png_chunk_benign_error(png_ptr, "out of place");2291return;2292}22932294else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)2295{2296png_crc_finish(png_ptr, length);2297png_chunk_benign_error(png_ptr, "duplicate");2298return;2299}23002301png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",2302length + 1);23032304buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);23052306if (buffer == NULL)2307{2308png_crc_finish(png_ptr, length);2309png_chunk_benign_error(png_ptr, "out of memory");2310return;2311}23122313png_crc_read(png_ptr, buffer, length);23142315if (png_crc_finish(png_ptr, 0) != 0)2316return;23172318buffer[length] = 0; /* Null terminate the last string */23192320png_debug(3, "Finding end of pCAL purpose string");2321for (buf = buffer; *buf; buf++)2322/* Empty loop */ ;23232324endptr = buffer + length;23252326/* We need to have at least 12 bytes after the purpose string2327* in order to get the parameter information.2328*/2329if (endptr - buf <= 12)2330{2331png_chunk_benign_error(png_ptr, "invalid");2332return;2333}23342335png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");2336X0 = png_get_int_32((png_bytep)buf+1);2337X1 = png_get_int_32((png_bytep)buf+5);2338type = buf[9];2339nparams = buf[10];2340units = buf + 11;23412342png_debug(3, "Checking pCAL equation type and number of parameters");2343/* Check that we have the right number of parameters for known2344* equation types.2345*/2346if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||2347(type == PNG_EQUATION_BASE_E && nparams != 3) ||2348(type == PNG_EQUATION_ARBITRARY && nparams != 3) ||2349(type == PNG_EQUATION_HYPERBOLIC && nparams != 4))2350{2351png_chunk_benign_error(png_ptr, "invalid parameter count");2352return;2353}23542355else if (type >= PNG_EQUATION_LAST)2356{2357png_chunk_benign_error(png_ptr, "unrecognized equation type");2358}23592360for (buf = units; *buf; buf++)2361/* Empty loop to move past the units string. */ ;23622363png_debug(3, "Allocating pCAL parameters array");23642365params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,2366nparams * (sizeof (png_charp))));23672368if (params == NULL)2369{2370png_chunk_benign_error(png_ptr, "out of memory");2371return;2372}23732374/* Get pointers to the start of each parameter string. */2375for (i = 0; i < nparams; i++)2376{2377buf++; /* Skip the null string terminator from previous parameter. */23782379png_debug1(3, "Reading pCAL parameter %d", i);23802381for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)2382/* Empty loop to move past each parameter string */ ;23832384/* Make sure we haven't run out of data yet */2385if (buf > endptr)2386{2387png_free(png_ptr, params);2388png_chunk_benign_error(png_ptr, "invalid data");2389return;2390}2391}23922393png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,2394(png_charp)units, params);23952396png_free(png_ptr, params);2397}2398#endif23992400#ifdef PNG_READ_sCAL_SUPPORTED2401/* Read the sCAL chunk */2402void /* PRIVATE */2403png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2404{2405png_bytep buffer;2406size_t i;2407int state;24082409png_debug(1, "in png_handle_sCAL");24102411if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2412png_chunk_error(png_ptr, "missing IHDR");24132414else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2415{2416png_crc_finish(png_ptr, length);2417png_chunk_benign_error(png_ptr, "out of place");2418return;2419}24202421else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)2422{2423png_crc_finish(png_ptr, length);2424png_chunk_benign_error(png_ptr, "duplicate");2425return;2426}24272428/* Need unit type, width, \0, height: minimum 4 bytes */2429else if (length < 4)2430{2431png_crc_finish(png_ptr, length);2432png_chunk_benign_error(png_ptr, "invalid");2433return;2434}24352436png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",2437length + 1);24382439buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);24402441if (buffer == NULL)2442{2443png_chunk_benign_error(png_ptr, "out of memory");2444png_crc_finish(png_ptr, length);2445return;2446}24472448png_crc_read(png_ptr, buffer, length);2449buffer[length] = 0; /* Null terminate the last string */24502451if (png_crc_finish(png_ptr, 0) != 0)2452return;24532454/* Validate the unit. */2455if (buffer[0] != 1 && buffer[0] != 2)2456{2457png_chunk_benign_error(png_ptr, "invalid unit");2458return;2459}24602461/* Validate the ASCII numbers, need two ASCII numbers separated by2462* a '\0' and they need to fit exactly in the chunk data.2463*/2464i = 1;2465state = 0;24662467if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||2468i >= length || buffer[i++] != 0)2469png_chunk_benign_error(png_ptr, "bad width format");24702471else if (PNG_FP_IS_POSITIVE(state) == 0)2472png_chunk_benign_error(png_ptr, "non-positive width");24732474else2475{2476size_t heighti = i;24772478state = 0;2479if (png_check_fp_number((png_const_charp)buffer, length,2480&state, &i) == 0 || i != length)2481png_chunk_benign_error(png_ptr, "bad height format");24822483else if (PNG_FP_IS_POSITIVE(state) == 0)2484png_chunk_benign_error(png_ptr, "non-positive height");24852486else2487/* This is the (only) success case. */2488png_set_sCAL_s(png_ptr, info_ptr, buffer[0],2489(png_charp)buffer+1, (png_charp)buffer+heighti);2490}2491}2492#endif24932494#ifdef PNG_READ_tIME_SUPPORTED2495void /* PRIVATE */2496png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2497{2498png_byte buf[7];2499png_time mod_time;25002501png_debug(1, "in png_handle_tIME");25022503if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2504png_chunk_error(png_ptr, "missing IHDR");25052506else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)2507{2508png_crc_finish(png_ptr, length);2509png_chunk_benign_error(png_ptr, "duplicate");2510return;2511}25122513if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2514png_ptr->mode |= PNG_AFTER_IDAT;25152516if (length != 7)2517{2518png_crc_finish(png_ptr, length);2519png_chunk_benign_error(png_ptr, "invalid");2520return;2521}25222523png_crc_read(png_ptr, buf, 7);25242525if (png_crc_finish(png_ptr, 0) != 0)2526return;25272528mod_time.second = buf[6];2529mod_time.minute = buf[5];2530mod_time.hour = buf[4];2531mod_time.day = buf[3];2532mod_time.month = buf[2];2533mod_time.year = png_get_uint_16(buf);25342535png_set_tIME(png_ptr, info_ptr, &mod_time);2536}2537#endif25382539#ifdef PNG_READ_tEXt_SUPPORTED2540/* Note: this does not properly handle chunks that are > 64K under DOS */2541void /* PRIVATE */2542png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2543{2544png_text text_info;2545png_bytep buffer;2546png_charp key;2547png_charp text;2548png_uint_32 skip = 0;25492550png_debug(1, "in png_handle_tEXt");25512552#ifdef PNG_USER_LIMITS_SUPPORTED2553if (png_ptr->user_chunk_cache_max != 0)2554{2555if (png_ptr->user_chunk_cache_max == 1)2556{2557png_crc_finish(png_ptr, length);2558return;2559}25602561if (--png_ptr->user_chunk_cache_max == 1)2562{2563png_crc_finish(png_ptr, length);2564png_chunk_benign_error(png_ptr, "no space in chunk cache");2565return;2566}2567}2568#endif25692570if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2571png_chunk_error(png_ptr, "missing IHDR");25722573if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2574png_ptr->mode |= PNG_AFTER_IDAT;25752576#ifdef PNG_MAX_MALLOC_64K2577if (length > 65535U)2578{2579png_crc_finish(png_ptr, length);2580png_chunk_benign_error(png_ptr, "too large to fit in memory");2581return;2582}2583#endif25842585buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);25862587if (buffer == NULL)2588{2589png_chunk_benign_error(png_ptr, "out of memory");2590return;2591}25922593png_crc_read(png_ptr, buffer, length);25942595if (png_crc_finish(png_ptr, skip) != 0)2596return;25972598key = (png_charp)buffer;2599key[length] = 0;26002601for (text = key; *text; text++)2602/* Empty loop to find end of key */ ;26032604if (text != key + length)2605text++;26062607text_info.compression = PNG_TEXT_COMPRESSION_NONE;2608text_info.key = key;2609text_info.lang = NULL;2610text_info.lang_key = NULL;2611text_info.itxt_length = 0;2612text_info.text = text;2613text_info.text_length = strlen(text);26142615if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)2616png_warning(png_ptr, "Insufficient memory to process text chunk");2617}2618#endif26192620#ifdef PNG_READ_zTXt_SUPPORTED2621/* Note: this does not correctly handle chunks that are > 64K under DOS */2622void /* PRIVATE */2623png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2624{2625png_const_charp errmsg = NULL;2626png_bytep buffer;2627png_uint_32 keyword_length;26282629png_debug(1, "in png_handle_zTXt");26302631#ifdef PNG_USER_LIMITS_SUPPORTED2632if (png_ptr->user_chunk_cache_max != 0)2633{2634if (png_ptr->user_chunk_cache_max == 1)2635{2636png_crc_finish(png_ptr, length);2637return;2638}26392640if (--png_ptr->user_chunk_cache_max == 1)2641{2642png_crc_finish(png_ptr, length);2643png_chunk_benign_error(png_ptr, "no space in chunk cache");2644return;2645}2646}2647#endif26482649if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2650png_chunk_error(png_ptr, "missing IHDR");26512652if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2653png_ptr->mode |= PNG_AFTER_IDAT;26542655/* Note, "length" is sufficient here; we won't be adding2656* a null terminator later.2657*/2658buffer = png_read_buffer(png_ptr, length, 2/*silent*/);26592660if (buffer == NULL)2661{2662png_crc_finish(png_ptr, length);2663png_chunk_benign_error(png_ptr, "out of memory");2664return;2665}26662667png_crc_read(png_ptr, buffer, length);26682669if (png_crc_finish(png_ptr, 0) != 0)2670return;26712672/* TODO: also check that the keyword contents match the spec! */2673for (keyword_length = 0;2674keyword_length < length && buffer[keyword_length] != 0;2675++keyword_length)2676/* Empty loop to find end of name */ ;26772678if (keyword_length > 79 || keyword_length < 1)2679errmsg = "bad keyword";26802681/* zTXt must have some LZ data after the keyword, although it may expand to2682* zero bytes; we need a '\0' at the end of the keyword, the compression type2683* then the LZ data:2684*/2685else if (keyword_length + 3 > length)2686errmsg = "truncated";26872688else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)2689errmsg = "unknown compression type";26902691else2692{2693png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;26942695/* TODO: at present png_decompress_chunk imposes a single application2696* level memory limit, this should be split to different values for iCCP2697* and text chunks.2698*/2699if (png_decompress_chunk(png_ptr, length, keyword_length+2,2700&uncompressed_length, 1/*terminate*/) == Z_STREAM_END)2701{2702png_text text;27032704if (png_ptr->read_buffer == NULL)2705errmsg="Read failure in png_handle_zTXt";2706else2707{2708/* It worked; png_ptr->read_buffer now looks like a tEXt chunk2709* except for the extra compression type byte and the fact that2710* it isn't necessarily '\0' terminated.2711*/2712buffer = png_ptr->read_buffer;2713buffer[uncompressed_length+(keyword_length+2)] = 0;27142715text.compression = PNG_TEXT_COMPRESSION_zTXt;2716text.key = (png_charp)buffer;2717text.text = (png_charp)(buffer + keyword_length+2);2718text.text_length = uncompressed_length;2719text.itxt_length = 0;2720text.lang = NULL;2721text.lang_key = NULL;27222723if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)2724errmsg = "insufficient memory";2725}2726}27272728else2729errmsg = png_ptr->zstream.msg;2730}27312732if (errmsg != NULL)2733png_chunk_benign_error(png_ptr, errmsg);2734}2735#endif27362737#ifdef PNG_READ_iTXt_SUPPORTED2738/* Note: this does not correctly handle chunks that are > 64K under DOS */2739void /* PRIVATE */2740png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)2741{2742png_const_charp errmsg = NULL;2743png_bytep buffer;2744png_uint_32 prefix_length;27452746png_debug(1, "in png_handle_iTXt");27472748#ifdef PNG_USER_LIMITS_SUPPORTED2749if (png_ptr->user_chunk_cache_max != 0)2750{2751if (png_ptr->user_chunk_cache_max == 1)2752{2753png_crc_finish(png_ptr, length);2754return;2755}27562757if (--png_ptr->user_chunk_cache_max == 1)2758{2759png_crc_finish(png_ptr, length);2760png_chunk_benign_error(png_ptr, "no space in chunk cache");2761return;2762}2763}2764#endif27652766if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)2767png_chunk_error(png_ptr, "missing IHDR");27682769if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)2770png_ptr->mode |= PNG_AFTER_IDAT;27712772buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);27732774if (buffer == NULL)2775{2776png_crc_finish(png_ptr, length);2777png_chunk_benign_error(png_ptr, "out of memory");2778return;2779}27802781png_crc_read(png_ptr, buffer, length);27822783if (png_crc_finish(png_ptr, 0) != 0)2784return;27852786/* First the keyword. */2787for (prefix_length=0;2788prefix_length < length && buffer[prefix_length] != 0;2789++prefix_length)2790/* Empty loop */ ;27912792/* Perform a basic check on the keyword length here. */2793if (prefix_length > 79 || prefix_length < 1)2794errmsg = "bad keyword";27952796/* Expect keyword, compression flag, compression type, language, translated2797* keyword (both may be empty but are 0 terminated) then the text, which may2798* be empty.2799*/2800else if (prefix_length + 5 > length)2801errmsg = "truncated";28022803else if (buffer[prefix_length+1] == 0 ||2804(buffer[prefix_length+1] == 1 &&2805buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))2806{2807int compressed = buffer[prefix_length+1] != 0;2808png_uint_32 language_offset, translated_keyword_offset;2809png_alloc_size_t uncompressed_length = 0;28102811/* Now the language tag */2812prefix_length += 3;2813language_offset = prefix_length;28142815for (; prefix_length < length && buffer[prefix_length] != 0;2816++prefix_length)2817/* Empty loop */ ;28182819/* WARNING: the length may be invalid here, this is checked below. */2820translated_keyword_offset = ++prefix_length;28212822for (; prefix_length < length && buffer[prefix_length] != 0;2823++prefix_length)2824/* Empty loop */ ;28252826/* prefix_length should now be at the trailing '\0' of the translated2827* keyword, but it may already be over the end. None of this arithmetic2828* can overflow because chunks are at most 2^31 bytes long, but on 16-bit2829* systems the available allocation may overflow.2830*/2831++prefix_length;28322833if (compressed == 0 && prefix_length <= length)2834uncompressed_length = length - prefix_length;28352836else if (compressed != 0 && prefix_length < length)2837{2838uncompressed_length = PNG_SIZE_MAX;28392840/* TODO: at present png_decompress_chunk imposes a single application2841* level memory limit, this should be split to different values for2842* iCCP and text chunks.2843*/2844if (png_decompress_chunk(png_ptr, length, prefix_length,2845&uncompressed_length, 1/*terminate*/) == Z_STREAM_END)2846buffer = png_ptr->read_buffer;28472848else2849errmsg = png_ptr->zstream.msg;2850}28512852else2853errmsg = "truncated";28542855if (errmsg == NULL)2856{2857png_text text;28582859buffer[uncompressed_length+prefix_length] = 0;28602861if (compressed == 0)2862text.compression = PNG_ITXT_COMPRESSION_NONE;28632864else2865text.compression = PNG_ITXT_COMPRESSION_zTXt;28662867text.key = (png_charp)buffer;2868text.lang = (png_charp)buffer + language_offset;2869text.lang_key = (png_charp)buffer + translated_keyword_offset;2870text.text = (png_charp)buffer + prefix_length;2871text.text_length = 0;2872text.itxt_length = uncompressed_length;28732874if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)2875errmsg = "insufficient memory";2876}2877}28782879else2880errmsg = "bad compression info";28812882if (errmsg != NULL)2883png_chunk_benign_error(png_ptr, errmsg);2884}2885#endif28862887#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED2888/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */2889static int2890png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)2891{2892png_alloc_size_t limit = PNG_SIZE_MAX;28932894if (png_ptr->unknown_chunk.data != NULL)2895{2896png_free(png_ptr, png_ptr->unknown_chunk.data);2897png_ptr->unknown_chunk.data = NULL;2898}28992900# ifdef PNG_SET_USER_LIMITS_SUPPORTED2901if (png_ptr->user_chunk_malloc_max > 0 &&2902png_ptr->user_chunk_malloc_max < limit)2903limit = png_ptr->user_chunk_malloc_max;29042905# elif PNG_USER_CHUNK_MALLOC_MAX > 02906if (PNG_USER_CHUNK_MALLOC_MAX < limit)2907limit = PNG_USER_CHUNK_MALLOC_MAX;2908# endif29092910if (length <= limit)2911{2912PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);2913/* The following is safe because of the PNG_SIZE_MAX init above */2914png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;2915/* 'mode' is a flag array, only the bottom four bits matter here */2916png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;29172918if (length == 0)2919png_ptr->unknown_chunk.data = NULL;29202921else2922{2923/* Do a 'warn' here - it is handled below. */2924png_ptr->unknown_chunk.data = png_voidcast(png_bytep,2925png_malloc_warn(png_ptr, length));2926}2927}29282929if (png_ptr->unknown_chunk.data == NULL && length > 0)2930{2931/* This is benign because we clean up correctly */2932png_crc_finish(png_ptr, length);2933png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");2934return 0;2935}29362937else2938{2939if (length > 0)2940png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);2941png_crc_finish(png_ptr, 0);2942return 1;2943}2944}2945#endif /* READ_UNKNOWN_CHUNKS */29462947/* Handle an unknown, or known but disabled, chunk */2948void /* PRIVATE */2949png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,2950png_uint_32 length, int keep)2951{2952int handled = 0; /* the chunk was handled */29532954png_debug(1, "in png_handle_unknown");29552956#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED2957/* NOTE: this code is based on the code in libpng-1.4.12 except for fixing2958* the bug which meant that setting a non-default behavior for a specific2959* chunk would be ignored (the default was always used unless a user2960* callback was installed).2961*2962* 'keep' is the value from the png_chunk_unknown_handling, the setting for2963* this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it2964* will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.2965* This is just an optimization to avoid multiple calls to the lookup2966* function.2967*/2968# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED2969# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED2970keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);2971# endif2972# endif29732974/* One of the following methods will read the chunk or skip it (at least one2975* of these is always defined because this is the only way to switch on2976* PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)2977*/2978# ifdef PNG_READ_USER_CHUNKS_SUPPORTED2979/* The user callback takes precedence over the chunk keep value, but the2980* keep value is still required to validate a save of a critical chunk.2981*/2982if (png_ptr->read_user_chunk_fn != NULL)2983{2984if (png_cache_unknown_chunk(png_ptr, length) != 0)2985{2986/* Callback to user unknown chunk handler */2987int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,2988&png_ptr->unknown_chunk);29892990/* ret is:2991* negative: An error occurred; png_chunk_error will be called.2992* zero: The chunk was not handled, the chunk will be discarded2993* unless png_set_keep_unknown_chunks has been used to set2994* a 'keep' behavior for this particular chunk, in which2995* case that will be used. A critical chunk will cause an2996* error at this point unless it is to be saved.2997* positive: The chunk was handled, libpng will ignore/discard it.2998*/2999if (ret < 0)3000png_chunk_error(png_ptr, "error in user chunk");30013002else if (ret == 0)3003{3004/* If the keep value is 'default' or 'never' override it, but3005* still error out on critical chunks unless the keep value is3006* 'always' While this is weird it is the behavior in 1.4.12.3007* A possible improvement would be to obey the value set for the3008* chunk, but this would be an API change that would probably3009* damage some applications.3010*3011* The png_app_warning below catches the case that matters, where3012* the application has not set specific save or ignore for this3013* chunk or global save or ignore.3014*/3015if (keep < PNG_HANDLE_CHUNK_IF_SAFE)3016{3017# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED3018if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)3019{3020png_chunk_warning(png_ptr, "Saving unknown chunk:");3021png_app_warning(png_ptr,3022"forcing save of an unhandled chunk;"3023" please call png_set_keep_unknown_chunks");3024/* with keep = PNG_HANDLE_CHUNK_IF_SAFE */3025}3026# endif3027keep = PNG_HANDLE_CHUNK_IF_SAFE;3028}3029}30303031else /* chunk was handled */3032{3033handled = 1;3034/* Critical chunks can be safely discarded at this point. */3035keep = PNG_HANDLE_CHUNK_NEVER;3036}3037}30383039else3040keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */3041}30423043else3044/* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */3045# endif /* READ_USER_CHUNKS */30463047# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED3048{3049/* keep is currently just the per-chunk setting, if there was no3050* setting change it to the global default now (not that this may3051* still be AS_DEFAULT) then obtain the cache of the chunk if required,3052* if not simply skip the chunk.3053*/3054if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)3055keep = png_ptr->unknown_default;30563057if (keep == PNG_HANDLE_CHUNK_ALWAYS ||3058(keep == PNG_HANDLE_CHUNK_IF_SAFE &&3059PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))3060{3061if (png_cache_unknown_chunk(png_ptr, length) == 0)3062keep = PNG_HANDLE_CHUNK_NEVER;3063}30643065else3066png_crc_finish(png_ptr, length);3067}3068# else3069# ifndef PNG_READ_USER_CHUNKS_SUPPORTED3070# error no method to support READ_UNKNOWN_CHUNKS3071# endif30723073{3074/* If here there is no read callback pointer set and no support is3075* compiled in to just save the unknown chunks, so simply skip this3076* chunk. If 'keep' is something other than AS_DEFAULT or NEVER then3077* the app has erroneously asked for unknown chunk saving when there3078* is no support.3079*/3080if (keep > PNG_HANDLE_CHUNK_NEVER)3081png_app_error(png_ptr, "no unknown chunk support available");30823083png_crc_finish(png_ptr, length);3084}3085# endif30863087# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED3088/* Now store the chunk in the chunk list if appropriate, and if the limits3089* permit it.3090*/3091if (keep == PNG_HANDLE_CHUNK_ALWAYS ||3092(keep == PNG_HANDLE_CHUNK_IF_SAFE &&3093PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))3094{3095# ifdef PNG_USER_LIMITS_SUPPORTED3096switch (png_ptr->user_chunk_cache_max)3097{3098case 2:3099png_ptr->user_chunk_cache_max = 1;3100png_chunk_benign_error(png_ptr, "no space in chunk cache");3101/* FALLTHROUGH */3102case 1:3103/* NOTE: prior to 1.6.0 this case resulted in an unknown critical3104* chunk being skipped, now there will be a hard error below.3105*/3106break;31073108default: /* not at limit */3109--(png_ptr->user_chunk_cache_max);3110/* FALLTHROUGH */3111case 0: /* no limit */3112# endif /* USER_LIMITS */3113/* Here when the limit isn't reached or when limits are compiled3114* out; store the chunk.3115*/3116png_set_unknown_chunks(png_ptr, info_ptr,3117&png_ptr->unknown_chunk, 1);3118handled = 1;3119# ifdef PNG_USER_LIMITS_SUPPORTED3120break;3121}3122# endif3123}3124# else /* no store support: the chunk must be handled by the user callback */3125PNG_UNUSED(info_ptr)3126# endif31273128/* Regardless of the error handling below the cached data (if any) can be3129* freed now. Notice that the data is not freed if there is a png_error, but3130* it will be freed by destroy_read_struct.3131*/3132if (png_ptr->unknown_chunk.data != NULL)3133png_free(png_ptr, png_ptr->unknown_chunk.data);3134png_ptr->unknown_chunk.data = NULL;31353136#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */3137/* There is no support to read an unknown chunk, so just skip it. */3138png_crc_finish(png_ptr, length);3139PNG_UNUSED(info_ptr)3140PNG_UNUSED(keep)3141#endif /* !READ_UNKNOWN_CHUNKS */31423143/* Check for unhandled critical chunks */3144if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))3145png_chunk_error(png_ptr, "unhandled critical chunk");3146}31473148/* This function is called to verify that a chunk name is valid.3149* This function can't have the "critical chunk check" incorporated3150* into it, since in the future we will need to be able to call user3151* functions to handle unknown critical chunks after we check that3152* the chunk name itself is valid.3153*/31543155/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:3156*3157* ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))3158*/31593160void /* PRIVATE */3161png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)3162{3163int i;3164png_uint_32 cn=chunk_name;31653166png_debug(1, "in png_check_chunk_name");31673168for (i=1; i<=4; ++i)3169{3170int c = cn & 0xff;31713172if (c < 65 || c > 122 || (c > 90 && c < 97))3173png_chunk_error(png_ptr, "invalid chunk type");31743175cn >>= 8;3176}3177}31783179void /* PRIVATE */3180png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)3181{3182png_alloc_size_t limit = PNG_UINT_31_MAX;31833184# ifdef PNG_SET_USER_LIMITS_SUPPORTED3185if (png_ptr->user_chunk_malloc_max > 0 &&3186png_ptr->user_chunk_malloc_max < limit)3187limit = png_ptr->user_chunk_malloc_max;3188# elif PNG_USER_CHUNK_MALLOC_MAX > 03189if (PNG_USER_CHUNK_MALLOC_MAX < limit)3190limit = PNG_USER_CHUNK_MALLOC_MAX;3191# endif3192if (png_ptr->chunk_name == png_IDAT)3193{3194png_alloc_size_t idat_limit = PNG_UINT_31_MAX;3195size_t row_factor =3196(size_t)png_ptr->width3197* (size_t)png_ptr->channels3198* (png_ptr->bit_depth > 8? 2: 1)3199+ 13200+ (png_ptr->interlaced? 6: 0);3201if (png_ptr->height > PNG_UINT_32_MAX/row_factor)3202idat_limit = PNG_UINT_31_MAX;3203else3204idat_limit = png_ptr->height * row_factor;3205row_factor = row_factor > 32566? 32566 : row_factor;3206idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */3207idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;3208limit = limit < idat_limit? idat_limit : limit;3209}32103211if (length > limit)3212{3213png_debug2(0," length = %lu, limit = %lu",3214(unsigned long)length,(unsigned long)limit);3215png_chunk_error(png_ptr, "chunk data is too large");3216}3217}32183219/* Combines the row recently read in with the existing pixels in the row. This3220* routine takes care of alpha and transparency if requested. This routine also3221* handles the two methods of progressive display of interlaced images,3222* depending on the 'display' value; if 'display' is true then the whole row3223* (dp) is filled from the start by replicating the available pixels. If3224* 'display' is false only those pixels present in the pass are filled in.3225*/3226void /* PRIVATE */3227png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)3228{3229unsigned int pixel_depth = png_ptr->transformed_pixel_depth;3230png_const_bytep sp = png_ptr->row_buf + 1;3231png_alloc_size_t row_width = png_ptr->width;3232unsigned int pass = png_ptr->pass;3233png_bytep end_ptr = 0;3234png_byte end_byte = 0;3235unsigned int end_mask;32363237png_debug(1, "in png_combine_row");32383239/* Added in 1.5.6: it should not be possible to enter this routine until at3240* least one row has been read from the PNG data and transformed.3241*/3242if (pixel_depth == 0)3243png_error(png_ptr, "internal row logic error");32443245/* Added in 1.5.4: the pixel depth should match the information returned by3246* any call to png_read_update_info at this point. Do not continue if we got3247* this wrong.3248*/3249if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=3250PNG_ROWBYTES(pixel_depth, row_width))3251png_error(png_ptr, "internal row size calculation error");32523253/* Don't expect this to ever happen: */3254if (row_width == 0)3255png_error(png_ptr, "internal row width error");32563257/* Preserve the last byte in cases where only part of it will be overwritten,3258* the multiply below may overflow, we don't care because ANSI-C guarantees3259* we get the low bits.3260*/3261end_mask = (pixel_depth * row_width) & 7;3262if (end_mask != 0)3263{3264/* end_ptr == NULL is a flag to say do nothing */3265end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;3266end_byte = *end_ptr;3267# ifdef PNG_READ_PACKSWAP_SUPPORTED3268if ((png_ptr->transformations & PNG_PACKSWAP) != 0)3269/* little-endian byte */3270end_mask = (unsigned int)(0xff << end_mask);32713272else /* big-endian byte */3273# endif3274end_mask = 0xff >> end_mask;3275/* end_mask is now the bits to *keep* from the destination row */3276}32773278/* For non-interlaced images this reduces to a memcpy(). A memcpy()3279* will also happen if interlacing isn't supported or if the application3280* does not call png_set_interlace_handling(). In the latter cases the3281* caller just gets a sequence of the unexpanded rows from each interlace3282* pass.3283*/3284#ifdef PNG_READ_INTERLACING_SUPPORTED3285if (png_ptr->interlaced != 0 &&3286(png_ptr->transformations & PNG_INTERLACE) != 0 &&3287pass < 6 && (display == 0 ||3288/* The following copies everything for 'display' on passes 0, 2 and 4. */3289(display == 1 && (pass & 1) != 0)))3290{3291/* Narrow images may have no bits in a pass; the caller should handle3292* this, but this test is cheap:3293*/3294if (row_width <= PNG_PASS_START_COL(pass))3295return;32963297if (pixel_depth < 8)3298{3299/* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit3300* into 32 bits, then a single loop over the bytes using the four byte3301* values in the 32-bit mask can be used. For the 'display' option the3302* expanded mask may also not require any masking within a byte. To3303* make this work the PACKSWAP option must be taken into account - it3304* simply requires the pixels to be reversed in each byte.3305*3306* The 'regular' case requires a mask for each of the first 6 passes,3307* the 'display' case does a copy for the even passes in the range3308* 0..6. This has already been handled in the test above.3309*3310* The masks are arranged as four bytes with the first byte to use in3311* the lowest bits (little-endian) regardless of the order (PACKSWAP or3312* not) of the pixels in each byte.3313*3314* NOTE: the whole of this logic depends on the caller of this function3315* only calling it on rows appropriate to the pass. This function only3316* understands the 'x' logic; the 'y' logic is handled by the caller.3317*3318* The following defines allow generation of compile time constant bit3319* masks for each pixel depth and each possibility of swapped or not3320* swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,3321* is in the range 0..7; and the result is 1 if the pixel is to be3322* copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'3323* for the block method.3324*3325* With some compilers a compile time expression of the general form:3326*3327* (shift >= 32) ? (a >> (shift-32)) : (b >> shift)3328*3329* Produces warnings with values of 'shift' in the range 33 to 633330* because the right hand side of the ?: expression is evaluated by3331* the compiler even though it isn't used. Microsoft Visual C (various3332* versions) and the Intel C compiler are known to do this. To avoid3333* this the following macros are used in 1.5.6. This is a temporary3334* solution to avoid destabilizing the code during the release process.3335*/3336# if PNG_USE_COMPILE_TIME_MASKS3337# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))3338# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))3339# else3340# define PNG_LSR(x,s) ((x)>>(s))3341# define PNG_LSL(x,s) ((x)<<(s))3342# endif3343# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\3344PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)3345# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\3346PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)33473348/* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is3349* little endian - the first pixel is at bit 0 - however the extra3350* parameter 's' can be set to cause the mask position to be swapped3351* within each byte, to match the PNG format. This is done by XOR of3352* the shift with 7, 6 or 4 for bit depths 1, 2 and 4.3353*/3354# define PIXEL_MASK(p,x,d,s) \3355(PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))33563357/* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.3358*/3359# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)3360# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)33613362/* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp3363* cases the result needs replicating, for the 4-bpp case the above3364* generates a full 32 bits.3365*/3366# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))33673368# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\3369S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\3370S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)33713372# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\3373B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\3374B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)33753376#if PNG_USE_COMPILE_TIME_MASKS3377/* Utility macros to construct all the masks for a depth/swap3378* combination. The 's' parameter says whether the format is PNG3379* (big endian bytes) or not. Only the three odd-numbered passes are3380* required for the display/block algorithm.3381*/3382# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\3383S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }33843385# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }33863387# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))33883389/* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and3390* then pass:3391*/3392static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =3393{3394/* Little-endian byte masks for PACKSWAP */3395{ S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },3396/* Normal (big-endian byte) masks - PNG format */3397{ S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }3398};33993400/* display_mask has only three entries for the odd passes, so index by3401* pass>>1.3402*/3403static const png_uint_32 display_mask[2][3][3] =3404{3405/* Little-endian byte masks for PACKSWAP */3406{ B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },3407/* Normal (big-endian byte) masks - PNG format */3408{ B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }3409};34103411# define MASK(pass,depth,display,png)\3412((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\3413row_mask[png][DEPTH_INDEX(depth)][pass])34143415#else /* !PNG_USE_COMPILE_TIME_MASKS */3416/* This is the runtime alternative: it seems unlikely that this will3417* ever be either smaller or faster than the compile time approach.3418*/3419# define MASK(pass,depth,display,png)\3420((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))3421#endif /* !USE_COMPILE_TIME_MASKS */34223423/* Use the appropriate mask to copy the required bits. In some cases3424* the byte mask will be 0 or 0xff; optimize these cases. row_width is3425* the number of pixels, but the code copies bytes, so it is necessary3426* to special case the end.3427*/3428png_uint_32 pixels_per_byte = 8 / pixel_depth;3429png_uint_32 mask;34303431# ifdef PNG_READ_PACKSWAP_SUPPORTED3432if ((png_ptr->transformations & PNG_PACKSWAP) != 0)3433mask = MASK(pass, pixel_depth, display, 0);34343435else3436# endif3437mask = MASK(pass, pixel_depth, display, 1);34383439for (;;)3440{3441png_uint_32 m;34423443/* It doesn't matter in the following if png_uint_32 has more than3444* 32 bits because the high bits always match those in m<<24; it is,3445* however, essential to use OR here, not +, because of this.3446*/3447m = mask;3448mask = (m >> 8) | (m << 24); /* rotate right to good compilers */3449m &= 0xff;34503451if (m != 0) /* something to copy */3452{3453if (m != 0xff)3454*dp = (png_byte)((*dp & ~m) | (*sp & m));3455else3456*dp = *sp;3457}34583459/* NOTE: this may overwrite the last byte with garbage if the image3460* is not an exact number of bytes wide; libpng has always done3461* this.3462*/3463if (row_width <= pixels_per_byte)3464break; /* May need to restore part of the last byte */34653466row_width -= pixels_per_byte;3467++dp;3468++sp;3469}3470}34713472else /* pixel_depth >= 8 */3473{3474unsigned int bytes_to_copy, bytes_to_jump;34753476/* Validate the depth - it must be a multiple of 8 */3477if (pixel_depth & 7)3478png_error(png_ptr, "invalid user transform pixel depth");34793480pixel_depth >>= 3; /* now in bytes */3481row_width *= pixel_depth;34823483/* Regardless of pass number the Adam 7 interlace always results in a3484* fixed number of pixels to copy then to skip. There may be a3485* different number of pixels to skip at the start though.3486*/3487{3488unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;34893490row_width -= offset;3491dp += offset;3492sp += offset;3493}34943495/* Work out the bytes to copy. */3496if (display != 0)3497{3498/* When doing the 'block' algorithm the pixel in the pass gets3499* replicated to adjacent pixels. This is why the even (0,2,4,6)3500* passes are skipped above - the entire expanded row is copied.3501*/3502bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;35033504/* But don't allow this number to exceed the actual row width. */3505if (bytes_to_copy > row_width)3506bytes_to_copy = (unsigned int)/*SAFE*/row_width;3507}35083509else /* normal row; Adam7 only ever gives us one pixel to copy. */3510bytes_to_copy = pixel_depth;35113512/* In Adam7 there is a constant offset between where the pixels go. */3513bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;35143515/* And simply copy these bytes. Some optimization is possible here,3516* depending on the value of 'bytes_to_copy'. Special case the low3517* byte counts, which we know to be frequent.3518*3519* Notice that these cases all 'return' rather than 'break' - this3520* avoids an unnecessary test on whether to restore the last byte3521* below.3522*/3523switch (bytes_to_copy)3524{3525case 1:3526for (;;)3527{3528*dp = *sp;35293530if (row_width <= bytes_to_jump)3531return;35323533dp += bytes_to_jump;3534sp += bytes_to_jump;3535row_width -= bytes_to_jump;3536}35373538case 2:3539/* There is a possibility of a partial copy at the end here; this3540* slows the code down somewhat.3541*/3542do3543{3544dp[0] = sp[0]; dp[1] = sp[1];35453546if (row_width <= bytes_to_jump)3547return;35483549sp += bytes_to_jump;3550dp += bytes_to_jump;3551row_width -= bytes_to_jump;3552}3553while (row_width > 1);35543555/* And there can only be one byte left at this point: */3556*dp = *sp;3557return;35583559case 3:3560/* This can only be the RGB case, so each copy is exactly one3561* pixel and it is not necessary to check for a partial copy.3562*/3563for (;;)3564{3565dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];35663567if (row_width <= bytes_to_jump)3568return;35693570sp += bytes_to_jump;3571dp += bytes_to_jump;3572row_width -= bytes_to_jump;3573}35743575default:3576#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE3577/* Check for double byte alignment and, if possible, use a3578* 16-bit copy. Don't attempt this for narrow images - ones that3579* are less than an interlace panel wide. Don't attempt it for3580* wide bytes_to_copy either - use the memcpy there.3581*/3582if (bytes_to_copy < 16 /*else use memcpy*/ &&3583png_isaligned(dp, png_uint_16) &&3584png_isaligned(sp, png_uint_16) &&3585bytes_to_copy % (sizeof (png_uint_16)) == 0 &&3586bytes_to_jump % (sizeof (png_uint_16)) == 0)3587{3588/* Everything is aligned for png_uint_16 copies, but try for3589* png_uint_32 first.3590*/3591if (png_isaligned(dp, png_uint_32) &&3592png_isaligned(sp, png_uint_32) &&3593bytes_to_copy % (sizeof (png_uint_32)) == 0 &&3594bytes_to_jump % (sizeof (png_uint_32)) == 0)3595{3596png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);3597png_const_uint_32p sp32 = png_aligncastconst(3598png_const_uint_32p, sp);3599size_t skip = (bytes_to_jump-bytes_to_copy) /3600(sizeof (png_uint_32));36013602do3603{3604size_t c = bytes_to_copy;3605do3606{3607*dp32++ = *sp32++;3608c -= (sizeof (png_uint_32));3609}3610while (c > 0);36113612if (row_width <= bytes_to_jump)3613return;36143615dp32 += skip;3616sp32 += skip;3617row_width -= bytes_to_jump;3618}3619while (bytes_to_copy <= row_width);36203621/* Get to here when the row_width truncates the final copy.3622* There will be 1-3 bytes left to copy, so don't try the3623* 16-bit loop below.3624*/3625dp = (png_bytep)dp32;3626sp = (png_const_bytep)sp32;3627do3628*dp++ = *sp++;3629while (--row_width > 0);3630return;3631}36323633/* Else do it in 16-bit quantities, but only if the size is3634* not too large.3635*/3636else3637{3638png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);3639png_const_uint_16p sp16 = png_aligncastconst(3640png_const_uint_16p, sp);3641size_t skip = (bytes_to_jump-bytes_to_copy) /3642(sizeof (png_uint_16));36433644do3645{3646size_t c = bytes_to_copy;3647do3648{3649*dp16++ = *sp16++;3650c -= (sizeof (png_uint_16));3651}3652while (c > 0);36533654if (row_width <= bytes_to_jump)3655return;36563657dp16 += skip;3658sp16 += skip;3659row_width -= bytes_to_jump;3660}3661while (bytes_to_copy <= row_width);36623663/* End of row - 1 byte left, bytes_to_copy > row_width: */3664dp = (png_bytep)dp16;3665sp = (png_const_bytep)sp16;3666do3667*dp++ = *sp++;3668while (--row_width > 0);3669return;3670}3671}3672#endif /* ALIGN_TYPE code */36733674/* The true default - use a memcpy: */3675for (;;)3676{3677memcpy(dp, sp, bytes_to_copy);36783679if (row_width <= bytes_to_jump)3680return;36813682sp += bytes_to_jump;3683dp += bytes_to_jump;3684row_width -= bytes_to_jump;3685if (bytes_to_copy > row_width)3686bytes_to_copy = (unsigned int)/*SAFE*/row_width;3687}3688}36893690/* NOT REACHED*/3691} /* pixel_depth >= 8 */36923693/* Here if pixel_depth < 8 to check 'end_ptr' below. */3694}3695else3696#endif /* READ_INTERLACING */36973698/* If here then the switch above wasn't used so just memcpy the whole row3699* from the temporary row buffer (notice that this overwrites the end of the3700* destination row if it is a partial byte.)3701*/3702memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));37033704/* Restore the overwritten bits from the last byte if necessary. */3705if (end_ptr != NULL)3706*end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));3707}37083709#ifdef PNG_READ_INTERLACING_SUPPORTED3710void /* PRIVATE */3711png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,3712png_uint_32 transformations /* Because these may affect the byte layout */)3713{3714/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */3715/* Offset to next interlace block */3716static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};37173718png_debug(1, "in png_do_read_interlace");3719if (row != NULL && row_info != NULL)3720{3721png_uint_32 final_width;37223723final_width = row_info->width * png_pass_inc[pass];37243725switch (row_info->pixel_depth)3726{3727case 1:3728{3729png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);3730png_bytep dp = row + (size_t)((final_width - 1) >> 3);3731unsigned int sshift, dshift;3732unsigned int s_start, s_end;3733int s_inc;3734int jstop = (int)png_pass_inc[pass];3735png_byte v;3736png_uint_32 i;3737int j;37383739#ifdef PNG_READ_PACKSWAP_SUPPORTED3740if ((transformations & PNG_PACKSWAP) != 0)3741{3742sshift = ((row_info->width + 7) & 0x07);3743dshift = ((final_width + 7) & 0x07);3744s_start = 7;3745s_end = 0;3746s_inc = -1;3747}37483749else3750#endif3751{3752sshift = 7 - ((row_info->width + 7) & 0x07);3753dshift = 7 - ((final_width + 7) & 0x07);3754s_start = 0;3755s_end = 7;3756s_inc = 1;3757}37583759for (i = 0; i < row_info->width; i++)3760{3761v = (png_byte)((*sp >> sshift) & 0x01);3762for (j = 0; j < jstop; j++)3763{3764unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));3765tmp |= (unsigned int)(v << dshift);3766*dp = (png_byte)(tmp & 0xff);37673768if (dshift == s_end)3769{3770dshift = s_start;3771dp--;3772}37733774else3775dshift = (unsigned int)((int)dshift + s_inc);3776}37773778if (sshift == s_end)3779{3780sshift = s_start;3781sp--;3782}37833784else3785sshift = (unsigned int)((int)sshift + s_inc);3786}3787break;3788}37893790case 2:3791{3792png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);3793png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);3794unsigned int sshift, dshift;3795unsigned int s_start, s_end;3796int s_inc;3797int jstop = (int)png_pass_inc[pass];3798png_uint_32 i;37993800#ifdef PNG_READ_PACKSWAP_SUPPORTED3801if ((transformations & PNG_PACKSWAP) != 0)3802{3803sshift = (((row_info->width + 3) & 0x03) << 1);3804dshift = (((final_width + 3) & 0x03) << 1);3805s_start = 6;3806s_end = 0;3807s_inc = -2;3808}38093810else3811#endif3812{3813sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);3814dshift = ((3 - ((final_width + 3) & 0x03)) << 1);3815s_start = 0;3816s_end = 6;3817s_inc = 2;3818}38193820for (i = 0; i < row_info->width; i++)3821{3822png_byte v;3823int j;38243825v = (png_byte)((*sp >> sshift) & 0x03);3826for (j = 0; j < jstop; j++)3827{3828unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));3829tmp |= (unsigned int)(v << dshift);3830*dp = (png_byte)(tmp & 0xff);38313832if (dshift == s_end)3833{3834dshift = s_start;3835dp--;3836}38373838else3839dshift = (unsigned int)((int)dshift + s_inc);3840}38413842if (sshift == s_end)3843{3844sshift = s_start;3845sp--;3846}38473848else3849sshift = (unsigned int)((int)sshift + s_inc);3850}3851break;3852}38533854case 4:3855{3856png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);3857png_bytep dp = row + (size_t)((final_width - 1) >> 1);3858unsigned int sshift, dshift;3859unsigned int s_start, s_end;3860int s_inc;3861png_uint_32 i;3862int jstop = (int)png_pass_inc[pass];38633864#ifdef PNG_READ_PACKSWAP_SUPPORTED3865if ((transformations & PNG_PACKSWAP) != 0)3866{3867sshift = (((row_info->width + 1) & 0x01) << 2);3868dshift = (((final_width + 1) & 0x01) << 2);3869s_start = 4;3870s_end = 0;3871s_inc = -4;3872}38733874else3875#endif3876{3877sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);3878dshift = ((1 - ((final_width + 1) & 0x01)) << 2);3879s_start = 0;3880s_end = 4;3881s_inc = 4;3882}38833884for (i = 0; i < row_info->width; i++)3885{3886png_byte v = (png_byte)((*sp >> sshift) & 0x0f);3887int j;38883889for (j = 0; j < jstop; j++)3890{3891unsigned int tmp = *dp & (0xf0f >> (4 - dshift));3892tmp |= (unsigned int)(v << dshift);3893*dp = (png_byte)(tmp & 0xff);38943895if (dshift == s_end)3896{3897dshift = s_start;3898dp--;3899}39003901else3902dshift = (unsigned int)((int)dshift + s_inc);3903}39043905if (sshift == s_end)3906{3907sshift = s_start;3908sp--;3909}39103911else3912sshift = (unsigned int)((int)sshift + s_inc);3913}3914break;3915}39163917default:3918{3919size_t pixel_bytes = (row_info->pixel_depth >> 3);39203921png_bytep sp = row + (size_t)(row_info->width - 1)3922* pixel_bytes;39233924png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;39253926int jstop = (int)png_pass_inc[pass];3927png_uint_32 i;39283929for (i = 0; i < row_info->width; i++)3930{3931png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */3932int j;39333934memcpy(v, sp, pixel_bytes);39353936for (j = 0; j < jstop; j++)3937{3938memcpy(dp, v, pixel_bytes);3939dp -= pixel_bytes;3940}39413942sp -= pixel_bytes;3943}3944break;3945}3946}39473948row_info->width = final_width;3949row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);3950}3951#ifndef PNG_READ_PACKSWAP_SUPPORTED3952PNG_UNUSED(transformations) /* Silence compiler warning */3953#endif3954}3955#endif /* READ_INTERLACING */39563957static void3958png_read_filter_row_sub(png_row_infop row_info, png_bytep row,3959png_const_bytep prev_row)3960{3961size_t i;3962size_t istop = row_info->rowbytes;3963unsigned int bpp = (row_info->pixel_depth + 7) >> 3;3964png_bytep rp = row + bpp;39653966PNG_UNUSED(prev_row)39673968for (i = bpp; i < istop; i++)3969{3970*rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);3971rp++;3972}3973}39743975static void3976png_read_filter_row_up(png_row_infop row_info, png_bytep row,3977png_const_bytep prev_row)3978{3979size_t i;3980size_t istop = row_info->rowbytes;3981png_bytep rp = row;3982png_const_bytep pp = prev_row;39833984for (i = 0; i < istop; i++)3985{3986*rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);3987rp++;3988}3989}39903991static void3992png_read_filter_row_avg(png_row_infop row_info, png_bytep row,3993png_const_bytep prev_row)3994{3995size_t i;3996png_bytep rp = row;3997png_const_bytep pp = prev_row;3998unsigned int bpp = (row_info->pixel_depth + 7) >> 3;3999size_t istop = row_info->rowbytes - bpp;40004001for (i = 0; i < bpp; i++)4002{4003*rp = (png_byte)(((int)(*rp) +4004((int)(*pp++) / 2 )) & 0xff);40054006rp++;4007}40084009for (i = 0; i < istop; i++)4010{4011*rp = (png_byte)(((int)(*rp) +4012(int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);40134014rp++;4015}4016}40174018static void4019png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,4020png_const_bytep prev_row)4021{4022png_bytep rp_end = row + row_info->rowbytes;4023int a, c;40244025/* First pixel/byte */4026c = *prev_row++;4027a = *row + c;4028*row++ = (png_byte)a;40294030/* Remainder */4031while (row < rp_end)4032{4033int b, pa, pb, pc, p;40344035a &= 0xff; /* From previous iteration or start */4036b = *prev_row++;40374038p = b - c;4039pc = a - c;40404041#ifdef PNG_USE_ABS4042pa = abs(p);4043pb = abs(pc);4044pc = abs(p + pc);4045#else4046pa = p < 0 ? -p : p;4047pb = pc < 0 ? -pc : pc;4048pc = (p + pc) < 0 ? -(p + pc) : p + pc;4049#endif40504051/* Find the best predictor, the least of pa, pb, pc favoring the earlier4052* ones in the case of a tie.4053*/4054if (pb < pa)4055{4056pa = pb; a = b;4057}4058if (pc < pa) a = c;40594060/* Calculate the current pixel in a, and move the previous row pixel to c4061* for the next time round the loop4062*/4063c = b;4064a += *row;4065*row++ = (png_byte)a;4066}4067}40684069static void4070png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,4071png_const_bytep prev_row)4072{4073unsigned int bpp = (row_info->pixel_depth + 7) >> 3;4074png_bytep rp_end = row + bpp;40754076/* Process the first pixel in the row completely (this is the same as 'up'4077* because there is only one candidate predictor for the first row).4078*/4079while (row < rp_end)4080{4081int a = *row + *prev_row++;4082*row++ = (png_byte)a;4083}40844085/* Remainder */4086rp_end = rp_end + (row_info->rowbytes - bpp);40874088while (row < rp_end)4089{4090int a, b, c, pa, pb, pc, p;40914092c = *(prev_row - bpp);4093a = *(row - bpp);4094b = *prev_row++;40954096p = b - c;4097pc = a - c;40984099#ifdef PNG_USE_ABS4100pa = abs(p);4101pb = abs(pc);4102pc = abs(p + pc);4103#else4104pa = p < 0 ? -p : p;4105pb = pc < 0 ? -pc : pc;4106pc = (p + pc) < 0 ? -(p + pc) : p + pc;4107#endif41084109if (pb < pa)4110{4111pa = pb; a = b;4112}4113if (pc < pa) a = c;41144115a += *row;4116*row++ = (png_byte)a;4117}4118}41194120static void4121png_init_filter_functions(png_structrp pp)4122/* This function is called once for every PNG image (except for PNG images4123* that only use PNG_FILTER_VALUE_NONE for all rows) to set the4124* implementations required to reverse the filtering of PNG rows. Reversing4125* the filter is the first transformation performed on the row data. It is4126* performed in place, therefore an implementation can be selected based on4127* the image pixel format. If the implementation depends on image width then4128* take care to ensure that it works correctly if the image is interlaced -4129* interlacing causes the actual row width to vary.4130*/4131{4132unsigned int bpp = (pp->pixel_depth + 7) >> 3;41334134pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;4135pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;4136pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;4137if (bpp == 1)4138pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =4139png_read_filter_row_paeth_1byte_pixel;4140else4141pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =4142png_read_filter_row_paeth_multibyte_pixel;41434144#ifdef PNG_FILTER_OPTIMIZATIONS4145/* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to4146* call to install hardware optimizations for the above functions; simply4147* replace whatever elements of the pp->read_filter[] array with a hardware4148* specific (or, for that matter, generic) optimization.4149*4150* To see an example of this examine what configure.ac does when4151* --enable-arm-neon is specified on the command line.4152*/4153PNG_FILTER_OPTIMIZATIONS(pp, bpp);4154#endif4155}41564157void /* PRIVATE */4158png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,4159png_const_bytep prev_row, int filter)4160{4161/* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define4162* PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic4163* implementations. See png_init_filter_functions above.4164*/4165if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)4166{4167if (pp->read_filter[0] == NULL)4168png_init_filter_functions(pp);41694170pp->read_filter[filter-1](row_info, row, prev_row);4171}4172}41734174#ifdef PNG_SEQUENTIAL_READ_SUPPORTED4175void /* PRIVATE */4176png_read_IDAT_data(png_structrp png_ptr, png_bytep output,4177png_alloc_size_t avail_out)4178{4179/* Loop reading IDATs and decompressing the result into output[avail_out] */4180png_ptr->zstream.next_out = output;4181png_ptr->zstream.avail_out = 0; /* safety: set below */41824183if (output == NULL)4184avail_out = 0;41854186do4187{4188int ret;4189png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];41904191if (png_ptr->zstream.avail_in == 0)4192{4193uInt avail_in;4194png_bytep buffer;41954196while (png_ptr->idat_size == 0)4197{4198png_crc_finish(png_ptr, 0);41994200png_ptr->idat_size = png_read_chunk_header(png_ptr);4201/* This is an error even in the 'check' case because the code just4202* consumed a non-IDAT header.4203*/4204if (png_ptr->chunk_name != png_IDAT)4205png_error(png_ptr, "Not enough image data");4206}42074208avail_in = png_ptr->IDAT_read_size;42094210if (avail_in > png_ptr->idat_size)4211avail_in = (uInt)png_ptr->idat_size;42124213/* A PNG with a gradually increasing IDAT size will defeat this attempt4214* to minimize memory usage by causing lots of re-allocs, but4215* realistically doing IDAT_read_size re-allocs is not likely to be a4216* big problem.4217*/4218buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);42194220png_crc_read(png_ptr, buffer, avail_in);4221png_ptr->idat_size -= avail_in;42224223png_ptr->zstream.next_in = buffer;4224png_ptr->zstream.avail_in = avail_in;4225}42264227/* And set up the output side. */4228if (output != NULL) /* standard read */4229{4230uInt out = ZLIB_IO_MAX;42314232if (out > avail_out)4233out = (uInt)avail_out;42344235avail_out -= out;4236png_ptr->zstream.avail_out = out;4237}42384239else /* after last row, checking for end */4240{4241png_ptr->zstream.next_out = tmpbuf;4242png_ptr->zstream.avail_out = (sizeof tmpbuf);4243}42444245/* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the4246* process. If the LZ stream is truncated the sequential reader will4247* terminally damage the stream, above, by reading the chunk header of the4248* following chunk (it then exits with png_error).4249*4250* TODO: deal more elegantly with truncated IDAT lists.4251*/4252ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);42534254/* Take the unconsumed output back. */4255if (output != NULL)4256avail_out += png_ptr->zstream.avail_out;42574258else /* avail_out counts the extra bytes */4259avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;42604261png_ptr->zstream.avail_out = 0;42624263if (ret == Z_STREAM_END)4264{4265/* Do this for safety; we won't read any more into this row. */4266png_ptr->zstream.next_out = NULL;42674268png_ptr->mode |= PNG_AFTER_IDAT;4269png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;42704271if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)4272png_chunk_benign_error(png_ptr, "Extra compressed data");4273break;4274}42754276if (ret != Z_OK)4277{4278png_zstream_error(png_ptr, ret);42794280if (output != NULL)4281png_chunk_error(png_ptr, png_ptr->zstream.msg);42824283else /* checking */4284{4285png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);4286return;4287}4288}4289} while (avail_out > 0);42904291if (avail_out > 0)4292{4293/* The stream ended before the image; this is the same as too few IDATs so4294* should be handled the same way.4295*/4296if (output != NULL)4297png_error(png_ptr, "Not enough image data");42984299else /* the deflate stream contained extra data */4300png_chunk_benign_error(png_ptr, "Too much image data");4301}4302}43034304void /* PRIVATE */4305png_read_finish_IDAT(png_structrp png_ptr)4306{4307/* We don't need any more data and the stream should have ended, however the4308* LZ end code may actually not have been processed. In this case we must4309* read it otherwise stray unread IDAT data or, more likely, an IDAT chunk4310* may still remain to be consumed.4311*/4312if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)4313{4314/* The NULL causes png_read_IDAT_data to swallow any remaining bytes in4315* the compressed stream, but the stream may be damaged too, so even after4316* this call we may need to terminate the zstream ownership.4317*/4318png_read_IDAT_data(png_ptr, NULL, 0);4319png_ptr->zstream.next_out = NULL; /* safety */43204321/* Now clear everything out for safety; the following may not have been4322* done.4323*/4324if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)4325{4326png_ptr->mode |= PNG_AFTER_IDAT;4327png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;4328}4329}43304331/* If the zstream has not been released do it now *and* terminate the reading4332* of the final IDAT chunk.4333*/4334if (png_ptr->zowner == png_IDAT)4335{4336/* Always do this; the pointers otherwise point into the read buffer. */4337png_ptr->zstream.next_in = NULL;4338png_ptr->zstream.avail_in = 0;43394340/* Now we no longer own the zstream. */4341png_ptr->zowner = 0;43424343/* The slightly weird semantics of the sequential IDAT reading is that we4344* are always in or at the end of an IDAT chunk, so we always need to do a4345* crc_finish here. If idat_size is non-zero we also need to read the4346* spurious bytes at the end of the chunk now.4347*/4348(void)png_crc_finish(png_ptr, png_ptr->idat_size);4349}4350}43514352void /* PRIVATE */4353png_read_finish_row(png_structrp png_ptr)4354{4355/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */43564357/* Start of interlace block */4358static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};43594360/* Offset to next interlace block */4361static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};43624363/* Start of interlace block in the y direction */4364static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};43654366/* Offset to next interlace block in the y direction */4367static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};43684369png_debug(1, "in png_read_finish_row");4370png_ptr->row_number++;4371if (png_ptr->row_number < png_ptr->num_rows)4372return;43734374if (png_ptr->interlaced != 0)4375{4376png_ptr->row_number = 0;43774378/* TO DO: don't do this if prev_row isn't needed (requires4379* read-ahead of the next row's filter byte.4380*/4381memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);43824383do4384{4385png_ptr->pass++;43864387if (png_ptr->pass >= 7)4388break;43894390png_ptr->iwidth = (png_ptr->width +4391png_pass_inc[png_ptr->pass] - 1 -4392png_pass_start[png_ptr->pass]) /4393png_pass_inc[png_ptr->pass];43944395if ((png_ptr->transformations & PNG_INTERLACE) == 0)4396{4397png_ptr->num_rows = (png_ptr->height +4398png_pass_yinc[png_ptr->pass] - 1 -4399png_pass_ystart[png_ptr->pass]) /4400png_pass_yinc[png_ptr->pass];4401}44024403else /* if (png_ptr->transformations & PNG_INTERLACE) */4404break; /* libpng deinterlacing sees every row */44054406} while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);44074408if (png_ptr->pass < 7)4409return;4410}44114412/* Here after at the end of the last row of the last pass. */4413png_read_finish_IDAT(png_ptr);4414}4415#endif /* SEQUENTIAL_READ */44164417void /* PRIVATE */4418png_read_start_row(png_structrp png_ptr)4419{4420/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */44214422/* Start of interlace block */4423static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};44244425/* Offset to next interlace block */4426static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};44274428/* Start of interlace block in the y direction */4429static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};44304431/* Offset to next interlace block in the y direction */4432static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};44334434unsigned int max_pixel_depth;4435size_t row_bytes;44364437png_debug(1, "in png_read_start_row");44384439#ifdef PNG_READ_TRANSFORMS_SUPPORTED4440png_init_read_transformations(png_ptr);4441#endif4442if (png_ptr->interlaced != 0)4443{4444if ((png_ptr->transformations & PNG_INTERLACE) == 0)4445png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -4446png_pass_ystart[0]) / png_pass_yinc[0];44474448else4449png_ptr->num_rows = png_ptr->height;44504451png_ptr->iwidth = (png_ptr->width +4452png_pass_inc[png_ptr->pass] - 1 -4453png_pass_start[png_ptr->pass]) /4454png_pass_inc[png_ptr->pass];4455}44564457else4458{4459png_ptr->num_rows = png_ptr->height;4460png_ptr->iwidth = png_ptr->width;4461}44624463max_pixel_depth = (unsigned int)png_ptr->pixel_depth;44644465/* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of4466* calculations to calculate the final pixel depth, then4467* png_do_read_transforms actually does the transforms. This means that the4468* code which effectively calculates this value is actually repeated in three4469* separate places. They must all match. Innocent changes to the order of4470* transformations can and will break libpng in a way that causes memory4471* overwrites.4472*4473* TODO: fix this.4474*/4475#ifdef PNG_READ_PACK_SUPPORTED4476if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)4477max_pixel_depth = 8;4478#endif44794480#ifdef PNG_READ_EXPAND_SUPPORTED4481if ((png_ptr->transformations & PNG_EXPAND) != 0)4482{4483if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)4484{4485if (png_ptr->num_trans != 0)4486max_pixel_depth = 32;44874488else4489max_pixel_depth = 24;4490}44914492else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)4493{4494if (max_pixel_depth < 8)4495max_pixel_depth = 8;44964497if (png_ptr->num_trans != 0)4498max_pixel_depth *= 2;4499}45004501else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)4502{4503if (png_ptr->num_trans != 0)4504{4505max_pixel_depth *= 4;4506max_pixel_depth /= 3;4507}4508}4509}4510#endif45114512#ifdef PNG_READ_EXPAND_16_SUPPORTED4513if ((png_ptr->transformations & PNG_EXPAND_16) != 0)4514{4515# ifdef PNG_READ_EXPAND_SUPPORTED4516/* In fact it is an error if it isn't supported, but checking is4517* the safe way.4518*/4519if ((png_ptr->transformations & PNG_EXPAND) != 0)4520{4521if (png_ptr->bit_depth < 16)4522max_pixel_depth *= 2;4523}4524else4525# endif4526png_ptr->transformations &= ~PNG_EXPAND_16;4527}4528#endif45294530#ifdef PNG_READ_FILLER_SUPPORTED4531if ((png_ptr->transformations & (PNG_FILLER)) != 0)4532{4533if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)4534{4535if (max_pixel_depth <= 8)4536max_pixel_depth = 16;45374538else4539max_pixel_depth = 32;4540}45414542else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||4543png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)4544{4545if (max_pixel_depth <= 32)4546max_pixel_depth = 32;45474548else4549max_pixel_depth = 64;4550}4551}4552#endif45534554#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED4555if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)4556{4557if (4558#ifdef PNG_READ_EXPAND_SUPPORTED4559(png_ptr->num_trans != 0 &&4560(png_ptr->transformations & PNG_EXPAND) != 0) ||4561#endif4562#ifdef PNG_READ_FILLER_SUPPORTED4563(png_ptr->transformations & (PNG_FILLER)) != 0 ||4564#endif4565png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)4566{4567if (max_pixel_depth <= 16)4568max_pixel_depth = 32;45694570else4571max_pixel_depth = 64;4572}45734574else4575{4576if (max_pixel_depth <= 8)4577{4578if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)4579max_pixel_depth = 32;45804581else4582max_pixel_depth = 24;4583}45844585else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)4586max_pixel_depth = 64;45874588else4589max_pixel_depth = 48;4590}4591}4592#endif45934594#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \4595defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)4596if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)4597{4598unsigned int user_pixel_depth = png_ptr->user_transform_depth *4599png_ptr->user_transform_channels;46004601if (user_pixel_depth > max_pixel_depth)4602max_pixel_depth = user_pixel_depth;4603}4604#endif46054606/* This value is stored in png_struct and double checked in the row read4607* code.4608*/4609png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;4610png_ptr->transformed_pixel_depth = 0; /* calculated on demand */46114612/* Align the width on the next larger 8 pixels. Mainly used4613* for interlacing4614*/4615row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));4616/* Calculate the maximum bytes needed, adding a byte and a pixel4617* for safety's sake4618*/4619row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +46201 + ((max_pixel_depth + 7) >> 3U);46214622#ifdef PNG_MAX_MALLOC_64K4623if (row_bytes > (png_uint_32)65536L)4624png_error(png_ptr, "This image requires a row greater than 64KB");4625#endif46264627if (row_bytes + 48 > png_ptr->old_big_row_buf_size)4628{4629png_free(png_ptr, png_ptr->big_row_buf);4630png_free(png_ptr, png_ptr->big_prev_row);46314632if (png_ptr->interlaced != 0)4633png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,4634row_bytes + 48);46354636else4637png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);46384639png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);46404641#ifdef PNG_ALIGNED_MEMORY_SUPPORTED4642/* Use 16-byte aligned memory for row_buf with at least 16 bytes4643* of padding before and after row_buf; treat prev_row similarly.4644* NOTE: the alignment is to the start of the pixels, one beyond the start4645* of the buffer, because of the filter byte. Prior to libpng 1.5.6 this4646* was incorrect; the filter byte was aligned, which had the exact4647* opposite effect of that intended.4648*/4649{4650png_bytep temp = png_ptr->big_row_buf + 32;4651int extra = (int)((temp - (png_bytep)0) & 0x0f);4652png_ptr->row_buf = temp - extra - 1/*filter byte*/;46534654temp = png_ptr->big_prev_row + 32;4655extra = (int)((temp - (png_bytep)0) & 0x0f);4656png_ptr->prev_row = temp - extra - 1/*filter byte*/;4657}46584659#else4660/* Use 31 bytes of padding before and 17 bytes after row_buf. */4661png_ptr->row_buf = png_ptr->big_row_buf + 31;4662png_ptr->prev_row = png_ptr->big_prev_row + 31;4663#endif4664png_ptr->old_big_row_buf_size = row_bytes + 48;4665}46664667#ifdef PNG_MAX_MALLOC_64K4668if (png_ptr->rowbytes > 65535)4669png_error(png_ptr, "This image requires a row greater than 64KB");46704671#endif4672if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))4673png_error(png_ptr, "Row has too many bytes to allocate in memory");46744675memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);46764677png_debug1(3, "width = %u,", png_ptr->width);4678png_debug1(3, "height = %u,", png_ptr->height);4679png_debug1(3, "iwidth = %u,", png_ptr->iwidth);4680png_debug1(3, "num_rows = %u,", png_ptr->num_rows);4681png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);4682png_debug1(3, "irowbytes = %lu",4683(unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);46844685/* The sequential reader needs a buffer for IDAT, but the progressive reader4686* does not, so free the read buffer now regardless; the sequential reader4687* reallocates it on demand.4688*/4689if (png_ptr->read_buffer != NULL)4690{4691png_bytep buffer = png_ptr->read_buffer;46924693png_ptr->read_buffer_size = 0;4694png_ptr->read_buffer = NULL;4695png_free(png_ptr, buffer);4696}46974698/* Finally claim the zstream for the inflate of the IDAT data, use the bits4699* value from the stream (note that this will result in a fatal error if the4700* IDAT stream has a bogus deflate header window_bits value, but this should4701* not be happening any longer!)4702*/4703if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)4704png_error(png_ptr, png_ptr->zstream.msg);47054706png_ptr->flags |= PNG_FLAG_ROW_INIT;4707}4708#endif /* READ */470947104711