xref: /aosp_15_r20/external/libpng/pngrutil.c (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1*a67afe4dSAndroid Build Coastguard Worker 
2*a67afe4dSAndroid Build Coastguard Worker /* pngrutil.c - utilities to read a PNG file
3*a67afe4dSAndroid Build Coastguard Worker  *
4*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 2018-2024 Cosmin Truta
5*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 1996-1997 Andreas Dilger
7*a67afe4dSAndroid Build Coastguard Worker  * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
8*a67afe4dSAndroid Build Coastguard Worker  *
9*a67afe4dSAndroid Build Coastguard Worker  * This code is released under the libpng license.
10*a67afe4dSAndroid Build Coastguard Worker  * For conditions of distribution and use, see the disclaimer
11*a67afe4dSAndroid Build Coastguard Worker  * and license in png.h
12*a67afe4dSAndroid Build Coastguard Worker  *
13*a67afe4dSAndroid Build Coastguard Worker  * This file contains routines that are only called from within
14*a67afe4dSAndroid Build Coastguard Worker  * libpng itself during the course of reading an image.
15*a67afe4dSAndroid Build Coastguard Worker  */
16*a67afe4dSAndroid Build Coastguard Worker 
17*a67afe4dSAndroid Build Coastguard Worker #include "pngpriv.h"
18*a67afe4dSAndroid Build Coastguard Worker 
19*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_SUPPORTED
20*a67afe4dSAndroid Build Coastguard Worker 
21*a67afe4dSAndroid Build Coastguard Worker png_uint_32 PNGAPI
png_get_uint_31(png_const_structrp png_ptr,png_const_bytep buf)22*a67afe4dSAndroid Build Coastguard Worker png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
23*a67afe4dSAndroid Build Coastguard Worker {
24*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 uval = png_get_uint_32(buf);
25*a67afe4dSAndroid Build Coastguard Worker 
26*a67afe4dSAndroid Build Coastguard Worker    if (uval > PNG_UINT_31_MAX)
27*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "PNG unsigned integer out of range");
28*a67afe4dSAndroid Build Coastguard Worker 
29*a67afe4dSAndroid Build Coastguard Worker    return uval;
30*a67afe4dSAndroid Build Coastguard Worker }
31*a67afe4dSAndroid Build Coastguard Worker 
32*a67afe4dSAndroid Build Coastguard Worker #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED)
33*a67afe4dSAndroid Build Coastguard Worker /* The following is a variation on the above for use with the fixed
34*a67afe4dSAndroid Build Coastguard Worker  * point values used for gAMA and cHRM.  Instead of png_error it
35*a67afe4dSAndroid Build Coastguard Worker  * issues a warning and returns (-1) - an invalid value because both
36*a67afe4dSAndroid Build Coastguard Worker  * gAMA and cHRM use *unsigned* integers for fixed point values.
37*a67afe4dSAndroid Build Coastguard Worker  */
38*a67afe4dSAndroid Build Coastguard Worker #define PNG_FIXED_ERROR (-1)
39*a67afe4dSAndroid Build Coastguard Worker 
40*a67afe4dSAndroid Build Coastguard Worker static png_fixed_point /* PRIVATE */
png_get_fixed_point(png_structrp png_ptr,png_const_bytep buf)41*a67afe4dSAndroid Build Coastguard Worker png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
42*a67afe4dSAndroid Build Coastguard Worker {
43*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 uval = png_get_uint_32(buf);
44*a67afe4dSAndroid Build Coastguard Worker 
45*a67afe4dSAndroid Build Coastguard Worker    if (uval <= PNG_UINT_31_MAX)
46*a67afe4dSAndroid Build Coastguard Worker       return (png_fixed_point)uval; /* known to be in range */
47*a67afe4dSAndroid Build Coastguard Worker 
48*a67afe4dSAndroid Build Coastguard Worker    /* The caller can turn off the warning by passing NULL. */
49*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr != NULL)
50*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "PNG fixed point integer out of range");
51*a67afe4dSAndroid Build Coastguard Worker 
52*a67afe4dSAndroid Build Coastguard Worker    return PNG_FIXED_ERROR;
53*a67afe4dSAndroid Build Coastguard Worker }
54*a67afe4dSAndroid Build Coastguard Worker #endif
55*a67afe4dSAndroid Build Coastguard Worker 
56*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
57*a67afe4dSAndroid Build Coastguard Worker /* NOTE: the read macros will obscure these definitions, so that if
58*a67afe4dSAndroid Build Coastguard Worker  * PNG_USE_READ_MACROS is set the library will not use them internally,
59*a67afe4dSAndroid Build Coastguard Worker  * but the APIs will still be available externally.
60*a67afe4dSAndroid Build Coastguard Worker  *
61*a67afe4dSAndroid Build Coastguard Worker  * The parentheses around "PNGAPI function_name" in the following three
62*a67afe4dSAndroid Build Coastguard Worker  * functions are necessary because they allow the macros to co-exist with
63*a67afe4dSAndroid Build Coastguard Worker  * these (unused but exported) functions.
64*a67afe4dSAndroid Build Coastguard Worker  */
65*a67afe4dSAndroid Build Coastguard Worker 
66*a67afe4dSAndroid Build Coastguard Worker /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
png_uint_32(PNGAPI png_get_uint_32)67*a67afe4dSAndroid Build Coastguard Worker png_uint_32 (PNGAPI
68*a67afe4dSAndroid Build Coastguard Worker png_get_uint_32)(png_const_bytep buf)
69*a67afe4dSAndroid Build Coastguard Worker {
70*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 uval =
71*a67afe4dSAndroid Build Coastguard Worker        ((png_uint_32)(*(buf    )) << 24) +
72*a67afe4dSAndroid Build Coastguard Worker        ((png_uint_32)(*(buf + 1)) << 16) +
73*a67afe4dSAndroid Build Coastguard Worker        ((png_uint_32)(*(buf + 2)) <<  8) +
74*a67afe4dSAndroid Build Coastguard Worker        ((png_uint_32)(*(buf + 3))      ) ;
75*a67afe4dSAndroid Build Coastguard Worker 
76*a67afe4dSAndroid Build Coastguard Worker    return uval;
77*a67afe4dSAndroid Build Coastguard Worker }
78*a67afe4dSAndroid Build Coastguard Worker 
79*a67afe4dSAndroid Build Coastguard Worker /* Grab a signed 32-bit integer from a buffer in big-endian format.  The
80*a67afe4dSAndroid Build Coastguard Worker  * data is stored in the PNG file in two's complement format and there
81*a67afe4dSAndroid Build Coastguard Worker  * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore
82*a67afe4dSAndroid Build Coastguard Worker  * the following code does a two's complement to native conversion.
83*a67afe4dSAndroid Build Coastguard Worker  */
png_int_32(PNGAPI png_get_int_32)84*a67afe4dSAndroid Build Coastguard Worker png_int_32 (PNGAPI
85*a67afe4dSAndroid Build Coastguard Worker png_get_int_32)(png_const_bytep buf)
86*a67afe4dSAndroid Build Coastguard Worker {
87*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 uval = png_get_uint_32(buf);
88*a67afe4dSAndroid Build Coastguard Worker    if ((uval & 0x80000000) == 0) /* non-negative */
89*a67afe4dSAndroid Build Coastguard Worker       return (png_int_32)uval;
90*a67afe4dSAndroid Build Coastguard Worker 
91*a67afe4dSAndroid Build Coastguard Worker    uval = (uval ^ 0xffffffff) + 1;  /* 2's complement: -x = ~x+1 */
92*a67afe4dSAndroid Build Coastguard Worker    if ((uval & 0x80000000) == 0) /* no overflow */
93*a67afe4dSAndroid Build Coastguard Worker       return -(png_int_32)uval;
94*a67afe4dSAndroid Build Coastguard Worker    /* The following has to be safe; this function only gets called on PNG data
95*a67afe4dSAndroid Build Coastguard Worker     * and if we get here that data is invalid.  0 is the most safe value and
96*a67afe4dSAndroid Build Coastguard Worker     * if not then an attacker would surely just generate a PNG with 0 instead.
97*a67afe4dSAndroid Build Coastguard Worker     */
98*a67afe4dSAndroid Build Coastguard Worker    return 0;
99*a67afe4dSAndroid Build Coastguard Worker }
100*a67afe4dSAndroid Build Coastguard Worker 
101*a67afe4dSAndroid Build Coastguard Worker /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
png_uint_16(PNGAPI png_get_uint_16)102*a67afe4dSAndroid Build Coastguard Worker png_uint_16 (PNGAPI
103*a67afe4dSAndroid Build Coastguard Worker png_get_uint_16)(png_const_bytep buf)
104*a67afe4dSAndroid Build Coastguard Worker {
105*a67afe4dSAndroid Build Coastguard Worker    /* ANSI-C requires an int value to accommodate at least 16 bits so this
106*a67afe4dSAndroid Build Coastguard Worker     * works and allows the compiler not to worry about possible narrowing
107*a67afe4dSAndroid Build Coastguard Worker     * on 32-bit systems.  (Pre-ANSI systems did not make integers smaller
108*a67afe4dSAndroid Build Coastguard Worker     * than 16 bits either.)
109*a67afe4dSAndroid Build Coastguard Worker     */
110*a67afe4dSAndroid Build Coastguard Worker    unsigned int val =
111*a67afe4dSAndroid Build Coastguard Worker        ((unsigned int)(*buf) << 8) +
112*a67afe4dSAndroid Build Coastguard Worker        ((unsigned int)(*(buf + 1)));
113*a67afe4dSAndroid Build Coastguard Worker 
114*a67afe4dSAndroid Build Coastguard Worker    return (png_uint_16)val;
115*a67afe4dSAndroid Build Coastguard Worker }
116*a67afe4dSAndroid Build Coastguard Worker 
117*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_INT_FUNCTIONS */
118*a67afe4dSAndroid Build Coastguard Worker 
119*a67afe4dSAndroid Build Coastguard Worker /* Read and check the PNG file signature */
120*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_sig(png_structrp png_ptr,png_inforp info_ptr)121*a67afe4dSAndroid Build Coastguard Worker png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
122*a67afe4dSAndroid Build Coastguard Worker {
123*a67afe4dSAndroid Build Coastguard Worker    size_t num_checked, num_to_check;
124*a67afe4dSAndroid Build Coastguard Worker 
125*a67afe4dSAndroid Build Coastguard Worker    /* Exit if the user application does not expect a signature. */
126*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->sig_bytes >= 8)
127*a67afe4dSAndroid Build Coastguard Worker       return;
128*a67afe4dSAndroid Build Coastguard Worker 
129*a67afe4dSAndroid Build Coastguard Worker    num_checked = png_ptr->sig_bytes;
130*a67afe4dSAndroid Build Coastguard Worker    num_to_check = 8 - num_checked;
131*a67afe4dSAndroid Build Coastguard Worker 
132*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_IO_STATE_SUPPORTED
133*a67afe4dSAndroid Build Coastguard Worker    png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
134*a67afe4dSAndroid Build Coastguard Worker #endif
135*a67afe4dSAndroid Build Coastguard Worker 
136*a67afe4dSAndroid Build Coastguard Worker    /* The signature must be serialized in a single I/O call. */
137*a67afe4dSAndroid Build Coastguard Worker    png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
138*a67afe4dSAndroid Build Coastguard Worker    png_ptr->sig_bytes = 8;
139*a67afe4dSAndroid Build Coastguard Worker 
140*a67afe4dSAndroid Build Coastguard Worker    if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
141*a67afe4dSAndroid Build Coastguard Worker    {
142*a67afe4dSAndroid Build Coastguard Worker       if (num_checked < 4 &&
143*a67afe4dSAndroid Build Coastguard Worker           png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
144*a67afe4dSAndroid Build Coastguard Worker          png_error(png_ptr, "Not a PNG file");
145*a67afe4dSAndroid Build Coastguard Worker       else
146*a67afe4dSAndroid Build Coastguard Worker          png_error(png_ptr, "PNG file corrupted by ASCII conversion");
147*a67afe4dSAndroid Build Coastguard Worker    }
148*a67afe4dSAndroid Build Coastguard Worker    if (num_checked < 3)
149*a67afe4dSAndroid Build Coastguard Worker       png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
150*a67afe4dSAndroid Build Coastguard Worker }
151*a67afe4dSAndroid Build Coastguard Worker 
152*a67afe4dSAndroid Build Coastguard Worker /* Read the chunk header (length + type name).
153*a67afe4dSAndroid Build Coastguard Worker  * Put the type name into png_ptr->chunk_name, and return the length.
154*a67afe4dSAndroid Build Coastguard Worker  */
155*a67afe4dSAndroid Build Coastguard Worker png_uint_32 /* PRIVATE */
png_read_chunk_header(png_structrp png_ptr)156*a67afe4dSAndroid Build Coastguard Worker png_read_chunk_header(png_structrp png_ptr)
157*a67afe4dSAndroid Build Coastguard Worker {
158*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[8];
159*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 length;
160*a67afe4dSAndroid Build Coastguard Worker 
161*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_IO_STATE_SUPPORTED
162*a67afe4dSAndroid Build Coastguard Worker    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
163*a67afe4dSAndroid Build Coastguard Worker #endif
164*a67afe4dSAndroid Build Coastguard Worker 
165*a67afe4dSAndroid Build Coastguard Worker    /* Read the length and the chunk name.
166*a67afe4dSAndroid Build Coastguard Worker     * This must be performed in a single I/O call.
167*a67afe4dSAndroid Build Coastguard Worker     */
168*a67afe4dSAndroid Build Coastguard Worker    png_read_data(png_ptr, buf, 8);
169*a67afe4dSAndroid Build Coastguard Worker    length = png_get_uint_31(png_ptr, buf);
170*a67afe4dSAndroid Build Coastguard Worker 
171*a67afe4dSAndroid Build Coastguard Worker    /* Put the chunk name into png_ptr->chunk_name. */
172*a67afe4dSAndroid Build Coastguard Worker    png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
173*a67afe4dSAndroid Build Coastguard Worker 
174*a67afe4dSAndroid Build Coastguard Worker    png_debug2(0, "Reading chunk typeid = 0x%lx, length = %lu",
175*a67afe4dSAndroid Build Coastguard Worker        (unsigned long)png_ptr->chunk_name, (unsigned long)length);
176*a67afe4dSAndroid Build Coastguard Worker 
177*a67afe4dSAndroid Build Coastguard Worker    /* Reset the crc and run it over the chunk name. */
178*a67afe4dSAndroid Build Coastguard Worker    png_reset_crc(png_ptr);
179*a67afe4dSAndroid Build Coastguard Worker    png_calculate_crc(png_ptr, buf + 4, 4);
180*a67afe4dSAndroid Build Coastguard Worker 
181*a67afe4dSAndroid Build Coastguard Worker    /* Check to see if chunk name is valid. */
182*a67afe4dSAndroid Build Coastguard Worker    png_check_chunk_name(png_ptr, png_ptr->chunk_name);
183*a67afe4dSAndroid Build Coastguard Worker 
184*a67afe4dSAndroid Build Coastguard Worker    /* Check for too-large chunk length */
185*a67afe4dSAndroid Build Coastguard Worker    png_check_chunk_length(png_ptr, length);
186*a67afe4dSAndroid Build Coastguard Worker 
187*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_IO_STATE_SUPPORTED
188*a67afe4dSAndroid Build Coastguard Worker    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
189*a67afe4dSAndroid Build Coastguard Worker #endif
190*a67afe4dSAndroid Build Coastguard Worker 
191*a67afe4dSAndroid Build Coastguard Worker    return length;
192*a67afe4dSAndroid Build Coastguard Worker }
193*a67afe4dSAndroid Build Coastguard Worker 
194*a67afe4dSAndroid Build Coastguard Worker /* Read data, and (optionally) run it through the CRC. */
195*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_crc_read(png_structrp png_ptr,png_bytep buf,png_uint_32 length)196*a67afe4dSAndroid Build Coastguard Worker png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
197*a67afe4dSAndroid Build Coastguard Worker {
198*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr == NULL)
199*a67afe4dSAndroid Build Coastguard Worker       return;
200*a67afe4dSAndroid Build Coastguard Worker 
201*a67afe4dSAndroid Build Coastguard Worker    png_read_data(png_ptr, buf, length);
202*a67afe4dSAndroid Build Coastguard Worker    png_calculate_crc(png_ptr, buf, length);
203*a67afe4dSAndroid Build Coastguard Worker }
204*a67afe4dSAndroid Build Coastguard Worker 
205*a67afe4dSAndroid Build Coastguard Worker /* Optionally skip data and then check the CRC.  Depending on whether we
206*a67afe4dSAndroid Build Coastguard Worker  * are reading an ancillary or critical chunk, and how the program has set
207*a67afe4dSAndroid Build Coastguard Worker  * things up, we may calculate the CRC on the data and print a message.
208*a67afe4dSAndroid Build Coastguard Worker  * Returns '1' if there was a CRC error, '0' otherwise.
209*a67afe4dSAndroid Build Coastguard Worker  */
210*a67afe4dSAndroid Build Coastguard Worker int /* PRIVATE */
png_crc_finish(png_structrp png_ptr,png_uint_32 skip)211*a67afe4dSAndroid Build Coastguard Worker png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
212*a67afe4dSAndroid Build Coastguard Worker {
213*a67afe4dSAndroid Build Coastguard Worker    /* The size of the local buffer for inflate is a good guess as to a
214*a67afe4dSAndroid Build Coastguard Worker     * reasonable size to use for buffering reads from the application.
215*a67afe4dSAndroid Build Coastguard Worker     */
216*a67afe4dSAndroid Build Coastguard Worker    while (skip > 0)
217*a67afe4dSAndroid Build Coastguard Worker    {
218*a67afe4dSAndroid Build Coastguard Worker       png_uint_32 len;
219*a67afe4dSAndroid Build Coastguard Worker       png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
220*a67afe4dSAndroid Build Coastguard Worker 
221*a67afe4dSAndroid Build Coastguard Worker       len = (sizeof tmpbuf);
222*a67afe4dSAndroid Build Coastguard Worker       if (len > skip)
223*a67afe4dSAndroid Build Coastguard Worker          len = skip;
224*a67afe4dSAndroid Build Coastguard Worker       skip -= len;
225*a67afe4dSAndroid Build Coastguard Worker 
226*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, tmpbuf, len);
227*a67afe4dSAndroid Build Coastguard Worker    }
228*a67afe4dSAndroid Build Coastguard Worker 
229*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_error(png_ptr) != 0)
230*a67afe4dSAndroid Build Coastguard Worker    {
231*a67afe4dSAndroid Build Coastguard Worker       if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
232*a67afe4dSAndroid Build Coastguard Worker           (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
233*a67afe4dSAndroid Build Coastguard Worker           (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
234*a67afe4dSAndroid Build Coastguard Worker       {
235*a67afe4dSAndroid Build Coastguard Worker          png_chunk_warning(png_ptr, "CRC error");
236*a67afe4dSAndroid Build Coastguard Worker       }
237*a67afe4dSAndroid Build Coastguard Worker 
238*a67afe4dSAndroid Build Coastguard Worker       else
239*a67afe4dSAndroid Build Coastguard Worker          png_chunk_error(png_ptr, "CRC error");
240*a67afe4dSAndroid Build Coastguard Worker 
241*a67afe4dSAndroid Build Coastguard Worker       return 1;
242*a67afe4dSAndroid Build Coastguard Worker    }
243*a67afe4dSAndroid Build Coastguard Worker 
244*a67afe4dSAndroid Build Coastguard Worker    return 0;
245*a67afe4dSAndroid Build Coastguard Worker }
246*a67afe4dSAndroid Build Coastguard Worker 
247*a67afe4dSAndroid Build Coastguard Worker /* Compare the CRC stored in the PNG file with that calculated by libpng from
248*a67afe4dSAndroid Build Coastguard Worker  * the data it has read thus far.
249*a67afe4dSAndroid Build Coastguard Worker  */
250*a67afe4dSAndroid Build Coastguard Worker int /* PRIVATE */
png_crc_error(png_structrp png_ptr)251*a67afe4dSAndroid Build Coastguard Worker png_crc_error(png_structrp png_ptr)
252*a67afe4dSAndroid Build Coastguard Worker {
253*a67afe4dSAndroid Build Coastguard Worker    png_byte crc_bytes[4];
254*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 crc;
255*a67afe4dSAndroid Build Coastguard Worker    int need_crc = 1;
256*a67afe4dSAndroid Build Coastguard Worker 
257*a67afe4dSAndroid Build Coastguard Worker    if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
258*a67afe4dSAndroid Build Coastguard Worker    {
259*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
260*a67afe4dSAndroid Build Coastguard Worker           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
261*a67afe4dSAndroid Build Coastguard Worker          need_crc = 0;
262*a67afe4dSAndroid Build Coastguard Worker    }
263*a67afe4dSAndroid Build Coastguard Worker 
264*a67afe4dSAndroid Build Coastguard Worker    else /* critical */
265*a67afe4dSAndroid Build Coastguard Worker    {
266*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
267*a67afe4dSAndroid Build Coastguard Worker          need_crc = 0;
268*a67afe4dSAndroid Build Coastguard Worker    }
269*a67afe4dSAndroid Build Coastguard Worker 
270*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_IO_STATE_SUPPORTED
271*a67afe4dSAndroid Build Coastguard Worker    png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
272*a67afe4dSAndroid Build Coastguard Worker #endif
273*a67afe4dSAndroid Build Coastguard Worker 
274*a67afe4dSAndroid Build Coastguard Worker    /* The chunk CRC must be serialized in a single I/O call. */
275*a67afe4dSAndroid Build Coastguard Worker    png_read_data(png_ptr, crc_bytes, 4);
276*a67afe4dSAndroid Build Coastguard Worker 
277*a67afe4dSAndroid Build Coastguard Worker    if (need_crc != 0)
278*a67afe4dSAndroid Build Coastguard Worker    {
279*a67afe4dSAndroid Build Coastguard Worker       crc = png_get_uint_32(crc_bytes);
280*a67afe4dSAndroid Build Coastguard Worker       return crc != png_ptr->crc;
281*a67afe4dSAndroid Build Coastguard Worker    }
282*a67afe4dSAndroid Build Coastguard Worker 
283*a67afe4dSAndroid Build Coastguard Worker    else
284*a67afe4dSAndroid Build Coastguard Worker       return 0;
285*a67afe4dSAndroid Build Coastguard Worker }
286*a67afe4dSAndroid Build Coastguard Worker 
287*a67afe4dSAndroid Build Coastguard Worker #if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
288*a67afe4dSAndroid Build Coastguard Worker     defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
289*a67afe4dSAndroid Build Coastguard Worker     defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
290*a67afe4dSAndroid Build Coastguard Worker     defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED)
291*a67afe4dSAndroid Build Coastguard Worker /* Manage the read buffer; this simply reallocates the buffer if it is not small
292*a67afe4dSAndroid Build Coastguard Worker  * enough (or if it is not allocated).  The routine returns a pointer to the
293*a67afe4dSAndroid Build Coastguard Worker  * buffer; if an error occurs and 'warn' is set the routine returns NULL, else
294*a67afe4dSAndroid Build Coastguard Worker  * it will call png_error (via png_malloc) on failure.  (warn == 2 means
295*a67afe4dSAndroid Build Coastguard Worker  * 'silent').
296*a67afe4dSAndroid Build Coastguard Worker  */
297*a67afe4dSAndroid Build Coastguard Worker static png_bytep
png_read_buffer(png_structrp png_ptr,png_alloc_size_t new_size,int warn)298*a67afe4dSAndroid Build Coastguard Worker png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
299*a67afe4dSAndroid Build Coastguard Worker {
300*a67afe4dSAndroid Build Coastguard Worker    png_bytep buffer = png_ptr->read_buffer;
301*a67afe4dSAndroid Build Coastguard Worker 
302*a67afe4dSAndroid Build Coastguard Worker    if (buffer != NULL && new_size > png_ptr->read_buffer_size)
303*a67afe4dSAndroid Build Coastguard Worker    {
304*a67afe4dSAndroid Build Coastguard Worker       png_ptr->read_buffer = NULL;
305*a67afe4dSAndroid Build Coastguard Worker       png_ptr->read_buffer_size = 0;
306*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, buffer);
307*a67afe4dSAndroid Build Coastguard Worker       buffer = NULL;
308*a67afe4dSAndroid Build Coastguard Worker    }
309*a67afe4dSAndroid Build Coastguard Worker 
310*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
311*a67afe4dSAndroid Build Coastguard Worker    {
312*a67afe4dSAndroid Build Coastguard Worker       buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
313*a67afe4dSAndroid Build Coastguard Worker 
314*a67afe4dSAndroid Build Coastguard Worker       if (buffer != NULL)
315*a67afe4dSAndroid Build Coastguard Worker       {
316*a67afe4dSAndroid Build Coastguard Worker          memset(buffer, 0, new_size); /* just in case */
317*a67afe4dSAndroid Build Coastguard Worker          png_ptr->read_buffer = buffer;
318*a67afe4dSAndroid Build Coastguard Worker          png_ptr->read_buffer_size = new_size;
319*a67afe4dSAndroid Build Coastguard Worker       }
320*a67afe4dSAndroid Build Coastguard Worker 
321*a67afe4dSAndroid Build Coastguard Worker       else if (warn < 2) /* else silent */
322*a67afe4dSAndroid Build Coastguard Worker       {
323*a67afe4dSAndroid Build Coastguard Worker          if (warn != 0)
324*a67afe4dSAndroid Build Coastguard Worker              png_chunk_warning(png_ptr, "insufficient memory to read chunk");
325*a67afe4dSAndroid Build Coastguard Worker 
326*a67afe4dSAndroid Build Coastguard Worker          else
327*a67afe4dSAndroid Build Coastguard Worker              png_chunk_error(png_ptr, "insufficient memory to read chunk");
328*a67afe4dSAndroid Build Coastguard Worker       }
329*a67afe4dSAndroid Build Coastguard Worker    }
330*a67afe4dSAndroid Build Coastguard Worker 
331*a67afe4dSAndroid Build Coastguard Worker    return buffer;
332*a67afe4dSAndroid Build Coastguard Worker }
333*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */
334*a67afe4dSAndroid Build Coastguard Worker 
335*a67afe4dSAndroid Build Coastguard Worker /* png_inflate_claim: claim the zstream for some nefarious purpose that involves
336*a67afe4dSAndroid Build Coastguard Worker  * decompression.  Returns Z_OK on success, else a zlib error code.  It checks
337*a67afe4dSAndroid Build Coastguard Worker  * the owner but, in final release builds, just issues a warning if some other
338*a67afe4dSAndroid Build Coastguard Worker  * chunk apparently owns the stream.  Prior to release it does a png_error.
339*a67afe4dSAndroid Build Coastguard Worker  */
340*a67afe4dSAndroid Build Coastguard Worker static int
png_inflate_claim(png_structrp png_ptr,png_uint_32 owner)341*a67afe4dSAndroid Build Coastguard Worker png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
342*a67afe4dSAndroid Build Coastguard Worker {
343*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->zowner != 0)
344*a67afe4dSAndroid Build Coastguard Worker    {
345*a67afe4dSAndroid Build Coastguard Worker       char msg[64];
346*a67afe4dSAndroid Build Coastguard Worker 
347*a67afe4dSAndroid Build Coastguard Worker       PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
348*a67afe4dSAndroid Build Coastguard Worker       /* So the message that results is "<chunk> using zstream"; this is an
349*a67afe4dSAndroid Build Coastguard Worker        * internal error, but is very useful for debugging.  i18n requirements
350*a67afe4dSAndroid Build Coastguard Worker        * are minimal.
351*a67afe4dSAndroid Build Coastguard Worker        */
352*a67afe4dSAndroid Build Coastguard Worker       (void)png_safecat(msg, (sizeof msg), 4, " using zstream");
353*a67afe4dSAndroid Build Coastguard Worker #if PNG_RELEASE_BUILD
354*a67afe4dSAndroid Build Coastguard Worker       png_chunk_warning(png_ptr, msg);
355*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zowner = 0;
356*a67afe4dSAndroid Build Coastguard Worker #else
357*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, msg);
358*a67afe4dSAndroid Build Coastguard Worker #endif
359*a67afe4dSAndroid Build Coastguard Worker    }
360*a67afe4dSAndroid Build Coastguard Worker 
361*a67afe4dSAndroid Build Coastguard Worker    /* Implementation note: unlike 'png_deflate_claim' this internal function
362*a67afe4dSAndroid Build Coastguard Worker     * does not take the size of the data as an argument.  Some efficiency could
363*a67afe4dSAndroid Build Coastguard Worker     * be gained by using this when it is known *if* the zlib stream itself does
364*a67afe4dSAndroid Build Coastguard Worker     * not record the number; however, this is an illusion: the original writer
365*a67afe4dSAndroid Build Coastguard Worker     * of the PNG may have selected a lower window size, and we really must
366*a67afe4dSAndroid Build Coastguard Worker     * follow that because, for systems with with limited capabilities, we
367*a67afe4dSAndroid Build Coastguard Worker     * would otherwise reject the application's attempts to use a smaller window
368*a67afe4dSAndroid Build Coastguard Worker     * size (zlib doesn't have an interface to say "this or lower"!).
369*a67afe4dSAndroid Build Coastguard Worker     *
370*a67afe4dSAndroid Build Coastguard Worker     * inflateReset2 was added to zlib 1.2.4; before this the window could not be
371*a67afe4dSAndroid Build Coastguard Worker     * reset, therefore it is necessary to always allocate the maximum window
372*a67afe4dSAndroid Build Coastguard Worker     * size with earlier zlibs just in case later compressed chunks need it.
373*a67afe4dSAndroid Build Coastguard Worker     */
374*a67afe4dSAndroid Build Coastguard Worker    {
375*a67afe4dSAndroid Build Coastguard Worker       int ret; /* zlib return code */
376*a67afe4dSAndroid Build Coastguard Worker #if ZLIB_VERNUM >= 0x1240
377*a67afe4dSAndroid Build Coastguard Worker       int window_bits = 0;
378*a67afe4dSAndroid Build Coastguard Worker 
379*a67afe4dSAndroid Build Coastguard Worker # if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
380*a67afe4dSAndroid Build Coastguard Worker       if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
381*a67afe4dSAndroid Build Coastguard Worker           PNG_OPTION_ON)
382*a67afe4dSAndroid Build Coastguard Worker       {
383*a67afe4dSAndroid Build Coastguard Worker          window_bits = 15;
384*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream_start = 0; /* fixed window size */
385*a67afe4dSAndroid Build Coastguard Worker       }
386*a67afe4dSAndroid Build Coastguard Worker 
387*a67afe4dSAndroid Build Coastguard Worker       else
388*a67afe4dSAndroid Build Coastguard Worker       {
389*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream_start = 1;
390*a67afe4dSAndroid Build Coastguard Worker       }
391*a67afe4dSAndroid Build Coastguard Worker # endif
392*a67afe4dSAndroid Build Coastguard Worker 
393*a67afe4dSAndroid Build Coastguard Worker #endif /* ZLIB_VERNUM >= 0x1240 */
394*a67afe4dSAndroid Build Coastguard Worker 
395*a67afe4dSAndroid Build Coastguard Worker       /* Set this for safety, just in case the previous owner left pointers to
396*a67afe4dSAndroid Build Coastguard Worker        * memory allocations.
397*a67afe4dSAndroid Build Coastguard Worker        */
398*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_in = NULL;
399*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_in = 0;
400*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_out = NULL;
401*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_out = 0;
402*a67afe4dSAndroid Build Coastguard Worker 
403*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
404*a67afe4dSAndroid Build Coastguard Worker       {
405*a67afe4dSAndroid Build Coastguard Worker #if ZLIB_VERNUM >= 0x1240
406*a67afe4dSAndroid Build Coastguard Worker          ret = inflateReset2(&png_ptr->zstream, window_bits);
407*a67afe4dSAndroid Build Coastguard Worker #else
408*a67afe4dSAndroid Build Coastguard Worker          ret = inflateReset(&png_ptr->zstream);
409*a67afe4dSAndroid Build Coastguard Worker #endif
410*a67afe4dSAndroid Build Coastguard Worker       }
411*a67afe4dSAndroid Build Coastguard Worker 
412*a67afe4dSAndroid Build Coastguard Worker       else
413*a67afe4dSAndroid Build Coastguard Worker       {
414*a67afe4dSAndroid Build Coastguard Worker #if ZLIB_VERNUM >= 0x1240
415*a67afe4dSAndroid Build Coastguard Worker          ret = inflateInit2(&png_ptr->zstream, window_bits);
416*a67afe4dSAndroid Build Coastguard Worker #else
417*a67afe4dSAndroid Build Coastguard Worker          ret = inflateInit(&png_ptr->zstream);
418*a67afe4dSAndroid Build Coastguard Worker #endif
419*a67afe4dSAndroid Build Coastguard Worker 
420*a67afe4dSAndroid Build Coastguard Worker          if (ret == Z_OK)
421*a67afe4dSAndroid Build Coastguard Worker             png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
422*a67afe4dSAndroid Build Coastguard Worker       }
423*a67afe4dSAndroid Build Coastguard Worker 
424*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_DISABLE_ADLER32_CHECK_SUPPORTED
425*a67afe4dSAndroid Build Coastguard Worker       if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
426*a67afe4dSAndroid Build Coastguard Worker          /* Turn off validation of the ADLER32 checksum in IDAT chunks */
427*a67afe4dSAndroid Build Coastguard Worker          ret = inflateValidate(&png_ptr->zstream, 0);
428*a67afe4dSAndroid Build Coastguard Worker #endif
429*a67afe4dSAndroid Build Coastguard Worker 
430*a67afe4dSAndroid Build Coastguard Worker       if (ret == Z_OK)
431*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zowner = owner;
432*a67afe4dSAndroid Build Coastguard Worker 
433*a67afe4dSAndroid Build Coastguard Worker       else
434*a67afe4dSAndroid Build Coastguard Worker          png_zstream_error(png_ptr, ret);
435*a67afe4dSAndroid Build Coastguard Worker 
436*a67afe4dSAndroid Build Coastguard Worker       return ret;
437*a67afe4dSAndroid Build Coastguard Worker    }
438*a67afe4dSAndroid Build Coastguard Worker 
439*a67afe4dSAndroid Build Coastguard Worker #ifdef window_bits
440*a67afe4dSAndroid Build Coastguard Worker # undef window_bits
441*a67afe4dSAndroid Build Coastguard Worker #endif
442*a67afe4dSAndroid Build Coastguard Worker }
443*a67afe4dSAndroid Build Coastguard Worker 
444*a67afe4dSAndroid Build Coastguard Worker #if ZLIB_VERNUM >= 0x1240
445*a67afe4dSAndroid Build Coastguard Worker /* Handle the start of the inflate stream if we called inflateInit2(strm,0);
446*a67afe4dSAndroid Build Coastguard Worker  * in this case some zlib versions skip validation of the CINFO field and, in
447*a67afe4dSAndroid Build Coastguard Worker  * certain circumstances, libpng may end up displaying an invalid image, in
448*a67afe4dSAndroid Build Coastguard Worker  * contrast to implementations that call zlib in the normal way (e.g. libpng
449*a67afe4dSAndroid Build Coastguard Worker  * 1.5).
450*a67afe4dSAndroid Build Coastguard Worker  */
451*a67afe4dSAndroid Build Coastguard Worker int /* PRIVATE */
png_zlib_inflate(png_structrp png_ptr,int flush)452*a67afe4dSAndroid Build Coastguard Worker png_zlib_inflate(png_structrp png_ptr, int flush)
453*a67afe4dSAndroid Build Coastguard Worker {
454*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
455*a67afe4dSAndroid Build Coastguard Worker    {
456*a67afe4dSAndroid Build Coastguard Worker       if ((*png_ptr->zstream.next_in >> 4) > 7)
457*a67afe4dSAndroid Build Coastguard Worker       {
458*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.msg = "invalid window size (libpng)";
459*a67afe4dSAndroid Build Coastguard Worker          return Z_DATA_ERROR;
460*a67afe4dSAndroid Build Coastguard Worker       }
461*a67afe4dSAndroid Build Coastguard Worker 
462*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream_start = 0;
463*a67afe4dSAndroid Build Coastguard Worker    }
464*a67afe4dSAndroid Build Coastguard Worker 
465*a67afe4dSAndroid Build Coastguard Worker    return inflate(&png_ptr->zstream, flush);
466*a67afe4dSAndroid Build Coastguard Worker }
467*a67afe4dSAndroid Build Coastguard Worker #endif /* Zlib >= 1.2.4 */
468*a67afe4dSAndroid Build Coastguard Worker 
469*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
470*a67afe4dSAndroid Build Coastguard Worker #if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
471*a67afe4dSAndroid Build Coastguard Worker /* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to
472*a67afe4dSAndroid Build Coastguard Worker  * allow the caller to do multiple calls if required.  If the 'finish' flag is
473*a67afe4dSAndroid Build Coastguard Worker  * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must
474*a67afe4dSAndroid Build Coastguard Worker  * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and
475*a67afe4dSAndroid Build Coastguard Worker  * Z_OK or Z_STREAM_END will be returned on success.
476*a67afe4dSAndroid Build Coastguard Worker  *
477*a67afe4dSAndroid Build Coastguard Worker  * The input and output sizes are updated to the actual amounts of data consumed
478*a67afe4dSAndroid Build Coastguard Worker  * or written, not the amount available (as in a z_stream).  The data pointers
479*a67afe4dSAndroid Build Coastguard Worker  * are not changed, so the next input is (data+input_size) and the next
480*a67afe4dSAndroid Build Coastguard Worker  * available output is (output+output_size).
481*a67afe4dSAndroid Build Coastguard Worker  */
482*a67afe4dSAndroid Build Coastguard Worker static int
png_inflate(png_structrp png_ptr,png_uint_32 owner,int finish,png_const_bytep input,png_uint_32p input_size_ptr,png_bytep output,png_alloc_size_t * output_size_ptr)483*a67afe4dSAndroid Build Coastguard Worker png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish,
484*a67afe4dSAndroid Build Coastguard Worker     /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr,
485*a67afe4dSAndroid Build Coastguard Worker     /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr)
486*a67afe4dSAndroid Build Coastguard Worker {
487*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->zowner == owner) /* Else not claimed */
488*a67afe4dSAndroid Build Coastguard Worker    {
489*a67afe4dSAndroid Build Coastguard Worker       int ret;
490*a67afe4dSAndroid Build Coastguard Worker       png_alloc_size_t avail_out = *output_size_ptr;
491*a67afe4dSAndroid Build Coastguard Worker       png_uint_32 avail_in = *input_size_ptr;
492*a67afe4dSAndroid Build Coastguard Worker 
493*a67afe4dSAndroid Build Coastguard Worker       /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it
494*a67afe4dSAndroid Build Coastguard Worker        * can't even necessarily handle 65536 bytes) because the type uInt is
495*a67afe4dSAndroid Build Coastguard Worker        * "16 bits or more".  Consequently it is necessary to chunk the input to
496*a67afe4dSAndroid Build Coastguard Worker        * zlib.  This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the
497*a67afe4dSAndroid Build Coastguard Worker        * maximum value that can be stored in a uInt.)  It is possible to set
498*a67afe4dSAndroid Build Coastguard Worker        * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have
499*a67afe4dSAndroid Build Coastguard Worker        * a performance advantage, because it reduces the amount of data accessed
500*a67afe4dSAndroid Build Coastguard Worker        * at each step and that may give the OS more time to page it in.
501*a67afe4dSAndroid Build Coastguard Worker        */
502*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
503*a67afe4dSAndroid Build Coastguard Worker       /* avail_in and avail_out are set below from 'size' */
504*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_in = 0;
505*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_out = 0;
506*a67afe4dSAndroid Build Coastguard Worker 
507*a67afe4dSAndroid Build Coastguard Worker       /* Read directly into the output if it is available (this is set to
508*a67afe4dSAndroid Build Coastguard Worker        * a local buffer below if output is NULL).
509*a67afe4dSAndroid Build Coastguard Worker        */
510*a67afe4dSAndroid Build Coastguard Worker       if (output != NULL)
511*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.next_out = output;
512*a67afe4dSAndroid Build Coastguard Worker 
513*a67afe4dSAndroid Build Coastguard Worker       do
514*a67afe4dSAndroid Build Coastguard Worker       {
515*a67afe4dSAndroid Build Coastguard Worker          uInt avail;
516*a67afe4dSAndroid Build Coastguard Worker          Byte local_buffer[PNG_INFLATE_BUF_SIZE];
517*a67afe4dSAndroid Build Coastguard Worker 
518*a67afe4dSAndroid Build Coastguard Worker          /* zlib INPUT BUFFER */
519*a67afe4dSAndroid Build Coastguard Worker          /* The setting of 'avail_in' used to be outside the loop; by setting it
520*a67afe4dSAndroid Build Coastguard Worker           * inside it is possible to chunk the input to zlib and simply rely on
521*a67afe4dSAndroid Build Coastguard Worker           * zlib to advance the 'next_in' pointer.  This allows arbitrary
522*a67afe4dSAndroid Build Coastguard Worker           * amounts of data to be passed through zlib at the unavoidable cost of
523*a67afe4dSAndroid Build Coastguard Worker           * requiring a window save (memcpy of up to 32768 output bytes)
524*a67afe4dSAndroid Build Coastguard Worker           * every ZLIB_IO_MAX input bytes.
525*a67afe4dSAndroid Build Coastguard Worker           */
526*a67afe4dSAndroid Build Coastguard Worker          avail_in += png_ptr->zstream.avail_in; /* not consumed last time */
527*a67afe4dSAndroid Build Coastguard Worker 
528*a67afe4dSAndroid Build Coastguard Worker          avail = ZLIB_IO_MAX;
529*a67afe4dSAndroid Build Coastguard Worker 
530*a67afe4dSAndroid Build Coastguard Worker          if (avail_in < avail)
531*a67afe4dSAndroid Build Coastguard Worker             avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */
532*a67afe4dSAndroid Build Coastguard Worker 
533*a67afe4dSAndroid Build Coastguard Worker          avail_in -= avail;
534*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.avail_in = avail;
535*a67afe4dSAndroid Build Coastguard Worker 
536*a67afe4dSAndroid Build Coastguard Worker          /* zlib OUTPUT BUFFER */
537*a67afe4dSAndroid Build Coastguard Worker          avail_out += png_ptr->zstream.avail_out; /* not written last time */
538*a67afe4dSAndroid Build Coastguard Worker 
539*a67afe4dSAndroid Build Coastguard Worker          avail = ZLIB_IO_MAX; /* maximum zlib can process */
540*a67afe4dSAndroid Build Coastguard Worker 
541*a67afe4dSAndroid Build Coastguard Worker          if (output == NULL)
542*a67afe4dSAndroid Build Coastguard Worker          {
543*a67afe4dSAndroid Build Coastguard Worker             /* Reset the output buffer each time round if output is NULL and
544*a67afe4dSAndroid Build Coastguard Worker              * make available the full buffer, up to 'remaining_space'
545*a67afe4dSAndroid Build Coastguard Worker              */
546*a67afe4dSAndroid Build Coastguard Worker             png_ptr->zstream.next_out = local_buffer;
547*a67afe4dSAndroid Build Coastguard Worker             if ((sizeof local_buffer) < avail)
548*a67afe4dSAndroid Build Coastguard Worker                avail = (sizeof local_buffer);
549*a67afe4dSAndroid Build Coastguard Worker          }
550*a67afe4dSAndroid Build Coastguard Worker 
551*a67afe4dSAndroid Build Coastguard Worker          if (avail_out < avail)
552*a67afe4dSAndroid Build Coastguard Worker             avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */
553*a67afe4dSAndroid Build Coastguard Worker 
554*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.avail_out = avail;
555*a67afe4dSAndroid Build Coastguard Worker          avail_out -= avail;
556*a67afe4dSAndroid Build Coastguard Worker 
557*a67afe4dSAndroid Build Coastguard Worker          /* zlib inflate call */
558*a67afe4dSAndroid Build Coastguard Worker          /* In fact 'avail_out' may be 0 at this point, that happens at the end
559*a67afe4dSAndroid Build Coastguard Worker           * of the read when the final LZ end code was not passed at the end of
560*a67afe4dSAndroid Build Coastguard Worker           * the previous chunk of input data.  Tell zlib if we have reached the
561*a67afe4dSAndroid Build Coastguard Worker           * end of the output buffer.
562*a67afe4dSAndroid Build Coastguard Worker           */
563*a67afe4dSAndroid Build Coastguard Worker          ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
564*a67afe4dSAndroid Build Coastguard Worker              (finish ? Z_FINISH : Z_SYNC_FLUSH));
565*a67afe4dSAndroid Build Coastguard Worker       } while (ret == Z_OK);
566*a67afe4dSAndroid Build Coastguard Worker 
567*a67afe4dSAndroid Build Coastguard Worker       /* For safety kill the local buffer pointer now */
568*a67afe4dSAndroid Build Coastguard Worker       if (output == NULL)
569*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.next_out = NULL;
570*a67afe4dSAndroid Build Coastguard Worker 
571*a67afe4dSAndroid Build Coastguard Worker       /* Claw back the 'size' and 'remaining_space' byte counts. */
572*a67afe4dSAndroid Build Coastguard Worker       avail_in += png_ptr->zstream.avail_in;
573*a67afe4dSAndroid Build Coastguard Worker       avail_out += png_ptr->zstream.avail_out;
574*a67afe4dSAndroid Build Coastguard Worker 
575*a67afe4dSAndroid Build Coastguard Worker       /* Update the input and output sizes; the updated values are the amount
576*a67afe4dSAndroid Build Coastguard Worker        * consumed or written, effectively the inverse of what zlib uses.
577*a67afe4dSAndroid Build Coastguard Worker        */
578*a67afe4dSAndroid Build Coastguard Worker       if (avail_out > 0)
579*a67afe4dSAndroid Build Coastguard Worker          *output_size_ptr -= avail_out;
580*a67afe4dSAndroid Build Coastguard Worker 
581*a67afe4dSAndroid Build Coastguard Worker       if (avail_in > 0)
582*a67afe4dSAndroid Build Coastguard Worker          *input_size_ptr -= avail_in;
583*a67afe4dSAndroid Build Coastguard Worker 
584*a67afe4dSAndroid Build Coastguard Worker       /* Ensure png_ptr->zstream.msg is set (even in the success case!) */
585*a67afe4dSAndroid Build Coastguard Worker       png_zstream_error(png_ptr, ret);
586*a67afe4dSAndroid Build Coastguard Worker       return ret;
587*a67afe4dSAndroid Build Coastguard Worker    }
588*a67afe4dSAndroid Build Coastguard Worker 
589*a67afe4dSAndroid Build Coastguard Worker    else
590*a67afe4dSAndroid Build Coastguard Worker    {
591*a67afe4dSAndroid Build Coastguard Worker       /* This is a bad internal error.  The recovery assigns to the zstream msg
592*a67afe4dSAndroid Build Coastguard Worker        * pointer, which is not owned by the caller, but this is safe; it's only
593*a67afe4dSAndroid Build Coastguard Worker        * used on errors!
594*a67afe4dSAndroid Build Coastguard Worker        */
595*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
596*a67afe4dSAndroid Build Coastguard Worker       return Z_STREAM_ERROR;
597*a67afe4dSAndroid Build Coastguard Worker    }
598*a67afe4dSAndroid Build Coastguard Worker }
599*a67afe4dSAndroid Build Coastguard Worker 
600*a67afe4dSAndroid Build Coastguard Worker /*
601*a67afe4dSAndroid Build Coastguard Worker  * Decompress trailing data in a chunk.  The assumption is that read_buffer
602*a67afe4dSAndroid Build Coastguard Worker  * points at an allocated area holding the contents of a chunk with a
603*a67afe4dSAndroid Build Coastguard Worker  * trailing compressed part.  What we get back is an allocated area
604*a67afe4dSAndroid Build Coastguard Worker  * holding the original prefix part and an uncompressed version of the
605*a67afe4dSAndroid Build Coastguard Worker  * trailing part (the malloc area passed in is freed).
606*a67afe4dSAndroid Build Coastguard Worker  */
607*a67afe4dSAndroid Build Coastguard Worker static int
png_decompress_chunk(png_structrp png_ptr,png_uint_32 chunklength,png_uint_32 prefix_size,png_alloc_size_t * newlength,int terminate)608*a67afe4dSAndroid Build Coastguard Worker png_decompress_chunk(png_structrp png_ptr,
609*a67afe4dSAndroid Build Coastguard Worker     png_uint_32 chunklength, png_uint_32 prefix_size,
610*a67afe4dSAndroid Build Coastguard Worker     png_alloc_size_t *newlength /* must be initialized to the maximum! */,
611*a67afe4dSAndroid Build Coastguard Worker     int terminate /*add a '\0' to the end of the uncompressed data*/)
612*a67afe4dSAndroid Build Coastguard Worker {
613*a67afe4dSAndroid Build Coastguard Worker    /* TODO: implement different limits for different types of chunk.
614*a67afe4dSAndroid Build Coastguard Worker     *
615*a67afe4dSAndroid Build Coastguard Worker     * The caller supplies *newlength set to the maximum length of the
616*a67afe4dSAndroid Build Coastguard Worker     * uncompressed data, but this routine allocates space for the prefix and
617*a67afe4dSAndroid Build Coastguard Worker     * maybe a '\0' terminator too.  We have to assume that 'prefix_size' is
618*a67afe4dSAndroid Build Coastguard Worker     * limited only by the maximum chunk size.
619*a67afe4dSAndroid Build Coastguard Worker     */
620*a67afe4dSAndroid Build Coastguard Worker    png_alloc_size_t limit = PNG_SIZE_MAX;
621*a67afe4dSAndroid Build Coastguard Worker 
622*a67afe4dSAndroid Build Coastguard Worker # ifdef PNG_SET_USER_LIMITS_SUPPORTED
623*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_malloc_max > 0 &&
624*a67afe4dSAndroid Build Coastguard Worker        png_ptr->user_chunk_malloc_max < limit)
625*a67afe4dSAndroid Build Coastguard Worker       limit = png_ptr->user_chunk_malloc_max;
626*a67afe4dSAndroid Build Coastguard Worker # elif PNG_USER_CHUNK_MALLOC_MAX > 0
627*a67afe4dSAndroid Build Coastguard Worker    if (PNG_USER_CHUNK_MALLOC_MAX < limit)
628*a67afe4dSAndroid Build Coastguard Worker       limit = PNG_USER_CHUNK_MALLOC_MAX;
629*a67afe4dSAndroid Build Coastguard Worker # endif
630*a67afe4dSAndroid Build Coastguard Worker 
631*a67afe4dSAndroid Build Coastguard Worker    if (limit >= prefix_size + (terminate != 0))
632*a67afe4dSAndroid Build Coastguard Worker    {
633*a67afe4dSAndroid Build Coastguard Worker       int ret;
634*a67afe4dSAndroid Build Coastguard Worker 
635*a67afe4dSAndroid Build Coastguard Worker       limit -= prefix_size + (terminate != 0);
636*a67afe4dSAndroid Build Coastguard Worker 
637*a67afe4dSAndroid Build Coastguard Worker       if (limit < *newlength)
638*a67afe4dSAndroid Build Coastguard Worker          *newlength = limit;
639*a67afe4dSAndroid Build Coastguard Worker 
640*a67afe4dSAndroid Build Coastguard Worker       /* Now try to claim the stream. */
641*a67afe4dSAndroid Build Coastguard Worker       ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
642*a67afe4dSAndroid Build Coastguard Worker 
643*a67afe4dSAndroid Build Coastguard Worker       if (ret == Z_OK)
644*a67afe4dSAndroid Build Coastguard Worker       {
645*a67afe4dSAndroid Build Coastguard Worker          png_uint_32 lzsize = chunklength - prefix_size;
646*a67afe4dSAndroid Build Coastguard Worker 
647*a67afe4dSAndroid Build Coastguard Worker          ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
648*a67afe4dSAndroid Build Coastguard Worker              /* input: */ png_ptr->read_buffer + prefix_size, &lzsize,
649*a67afe4dSAndroid Build Coastguard Worker              /* output: */ NULL, newlength);
650*a67afe4dSAndroid Build Coastguard Worker 
651*a67afe4dSAndroid Build Coastguard Worker          if (ret == Z_STREAM_END)
652*a67afe4dSAndroid Build Coastguard Worker          {
653*a67afe4dSAndroid Build Coastguard Worker             /* Use 'inflateReset' here, not 'inflateReset2' because this
654*a67afe4dSAndroid Build Coastguard Worker              * preserves the previously decided window size (otherwise it would
655*a67afe4dSAndroid Build Coastguard Worker              * be necessary to store the previous window size.)  In practice
656*a67afe4dSAndroid Build Coastguard Worker              * this doesn't matter anyway, because png_inflate will call inflate
657*a67afe4dSAndroid Build Coastguard Worker              * with Z_FINISH in almost all cases, so the window will not be
658*a67afe4dSAndroid Build Coastguard Worker              * maintained.
659*a67afe4dSAndroid Build Coastguard Worker              */
660*a67afe4dSAndroid Build Coastguard Worker             if (inflateReset(&png_ptr->zstream) == Z_OK)
661*a67afe4dSAndroid Build Coastguard Worker             {
662*a67afe4dSAndroid Build Coastguard Worker                /* Because of the limit checks above we know that the new,
663*a67afe4dSAndroid Build Coastguard Worker                 * expanded, size will fit in a size_t (let alone an
664*a67afe4dSAndroid Build Coastguard Worker                 * png_alloc_size_t).  Use png_malloc_base here to avoid an
665*a67afe4dSAndroid Build Coastguard Worker                 * extra OOM message.
666*a67afe4dSAndroid Build Coastguard Worker                 */
667*a67afe4dSAndroid Build Coastguard Worker                png_alloc_size_t new_size = *newlength;
668*a67afe4dSAndroid Build Coastguard Worker                png_alloc_size_t buffer_size = prefix_size + new_size +
669*a67afe4dSAndroid Build Coastguard Worker                    (terminate != 0);
670*a67afe4dSAndroid Build Coastguard Worker                png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
671*a67afe4dSAndroid Build Coastguard Worker                    buffer_size));
672*a67afe4dSAndroid Build Coastguard Worker 
673*a67afe4dSAndroid Build Coastguard Worker                if (text != NULL)
674*a67afe4dSAndroid Build Coastguard Worker                {
675*a67afe4dSAndroid Build Coastguard Worker                   memset(text, 0, buffer_size);
676*a67afe4dSAndroid Build Coastguard Worker 
677*a67afe4dSAndroid Build Coastguard Worker                   ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/,
678*a67afe4dSAndroid Build Coastguard Worker                       png_ptr->read_buffer + prefix_size, &lzsize,
679*a67afe4dSAndroid Build Coastguard Worker                       text + prefix_size, newlength);
680*a67afe4dSAndroid Build Coastguard Worker 
681*a67afe4dSAndroid Build Coastguard Worker                   if (ret == Z_STREAM_END)
682*a67afe4dSAndroid Build Coastguard Worker                   {
683*a67afe4dSAndroid Build Coastguard Worker                      if (new_size == *newlength)
684*a67afe4dSAndroid Build Coastguard Worker                      {
685*a67afe4dSAndroid Build Coastguard Worker                         if (terminate != 0)
686*a67afe4dSAndroid Build Coastguard Worker                            text[prefix_size + *newlength] = 0;
687*a67afe4dSAndroid Build Coastguard Worker 
688*a67afe4dSAndroid Build Coastguard Worker                         if (prefix_size > 0)
689*a67afe4dSAndroid Build Coastguard Worker                            memcpy(text, png_ptr->read_buffer, prefix_size);
690*a67afe4dSAndroid Build Coastguard Worker 
691*a67afe4dSAndroid Build Coastguard Worker                         {
692*a67afe4dSAndroid Build Coastguard Worker                            png_bytep old_ptr = png_ptr->read_buffer;
693*a67afe4dSAndroid Build Coastguard Worker 
694*a67afe4dSAndroid Build Coastguard Worker                            png_ptr->read_buffer = text;
695*a67afe4dSAndroid Build Coastguard Worker                            png_ptr->read_buffer_size = buffer_size;
696*a67afe4dSAndroid Build Coastguard Worker                            text = old_ptr; /* freed below */
697*a67afe4dSAndroid Build Coastguard Worker                         }
698*a67afe4dSAndroid Build Coastguard Worker                      }
699*a67afe4dSAndroid Build Coastguard Worker 
700*a67afe4dSAndroid Build Coastguard Worker                      else
701*a67afe4dSAndroid Build Coastguard Worker                      {
702*a67afe4dSAndroid Build Coastguard Worker                         /* The size changed on the second read, there can be no
703*a67afe4dSAndroid Build Coastguard Worker                          * guarantee that anything is correct at this point.
704*a67afe4dSAndroid Build Coastguard Worker                          * The 'msg' pointer has been set to "unexpected end of
705*a67afe4dSAndroid Build Coastguard Worker                          * LZ stream", which is fine, but return an error code
706*a67afe4dSAndroid Build Coastguard Worker                          * that the caller won't accept.
707*a67afe4dSAndroid Build Coastguard Worker                          */
708*a67afe4dSAndroid Build Coastguard Worker                         ret = PNG_UNEXPECTED_ZLIB_RETURN;
709*a67afe4dSAndroid Build Coastguard Worker                      }
710*a67afe4dSAndroid Build Coastguard Worker                   }
711*a67afe4dSAndroid Build Coastguard Worker 
712*a67afe4dSAndroid Build Coastguard Worker                   else if (ret == Z_OK)
713*a67afe4dSAndroid Build Coastguard Worker                      ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */
714*a67afe4dSAndroid Build Coastguard Worker 
715*a67afe4dSAndroid Build Coastguard Worker                   /* Free the text pointer (this is the old read_buffer on
716*a67afe4dSAndroid Build Coastguard Worker                    * success)
717*a67afe4dSAndroid Build Coastguard Worker                    */
718*a67afe4dSAndroid Build Coastguard Worker                   png_free(png_ptr, text);
719*a67afe4dSAndroid Build Coastguard Worker 
720*a67afe4dSAndroid Build Coastguard Worker                   /* This really is very benign, but it's still an error because
721*a67afe4dSAndroid Build Coastguard Worker                    * the extra space may otherwise be used as a Trojan Horse.
722*a67afe4dSAndroid Build Coastguard Worker                    */
723*a67afe4dSAndroid Build Coastguard Worker                   if (ret == Z_STREAM_END &&
724*a67afe4dSAndroid Build Coastguard Worker                       chunklength - prefix_size != lzsize)
725*a67afe4dSAndroid Build Coastguard Worker                      png_chunk_benign_error(png_ptr, "extra compressed data");
726*a67afe4dSAndroid Build Coastguard Worker                }
727*a67afe4dSAndroid Build Coastguard Worker 
728*a67afe4dSAndroid Build Coastguard Worker                else
729*a67afe4dSAndroid Build Coastguard Worker                {
730*a67afe4dSAndroid Build Coastguard Worker                   /* Out of memory allocating the buffer */
731*a67afe4dSAndroid Build Coastguard Worker                   ret = Z_MEM_ERROR;
732*a67afe4dSAndroid Build Coastguard Worker                   png_zstream_error(png_ptr, Z_MEM_ERROR);
733*a67afe4dSAndroid Build Coastguard Worker                }
734*a67afe4dSAndroid Build Coastguard Worker             }
735*a67afe4dSAndroid Build Coastguard Worker 
736*a67afe4dSAndroid Build Coastguard Worker             else
737*a67afe4dSAndroid Build Coastguard Worker             {
738*a67afe4dSAndroid Build Coastguard Worker                /* inflateReset failed, store the error message */
739*a67afe4dSAndroid Build Coastguard Worker                png_zstream_error(png_ptr, ret);
740*a67afe4dSAndroid Build Coastguard Worker                ret = PNG_UNEXPECTED_ZLIB_RETURN;
741*a67afe4dSAndroid Build Coastguard Worker             }
742*a67afe4dSAndroid Build Coastguard Worker          }
743*a67afe4dSAndroid Build Coastguard Worker 
744*a67afe4dSAndroid Build Coastguard Worker          else if (ret == Z_OK)
745*a67afe4dSAndroid Build Coastguard Worker             ret = PNG_UNEXPECTED_ZLIB_RETURN;
746*a67afe4dSAndroid Build Coastguard Worker 
747*a67afe4dSAndroid Build Coastguard Worker          /* Release the claimed stream */
748*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zowner = 0;
749*a67afe4dSAndroid Build Coastguard Worker       }
750*a67afe4dSAndroid Build Coastguard Worker 
751*a67afe4dSAndroid Build Coastguard Worker       else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */
752*a67afe4dSAndroid Build Coastguard Worker          ret = PNG_UNEXPECTED_ZLIB_RETURN;
753*a67afe4dSAndroid Build Coastguard Worker 
754*a67afe4dSAndroid Build Coastguard Worker       return ret;
755*a67afe4dSAndroid Build Coastguard Worker    }
756*a67afe4dSAndroid Build Coastguard Worker 
757*a67afe4dSAndroid Build Coastguard Worker    else
758*a67afe4dSAndroid Build Coastguard Worker    {
759*a67afe4dSAndroid Build Coastguard Worker       /* Application/configuration limits exceeded */
760*a67afe4dSAndroid Build Coastguard Worker       png_zstream_error(png_ptr, Z_MEM_ERROR);
761*a67afe4dSAndroid Build Coastguard Worker       return Z_MEM_ERROR;
762*a67afe4dSAndroid Build Coastguard Worker    }
763*a67afe4dSAndroid Build Coastguard Worker }
764*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_zTXt || READ_iTXt */
765*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_COMPRESSED_TEXT */
766*a67afe4dSAndroid Build Coastguard Worker 
767*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_iCCP_SUPPORTED
768*a67afe4dSAndroid Build Coastguard Worker /* Perform a partial read and decompress, producing 'avail_out' bytes and
769*a67afe4dSAndroid Build Coastguard Worker  * reading from the current chunk as required.
770*a67afe4dSAndroid Build Coastguard Worker  */
771*a67afe4dSAndroid Build Coastguard Worker static int
png_inflate_read(png_structrp png_ptr,png_bytep read_buffer,uInt read_size,png_uint_32p chunk_bytes,png_bytep next_out,png_alloc_size_t * out_size,int finish)772*a67afe4dSAndroid Build Coastguard Worker png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
773*a67afe4dSAndroid Build Coastguard Worker     png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
774*a67afe4dSAndroid Build Coastguard Worker     int finish)
775*a67afe4dSAndroid Build Coastguard Worker {
776*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->zowner == png_ptr->chunk_name)
777*a67afe4dSAndroid Build Coastguard Worker    {
778*a67afe4dSAndroid Build Coastguard Worker       int ret;
779*a67afe4dSAndroid Build Coastguard Worker 
780*a67afe4dSAndroid Build Coastguard Worker       /* next_in and avail_in must have been initialized by the caller. */
781*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_out = next_out;
782*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_out = 0; /* set in the loop */
783*a67afe4dSAndroid Build Coastguard Worker 
784*a67afe4dSAndroid Build Coastguard Worker       do
785*a67afe4dSAndroid Build Coastguard Worker       {
786*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->zstream.avail_in == 0)
787*a67afe4dSAndroid Build Coastguard Worker          {
788*a67afe4dSAndroid Build Coastguard Worker             if (read_size > *chunk_bytes)
789*a67afe4dSAndroid Build Coastguard Worker                read_size = (uInt)*chunk_bytes;
790*a67afe4dSAndroid Build Coastguard Worker             *chunk_bytes -= read_size;
791*a67afe4dSAndroid Build Coastguard Worker 
792*a67afe4dSAndroid Build Coastguard Worker             if (read_size > 0)
793*a67afe4dSAndroid Build Coastguard Worker                png_crc_read(png_ptr, read_buffer, read_size);
794*a67afe4dSAndroid Build Coastguard Worker 
795*a67afe4dSAndroid Build Coastguard Worker             png_ptr->zstream.next_in = read_buffer;
796*a67afe4dSAndroid Build Coastguard Worker             png_ptr->zstream.avail_in = read_size;
797*a67afe4dSAndroid Build Coastguard Worker          }
798*a67afe4dSAndroid Build Coastguard Worker 
799*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->zstream.avail_out == 0)
800*a67afe4dSAndroid Build Coastguard Worker          {
801*a67afe4dSAndroid Build Coastguard Worker             uInt avail = ZLIB_IO_MAX;
802*a67afe4dSAndroid Build Coastguard Worker             if (avail > *out_size)
803*a67afe4dSAndroid Build Coastguard Worker                avail = (uInt)*out_size;
804*a67afe4dSAndroid Build Coastguard Worker             *out_size -= avail;
805*a67afe4dSAndroid Build Coastguard Worker 
806*a67afe4dSAndroid Build Coastguard Worker             png_ptr->zstream.avail_out = avail;
807*a67afe4dSAndroid Build Coastguard Worker          }
808*a67afe4dSAndroid Build Coastguard Worker 
809*a67afe4dSAndroid Build Coastguard Worker          /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all
810*a67afe4dSAndroid Build Coastguard Worker           * the available output is produced; this allows reading of truncated
811*a67afe4dSAndroid Build Coastguard Worker           * streams.
812*a67afe4dSAndroid Build Coastguard Worker           */
813*a67afe4dSAndroid Build Coastguard Worker          ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
814*a67afe4dSAndroid Build Coastguard Worker              Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
815*a67afe4dSAndroid Build Coastguard Worker       }
816*a67afe4dSAndroid Build Coastguard Worker       while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
817*a67afe4dSAndroid Build Coastguard Worker 
818*a67afe4dSAndroid Build Coastguard Worker       *out_size += png_ptr->zstream.avail_out;
819*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */
820*a67afe4dSAndroid Build Coastguard Worker 
821*a67afe4dSAndroid Build Coastguard Worker       /* Ensure the error message pointer is always set: */
822*a67afe4dSAndroid Build Coastguard Worker       png_zstream_error(png_ptr, ret);
823*a67afe4dSAndroid Build Coastguard Worker       return ret;
824*a67afe4dSAndroid Build Coastguard Worker    }
825*a67afe4dSAndroid Build Coastguard Worker 
826*a67afe4dSAndroid Build Coastguard Worker    else
827*a67afe4dSAndroid Build Coastguard Worker    {
828*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed");
829*a67afe4dSAndroid Build Coastguard Worker       return Z_STREAM_ERROR;
830*a67afe4dSAndroid Build Coastguard Worker    }
831*a67afe4dSAndroid Build Coastguard Worker }
832*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_iCCP */
833*a67afe4dSAndroid Build Coastguard Worker 
834*a67afe4dSAndroid Build Coastguard Worker /* Read and check the IDHR chunk */
835*a67afe4dSAndroid Build Coastguard Worker 
836*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_IHDR(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)837*a67afe4dSAndroid Build Coastguard Worker png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
838*a67afe4dSAndroid Build Coastguard Worker {
839*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[13];
840*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 width, height;
841*a67afe4dSAndroid Build Coastguard Worker    int bit_depth, color_type, compression_type, filter_type;
842*a67afe4dSAndroid Build Coastguard Worker    int interlace_type;
843*a67afe4dSAndroid Build Coastguard Worker 
844*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_IHDR");
845*a67afe4dSAndroid Build Coastguard Worker 
846*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
847*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "out of place");
848*a67afe4dSAndroid Build Coastguard Worker 
849*a67afe4dSAndroid Build Coastguard Worker    /* Check the length */
850*a67afe4dSAndroid Build Coastguard Worker    if (length != 13)
851*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "invalid");
852*a67afe4dSAndroid Build Coastguard Worker 
853*a67afe4dSAndroid Build Coastguard Worker    png_ptr->mode |= PNG_HAVE_IHDR;
854*a67afe4dSAndroid Build Coastguard Worker 
855*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 13);
856*a67afe4dSAndroid Build Coastguard Worker    png_crc_finish(png_ptr, 0);
857*a67afe4dSAndroid Build Coastguard Worker 
858*a67afe4dSAndroid Build Coastguard Worker    width = png_get_uint_31(png_ptr, buf);
859*a67afe4dSAndroid Build Coastguard Worker    height = png_get_uint_31(png_ptr, buf + 4);
860*a67afe4dSAndroid Build Coastguard Worker    bit_depth = buf[8];
861*a67afe4dSAndroid Build Coastguard Worker    color_type = buf[9];
862*a67afe4dSAndroid Build Coastguard Worker    compression_type = buf[10];
863*a67afe4dSAndroid Build Coastguard Worker    filter_type = buf[11];
864*a67afe4dSAndroid Build Coastguard Worker    interlace_type = buf[12];
865*a67afe4dSAndroid Build Coastguard Worker 
866*a67afe4dSAndroid Build Coastguard Worker    /* Set internal variables */
867*a67afe4dSAndroid Build Coastguard Worker    png_ptr->width = width;
868*a67afe4dSAndroid Build Coastguard Worker    png_ptr->height = height;
869*a67afe4dSAndroid Build Coastguard Worker    png_ptr->bit_depth = (png_byte)bit_depth;
870*a67afe4dSAndroid Build Coastguard Worker    png_ptr->interlaced = (png_byte)interlace_type;
871*a67afe4dSAndroid Build Coastguard Worker    png_ptr->color_type = (png_byte)color_type;
872*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_MNG_FEATURES_SUPPORTED
873*a67afe4dSAndroid Build Coastguard Worker    png_ptr->filter_type = (png_byte)filter_type;
874*a67afe4dSAndroid Build Coastguard Worker #endif
875*a67afe4dSAndroid Build Coastguard Worker    png_ptr->compression_type = (png_byte)compression_type;
876*a67afe4dSAndroid Build Coastguard Worker 
877*a67afe4dSAndroid Build Coastguard Worker    /* Find number of channels */
878*a67afe4dSAndroid Build Coastguard Worker    switch (png_ptr->color_type)
879*a67afe4dSAndroid Build Coastguard Worker    {
880*a67afe4dSAndroid Build Coastguard Worker       default: /* invalid, png_set_IHDR calls png_error */
881*a67afe4dSAndroid Build Coastguard Worker       case PNG_COLOR_TYPE_GRAY:
882*a67afe4dSAndroid Build Coastguard Worker       case PNG_COLOR_TYPE_PALETTE:
883*a67afe4dSAndroid Build Coastguard Worker          png_ptr->channels = 1;
884*a67afe4dSAndroid Build Coastguard Worker          break;
885*a67afe4dSAndroid Build Coastguard Worker 
886*a67afe4dSAndroid Build Coastguard Worker       case PNG_COLOR_TYPE_RGB:
887*a67afe4dSAndroid Build Coastguard Worker          png_ptr->channels = 3;
888*a67afe4dSAndroid Build Coastguard Worker          break;
889*a67afe4dSAndroid Build Coastguard Worker 
890*a67afe4dSAndroid Build Coastguard Worker       case PNG_COLOR_TYPE_GRAY_ALPHA:
891*a67afe4dSAndroid Build Coastguard Worker          png_ptr->channels = 2;
892*a67afe4dSAndroid Build Coastguard Worker          break;
893*a67afe4dSAndroid Build Coastguard Worker 
894*a67afe4dSAndroid Build Coastguard Worker       case PNG_COLOR_TYPE_RGB_ALPHA:
895*a67afe4dSAndroid Build Coastguard Worker          png_ptr->channels = 4;
896*a67afe4dSAndroid Build Coastguard Worker          break;
897*a67afe4dSAndroid Build Coastguard Worker    }
898*a67afe4dSAndroid Build Coastguard Worker 
899*a67afe4dSAndroid Build Coastguard Worker    /* Set up other useful info */
900*a67afe4dSAndroid Build Coastguard Worker    png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
901*a67afe4dSAndroid Build Coastguard Worker    png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
902*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
903*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "channels = %d", png_ptr->channels);
904*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
905*a67afe4dSAndroid Build Coastguard Worker    png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
906*a67afe4dSAndroid Build Coastguard Worker        color_type, interlace_type, compression_type, filter_type);
907*a67afe4dSAndroid Build Coastguard Worker }
908*a67afe4dSAndroid Build Coastguard Worker 
909*a67afe4dSAndroid Build Coastguard Worker /* Read and check the palette */
910*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_PLTE(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)911*a67afe4dSAndroid Build Coastguard Worker png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
912*a67afe4dSAndroid Build Coastguard Worker {
913*a67afe4dSAndroid Build Coastguard Worker    png_color palette[PNG_MAX_PALETTE_LENGTH];
914*a67afe4dSAndroid Build Coastguard Worker    int max_palette_length, num, i;
915*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_POINTER_INDEXING_SUPPORTED
916*a67afe4dSAndroid Build Coastguard Worker    png_colorp pal_ptr;
917*a67afe4dSAndroid Build Coastguard Worker #endif
918*a67afe4dSAndroid Build Coastguard Worker 
919*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_PLTE");
920*a67afe4dSAndroid Build Coastguard Worker 
921*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
922*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
923*a67afe4dSAndroid Build Coastguard Worker 
924*a67afe4dSAndroid Build Coastguard Worker    /* Moved to before the 'after IDAT' check below because otherwise duplicate
925*a67afe4dSAndroid Build Coastguard Worker     * PLTE chunks are potentially ignored (the spec says there shall not be more
926*a67afe4dSAndroid Build Coastguard Worker     * than one PLTE, the error is not treated as benign, so this check trumps
927*a67afe4dSAndroid Build Coastguard Worker     * the requirement that PLTE appears before IDAT.)
928*a67afe4dSAndroid Build Coastguard Worker     */
929*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
930*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "duplicate");
931*a67afe4dSAndroid Build Coastguard Worker 
932*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
933*a67afe4dSAndroid Build Coastguard Worker    {
934*a67afe4dSAndroid Build Coastguard Worker       /* This is benign because the non-benign error happened before, when an
935*a67afe4dSAndroid Build Coastguard Worker        * IDAT was encountered in a color-mapped image with no PLTE.
936*a67afe4dSAndroid Build Coastguard Worker        */
937*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
938*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
939*a67afe4dSAndroid Build Coastguard Worker       return;
940*a67afe4dSAndroid Build Coastguard Worker    }
941*a67afe4dSAndroid Build Coastguard Worker 
942*a67afe4dSAndroid Build Coastguard Worker    png_ptr->mode |= PNG_HAVE_PLTE;
943*a67afe4dSAndroid Build Coastguard Worker 
944*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
945*a67afe4dSAndroid Build Coastguard Worker    {
946*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
947*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
948*a67afe4dSAndroid Build Coastguard Worker       return;
949*a67afe4dSAndroid Build Coastguard Worker    }
950*a67afe4dSAndroid Build Coastguard Worker 
951*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_READ_OPT_PLTE_SUPPORTED
952*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
953*a67afe4dSAndroid Build Coastguard Worker    {
954*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
955*a67afe4dSAndroid Build Coastguard Worker       return;
956*a67afe4dSAndroid Build Coastguard Worker    }
957*a67afe4dSAndroid Build Coastguard Worker #endif
958*a67afe4dSAndroid Build Coastguard Worker 
959*a67afe4dSAndroid Build Coastguard Worker    if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
960*a67afe4dSAndroid Build Coastguard Worker    {
961*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
962*a67afe4dSAndroid Build Coastguard Worker 
963*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
964*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid");
965*a67afe4dSAndroid Build Coastguard Worker 
966*a67afe4dSAndroid Build Coastguard Worker       else
967*a67afe4dSAndroid Build Coastguard Worker          png_chunk_error(png_ptr, "invalid");
968*a67afe4dSAndroid Build Coastguard Worker 
969*a67afe4dSAndroid Build Coastguard Worker       return;
970*a67afe4dSAndroid Build Coastguard Worker    }
971*a67afe4dSAndroid Build Coastguard Worker 
972*a67afe4dSAndroid Build Coastguard Worker    /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
973*a67afe4dSAndroid Build Coastguard Worker    num = (int)length / 3;
974*a67afe4dSAndroid Build Coastguard Worker 
975*a67afe4dSAndroid Build Coastguard Worker    /* If the palette has 256 or fewer entries but is too large for the bit
976*a67afe4dSAndroid Build Coastguard Worker     * depth, we don't issue an error, to preserve the behavior of previous
977*a67afe4dSAndroid Build Coastguard Worker     * libpng versions. We silently truncate the unused extra palette entries
978*a67afe4dSAndroid Build Coastguard Worker     * here.
979*a67afe4dSAndroid Build Coastguard Worker     */
980*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
981*a67afe4dSAndroid Build Coastguard Worker       max_palette_length = (1 << png_ptr->bit_depth);
982*a67afe4dSAndroid Build Coastguard Worker    else
983*a67afe4dSAndroid Build Coastguard Worker       max_palette_length = PNG_MAX_PALETTE_LENGTH;
984*a67afe4dSAndroid Build Coastguard Worker 
985*a67afe4dSAndroid Build Coastguard Worker    if (num > max_palette_length)
986*a67afe4dSAndroid Build Coastguard Worker       num = max_palette_length;
987*a67afe4dSAndroid Build Coastguard Worker 
988*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_POINTER_INDEXING_SUPPORTED
989*a67afe4dSAndroid Build Coastguard Worker    for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
990*a67afe4dSAndroid Build Coastguard Worker    {
991*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[3];
992*a67afe4dSAndroid Build Coastguard Worker 
993*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, 3);
994*a67afe4dSAndroid Build Coastguard Worker       pal_ptr->red = buf[0];
995*a67afe4dSAndroid Build Coastguard Worker       pal_ptr->green = buf[1];
996*a67afe4dSAndroid Build Coastguard Worker       pal_ptr->blue = buf[2];
997*a67afe4dSAndroid Build Coastguard Worker    }
998*a67afe4dSAndroid Build Coastguard Worker #else
999*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < num; i++)
1000*a67afe4dSAndroid Build Coastguard Worker    {
1001*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[3];
1002*a67afe4dSAndroid Build Coastguard Worker 
1003*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, 3);
1004*a67afe4dSAndroid Build Coastguard Worker       /* Don't depend upon png_color being any order */
1005*a67afe4dSAndroid Build Coastguard Worker       palette[i].red = buf[0];
1006*a67afe4dSAndroid Build Coastguard Worker       palette[i].green = buf[1];
1007*a67afe4dSAndroid Build Coastguard Worker       palette[i].blue = buf[2];
1008*a67afe4dSAndroid Build Coastguard Worker    }
1009*a67afe4dSAndroid Build Coastguard Worker #endif
1010*a67afe4dSAndroid Build Coastguard Worker 
1011*a67afe4dSAndroid Build Coastguard Worker    /* If we actually need the PLTE chunk (ie for a paletted image), we do
1012*a67afe4dSAndroid Build Coastguard Worker     * whatever the normal CRC configuration tells us.  However, if we
1013*a67afe4dSAndroid Build Coastguard Worker     * have an RGB image, the PLTE can be considered ancillary, so
1014*a67afe4dSAndroid Build Coastguard Worker     * we will act as though it is.
1015*a67afe4dSAndroid Build Coastguard Worker     */
1016*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1017*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1018*a67afe4dSAndroid Build Coastguard Worker #endif
1019*a67afe4dSAndroid Build Coastguard Worker    {
1020*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3));
1021*a67afe4dSAndroid Build Coastguard Worker    }
1022*a67afe4dSAndroid Build Coastguard Worker 
1023*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_READ_OPT_PLTE_SUPPORTED
1024*a67afe4dSAndroid Build Coastguard Worker    else if (png_crc_error(png_ptr) != 0)  /* Only if we have a CRC error */
1025*a67afe4dSAndroid Build Coastguard Worker    {
1026*a67afe4dSAndroid Build Coastguard Worker       /* If we don't want to use the data from an ancillary chunk,
1027*a67afe4dSAndroid Build Coastguard Worker        * we have two options: an error abort, or a warning and we
1028*a67afe4dSAndroid Build Coastguard Worker        * ignore the data in this chunk (which should be OK, since
1029*a67afe4dSAndroid Build Coastguard Worker        * it's considered ancillary for a RGB or RGBA image).
1030*a67afe4dSAndroid Build Coastguard Worker        *
1031*a67afe4dSAndroid Build Coastguard Worker        * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
1032*a67afe4dSAndroid Build Coastguard Worker        * chunk type to determine whether to check the ancillary or the critical
1033*a67afe4dSAndroid Build Coastguard Worker        * flags.
1034*a67afe4dSAndroid Build Coastguard Worker        */
1035*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
1036*a67afe4dSAndroid Build Coastguard Worker       {
1037*a67afe4dSAndroid Build Coastguard Worker          if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
1038*a67afe4dSAndroid Build Coastguard Worker             return;
1039*a67afe4dSAndroid Build Coastguard Worker 
1040*a67afe4dSAndroid Build Coastguard Worker          else
1041*a67afe4dSAndroid Build Coastguard Worker             png_chunk_error(png_ptr, "CRC error");
1042*a67afe4dSAndroid Build Coastguard Worker       }
1043*a67afe4dSAndroid Build Coastguard Worker 
1044*a67afe4dSAndroid Build Coastguard Worker       /* Otherwise, we (optionally) emit a warning and use the chunk. */
1045*a67afe4dSAndroid Build Coastguard Worker       else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
1046*a67afe4dSAndroid Build Coastguard Worker          png_chunk_warning(png_ptr, "CRC error");
1047*a67afe4dSAndroid Build Coastguard Worker    }
1048*a67afe4dSAndroid Build Coastguard Worker #endif
1049*a67afe4dSAndroid Build Coastguard Worker 
1050*a67afe4dSAndroid Build Coastguard Worker    /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
1051*a67afe4dSAndroid Build Coastguard Worker     * own copy of the palette.  This has the side effect that when png_start_row
1052*a67afe4dSAndroid Build Coastguard Worker     * is called (this happens after any call to png_read_update_info) the
1053*a67afe4dSAndroid Build Coastguard Worker     * info_ptr palette gets changed.  This is extremely unexpected and
1054*a67afe4dSAndroid Build Coastguard Worker     * confusing.
1055*a67afe4dSAndroid Build Coastguard Worker     *
1056*a67afe4dSAndroid Build Coastguard Worker     * Fix this by not sharing the palette in this way.
1057*a67afe4dSAndroid Build Coastguard Worker     */
1058*a67afe4dSAndroid Build Coastguard Worker    png_set_PLTE(png_ptr, info_ptr, palette, num);
1059*a67afe4dSAndroid Build Coastguard Worker 
1060*a67afe4dSAndroid Build Coastguard Worker    /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1061*a67afe4dSAndroid Build Coastguard Worker     * IDAT.  Prior to 1.6.0 this was not checked; instead the code merely
1062*a67afe4dSAndroid Build Coastguard Worker     * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1063*a67afe4dSAndroid Build Coastguard Worker     * palette PNG.  1.6.0 attempts to rigorously follow the standard and
1064*a67afe4dSAndroid Build Coastguard Worker     * therefore does a benign error if the erroneous condition is detected *and*
1065*a67afe4dSAndroid Build Coastguard Worker     * cancels the tRNS if the benign error returns.  The alternative is to
1066*a67afe4dSAndroid Build Coastguard Worker     * amend the standard since it would be rather hypocritical of the standards
1067*a67afe4dSAndroid Build Coastguard Worker     * maintainers to ignore it.
1068*a67afe4dSAndroid Build Coastguard Worker     */
1069*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_tRNS_SUPPORTED
1070*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->num_trans > 0 ||
1071*a67afe4dSAndroid Build Coastguard Worker        (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1072*a67afe4dSAndroid Build Coastguard Worker    {
1073*a67afe4dSAndroid Build Coastguard Worker       /* Cancel this because otherwise it would be used if the transforms
1074*a67afe4dSAndroid Build Coastguard Worker        * require it.  Don't cancel the 'valid' flag because this would prevent
1075*a67afe4dSAndroid Build Coastguard Worker        * detection of duplicate chunks.
1076*a67afe4dSAndroid Build Coastguard Worker        */
1077*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_trans = 0;
1078*a67afe4dSAndroid Build Coastguard Worker 
1079*a67afe4dSAndroid Build Coastguard Worker       if (info_ptr != NULL)
1080*a67afe4dSAndroid Build Coastguard Worker          info_ptr->num_trans = 0;
1081*a67afe4dSAndroid Build Coastguard Worker 
1082*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "tRNS must be after");
1083*a67afe4dSAndroid Build Coastguard Worker    }
1084*a67afe4dSAndroid Build Coastguard Worker #endif
1085*a67afe4dSAndroid Build Coastguard Worker 
1086*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_hIST_SUPPORTED
1087*a67afe4dSAndroid Build Coastguard Worker    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1088*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "hIST must be after");
1089*a67afe4dSAndroid Build Coastguard Worker #endif
1090*a67afe4dSAndroid Build Coastguard Worker 
1091*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_bKGD_SUPPORTED
1092*a67afe4dSAndroid Build Coastguard Worker    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1093*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "bKGD must be after");
1094*a67afe4dSAndroid Build Coastguard Worker #endif
1095*a67afe4dSAndroid Build Coastguard Worker }
1096*a67afe4dSAndroid Build Coastguard Worker 
1097*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_IEND(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1098*a67afe4dSAndroid Build Coastguard Worker png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1099*a67afe4dSAndroid Build Coastguard Worker {
1100*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_IEND");
1101*a67afe4dSAndroid Build Coastguard Worker 
1102*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1103*a67afe4dSAndroid Build Coastguard Worker        (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1104*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "out of place");
1105*a67afe4dSAndroid Build Coastguard Worker 
1106*a67afe4dSAndroid Build Coastguard Worker    png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1107*a67afe4dSAndroid Build Coastguard Worker 
1108*a67afe4dSAndroid Build Coastguard Worker    png_crc_finish(png_ptr, length);
1109*a67afe4dSAndroid Build Coastguard Worker 
1110*a67afe4dSAndroid Build Coastguard Worker    if (length != 0)
1111*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1112*a67afe4dSAndroid Build Coastguard Worker 
1113*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(info_ptr)
1114*a67afe4dSAndroid Build Coastguard Worker }
1115*a67afe4dSAndroid Build Coastguard Worker 
1116*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_gAMA_SUPPORTED
1117*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_gAMA(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1118*a67afe4dSAndroid Build Coastguard Worker png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1119*a67afe4dSAndroid Build Coastguard Worker {
1120*a67afe4dSAndroid Build Coastguard Worker    png_fixed_point igamma;
1121*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[4];
1122*a67afe4dSAndroid Build Coastguard Worker 
1123*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_gAMA");
1124*a67afe4dSAndroid Build Coastguard Worker 
1125*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1126*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1127*a67afe4dSAndroid Build Coastguard Worker 
1128*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1129*a67afe4dSAndroid Build Coastguard Worker    {
1130*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1131*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1132*a67afe4dSAndroid Build Coastguard Worker       return;
1133*a67afe4dSAndroid Build Coastguard Worker    }
1134*a67afe4dSAndroid Build Coastguard Worker 
1135*a67afe4dSAndroid Build Coastguard Worker    if (length != 4)
1136*a67afe4dSAndroid Build Coastguard Worker    {
1137*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1138*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1139*a67afe4dSAndroid Build Coastguard Worker       return;
1140*a67afe4dSAndroid Build Coastguard Worker    }
1141*a67afe4dSAndroid Build Coastguard Worker 
1142*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 4);
1143*a67afe4dSAndroid Build Coastguard Worker 
1144*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1145*a67afe4dSAndroid Build Coastguard Worker       return;
1146*a67afe4dSAndroid Build Coastguard Worker 
1147*a67afe4dSAndroid Build Coastguard Worker    igamma = png_get_fixed_point(NULL, buf);
1148*a67afe4dSAndroid Build Coastguard Worker 
1149*a67afe4dSAndroid Build Coastguard Worker    png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1150*a67afe4dSAndroid Build Coastguard Worker    png_colorspace_sync(png_ptr, info_ptr);
1151*a67afe4dSAndroid Build Coastguard Worker }
1152*a67afe4dSAndroid Build Coastguard Worker #endif
1153*a67afe4dSAndroid Build Coastguard Worker 
1154*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_sBIT_SUPPORTED
1155*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_sBIT(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1156*a67afe4dSAndroid Build Coastguard Worker png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1157*a67afe4dSAndroid Build Coastguard Worker {
1158*a67afe4dSAndroid Build Coastguard Worker    unsigned int truelen, i;
1159*a67afe4dSAndroid Build Coastguard Worker    png_byte sample_depth;
1160*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[4];
1161*a67afe4dSAndroid Build Coastguard Worker 
1162*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_sBIT");
1163*a67afe4dSAndroid Build Coastguard Worker 
1164*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1165*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1166*a67afe4dSAndroid Build Coastguard Worker 
1167*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1168*a67afe4dSAndroid Build Coastguard Worker    {
1169*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1170*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1171*a67afe4dSAndroid Build Coastguard Worker       return;
1172*a67afe4dSAndroid Build Coastguard Worker    }
1173*a67afe4dSAndroid Build Coastguard Worker 
1174*a67afe4dSAndroid Build Coastguard Worker    if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1175*a67afe4dSAndroid Build Coastguard Worker    {
1176*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1177*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
1178*a67afe4dSAndroid Build Coastguard Worker       return;
1179*a67afe4dSAndroid Build Coastguard Worker    }
1180*a67afe4dSAndroid Build Coastguard Worker 
1181*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1182*a67afe4dSAndroid Build Coastguard Worker    {
1183*a67afe4dSAndroid Build Coastguard Worker       truelen = 3;
1184*a67afe4dSAndroid Build Coastguard Worker       sample_depth = 8;
1185*a67afe4dSAndroid Build Coastguard Worker    }
1186*a67afe4dSAndroid Build Coastguard Worker 
1187*a67afe4dSAndroid Build Coastguard Worker    else
1188*a67afe4dSAndroid Build Coastguard Worker    {
1189*a67afe4dSAndroid Build Coastguard Worker       truelen = png_ptr->channels;
1190*a67afe4dSAndroid Build Coastguard Worker       sample_depth = png_ptr->bit_depth;
1191*a67afe4dSAndroid Build Coastguard Worker    }
1192*a67afe4dSAndroid Build Coastguard Worker 
1193*a67afe4dSAndroid Build Coastguard Worker    if (length != truelen || length > 4)
1194*a67afe4dSAndroid Build Coastguard Worker    {
1195*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1196*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1197*a67afe4dSAndroid Build Coastguard Worker       return;
1198*a67afe4dSAndroid Build Coastguard Worker    }
1199*a67afe4dSAndroid Build Coastguard Worker 
1200*a67afe4dSAndroid Build Coastguard Worker    buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1201*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, truelen);
1202*a67afe4dSAndroid Build Coastguard Worker 
1203*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1204*a67afe4dSAndroid Build Coastguard Worker       return;
1205*a67afe4dSAndroid Build Coastguard Worker 
1206*a67afe4dSAndroid Build Coastguard Worker    for (i=0; i<truelen; ++i)
1207*a67afe4dSAndroid Build Coastguard Worker    {
1208*a67afe4dSAndroid Build Coastguard Worker       if (buf[i] == 0 || buf[i] > sample_depth)
1209*a67afe4dSAndroid Build Coastguard Worker       {
1210*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid");
1211*a67afe4dSAndroid Build Coastguard Worker          return;
1212*a67afe4dSAndroid Build Coastguard Worker       }
1213*a67afe4dSAndroid Build Coastguard Worker    }
1214*a67afe4dSAndroid Build Coastguard Worker 
1215*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1216*a67afe4dSAndroid Build Coastguard Worker    {
1217*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.red = buf[0];
1218*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.green = buf[1];
1219*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.blue = buf[2];
1220*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.alpha = buf[3];
1221*a67afe4dSAndroid Build Coastguard Worker    }
1222*a67afe4dSAndroid Build Coastguard Worker 
1223*a67afe4dSAndroid Build Coastguard Worker    else
1224*a67afe4dSAndroid Build Coastguard Worker    {
1225*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.gray = buf[0];
1226*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.red = buf[0];
1227*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.green = buf[0];
1228*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.blue = buf[0];
1229*a67afe4dSAndroid Build Coastguard Worker       png_ptr->sig_bit.alpha = buf[1];
1230*a67afe4dSAndroid Build Coastguard Worker    }
1231*a67afe4dSAndroid Build Coastguard Worker 
1232*a67afe4dSAndroid Build Coastguard Worker    png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1233*a67afe4dSAndroid Build Coastguard Worker }
1234*a67afe4dSAndroid Build Coastguard Worker #endif
1235*a67afe4dSAndroid Build Coastguard Worker 
1236*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_cHRM_SUPPORTED
1237*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_cHRM(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1238*a67afe4dSAndroid Build Coastguard Worker png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1239*a67afe4dSAndroid Build Coastguard Worker {
1240*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[32];
1241*a67afe4dSAndroid Build Coastguard Worker    png_xy xy;
1242*a67afe4dSAndroid Build Coastguard Worker 
1243*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_cHRM");
1244*a67afe4dSAndroid Build Coastguard Worker 
1245*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1246*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1247*a67afe4dSAndroid Build Coastguard Worker 
1248*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1249*a67afe4dSAndroid Build Coastguard Worker    {
1250*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1251*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1252*a67afe4dSAndroid Build Coastguard Worker       return;
1253*a67afe4dSAndroid Build Coastguard Worker    }
1254*a67afe4dSAndroid Build Coastguard Worker 
1255*a67afe4dSAndroid Build Coastguard Worker    if (length != 32)
1256*a67afe4dSAndroid Build Coastguard Worker    {
1257*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1258*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1259*a67afe4dSAndroid Build Coastguard Worker       return;
1260*a67afe4dSAndroid Build Coastguard Worker    }
1261*a67afe4dSAndroid Build Coastguard Worker 
1262*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 32);
1263*a67afe4dSAndroid Build Coastguard Worker 
1264*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1265*a67afe4dSAndroid Build Coastguard Worker       return;
1266*a67afe4dSAndroid Build Coastguard Worker 
1267*a67afe4dSAndroid Build Coastguard Worker    xy.whitex = png_get_fixed_point(NULL, buf);
1268*a67afe4dSAndroid Build Coastguard Worker    xy.whitey = png_get_fixed_point(NULL, buf + 4);
1269*a67afe4dSAndroid Build Coastguard Worker    xy.redx   = png_get_fixed_point(NULL, buf + 8);
1270*a67afe4dSAndroid Build Coastguard Worker    xy.redy   = png_get_fixed_point(NULL, buf + 12);
1271*a67afe4dSAndroid Build Coastguard Worker    xy.greenx = png_get_fixed_point(NULL, buf + 16);
1272*a67afe4dSAndroid Build Coastguard Worker    xy.greeny = png_get_fixed_point(NULL, buf + 20);
1273*a67afe4dSAndroid Build Coastguard Worker    xy.bluex  = png_get_fixed_point(NULL, buf + 24);
1274*a67afe4dSAndroid Build Coastguard Worker    xy.bluey  = png_get_fixed_point(NULL, buf + 28);
1275*a67afe4dSAndroid Build Coastguard Worker 
1276*a67afe4dSAndroid Build Coastguard Worker    if (xy.whitex == PNG_FIXED_ERROR ||
1277*a67afe4dSAndroid Build Coastguard Worker        xy.whitey == PNG_FIXED_ERROR ||
1278*a67afe4dSAndroid Build Coastguard Worker        xy.redx   == PNG_FIXED_ERROR ||
1279*a67afe4dSAndroid Build Coastguard Worker        xy.redy   == PNG_FIXED_ERROR ||
1280*a67afe4dSAndroid Build Coastguard Worker        xy.greenx == PNG_FIXED_ERROR ||
1281*a67afe4dSAndroid Build Coastguard Worker        xy.greeny == PNG_FIXED_ERROR ||
1282*a67afe4dSAndroid Build Coastguard Worker        xy.bluex  == PNG_FIXED_ERROR ||
1283*a67afe4dSAndroid Build Coastguard Worker        xy.bluey  == PNG_FIXED_ERROR)
1284*a67afe4dSAndroid Build Coastguard Worker    {
1285*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid values");
1286*a67afe4dSAndroid Build Coastguard Worker       return;
1287*a67afe4dSAndroid Build Coastguard Worker    }
1288*a67afe4dSAndroid Build Coastguard Worker 
1289*a67afe4dSAndroid Build Coastguard Worker    /* If a colorspace error has already been output skip this chunk */
1290*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1291*a67afe4dSAndroid Build Coastguard Worker       return;
1292*a67afe4dSAndroid Build Coastguard Worker 
1293*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1294*a67afe4dSAndroid Build Coastguard Worker    {
1295*a67afe4dSAndroid Build Coastguard Worker       png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1296*a67afe4dSAndroid Build Coastguard Worker       png_colorspace_sync(png_ptr, info_ptr);
1297*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
1298*a67afe4dSAndroid Build Coastguard Worker       return;
1299*a67afe4dSAndroid Build Coastguard Worker    }
1300*a67afe4dSAndroid Build Coastguard Worker 
1301*a67afe4dSAndroid Build Coastguard Worker    png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1302*a67afe4dSAndroid Build Coastguard Worker    (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1303*a67afe4dSAndroid Build Coastguard Worker        1/*prefer cHRM values*/);
1304*a67afe4dSAndroid Build Coastguard Worker    png_colorspace_sync(png_ptr, info_ptr);
1305*a67afe4dSAndroid Build Coastguard Worker }
1306*a67afe4dSAndroid Build Coastguard Worker #endif
1307*a67afe4dSAndroid Build Coastguard Worker 
1308*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_sRGB_SUPPORTED
1309*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_sRGB(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1310*a67afe4dSAndroid Build Coastguard Worker png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1311*a67afe4dSAndroid Build Coastguard Worker {
1312*a67afe4dSAndroid Build Coastguard Worker    png_byte intent;
1313*a67afe4dSAndroid Build Coastguard Worker 
1314*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_sRGB");
1315*a67afe4dSAndroid Build Coastguard Worker 
1316*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1317*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1318*a67afe4dSAndroid Build Coastguard Worker 
1319*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1320*a67afe4dSAndroid Build Coastguard Worker    {
1321*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1322*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1323*a67afe4dSAndroid Build Coastguard Worker       return;
1324*a67afe4dSAndroid Build Coastguard Worker    }
1325*a67afe4dSAndroid Build Coastguard Worker 
1326*a67afe4dSAndroid Build Coastguard Worker    if (length != 1)
1327*a67afe4dSAndroid Build Coastguard Worker    {
1328*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1329*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1330*a67afe4dSAndroid Build Coastguard Worker       return;
1331*a67afe4dSAndroid Build Coastguard Worker    }
1332*a67afe4dSAndroid Build Coastguard Worker 
1333*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, &intent, 1);
1334*a67afe4dSAndroid Build Coastguard Worker 
1335*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1336*a67afe4dSAndroid Build Coastguard Worker       return;
1337*a67afe4dSAndroid Build Coastguard Worker 
1338*a67afe4dSAndroid Build Coastguard Worker    /* If a colorspace error has already been output skip this chunk */
1339*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1340*a67afe4dSAndroid Build Coastguard Worker       return;
1341*a67afe4dSAndroid Build Coastguard Worker 
1342*a67afe4dSAndroid Build Coastguard Worker    /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1343*a67afe4dSAndroid Build Coastguard Worker     * this.
1344*a67afe4dSAndroid Build Coastguard Worker     */
1345*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1346*a67afe4dSAndroid Build Coastguard Worker    {
1347*a67afe4dSAndroid Build Coastguard Worker       png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1348*a67afe4dSAndroid Build Coastguard Worker       png_colorspace_sync(png_ptr, info_ptr);
1349*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "too many profiles");
1350*a67afe4dSAndroid Build Coastguard Worker       return;
1351*a67afe4dSAndroid Build Coastguard Worker    }
1352*a67afe4dSAndroid Build Coastguard Worker 
1353*a67afe4dSAndroid Build Coastguard Worker    (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1354*a67afe4dSAndroid Build Coastguard Worker    png_colorspace_sync(png_ptr, info_ptr);
1355*a67afe4dSAndroid Build Coastguard Worker }
1356*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_sRGB */
1357*a67afe4dSAndroid Build Coastguard Worker 
1358*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_iCCP_SUPPORTED
1359*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_iCCP(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1360*a67afe4dSAndroid Build Coastguard Worker png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1361*a67afe4dSAndroid Build Coastguard Worker /* Note: this does not properly handle profiles that are > 64K under DOS */
1362*a67afe4dSAndroid Build Coastguard Worker {
1363*a67afe4dSAndroid Build Coastguard Worker    png_const_charp errmsg = NULL; /* error message output, or no error */
1364*a67afe4dSAndroid Build Coastguard Worker    int finished = 0; /* crc checked */
1365*a67afe4dSAndroid Build Coastguard Worker 
1366*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_iCCP");
1367*a67afe4dSAndroid Build Coastguard Worker 
1368*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1369*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1370*a67afe4dSAndroid Build Coastguard Worker 
1371*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1372*a67afe4dSAndroid Build Coastguard Worker    {
1373*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1374*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1375*a67afe4dSAndroid Build Coastguard Worker       return;
1376*a67afe4dSAndroid Build Coastguard Worker    }
1377*a67afe4dSAndroid Build Coastguard Worker 
1378*a67afe4dSAndroid Build Coastguard Worker    /* Consistent with all the above colorspace handling an obviously *invalid*
1379*a67afe4dSAndroid Build Coastguard Worker     * chunk is just ignored, so does not invalidate the color space.  An
1380*a67afe4dSAndroid Build Coastguard Worker     * alternative is to set the 'invalid' flags at the start of this routine
1381*a67afe4dSAndroid Build Coastguard Worker     * and only clear them in they were not set before and all the tests pass.
1382*a67afe4dSAndroid Build Coastguard Worker     */
1383*a67afe4dSAndroid Build Coastguard Worker 
1384*a67afe4dSAndroid Build Coastguard Worker    /* The keyword must be at least one character and there is a
1385*a67afe4dSAndroid Build Coastguard Worker     * terminator (0) byte and the compression method byte, and the
1386*a67afe4dSAndroid Build Coastguard Worker     * 'zlib' datastream is at least 11 bytes.
1387*a67afe4dSAndroid Build Coastguard Worker     */
1388*a67afe4dSAndroid Build Coastguard Worker    if (length < 14)
1389*a67afe4dSAndroid Build Coastguard Worker    {
1390*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1391*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "too short");
1392*a67afe4dSAndroid Build Coastguard Worker       return;
1393*a67afe4dSAndroid Build Coastguard Worker    }
1394*a67afe4dSAndroid Build Coastguard Worker 
1395*a67afe4dSAndroid Build Coastguard Worker    /* If a colorspace error has already been output skip this chunk */
1396*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1397*a67afe4dSAndroid Build Coastguard Worker    {
1398*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1399*a67afe4dSAndroid Build Coastguard Worker       return;
1400*a67afe4dSAndroid Build Coastguard Worker    }
1401*a67afe4dSAndroid Build Coastguard Worker 
1402*a67afe4dSAndroid Build Coastguard Worker    /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1403*a67afe4dSAndroid Build Coastguard Worker     * this.
1404*a67afe4dSAndroid Build Coastguard Worker     */
1405*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1406*a67afe4dSAndroid Build Coastguard Worker    {
1407*a67afe4dSAndroid Build Coastguard Worker       uInt read_length, keyword_length;
1408*a67afe4dSAndroid Build Coastguard Worker       char keyword[81];
1409*a67afe4dSAndroid Build Coastguard Worker 
1410*a67afe4dSAndroid Build Coastguard Worker       /* Find the keyword; the keyword plus separator and compression method
1411*a67afe4dSAndroid Build Coastguard Worker        * bytes can be at most 81 characters long.
1412*a67afe4dSAndroid Build Coastguard Worker        */
1413*a67afe4dSAndroid Build Coastguard Worker       read_length = 81; /* maximum */
1414*a67afe4dSAndroid Build Coastguard Worker       if (read_length > length)
1415*a67afe4dSAndroid Build Coastguard Worker          read_length = (uInt)length;
1416*a67afe4dSAndroid Build Coastguard Worker 
1417*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1418*a67afe4dSAndroid Build Coastguard Worker       length -= read_length;
1419*a67afe4dSAndroid Build Coastguard Worker 
1420*a67afe4dSAndroid Build Coastguard Worker       /* The minimum 'zlib' stream is assumed to be just the 2 byte header,
1421*a67afe4dSAndroid Build Coastguard Worker        * 5 bytes minimum 'deflate' stream, and the 4 byte checksum.
1422*a67afe4dSAndroid Build Coastguard Worker        */
1423*a67afe4dSAndroid Build Coastguard Worker       if (length < 11)
1424*a67afe4dSAndroid Build Coastguard Worker       {
1425*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1426*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "too short");
1427*a67afe4dSAndroid Build Coastguard Worker          return;
1428*a67afe4dSAndroid Build Coastguard Worker       }
1429*a67afe4dSAndroid Build Coastguard Worker 
1430*a67afe4dSAndroid Build Coastguard Worker       keyword_length = 0;
1431*a67afe4dSAndroid Build Coastguard Worker       while (keyword_length < 80 && keyword_length < read_length &&
1432*a67afe4dSAndroid Build Coastguard Worker          keyword[keyword_length] != 0)
1433*a67afe4dSAndroid Build Coastguard Worker          ++keyword_length;
1434*a67afe4dSAndroid Build Coastguard Worker 
1435*a67afe4dSAndroid Build Coastguard Worker       /* TODO: make the keyword checking common */
1436*a67afe4dSAndroid Build Coastguard Worker       if (keyword_length >= 1 && keyword_length <= 79)
1437*a67afe4dSAndroid Build Coastguard Worker       {
1438*a67afe4dSAndroid Build Coastguard Worker          /* We only understand '0' compression - deflate - so if we get a
1439*a67afe4dSAndroid Build Coastguard Worker           * different value we can't safely decode the chunk.
1440*a67afe4dSAndroid Build Coastguard Worker           */
1441*a67afe4dSAndroid Build Coastguard Worker          if (keyword_length+1 < read_length &&
1442*a67afe4dSAndroid Build Coastguard Worker             keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1443*a67afe4dSAndroid Build Coastguard Worker          {
1444*a67afe4dSAndroid Build Coastguard Worker             read_length -= keyword_length+2;
1445*a67afe4dSAndroid Build Coastguard Worker 
1446*a67afe4dSAndroid Build Coastguard Worker             if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1447*a67afe4dSAndroid Build Coastguard Worker             {
1448*a67afe4dSAndroid Build Coastguard Worker                Byte profile_header[132]={0};
1449*a67afe4dSAndroid Build Coastguard Worker                Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1450*a67afe4dSAndroid Build Coastguard Worker                png_alloc_size_t size = (sizeof profile_header);
1451*a67afe4dSAndroid Build Coastguard Worker 
1452*a67afe4dSAndroid Build Coastguard Worker                png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1453*a67afe4dSAndroid Build Coastguard Worker                png_ptr->zstream.avail_in = read_length;
1454*a67afe4dSAndroid Build Coastguard Worker                (void)png_inflate_read(png_ptr, local_buffer,
1455*a67afe4dSAndroid Build Coastguard Worker                    (sizeof local_buffer), &length, profile_header, &size,
1456*a67afe4dSAndroid Build Coastguard Worker                    0/*finish: don't, because the output is too small*/);
1457*a67afe4dSAndroid Build Coastguard Worker 
1458*a67afe4dSAndroid Build Coastguard Worker                if (size == 0)
1459*a67afe4dSAndroid Build Coastguard Worker                {
1460*a67afe4dSAndroid Build Coastguard Worker                   /* We have the ICC profile header; do the basic header checks.
1461*a67afe4dSAndroid Build Coastguard Worker                    */
1462*a67afe4dSAndroid Build Coastguard Worker                   png_uint_32 profile_length = png_get_uint_32(profile_header);
1463*a67afe4dSAndroid Build Coastguard Worker 
1464*a67afe4dSAndroid Build Coastguard Worker                   if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1465*a67afe4dSAndroid Build Coastguard Worker                       keyword, profile_length) != 0)
1466*a67afe4dSAndroid Build Coastguard Worker                   {
1467*a67afe4dSAndroid Build Coastguard Worker                      /* The length is apparently ok, so we can check the 132
1468*a67afe4dSAndroid Build Coastguard Worker                       * byte header.
1469*a67afe4dSAndroid Build Coastguard Worker                       */
1470*a67afe4dSAndroid Build Coastguard Worker                      if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1471*a67afe4dSAndroid Build Coastguard Worker                          keyword, profile_length, profile_header,
1472*a67afe4dSAndroid Build Coastguard Worker                          png_ptr->color_type) != 0)
1473*a67afe4dSAndroid Build Coastguard Worker                      {
1474*a67afe4dSAndroid Build Coastguard Worker                         /* Now read the tag table; a variable size buffer is
1475*a67afe4dSAndroid Build Coastguard Worker                          * needed at this point, allocate one for the whole
1476*a67afe4dSAndroid Build Coastguard Worker                          * profile.  The header check has already validated
1477*a67afe4dSAndroid Build Coastguard Worker                          * that none of this stuff will overflow.
1478*a67afe4dSAndroid Build Coastguard Worker                          */
1479*a67afe4dSAndroid Build Coastguard Worker                         png_uint_32 tag_count =
1480*a67afe4dSAndroid Build Coastguard Worker                            png_get_uint_32(profile_header + 128);
1481*a67afe4dSAndroid Build Coastguard Worker                         png_bytep profile = png_read_buffer(png_ptr,
1482*a67afe4dSAndroid Build Coastguard Worker                             profile_length, 2/*silent*/);
1483*a67afe4dSAndroid Build Coastguard Worker 
1484*a67afe4dSAndroid Build Coastguard Worker                         if (profile != NULL)
1485*a67afe4dSAndroid Build Coastguard Worker                         {
1486*a67afe4dSAndroid Build Coastguard Worker                            memcpy(profile, profile_header,
1487*a67afe4dSAndroid Build Coastguard Worker                                (sizeof profile_header));
1488*a67afe4dSAndroid Build Coastguard Worker 
1489*a67afe4dSAndroid Build Coastguard Worker                            size = 12 * tag_count;
1490*a67afe4dSAndroid Build Coastguard Worker 
1491*a67afe4dSAndroid Build Coastguard Worker                            (void)png_inflate_read(png_ptr, local_buffer,
1492*a67afe4dSAndroid Build Coastguard Worker                                (sizeof local_buffer), &length,
1493*a67afe4dSAndroid Build Coastguard Worker                                profile + (sizeof profile_header), &size, 0);
1494*a67afe4dSAndroid Build Coastguard Worker 
1495*a67afe4dSAndroid Build Coastguard Worker                            /* Still expect a buffer error because we expect
1496*a67afe4dSAndroid Build Coastguard Worker                             * there to be some tag data!
1497*a67afe4dSAndroid Build Coastguard Worker                             */
1498*a67afe4dSAndroid Build Coastguard Worker                            if (size == 0)
1499*a67afe4dSAndroid Build Coastguard Worker                            {
1500*a67afe4dSAndroid Build Coastguard Worker                               if (png_icc_check_tag_table(png_ptr,
1501*a67afe4dSAndroid Build Coastguard Worker                                   &png_ptr->colorspace, keyword, profile_length,
1502*a67afe4dSAndroid Build Coastguard Worker                                   profile) != 0)
1503*a67afe4dSAndroid Build Coastguard Worker                               {
1504*a67afe4dSAndroid Build Coastguard Worker                                  /* The profile has been validated for basic
1505*a67afe4dSAndroid Build Coastguard Worker                                   * security issues, so read the whole thing in.
1506*a67afe4dSAndroid Build Coastguard Worker                                   */
1507*a67afe4dSAndroid Build Coastguard Worker                                  size = profile_length - (sizeof profile_header)
1508*a67afe4dSAndroid Build Coastguard Worker                                      - 12 * tag_count;
1509*a67afe4dSAndroid Build Coastguard Worker 
1510*a67afe4dSAndroid Build Coastguard Worker                                  (void)png_inflate_read(png_ptr, local_buffer,
1511*a67afe4dSAndroid Build Coastguard Worker                                      (sizeof local_buffer), &length,
1512*a67afe4dSAndroid Build Coastguard Worker                                      profile + (sizeof profile_header) +
1513*a67afe4dSAndroid Build Coastguard Worker                                      12 * tag_count, &size, 1/*finish*/);
1514*a67afe4dSAndroid Build Coastguard Worker 
1515*a67afe4dSAndroid Build Coastguard Worker                                  if (length > 0 && !(png_ptr->flags &
1516*a67afe4dSAndroid Build Coastguard Worker                                      PNG_FLAG_BENIGN_ERRORS_WARN))
1517*a67afe4dSAndroid Build Coastguard Worker                                     errmsg = "extra compressed data";
1518*a67afe4dSAndroid Build Coastguard Worker 
1519*a67afe4dSAndroid Build Coastguard Worker                                  /* But otherwise allow extra data: */
1520*a67afe4dSAndroid Build Coastguard Worker                                  else if (size == 0)
1521*a67afe4dSAndroid Build Coastguard Worker                                  {
1522*a67afe4dSAndroid Build Coastguard Worker                                     if (length > 0)
1523*a67afe4dSAndroid Build Coastguard Worker                                     {
1524*a67afe4dSAndroid Build Coastguard Worker                                        /* This can be handled completely, so
1525*a67afe4dSAndroid Build Coastguard Worker                                         * keep going.
1526*a67afe4dSAndroid Build Coastguard Worker                                         */
1527*a67afe4dSAndroid Build Coastguard Worker                                        png_chunk_warning(png_ptr,
1528*a67afe4dSAndroid Build Coastguard Worker                                            "extra compressed data");
1529*a67afe4dSAndroid Build Coastguard Worker                                     }
1530*a67afe4dSAndroid Build Coastguard Worker 
1531*a67afe4dSAndroid Build Coastguard Worker                                     png_crc_finish(png_ptr, length);
1532*a67afe4dSAndroid Build Coastguard Worker                                     finished = 1;
1533*a67afe4dSAndroid Build Coastguard Worker 
1534*a67afe4dSAndroid Build Coastguard Worker # if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
1535*a67afe4dSAndroid Build Coastguard Worker                                     /* Check for a match against sRGB */
1536*a67afe4dSAndroid Build Coastguard Worker                                     png_icc_set_sRGB(png_ptr,
1537*a67afe4dSAndroid Build Coastguard Worker                                         &png_ptr->colorspace, profile,
1538*a67afe4dSAndroid Build Coastguard Worker                                         png_ptr->zstream.adler);
1539*a67afe4dSAndroid Build Coastguard Worker # endif
1540*a67afe4dSAndroid Build Coastguard Worker 
1541*a67afe4dSAndroid Build Coastguard Worker                                     /* Steal the profile for info_ptr. */
1542*a67afe4dSAndroid Build Coastguard Worker                                     if (info_ptr != NULL)
1543*a67afe4dSAndroid Build Coastguard Worker                                     {
1544*a67afe4dSAndroid Build Coastguard Worker                                        png_free_data(png_ptr, info_ptr,
1545*a67afe4dSAndroid Build Coastguard Worker                                            PNG_FREE_ICCP, 0);
1546*a67afe4dSAndroid Build Coastguard Worker 
1547*a67afe4dSAndroid Build Coastguard Worker                                        info_ptr->iccp_name = png_voidcast(char*,
1548*a67afe4dSAndroid Build Coastguard Worker                                            png_malloc_base(png_ptr,
1549*a67afe4dSAndroid Build Coastguard Worker                                            keyword_length+1));
1550*a67afe4dSAndroid Build Coastguard Worker                                        if (info_ptr->iccp_name != NULL)
1551*a67afe4dSAndroid Build Coastguard Worker                                        {
1552*a67afe4dSAndroid Build Coastguard Worker                                           memcpy(info_ptr->iccp_name, keyword,
1553*a67afe4dSAndroid Build Coastguard Worker                                               keyword_length+1);
1554*a67afe4dSAndroid Build Coastguard Worker                                           info_ptr->iccp_proflen =
1555*a67afe4dSAndroid Build Coastguard Worker                                               profile_length;
1556*a67afe4dSAndroid Build Coastguard Worker                                           info_ptr->iccp_profile = profile;
1557*a67afe4dSAndroid Build Coastguard Worker                                           png_ptr->read_buffer = NULL; /*steal*/
1558*a67afe4dSAndroid Build Coastguard Worker                                           info_ptr->free_me |= PNG_FREE_ICCP;
1559*a67afe4dSAndroid Build Coastguard Worker                                           info_ptr->valid |= PNG_INFO_iCCP;
1560*a67afe4dSAndroid Build Coastguard Worker                                        }
1561*a67afe4dSAndroid Build Coastguard Worker 
1562*a67afe4dSAndroid Build Coastguard Worker                                        else
1563*a67afe4dSAndroid Build Coastguard Worker                                        {
1564*a67afe4dSAndroid Build Coastguard Worker                                           png_ptr->colorspace.flags |=
1565*a67afe4dSAndroid Build Coastguard Worker                                              PNG_COLORSPACE_INVALID;
1566*a67afe4dSAndroid Build Coastguard Worker                                           errmsg = "out of memory";
1567*a67afe4dSAndroid Build Coastguard Worker                                        }
1568*a67afe4dSAndroid Build Coastguard Worker                                     }
1569*a67afe4dSAndroid Build Coastguard Worker 
1570*a67afe4dSAndroid Build Coastguard Worker                                     /* else the profile remains in the read
1571*a67afe4dSAndroid Build Coastguard Worker                                      * buffer which gets reused for subsequent
1572*a67afe4dSAndroid Build Coastguard Worker                                      * chunks.
1573*a67afe4dSAndroid Build Coastguard Worker                                      */
1574*a67afe4dSAndroid Build Coastguard Worker 
1575*a67afe4dSAndroid Build Coastguard Worker                                     if (info_ptr != NULL)
1576*a67afe4dSAndroid Build Coastguard Worker                                        png_colorspace_sync(png_ptr, info_ptr);
1577*a67afe4dSAndroid Build Coastguard Worker 
1578*a67afe4dSAndroid Build Coastguard Worker                                     if (errmsg == NULL)
1579*a67afe4dSAndroid Build Coastguard Worker                                     {
1580*a67afe4dSAndroid Build Coastguard Worker                                        png_ptr->zowner = 0;
1581*a67afe4dSAndroid Build Coastguard Worker                                        return;
1582*a67afe4dSAndroid Build Coastguard Worker                                     }
1583*a67afe4dSAndroid Build Coastguard Worker                                  }
1584*a67afe4dSAndroid Build Coastguard Worker                                  if (errmsg == NULL)
1585*a67afe4dSAndroid Build Coastguard Worker                                     errmsg = png_ptr->zstream.msg;
1586*a67afe4dSAndroid Build Coastguard Worker                               }
1587*a67afe4dSAndroid Build Coastguard Worker                               /* else png_icc_check_tag_table output an error */
1588*a67afe4dSAndroid Build Coastguard Worker                            }
1589*a67afe4dSAndroid Build Coastguard Worker                            else /* profile truncated */
1590*a67afe4dSAndroid Build Coastguard Worker                               errmsg = png_ptr->zstream.msg;
1591*a67afe4dSAndroid Build Coastguard Worker                         }
1592*a67afe4dSAndroid Build Coastguard Worker 
1593*a67afe4dSAndroid Build Coastguard Worker                         else
1594*a67afe4dSAndroid Build Coastguard Worker                            errmsg = "out of memory";
1595*a67afe4dSAndroid Build Coastguard Worker                      }
1596*a67afe4dSAndroid Build Coastguard Worker 
1597*a67afe4dSAndroid Build Coastguard Worker                      /* else png_icc_check_header output an error */
1598*a67afe4dSAndroid Build Coastguard Worker                   }
1599*a67afe4dSAndroid Build Coastguard Worker 
1600*a67afe4dSAndroid Build Coastguard Worker                   /* else png_icc_check_length output an error */
1601*a67afe4dSAndroid Build Coastguard Worker                }
1602*a67afe4dSAndroid Build Coastguard Worker 
1603*a67afe4dSAndroid Build Coastguard Worker                else /* profile truncated */
1604*a67afe4dSAndroid Build Coastguard Worker                   errmsg = png_ptr->zstream.msg;
1605*a67afe4dSAndroid Build Coastguard Worker 
1606*a67afe4dSAndroid Build Coastguard Worker                /* Release the stream */
1607*a67afe4dSAndroid Build Coastguard Worker                png_ptr->zowner = 0;
1608*a67afe4dSAndroid Build Coastguard Worker             }
1609*a67afe4dSAndroid Build Coastguard Worker 
1610*a67afe4dSAndroid Build Coastguard Worker             else /* png_inflate_claim failed */
1611*a67afe4dSAndroid Build Coastguard Worker                errmsg = png_ptr->zstream.msg;
1612*a67afe4dSAndroid Build Coastguard Worker          }
1613*a67afe4dSAndroid Build Coastguard Worker 
1614*a67afe4dSAndroid Build Coastguard Worker          else
1615*a67afe4dSAndroid Build Coastguard Worker             errmsg = "bad compression method"; /* or missing */
1616*a67afe4dSAndroid Build Coastguard Worker       }
1617*a67afe4dSAndroid Build Coastguard Worker 
1618*a67afe4dSAndroid Build Coastguard Worker       else
1619*a67afe4dSAndroid Build Coastguard Worker          errmsg = "bad keyword";
1620*a67afe4dSAndroid Build Coastguard Worker    }
1621*a67afe4dSAndroid Build Coastguard Worker 
1622*a67afe4dSAndroid Build Coastguard Worker    else
1623*a67afe4dSAndroid Build Coastguard Worker       errmsg = "too many profiles";
1624*a67afe4dSAndroid Build Coastguard Worker 
1625*a67afe4dSAndroid Build Coastguard Worker    /* Failure: the reason is in 'errmsg' */
1626*a67afe4dSAndroid Build Coastguard Worker    if (finished == 0)
1627*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1628*a67afe4dSAndroid Build Coastguard Worker 
1629*a67afe4dSAndroid Build Coastguard Worker    png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1630*a67afe4dSAndroid Build Coastguard Worker    png_colorspace_sync(png_ptr, info_ptr);
1631*a67afe4dSAndroid Build Coastguard Worker    if (errmsg != NULL) /* else already output */
1632*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, errmsg);
1633*a67afe4dSAndroid Build Coastguard Worker }
1634*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_iCCP */
1635*a67afe4dSAndroid Build Coastguard Worker 
1636*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_sPLT_SUPPORTED
1637*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_sPLT(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1638*a67afe4dSAndroid Build Coastguard Worker png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1639*a67afe4dSAndroid Build Coastguard Worker /* Note: this does not properly handle chunks that are > 64K under DOS */
1640*a67afe4dSAndroid Build Coastguard Worker {
1641*a67afe4dSAndroid Build Coastguard Worker    png_bytep entry_start, buffer;
1642*a67afe4dSAndroid Build Coastguard Worker    png_sPLT_t new_palette;
1643*a67afe4dSAndroid Build Coastguard Worker    png_sPLT_entryp pp;
1644*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 data_length;
1645*a67afe4dSAndroid Build Coastguard Worker    int entry_size, i;
1646*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 skip = 0;
1647*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 dl;
1648*a67afe4dSAndroid Build Coastguard Worker    size_t max_dl;
1649*a67afe4dSAndroid Build Coastguard Worker 
1650*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_sPLT");
1651*a67afe4dSAndroid Build Coastguard Worker 
1652*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USER_LIMITS_SUPPORTED
1653*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_cache_max != 0)
1654*a67afe4dSAndroid Build Coastguard Worker    {
1655*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->user_chunk_cache_max == 1)
1656*a67afe4dSAndroid Build Coastguard Worker       {
1657*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1658*a67afe4dSAndroid Build Coastguard Worker          return;
1659*a67afe4dSAndroid Build Coastguard Worker       }
1660*a67afe4dSAndroid Build Coastguard Worker 
1661*a67afe4dSAndroid Build Coastguard Worker       if (--png_ptr->user_chunk_cache_max == 1)
1662*a67afe4dSAndroid Build Coastguard Worker       {
1663*a67afe4dSAndroid Build Coastguard Worker          png_warning(png_ptr, "No space in chunk cache for sPLT");
1664*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1665*a67afe4dSAndroid Build Coastguard Worker          return;
1666*a67afe4dSAndroid Build Coastguard Worker       }
1667*a67afe4dSAndroid Build Coastguard Worker    }
1668*a67afe4dSAndroid Build Coastguard Worker #endif
1669*a67afe4dSAndroid Build Coastguard Worker 
1670*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1671*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1672*a67afe4dSAndroid Build Coastguard Worker 
1673*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1674*a67afe4dSAndroid Build Coastguard Worker    {
1675*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1676*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1677*a67afe4dSAndroid Build Coastguard Worker       return;
1678*a67afe4dSAndroid Build Coastguard Worker    }
1679*a67afe4dSAndroid Build Coastguard Worker 
1680*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_MAX_MALLOC_64K
1681*a67afe4dSAndroid Build Coastguard Worker    if (length > 65535U)
1682*a67afe4dSAndroid Build Coastguard Worker    {
1683*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1684*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "too large to fit in memory");
1685*a67afe4dSAndroid Build Coastguard Worker       return;
1686*a67afe4dSAndroid Build Coastguard Worker    }
1687*a67afe4dSAndroid Build Coastguard Worker #endif
1688*a67afe4dSAndroid Build Coastguard Worker 
1689*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1690*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
1691*a67afe4dSAndroid Build Coastguard Worker    {
1692*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1693*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
1694*a67afe4dSAndroid Build Coastguard Worker       return;
1695*a67afe4dSAndroid Build Coastguard Worker    }
1696*a67afe4dSAndroid Build Coastguard Worker 
1697*a67afe4dSAndroid Build Coastguard Worker 
1698*a67afe4dSAndroid Build Coastguard Worker    /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1699*a67afe4dSAndroid Build Coastguard Worker     * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1700*a67afe4dSAndroid Build Coastguard Worker     * potential breakage point if the types in pngconf.h aren't exactly right.
1701*a67afe4dSAndroid Build Coastguard Worker     */
1702*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
1703*a67afe4dSAndroid Build Coastguard Worker 
1704*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, skip) != 0)
1705*a67afe4dSAndroid Build Coastguard Worker       return;
1706*a67afe4dSAndroid Build Coastguard Worker 
1707*a67afe4dSAndroid Build Coastguard Worker    buffer[length] = 0;
1708*a67afe4dSAndroid Build Coastguard Worker 
1709*a67afe4dSAndroid Build Coastguard Worker    for (entry_start = buffer; *entry_start; entry_start++)
1710*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop to find end of name */ ;
1711*a67afe4dSAndroid Build Coastguard Worker 
1712*a67afe4dSAndroid Build Coastguard Worker    ++entry_start;
1713*a67afe4dSAndroid Build Coastguard Worker 
1714*a67afe4dSAndroid Build Coastguard Worker    /* A sample depth should follow the separator, and we should be on it  */
1715*a67afe4dSAndroid Build Coastguard Worker    if (length < 2U || entry_start > buffer + (length - 2U))
1716*a67afe4dSAndroid Build Coastguard Worker    {
1717*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "malformed sPLT chunk");
1718*a67afe4dSAndroid Build Coastguard Worker       return;
1719*a67afe4dSAndroid Build Coastguard Worker    }
1720*a67afe4dSAndroid Build Coastguard Worker 
1721*a67afe4dSAndroid Build Coastguard Worker    new_palette.depth = *entry_start++;
1722*a67afe4dSAndroid Build Coastguard Worker    entry_size = (new_palette.depth == 8 ? 6 : 10);
1723*a67afe4dSAndroid Build Coastguard Worker    /* This must fit in a png_uint_32 because it is derived from the original
1724*a67afe4dSAndroid Build Coastguard Worker     * chunk data length.
1725*a67afe4dSAndroid Build Coastguard Worker     */
1726*a67afe4dSAndroid Build Coastguard Worker    data_length = length - (png_uint_32)(entry_start - buffer);
1727*a67afe4dSAndroid Build Coastguard Worker 
1728*a67afe4dSAndroid Build Coastguard Worker    /* Integrity-check the data length */
1729*a67afe4dSAndroid Build Coastguard Worker    if ((data_length % (unsigned int)entry_size) != 0)
1730*a67afe4dSAndroid Build Coastguard Worker    {
1731*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "sPLT chunk has bad length");
1732*a67afe4dSAndroid Build Coastguard Worker       return;
1733*a67afe4dSAndroid Build Coastguard Worker    }
1734*a67afe4dSAndroid Build Coastguard Worker 
1735*a67afe4dSAndroid Build Coastguard Worker    dl = (png_uint_32)(data_length / (unsigned int)entry_size);
1736*a67afe4dSAndroid Build Coastguard Worker    max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1737*a67afe4dSAndroid Build Coastguard Worker 
1738*a67afe4dSAndroid Build Coastguard Worker    if (dl > max_dl)
1739*a67afe4dSAndroid Build Coastguard Worker    {
1740*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "sPLT chunk too long");
1741*a67afe4dSAndroid Build Coastguard Worker       return;
1742*a67afe4dSAndroid Build Coastguard Worker    }
1743*a67afe4dSAndroid Build Coastguard Worker 
1744*a67afe4dSAndroid Build Coastguard Worker    new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size);
1745*a67afe4dSAndroid Build Coastguard Worker 
1746*a67afe4dSAndroid Build Coastguard Worker    new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1747*a67afe4dSAndroid Build Coastguard Worker        (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1748*a67afe4dSAndroid Build Coastguard Worker 
1749*a67afe4dSAndroid Build Coastguard Worker    if (new_palette.entries == NULL)
1750*a67afe4dSAndroid Build Coastguard Worker    {
1751*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "sPLT chunk requires too much memory");
1752*a67afe4dSAndroid Build Coastguard Worker       return;
1753*a67afe4dSAndroid Build Coastguard Worker    }
1754*a67afe4dSAndroid Build Coastguard Worker 
1755*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_POINTER_INDEXING_SUPPORTED
1756*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < new_palette.nentries; i++)
1757*a67afe4dSAndroid Build Coastguard Worker    {
1758*a67afe4dSAndroid Build Coastguard Worker       pp = new_palette.entries + i;
1759*a67afe4dSAndroid Build Coastguard Worker 
1760*a67afe4dSAndroid Build Coastguard Worker       if (new_palette.depth == 8)
1761*a67afe4dSAndroid Build Coastguard Worker       {
1762*a67afe4dSAndroid Build Coastguard Worker          pp->red = *entry_start++;
1763*a67afe4dSAndroid Build Coastguard Worker          pp->green = *entry_start++;
1764*a67afe4dSAndroid Build Coastguard Worker          pp->blue = *entry_start++;
1765*a67afe4dSAndroid Build Coastguard Worker          pp->alpha = *entry_start++;
1766*a67afe4dSAndroid Build Coastguard Worker       }
1767*a67afe4dSAndroid Build Coastguard Worker 
1768*a67afe4dSAndroid Build Coastguard Worker       else
1769*a67afe4dSAndroid Build Coastguard Worker       {
1770*a67afe4dSAndroid Build Coastguard Worker          pp->red   = png_get_uint_16(entry_start); entry_start += 2;
1771*a67afe4dSAndroid Build Coastguard Worker          pp->green = png_get_uint_16(entry_start); entry_start += 2;
1772*a67afe4dSAndroid Build Coastguard Worker          pp->blue  = png_get_uint_16(entry_start); entry_start += 2;
1773*a67afe4dSAndroid Build Coastguard Worker          pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1774*a67afe4dSAndroid Build Coastguard Worker       }
1775*a67afe4dSAndroid Build Coastguard Worker 
1776*a67afe4dSAndroid Build Coastguard Worker       pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1777*a67afe4dSAndroid Build Coastguard Worker    }
1778*a67afe4dSAndroid Build Coastguard Worker #else
1779*a67afe4dSAndroid Build Coastguard Worker    pp = new_palette.entries;
1780*a67afe4dSAndroid Build Coastguard Worker 
1781*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < new_palette.nentries; i++)
1782*a67afe4dSAndroid Build Coastguard Worker    {
1783*a67afe4dSAndroid Build Coastguard Worker 
1784*a67afe4dSAndroid Build Coastguard Worker       if (new_palette.depth == 8)
1785*a67afe4dSAndroid Build Coastguard Worker       {
1786*a67afe4dSAndroid Build Coastguard Worker          pp[i].red   = *entry_start++;
1787*a67afe4dSAndroid Build Coastguard Worker          pp[i].green = *entry_start++;
1788*a67afe4dSAndroid Build Coastguard Worker          pp[i].blue  = *entry_start++;
1789*a67afe4dSAndroid Build Coastguard Worker          pp[i].alpha = *entry_start++;
1790*a67afe4dSAndroid Build Coastguard Worker       }
1791*a67afe4dSAndroid Build Coastguard Worker 
1792*a67afe4dSAndroid Build Coastguard Worker       else
1793*a67afe4dSAndroid Build Coastguard Worker       {
1794*a67afe4dSAndroid Build Coastguard Worker          pp[i].red   = png_get_uint_16(entry_start); entry_start += 2;
1795*a67afe4dSAndroid Build Coastguard Worker          pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1796*a67afe4dSAndroid Build Coastguard Worker          pp[i].blue  = png_get_uint_16(entry_start); entry_start += 2;
1797*a67afe4dSAndroid Build Coastguard Worker          pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1798*a67afe4dSAndroid Build Coastguard Worker       }
1799*a67afe4dSAndroid Build Coastguard Worker 
1800*a67afe4dSAndroid Build Coastguard Worker       pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1801*a67afe4dSAndroid Build Coastguard Worker    }
1802*a67afe4dSAndroid Build Coastguard Worker #endif
1803*a67afe4dSAndroid Build Coastguard Worker 
1804*a67afe4dSAndroid Build Coastguard Worker    /* Discard all chunk data except the name and stash that */
1805*a67afe4dSAndroid Build Coastguard Worker    new_palette.name = (png_charp)buffer;
1806*a67afe4dSAndroid Build Coastguard Worker 
1807*a67afe4dSAndroid Build Coastguard Worker    png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1808*a67afe4dSAndroid Build Coastguard Worker 
1809*a67afe4dSAndroid Build Coastguard Worker    png_free(png_ptr, new_palette.entries);
1810*a67afe4dSAndroid Build Coastguard Worker }
1811*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_sPLT */
1812*a67afe4dSAndroid Build Coastguard Worker 
1813*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_tRNS_SUPPORTED
1814*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_tRNS(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1815*a67afe4dSAndroid Build Coastguard Worker png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1816*a67afe4dSAndroid Build Coastguard Worker {
1817*a67afe4dSAndroid Build Coastguard Worker    png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1818*a67afe4dSAndroid Build Coastguard Worker 
1819*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_tRNS");
1820*a67afe4dSAndroid Build Coastguard Worker 
1821*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1822*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1823*a67afe4dSAndroid Build Coastguard Worker 
1824*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1825*a67afe4dSAndroid Build Coastguard Worker    {
1826*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1827*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1828*a67afe4dSAndroid Build Coastguard Worker       return;
1829*a67afe4dSAndroid Build Coastguard Worker    }
1830*a67afe4dSAndroid Build Coastguard Worker 
1831*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1832*a67afe4dSAndroid Build Coastguard Worker    {
1833*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1834*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
1835*a67afe4dSAndroid Build Coastguard Worker       return;
1836*a67afe4dSAndroid Build Coastguard Worker    }
1837*a67afe4dSAndroid Build Coastguard Worker 
1838*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1839*a67afe4dSAndroid Build Coastguard Worker    {
1840*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[2];
1841*a67afe4dSAndroid Build Coastguard Worker 
1842*a67afe4dSAndroid Build Coastguard Worker       if (length != 2)
1843*a67afe4dSAndroid Build Coastguard Worker       {
1844*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1845*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid");
1846*a67afe4dSAndroid Build Coastguard Worker          return;
1847*a67afe4dSAndroid Build Coastguard Worker       }
1848*a67afe4dSAndroid Build Coastguard Worker 
1849*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, 2);
1850*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_trans = 1;
1851*a67afe4dSAndroid Build Coastguard Worker       png_ptr->trans_color.gray = png_get_uint_16(buf);
1852*a67afe4dSAndroid Build Coastguard Worker    }
1853*a67afe4dSAndroid Build Coastguard Worker 
1854*a67afe4dSAndroid Build Coastguard Worker    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1855*a67afe4dSAndroid Build Coastguard Worker    {
1856*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[6];
1857*a67afe4dSAndroid Build Coastguard Worker 
1858*a67afe4dSAndroid Build Coastguard Worker       if (length != 6)
1859*a67afe4dSAndroid Build Coastguard Worker       {
1860*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1861*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid");
1862*a67afe4dSAndroid Build Coastguard Worker          return;
1863*a67afe4dSAndroid Build Coastguard Worker       }
1864*a67afe4dSAndroid Build Coastguard Worker 
1865*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, length);
1866*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_trans = 1;
1867*a67afe4dSAndroid Build Coastguard Worker       png_ptr->trans_color.red = png_get_uint_16(buf);
1868*a67afe4dSAndroid Build Coastguard Worker       png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1869*a67afe4dSAndroid Build Coastguard Worker       png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1870*a67afe4dSAndroid Build Coastguard Worker    }
1871*a67afe4dSAndroid Build Coastguard Worker 
1872*a67afe4dSAndroid Build Coastguard Worker    else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1873*a67afe4dSAndroid Build Coastguard Worker    {
1874*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1875*a67afe4dSAndroid Build Coastguard Worker       {
1876*a67afe4dSAndroid Build Coastguard Worker          /* TODO: is this actually an error in the ISO spec? */
1877*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1878*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "out of place");
1879*a67afe4dSAndroid Build Coastguard Worker          return;
1880*a67afe4dSAndroid Build Coastguard Worker       }
1881*a67afe4dSAndroid Build Coastguard Worker 
1882*a67afe4dSAndroid Build Coastguard Worker       if (length > (unsigned int) png_ptr->num_palette ||
1883*a67afe4dSAndroid Build Coastguard Worker          length > (unsigned int) PNG_MAX_PALETTE_LENGTH ||
1884*a67afe4dSAndroid Build Coastguard Worker          length == 0)
1885*a67afe4dSAndroid Build Coastguard Worker       {
1886*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
1887*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid");
1888*a67afe4dSAndroid Build Coastguard Worker          return;
1889*a67afe4dSAndroid Build Coastguard Worker       }
1890*a67afe4dSAndroid Build Coastguard Worker 
1891*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, readbuf, length);
1892*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_trans = (png_uint_16)length;
1893*a67afe4dSAndroid Build Coastguard Worker    }
1894*a67afe4dSAndroid Build Coastguard Worker 
1895*a67afe4dSAndroid Build Coastguard Worker    else
1896*a67afe4dSAndroid Build Coastguard Worker    {
1897*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1898*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1899*a67afe4dSAndroid Build Coastguard Worker       return;
1900*a67afe4dSAndroid Build Coastguard Worker    }
1901*a67afe4dSAndroid Build Coastguard Worker 
1902*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1903*a67afe4dSAndroid Build Coastguard Worker    {
1904*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_trans = 0;
1905*a67afe4dSAndroid Build Coastguard Worker       return;
1906*a67afe4dSAndroid Build Coastguard Worker    }
1907*a67afe4dSAndroid Build Coastguard Worker 
1908*a67afe4dSAndroid Build Coastguard Worker    /* TODO: this is a horrible side effect in the palette case because the
1909*a67afe4dSAndroid Build Coastguard Worker     * png_struct ends up with a pointer to the tRNS buffer owned by the
1910*a67afe4dSAndroid Build Coastguard Worker     * png_info.  Fix this.
1911*a67afe4dSAndroid Build Coastguard Worker     */
1912*a67afe4dSAndroid Build Coastguard Worker    png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1913*a67afe4dSAndroid Build Coastguard Worker        &(png_ptr->trans_color));
1914*a67afe4dSAndroid Build Coastguard Worker }
1915*a67afe4dSAndroid Build Coastguard Worker #endif
1916*a67afe4dSAndroid Build Coastguard Worker 
1917*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_bKGD_SUPPORTED
1918*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_bKGD(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)1919*a67afe4dSAndroid Build Coastguard Worker png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1920*a67afe4dSAndroid Build Coastguard Worker {
1921*a67afe4dSAndroid Build Coastguard Worker    unsigned int truelen;
1922*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[6];
1923*a67afe4dSAndroid Build Coastguard Worker    png_color_16 background;
1924*a67afe4dSAndroid Build Coastguard Worker 
1925*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_bKGD");
1926*a67afe4dSAndroid Build Coastguard Worker 
1927*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1928*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
1929*a67afe4dSAndroid Build Coastguard Worker 
1930*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1931*a67afe4dSAndroid Build Coastguard Worker        (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1932*a67afe4dSAndroid Build Coastguard Worker        (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1933*a67afe4dSAndroid Build Coastguard Worker    {
1934*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1935*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
1936*a67afe4dSAndroid Build Coastguard Worker       return;
1937*a67afe4dSAndroid Build Coastguard Worker    }
1938*a67afe4dSAndroid Build Coastguard Worker 
1939*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1940*a67afe4dSAndroid Build Coastguard Worker    {
1941*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1942*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
1943*a67afe4dSAndroid Build Coastguard Worker       return;
1944*a67afe4dSAndroid Build Coastguard Worker    }
1945*a67afe4dSAndroid Build Coastguard Worker 
1946*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1947*a67afe4dSAndroid Build Coastguard Worker       truelen = 1;
1948*a67afe4dSAndroid Build Coastguard Worker 
1949*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1950*a67afe4dSAndroid Build Coastguard Worker       truelen = 6;
1951*a67afe4dSAndroid Build Coastguard Worker 
1952*a67afe4dSAndroid Build Coastguard Worker    else
1953*a67afe4dSAndroid Build Coastguard Worker       truelen = 2;
1954*a67afe4dSAndroid Build Coastguard Worker 
1955*a67afe4dSAndroid Build Coastguard Worker    if (length != truelen)
1956*a67afe4dSAndroid Build Coastguard Worker    {
1957*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
1958*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
1959*a67afe4dSAndroid Build Coastguard Worker       return;
1960*a67afe4dSAndroid Build Coastguard Worker    }
1961*a67afe4dSAndroid Build Coastguard Worker 
1962*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, truelen);
1963*a67afe4dSAndroid Build Coastguard Worker 
1964*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
1965*a67afe4dSAndroid Build Coastguard Worker       return;
1966*a67afe4dSAndroid Build Coastguard Worker 
1967*a67afe4dSAndroid Build Coastguard Worker    /* We convert the index value into RGB components so that we can allow
1968*a67afe4dSAndroid Build Coastguard Worker     * arbitrary RGB values for background when we have transparency, and
1969*a67afe4dSAndroid Build Coastguard Worker     * so it is easy to determine the RGB values of the background color
1970*a67afe4dSAndroid Build Coastguard Worker     * from the info_ptr struct.
1971*a67afe4dSAndroid Build Coastguard Worker     */
1972*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1973*a67afe4dSAndroid Build Coastguard Worker    {
1974*a67afe4dSAndroid Build Coastguard Worker       background.index = buf[0];
1975*a67afe4dSAndroid Build Coastguard Worker 
1976*a67afe4dSAndroid Build Coastguard Worker       if (info_ptr != NULL && info_ptr->num_palette != 0)
1977*a67afe4dSAndroid Build Coastguard Worker       {
1978*a67afe4dSAndroid Build Coastguard Worker          if (buf[0] >= info_ptr->num_palette)
1979*a67afe4dSAndroid Build Coastguard Worker          {
1980*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "invalid index");
1981*a67afe4dSAndroid Build Coastguard Worker             return;
1982*a67afe4dSAndroid Build Coastguard Worker          }
1983*a67afe4dSAndroid Build Coastguard Worker 
1984*a67afe4dSAndroid Build Coastguard Worker          background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1985*a67afe4dSAndroid Build Coastguard Worker          background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1986*a67afe4dSAndroid Build Coastguard Worker          background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1987*a67afe4dSAndroid Build Coastguard Worker       }
1988*a67afe4dSAndroid Build Coastguard Worker 
1989*a67afe4dSAndroid Build Coastguard Worker       else
1990*a67afe4dSAndroid Build Coastguard Worker          background.red = background.green = background.blue = 0;
1991*a67afe4dSAndroid Build Coastguard Worker 
1992*a67afe4dSAndroid Build Coastguard Worker       background.gray = 0;
1993*a67afe4dSAndroid Build Coastguard Worker    }
1994*a67afe4dSAndroid Build Coastguard Worker 
1995*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
1996*a67afe4dSAndroid Build Coastguard Worker    {
1997*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->bit_depth <= 8)
1998*a67afe4dSAndroid Build Coastguard Worker       {
1999*a67afe4dSAndroid Build Coastguard Worker          if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth))
2000*a67afe4dSAndroid Build Coastguard Worker          {
2001*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "invalid gray level");
2002*a67afe4dSAndroid Build Coastguard Worker             return;
2003*a67afe4dSAndroid Build Coastguard Worker          }
2004*a67afe4dSAndroid Build Coastguard Worker       }
2005*a67afe4dSAndroid Build Coastguard Worker 
2006*a67afe4dSAndroid Build Coastguard Worker       background.index = 0;
2007*a67afe4dSAndroid Build Coastguard Worker       background.red =
2008*a67afe4dSAndroid Build Coastguard Worker       background.green =
2009*a67afe4dSAndroid Build Coastguard Worker       background.blue =
2010*a67afe4dSAndroid Build Coastguard Worker       background.gray = png_get_uint_16(buf);
2011*a67afe4dSAndroid Build Coastguard Worker    }
2012*a67afe4dSAndroid Build Coastguard Worker 
2013*a67afe4dSAndroid Build Coastguard Worker    else
2014*a67afe4dSAndroid Build Coastguard Worker    {
2015*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->bit_depth <= 8)
2016*a67afe4dSAndroid Build Coastguard Worker       {
2017*a67afe4dSAndroid Build Coastguard Worker          if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
2018*a67afe4dSAndroid Build Coastguard Worker          {
2019*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "invalid color");
2020*a67afe4dSAndroid Build Coastguard Worker             return;
2021*a67afe4dSAndroid Build Coastguard Worker          }
2022*a67afe4dSAndroid Build Coastguard Worker       }
2023*a67afe4dSAndroid Build Coastguard Worker 
2024*a67afe4dSAndroid Build Coastguard Worker       background.index = 0;
2025*a67afe4dSAndroid Build Coastguard Worker       background.red = png_get_uint_16(buf);
2026*a67afe4dSAndroid Build Coastguard Worker       background.green = png_get_uint_16(buf + 2);
2027*a67afe4dSAndroid Build Coastguard Worker       background.blue = png_get_uint_16(buf + 4);
2028*a67afe4dSAndroid Build Coastguard Worker       background.gray = 0;
2029*a67afe4dSAndroid Build Coastguard Worker    }
2030*a67afe4dSAndroid Build Coastguard Worker 
2031*a67afe4dSAndroid Build Coastguard Worker    png_set_bKGD(png_ptr, info_ptr, &background);
2032*a67afe4dSAndroid Build Coastguard Worker }
2033*a67afe4dSAndroid Build Coastguard Worker #endif
2034*a67afe4dSAndroid Build Coastguard Worker 
2035*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_eXIf_SUPPORTED
2036*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_eXIf(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2037*a67afe4dSAndroid Build Coastguard Worker png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2038*a67afe4dSAndroid Build Coastguard Worker {
2039*a67afe4dSAndroid Build Coastguard Worker    unsigned int i;
2040*a67afe4dSAndroid Build Coastguard Worker 
2041*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_eXIf");
2042*a67afe4dSAndroid Build Coastguard Worker 
2043*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2044*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2045*a67afe4dSAndroid Build Coastguard Worker 
2046*a67afe4dSAndroid Build Coastguard Worker    if (length < 2)
2047*a67afe4dSAndroid Build Coastguard Worker    {
2048*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2049*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "too short");
2050*a67afe4dSAndroid Build Coastguard Worker       return;
2051*a67afe4dSAndroid Build Coastguard Worker    }
2052*a67afe4dSAndroid Build Coastguard Worker 
2053*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0)
2054*a67afe4dSAndroid Build Coastguard Worker    {
2055*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2056*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2057*a67afe4dSAndroid Build Coastguard Worker       return;
2058*a67afe4dSAndroid Build Coastguard Worker    }
2059*a67afe4dSAndroid Build Coastguard Worker 
2060*a67afe4dSAndroid Build Coastguard Worker    info_ptr->free_me |= PNG_FREE_EXIF;
2061*a67afe4dSAndroid Build Coastguard Worker 
2062*a67afe4dSAndroid Build Coastguard Worker    info_ptr->eXIf_buf = png_voidcast(png_bytep,
2063*a67afe4dSAndroid Build Coastguard Worker              png_malloc_warn(png_ptr, length));
2064*a67afe4dSAndroid Build Coastguard Worker 
2065*a67afe4dSAndroid Build Coastguard Worker    if (info_ptr->eXIf_buf == NULL)
2066*a67afe4dSAndroid Build Coastguard Worker    {
2067*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2068*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2069*a67afe4dSAndroid Build Coastguard Worker       return;
2070*a67afe4dSAndroid Build Coastguard Worker    }
2071*a67afe4dSAndroid Build Coastguard Worker 
2072*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < length; i++)
2073*a67afe4dSAndroid Build Coastguard Worker    {
2074*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[1];
2075*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, 1);
2076*a67afe4dSAndroid Build Coastguard Worker       info_ptr->eXIf_buf[i] = buf[0];
2077*a67afe4dSAndroid Build Coastguard Worker       if (i == 1)
2078*a67afe4dSAndroid Build Coastguard Worker       {
2079*a67afe4dSAndroid Build Coastguard Worker          if ((buf[0] != 'M' && buf[0] != 'I') ||
2080*a67afe4dSAndroid Build Coastguard Worker              (info_ptr->eXIf_buf[0] != buf[0]))
2081*a67afe4dSAndroid Build Coastguard Worker          {
2082*a67afe4dSAndroid Build Coastguard Worker             png_crc_finish(png_ptr, length - 2);
2083*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "incorrect byte-order specifier");
2084*a67afe4dSAndroid Build Coastguard Worker             png_free(png_ptr, info_ptr->eXIf_buf);
2085*a67afe4dSAndroid Build Coastguard Worker             info_ptr->eXIf_buf = NULL;
2086*a67afe4dSAndroid Build Coastguard Worker             return;
2087*a67afe4dSAndroid Build Coastguard Worker          }
2088*a67afe4dSAndroid Build Coastguard Worker       }
2089*a67afe4dSAndroid Build Coastguard Worker    }
2090*a67afe4dSAndroid Build Coastguard Worker 
2091*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) == 0)
2092*a67afe4dSAndroid Build Coastguard Worker       png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf);
2093*a67afe4dSAndroid Build Coastguard Worker 
2094*a67afe4dSAndroid Build Coastguard Worker    png_free(png_ptr, info_ptr->eXIf_buf);
2095*a67afe4dSAndroid Build Coastguard Worker    info_ptr->eXIf_buf = NULL;
2096*a67afe4dSAndroid Build Coastguard Worker }
2097*a67afe4dSAndroid Build Coastguard Worker #endif
2098*a67afe4dSAndroid Build Coastguard Worker 
2099*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_hIST_SUPPORTED
2100*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_hIST(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2101*a67afe4dSAndroid Build Coastguard Worker png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2102*a67afe4dSAndroid Build Coastguard Worker {
2103*a67afe4dSAndroid Build Coastguard Worker    unsigned int num, i;
2104*a67afe4dSAndroid Build Coastguard Worker    png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2105*a67afe4dSAndroid Build Coastguard Worker 
2106*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_hIST");
2107*a67afe4dSAndroid Build Coastguard Worker 
2108*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2109*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2110*a67afe4dSAndroid Build Coastguard Worker 
2111*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
2112*a67afe4dSAndroid Build Coastguard Worker        (png_ptr->mode & PNG_HAVE_PLTE) == 0)
2113*a67afe4dSAndroid Build Coastguard Worker    {
2114*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2115*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
2116*a67afe4dSAndroid Build Coastguard Worker       return;
2117*a67afe4dSAndroid Build Coastguard Worker    }
2118*a67afe4dSAndroid Build Coastguard Worker 
2119*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
2120*a67afe4dSAndroid Build Coastguard Worker    {
2121*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2122*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2123*a67afe4dSAndroid Build Coastguard Worker       return;
2124*a67afe4dSAndroid Build Coastguard Worker    }
2125*a67afe4dSAndroid Build Coastguard Worker 
2126*a67afe4dSAndroid Build Coastguard Worker    num = length / 2 ;
2127*a67afe4dSAndroid Build Coastguard Worker 
2128*a67afe4dSAndroid Build Coastguard Worker    if (length != num * 2 ||
2129*a67afe4dSAndroid Build Coastguard Worker        num != (unsigned int)png_ptr->num_palette ||
2130*a67afe4dSAndroid Build Coastguard Worker        num > (unsigned int)PNG_MAX_PALETTE_LENGTH)
2131*a67afe4dSAndroid Build Coastguard Worker    {
2132*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2133*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2134*a67afe4dSAndroid Build Coastguard Worker       return;
2135*a67afe4dSAndroid Build Coastguard Worker    }
2136*a67afe4dSAndroid Build Coastguard Worker 
2137*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < num; i++)
2138*a67afe4dSAndroid Build Coastguard Worker    {
2139*a67afe4dSAndroid Build Coastguard Worker       png_byte buf[2];
2140*a67afe4dSAndroid Build Coastguard Worker 
2141*a67afe4dSAndroid Build Coastguard Worker       png_crc_read(png_ptr, buf, 2);
2142*a67afe4dSAndroid Build Coastguard Worker       readbuf[i] = png_get_uint_16(buf);
2143*a67afe4dSAndroid Build Coastguard Worker    }
2144*a67afe4dSAndroid Build Coastguard Worker 
2145*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2146*a67afe4dSAndroid Build Coastguard Worker       return;
2147*a67afe4dSAndroid Build Coastguard Worker 
2148*a67afe4dSAndroid Build Coastguard Worker    png_set_hIST(png_ptr, info_ptr, readbuf);
2149*a67afe4dSAndroid Build Coastguard Worker }
2150*a67afe4dSAndroid Build Coastguard Worker #endif
2151*a67afe4dSAndroid Build Coastguard Worker 
2152*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_pHYs_SUPPORTED
2153*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_pHYs(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2154*a67afe4dSAndroid Build Coastguard Worker png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2155*a67afe4dSAndroid Build Coastguard Worker {
2156*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[9];
2157*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 res_x, res_y;
2158*a67afe4dSAndroid Build Coastguard Worker    int unit_type;
2159*a67afe4dSAndroid Build Coastguard Worker 
2160*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_pHYs");
2161*a67afe4dSAndroid Build Coastguard Worker 
2162*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2163*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2164*a67afe4dSAndroid Build Coastguard Worker 
2165*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2166*a67afe4dSAndroid Build Coastguard Worker    {
2167*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2168*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
2169*a67afe4dSAndroid Build Coastguard Worker       return;
2170*a67afe4dSAndroid Build Coastguard Worker    }
2171*a67afe4dSAndroid Build Coastguard Worker 
2172*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2173*a67afe4dSAndroid Build Coastguard Worker    {
2174*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2175*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2176*a67afe4dSAndroid Build Coastguard Worker       return;
2177*a67afe4dSAndroid Build Coastguard Worker    }
2178*a67afe4dSAndroid Build Coastguard Worker 
2179*a67afe4dSAndroid Build Coastguard Worker    if (length != 9)
2180*a67afe4dSAndroid Build Coastguard Worker    {
2181*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2182*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2183*a67afe4dSAndroid Build Coastguard Worker       return;
2184*a67afe4dSAndroid Build Coastguard Worker    }
2185*a67afe4dSAndroid Build Coastguard Worker 
2186*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 9);
2187*a67afe4dSAndroid Build Coastguard Worker 
2188*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2189*a67afe4dSAndroid Build Coastguard Worker       return;
2190*a67afe4dSAndroid Build Coastguard Worker 
2191*a67afe4dSAndroid Build Coastguard Worker    res_x = png_get_uint_32(buf);
2192*a67afe4dSAndroid Build Coastguard Worker    res_y = png_get_uint_32(buf + 4);
2193*a67afe4dSAndroid Build Coastguard Worker    unit_type = buf[8];
2194*a67afe4dSAndroid Build Coastguard Worker    png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2195*a67afe4dSAndroid Build Coastguard Worker }
2196*a67afe4dSAndroid Build Coastguard Worker #endif
2197*a67afe4dSAndroid Build Coastguard Worker 
2198*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_oFFs_SUPPORTED
2199*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_oFFs(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2200*a67afe4dSAndroid Build Coastguard Worker png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2201*a67afe4dSAndroid Build Coastguard Worker {
2202*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[9];
2203*a67afe4dSAndroid Build Coastguard Worker    png_int_32 offset_x, offset_y;
2204*a67afe4dSAndroid Build Coastguard Worker    int unit_type;
2205*a67afe4dSAndroid Build Coastguard Worker 
2206*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_oFFs");
2207*a67afe4dSAndroid Build Coastguard Worker 
2208*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2209*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2210*a67afe4dSAndroid Build Coastguard Worker 
2211*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2212*a67afe4dSAndroid Build Coastguard Worker    {
2213*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2214*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
2215*a67afe4dSAndroid Build Coastguard Worker       return;
2216*a67afe4dSAndroid Build Coastguard Worker    }
2217*a67afe4dSAndroid Build Coastguard Worker 
2218*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2219*a67afe4dSAndroid Build Coastguard Worker    {
2220*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2221*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2222*a67afe4dSAndroid Build Coastguard Worker       return;
2223*a67afe4dSAndroid Build Coastguard Worker    }
2224*a67afe4dSAndroid Build Coastguard Worker 
2225*a67afe4dSAndroid Build Coastguard Worker    if (length != 9)
2226*a67afe4dSAndroid Build Coastguard Worker    {
2227*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2228*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2229*a67afe4dSAndroid Build Coastguard Worker       return;
2230*a67afe4dSAndroid Build Coastguard Worker    }
2231*a67afe4dSAndroid Build Coastguard Worker 
2232*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 9);
2233*a67afe4dSAndroid Build Coastguard Worker 
2234*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2235*a67afe4dSAndroid Build Coastguard Worker       return;
2236*a67afe4dSAndroid Build Coastguard Worker 
2237*a67afe4dSAndroid Build Coastguard Worker    offset_x = png_get_int_32(buf);
2238*a67afe4dSAndroid Build Coastguard Worker    offset_y = png_get_int_32(buf + 4);
2239*a67afe4dSAndroid Build Coastguard Worker    unit_type = buf[8];
2240*a67afe4dSAndroid Build Coastguard Worker    png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2241*a67afe4dSAndroid Build Coastguard Worker }
2242*a67afe4dSAndroid Build Coastguard Worker #endif
2243*a67afe4dSAndroid Build Coastguard Worker 
2244*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_pCAL_SUPPORTED
2245*a67afe4dSAndroid Build Coastguard Worker /* Read the pCAL chunk (described in the PNG Extensions document) */
2246*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_pCAL(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2247*a67afe4dSAndroid Build Coastguard Worker png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2248*a67afe4dSAndroid Build Coastguard Worker {
2249*a67afe4dSAndroid Build Coastguard Worker    png_int_32 X0, X1;
2250*a67afe4dSAndroid Build Coastguard Worker    png_byte type, nparams;
2251*a67afe4dSAndroid Build Coastguard Worker    png_bytep buffer, buf, units, endptr;
2252*a67afe4dSAndroid Build Coastguard Worker    png_charpp params;
2253*a67afe4dSAndroid Build Coastguard Worker    int i;
2254*a67afe4dSAndroid Build Coastguard Worker 
2255*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_pCAL");
2256*a67afe4dSAndroid Build Coastguard Worker 
2257*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2258*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2259*a67afe4dSAndroid Build Coastguard Worker 
2260*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2261*a67afe4dSAndroid Build Coastguard Worker    {
2262*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2263*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
2264*a67afe4dSAndroid Build Coastguard Worker       return;
2265*a67afe4dSAndroid Build Coastguard Worker    }
2266*a67afe4dSAndroid Build Coastguard Worker 
2267*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2268*a67afe4dSAndroid Build Coastguard Worker    {
2269*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2270*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2271*a67afe4dSAndroid Build Coastguard Worker       return;
2272*a67afe4dSAndroid Build Coastguard Worker    }
2273*a67afe4dSAndroid Build Coastguard Worker 
2274*a67afe4dSAndroid Build Coastguard Worker    png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2275*a67afe4dSAndroid Build Coastguard Worker        length + 1);
2276*a67afe4dSAndroid Build Coastguard Worker 
2277*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2278*a67afe4dSAndroid Build Coastguard Worker 
2279*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
2280*a67afe4dSAndroid Build Coastguard Worker    {
2281*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2282*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2283*a67afe4dSAndroid Build Coastguard Worker       return;
2284*a67afe4dSAndroid Build Coastguard Worker    }
2285*a67afe4dSAndroid Build Coastguard Worker 
2286*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
2287*a67afe4dSAndroid Build Coastguard Worker 
2288*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2289*a67afe4dSAndroid Build Coastguard Worker       return;
2290*a67afe4dSAndroid Build Coastguard Worker 
2291*a67afe4dSAndroid Build Coastguard Worker    buffer[length] = 0; /* Null terminate the last string */
2292*a67afe4dSAndroid Build Coastguard Worker 
2293*a67afe4dSAndroid Build Coastguard Worker    png_debug(3, "Finding end of pCAL purpose string");
2294*a67afe4dSAndroid Build Coastguard Worker    for (buf = buffer; *buf; buf++)
2295*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop */ ;
2296*a67afe4dSAndroid Build Coastguard Worker 
2297*a67afe4dSAndroid Build Coastguard Worker    endptr = buffer + length;
2298*a67afe4dSAndroid Build Coastguard Worker 
2299*a67afe4dSAndroid Build Coastguard Worker    /* We need to have at least 12 bytes after the purpose string
2300*a67afe4dSAndroid Build Coastguard Worker     * in order to get the parameter information.
2301*a67afe4dSAndroid Build Coastguard Worker     */
2302*a67afe4dSAndroid Build Coastguard Worker    if (endptr - buf <= 12)
2303*a67afe4dSAndroid Build Coastguard Worker    {
2304*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2305*a67afe4dSAndroid Build Coastguard Worker       return;
2306*a67afe4dSAndroid Build Coastguard Worker    }
2307*a67afe4dSAndroid Build Coastguard Worker 
2308*a67afe4dSAndroid Build Coastguard Worker    png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2309*a67afe4dSAndroid Build Coastguard Worker    X0 = png_get_int_32((png_bytep)buf+1);
2310*a67afe4dSAndroid Build Coastguard Worker    X1 = png_get_int_32((png_bytep)buf+5);
2311*a67afe4dSAndroid Build Coastguard Worker    type = buf[9];
2312*a67afe4dSAndroid Build Coastguard Worker    nparams = buf[10];
2313*a67afe4dSAndroid Build Coastguard Worker    units = buf + 11;
2314*a67afe4dSAndroid Build Coastguard Worker 
2315*a67afe4dSAndroid Build Coastguard Worker    png_debug(3, "Checking pCAL equation type and number of parameters");
2316*a67afe4dSAndroid Build Coastguard Worker    /* Check that we have the right number of parameters for known
2317*a67afe4dSAndroid Build Coastguard Worker     * equation types.
2318*a67afe4dSAndroid Build Coastguard Worker     */
2319*a67afe4dSAndroid Build Coastguard Worker    if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2320*a67afe4dSAndroid Build Coastguard Worker        (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2321*a67afe4dSAndroid Build Coastguard Worker        (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2322*a67afe4dSAndroid Build Coastguard Worker        (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2323*a67afe4dSAndroid Build Coastguard Worker    {
2324*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid parameter count");
2325*a67afe4dSAndroid Build Coastguard Worker       return;
2326*a67afe4dSAndroid Build Coastguard Worker    }
2327*a67afe4dSAndroid Build Coastguard Worker 
2328*a67afe4dSAndroid Build Coastguard Worker    else if (type >= PNG_EQUATION_LAST)
2329*a67afe4dSAndroid Build Coastguard Worker    {
2330*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "unrecognized equation type");
2331*a67afe4dSAndroid Build Coastguard Worker    }
2332*a67afe4dSAndroid Build Coastguard Worker 
2333*a67afe4dSAndroid Build Coastguard Worker    for (buf = units; *buf; buf++)
2334*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop to move past the units string. */ ;
2335*a67afe4dSAndroid Build Coastguard Worker 
2336*a67afe4dSAndroid Build Coastguard Worker    png_debug(3, "Allocating pCAL parameters array");
2337*a67afe4dSAndroid Build Coastguard Worker 
2338*a67afe4dSAndroid Build Coastguard Worker    params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2339*a67afe4dSAndroid Build Coastguard Worker        nparams * (sizeof (png_charp))));
2340*a67afe4dSAndroid Build Coastguard Worker 
2341*a67afe4dSAndroid Build Coastguard Worker    if (params == NULL)
2342*a67afe4dSAndroid Build Coastguard Worker    {
2343*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2344*a67afe4dSAndroid Build Coastguard Worker       return;
2345*a67afe4dSAndroid Build Coastguard Worker    }
2346*a67afe4dSAndroid Build Coastguard Worker 
2347*a67afe4dSAndroid Build Coastguard Worker    /* Get pointers to the start of each parameter string. */
2348*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < nparams; i++)
2349*a67afe4dSAndroid Build Coastguard Worker    {
2350*a67afe4dSAndroid Build Coastguard Worker       buf++; /* Skip the null string terminator from previous parameter. */
2351*a67afe4dSAndroid Build Coastguard Worker 
2352*a67afe4dSAndroid Build Coastguard Worker       png_debug1(3, "Reading pCAL parameter %d", i);
2353*a67afe4dSAndroid Build Coastguard Worker 
2354*a67afe4dSAndroid Build Coastguard Worker       for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2355*a67afe4dSAndroid Build Coastguard Worker          /* Empty loop to move past each parameter string */ ;
2356*a67afe4dSAndroid Build Coastguard Worker 
2357*a67afe4dSAndroid Build Coastguard Worker       /* Make sure we haven't run out of data yet */
2358*a67afe4dSAndroid Build Coastguard Worker       if (buf > endptr)
2359*a67afe4dSAndroid Build Coastguard Worker       {
2360*a67afe4dSAndroid Build Coastguard Worker          png_free(png_ptr, params);
2361*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "invalid data");
2362*a67afe4dSAndroid Build Coastguard Worker          return;
2363*a67afe4dSAndroid Build Coastguard Worker       }
2364*a67afe4dSAndroid Build Coastguard Worker    }
2365*a67afe4dSAndroid Build Coastguard Worker 
2366*a67afe4dSAndroid Build Coastguard Worker    png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2367*a67afe4dSAndroid Build Coastguard Worker        (png_charp)units, params);
2368*a67afe4dSAndroid Build Coastguard Worker 
2369*a67afe4dSAndroid Build Coastguard Worker    png_free(png_ptr, params);
2370*a67afe4dSAndroid Build Coastguard Worker }
2371*a67afe4dSAndroid Build Coastguard Worker #endif
2372*a67afe4dSAndroid Build Coastguard Worker 
2373*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_sCAL_SUPPORTED
2374*a67afe4dSAndroid Build Coastguard Worker /* Read the sCAL chunk */
2375*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_sCAL(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2376*a67afe4dSAndroid Build Coastguard Worker png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2377*a67afe4dSAndroid Build Coastguard Worker {
2378*a67afe4dSAndroid Build Coastguard Worker    png_bytep buffer;
2379*a67afe4dSAndroid Build Coastguard Worker    size_t i;
2380*a67afe4dSAndroid Build Coastguard Worker    int state;
2381*a67afe4dSAndroid Build Coastguard Worker 
2382*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_sCAL");
2383*a67afe4dSAndroid Build Coastguard Worker 
2384*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2385*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2386*a67afe4dSAndroid Build Coastguard Worker 
2387*a67afe4dSAndroid Build Coastguard Worker    else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2388*a67afe4dSAndroid Build Coastguard Worker    {
2389*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2390*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of place");
2391*a67afe4dSAndroid Build Coastguard Worker       return;
2392*a67afe4dSAndroid Build Coastguard Worker    }
2393*a67afe4dSAndroid Build Coastguard Worker 
2394*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2395*a67afe4dSAndroid Build Coastguard Worker    {
2396*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2397*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2398*a67afe4dSAndroid Build Coastguard Worker       return;
2399*a67afe4dSAndroid Build Coastguard Worker    }
2400*a67afe4dSAndroid Build Coastguard Worker 
2401*a67afe4dSAndroid Build Coastguard Worker    /* Need unit type, width, \0, height: minimum 4 bytes */
2402*a67afe4dSAndroid Build Coastguard Worker    else if (length < 4)
2403*a67afe4dSAndroid Build Coastguard Worker    {
2404*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2405*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2406*a67afe4dSAndroid Build Coastguard Worker       return;
2407*a67afe4dSAndroid Build Coastguard Worker    }
2408*a67afe4dSAndroid Build Coastguard Worker 
2409*a67afe4dSAndroid Build Coastguard Worker    png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2410*a67afe4dSAndroid Build Coastguard Worker        length + 1);
2411*a67afe4dSAndroid Build Coastguard Worker 
2412*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2413*a67afe4dSAndroid Build Coastguard Worker 
2414*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
2415*a67afe4dSAndroid Build Coastguard Worker    {
2416*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2417*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2418*a67afe4dSAndroid Build Coastguard Worker       return;
2419*a67afe4dSAndroid Build Coastguard Worker    }
2420*a67afe4dSAndroid Build Coastguard Worker 
2421*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
2422*a67afe4dSAndroid Build Coastguard Worker    buffer[length] = 0; /* Null terminate the last string */
2423*a67afe4dSAndroid Build Coastguard Worker 
2424*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2425*a67afe4dSAndroid Build Coastguard Worker       return;
2426*a67afe4dSAndroid Build Coastguard Worker 
2427*a67afe4dSAndroid Build Coastguard Worker    /* Validate the unit. */
2428*a67afe4dSAndroid Build Coastguard Worker    if (buffer[0] != 1 && buffer[0] != 2)
2429*a67afe4dSAndroid Build Coastguard Worker    {
2430*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid unit");
2431*a67afe4dSAndroid Build Coastguard Worker       return;
2432*a67afe4dSAndroid Build Coastguard Worker    }
2433*a67afe4dSAndroid Build Coastguard Worker 
2434*a67afe4dSAndroid Build Coastguard Worker    /* Validate the ASCII numbers, need two ASCII numbers separated by
2435*a67afe4dSAndroid Build Coastguard Worker     * a '\0' and they need to fit exactly in the chunk data.
2436*a67afe4dSAndroid Build Coastguard Worker     */
2437*a67afe4dSAndroid Build Coastguard Worker    i = 1;
2438*a67afe4dSAndroid Build Coastguard Worker    state = 0;
2439*a67afe4dSAndroid Build Coastguard Worker 
2440*a67afe4dSAndroid Build Coastguard Worker    if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2441*a67afe4dSAndroid Build Coastguard Worker        i >= length || buffer[i++] != 0)
2442*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "bad width format");
2443*a67afe4dSAndroid Build Coastguard Worker 
2444*a67afe4dSAndroid Build Coastguard Worker    else if (PNG_FP_IS_POSITIVE(state) == 0)
2445*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "non-positive width");
2446*a67afe4dSAndroid Build Coastguard Worker 
2447*a67afe4dSAndroid Build Coastguard Worker    else
2448*a67afe4dSAndroid Build Coastguard Worker    {
2449*a67afe4dSAndroid Build Coastguard Worker       size_t heighti = i;
2450*a67afe4dSAndroid Build Coastguard Worker 
2451*a67afe4dSAndroid Build Coastguard Worker       state = 0;
2452*a67afe4dSAndroid Build Coastguard Worker       if (png_check_fp_number((png_const_charp)buffer, length,
2453*a67afe4dSAndroid Build Coastguard Worker           &state, &i) == 0 || i != length)
2454*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "bad height format");
2455*a67afe4dSAndroid Build Coastguard Worker 
2456*a67afe4dSAndroid Build Coastguard Worker       else if (PNG_FP_IS_POSITIVE(state) == 0)
2457*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "non-positive height");
2458*a67afe4dSAndroid Build Coastguard Worker 
2459*a67afe4dSAndroid Build Coastguard Worker       else
2460*a67afe4dSAndroid Build Coastguard Worker          /* This is the (only) success case. */
2461*a67afe4dSAndroid Build Coastguard Worker          png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2462*a67afe4dSAndroid Build Coastguard Worker              (png_charp)buffer+1, (png_charp)buffer+heighti);
2463*a67afe4dSAndroid Build Coastguard Worker    }
2464*a67afe4dSAndroid Build Coastguard Worker }
2465*a67afe4dSAndroid Build Coastguard Worker #endif
2466*a67afe4dSAndroid Build Coastguard Worker 
2467*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_tIME_SUPPORTED
2468*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_tIME(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2469*a67afe4dSAndroid Build Coastguard Worker png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2470*a67afe4dSAndroid Build Coastguard Worker {
2471*a67afe4dSAndroid Build Coastguard Worker    png_byte buf[7];
2472*a67afe4dSAndroid Build Coastguard Worker    png_time mod_time;
2473*a67afe4dSAndroid Build Coastguard Worker 
2474*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_tIME");
2475*a67afe4dSAndroid Build Coastguard Worker 
2476*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2477*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2478*a67afe4dSAndroid Build Coastguard Worker 
2479*a67afe4dSAndroid Build Coastguard Worker    else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2480*a67afe4dSAndroid Build Coastguard Worker    {
2481*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2482*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "duplicate");
2483*a67afe4dSAndroid Build Coastguard Worker       return;
2484*a67afe4dSAndroid Build Coastguard Worker    }
2485*a67afe4dSAndroid Build Coastguard Worker 
2486*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2487*a67afe4dSAndroid Build Coastguard Worker       png_ptr->mode |= PNG_AFTER_IDAT;
2488*a67afe4dSAndroid Build Coastguard Worker 
2489*a67afe4dSAndroid Build Coastguard Worker    if (length != 7)
2490*a67afe4dSAndroid Build Coastguard Worker    {
2491*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2492*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "invalid");
2493*a67afe4dSAndroid Build Coastguard Worker       return;
2494*a67afe4dSAndroid Build Coastguard Worker    }
2495*a67afe4dSAndroid Build Coastguard Worker 
2496*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buf, 7);
2497*a67afe4dSAndroid Build Coastguard Worker 
2498*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2499*a67afe4dSAndroid Build Coastguard Worker       return;
2500*a67afe4dSAndroid Build Coastguard Worker 
2501*a67afe4dSAndroid Build Coastguard Worker    mod_time.second = buf[6];
2502*a67afe4dSAndroid Build Coastguard Worker    mod_time.minute = buf[5];
2503*a67afe4dSAndroid Build Coastguard Worker    mod_time.hour = buf[4];
2504*a67afe4dSAndroid Build Coastguard Worker    mod_time.day = buf[3];
2505*a67afe4dSAndroid Build Coastguard Worker    mod_time.month = buf[2];
2506*a67afe4dSAndroid Build Coastguard Worker    mod_time.year = png_get_uint_16(buf);
2507*a67afe4dSAndroid Build Coastguard Worker 
2508*a67afe4dSAndroid Build Coastguard Worker    png_set_tIME(png_ptr, info_ptr, &mod_time);
2509*a67afe4dSAndroid Build Coastguard Worker }
2510*a67afe4dSAndroid Build Coastguard Worker #endif
2511*a67afe4dSAndroid Build Coastguard Worker 
2512*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_tEXt_SUPPORTED
2513*a67afe4dSAndroid Build Coastguard Worker /* Note: this does not properly handle chunks that are > 64K under DOS */
2514*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_tEXt(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2515*a67afe4dSAndroid Build Coastguard Worker png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2516*a67afe4dSAndroid Build Coastguard Worker {
2517*a67afe4dSAndroid Build Coastguard Worker    png_text  text_info;
2518*a67afe4dSAndroid Build Coastguard Worker    png_bytep buffer;
2519*a67afe4dSAndroid Build Coastguard Worker    png_charp key;
2520*a67afe4dSAndroid Build Coastguard Worker    png_charp text;
2521*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 skip = 0;
2522*a67afe4dSAndroid Build Coastguard Worker 
2523*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_tEXt");
2524*a67afe4dSAndroid Build Coastguard Worker 
2525*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USER_LIMITS_SUPPORTED
2526*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_cache_max != 0)
2527*a67afe4dSAndroid Build Coastguard Worker    {
2528*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->user_chunk_cache_max == 1)
2529*a67afe4dSAndroid Build Coastguard Worker       {
2530*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2531*a67afe4dSAndroid Build Coastguard Worker          return;
2532*a67afe4dSAndroid Build Coastguard Worker       }
2533*a67afe4dSAndroid Build Coastguard Worker 
2534*a67afe4dSAndroid Build Coastguard Worker       if (--png_ptr->user_chunk_cache_max == 1)
2535*a67afe4dSAndroid Build Coastguard Worker       {
2536*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2537*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "no space in chunk cache");
2538*a67afe4dSAndroid Build Coastguard Worker          return;
2539*a67afe4dSAndroid Build Coastguard Worker       }
2540*a67afe4dSAndroid Build Coastguard Worker    }
2541*a67afe4dSAndroid Build Coastguard Worker #endif
2542*a67afe4dSAndroid Build Coastguard Worker 
2543*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2544*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2545*a67afe4dSAndroid Build Coastguard Worker 
2546*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2547*a67afe4dSAndroid Build Coastguard Worker       png_ptr->mode |= PNG_AFTER_IDAT;
2548*a67afe4dSAndroid Build Coastguard Worker 
2549*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_MAX_MALLOC_64K
2550*a67afe4dSAndroid Build Coastguard Worker    if (length > 65535U)
2551*a67afe4dSAndroid Build Coastguard Worker    {
2552*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2553*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "too large to fit in memory");
2554*a67afe4dSAndroid Build Coastguard Worker       return;
2555*a67afe4dSAndroid Build Coastguard Worker    }
2556*a67afe4dSAndroid Build Coastguard Worker #endif
2557*a67afe4dSAndroid Build Coastguard Worker 
2558*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2559*a67afe4dSAndroid Build Coastguard Worker 
2560*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
2561*a67afe4dSAndroid Build Coastguard Worker    {
2562*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2563*a67afe4dSAndroid Build Coastguard Worker       return;
2564*a67afe4dSAndroid Build Coastguard Worker    }
2565*a67afe4dSAndroid Build Coastguard Worker 
2566*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
2567*a67afe4dSAndroid Build Coastguard Worker 
2568*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, skip) != 0)
2569*a67afe4dSAndroid Build Coastguard Worker       return;
2570*a67afe4dSAndroid Build Coastguard Worker 
2571*a67afe4dSAndroid Build Coastguard Worker    key = (png_charp)buffer;
2572*a67afe4dSAndroid Build Coastguard Worker    key[length] = 0;
2573*a67afe4dSAndroid Build Coastguard Worker 
2574*a67afe4dSAndroid Build Coastguard Worker    for (text = key; *text; text++)
2575*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop to find end of key */ ;
2576*a67afe4dSAndroid Build Coastguard Worker 
2577*a67afe4dSAndroid Build Coastguard Worker    if (text != key + length)
2578*a67afe4dSAndroid Build Coastguard Worker       text++;
2579*a67afe4dSAndroid Build Coastguard Worker 
2580*a67afe4dSAndroid Build Coastguard Worker    text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2581*a67afe4dSAndroid Build Coastguard Worker    text_info.key = key;
2582*a67afe4dSAndroid Build Coastguard Worker    text_info.lang = NULL;
2583*a67afe4dSAndroid Build Coastguard Worker    text_info.lang_key = NULL;
2584*a67afe4dSAndroid Build Coastguard Worker    text_info.itxt_length = 0;
2585*a67afe4dSAndroid Build Coastguard Worker    text_info.text = text;
2586*a67afe4dSAndroid Build Coastguard Worker    text_info.text_length = strlen(text);
2587*a67afe4dSAndroid Build Coastguard Worker 
2588*a67afe4dSAndroid Build Coastguard Worker    if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2589*a67afe4dSAndroid Build Coastguard Worker       png_warning(png_ptr, "Insufficient memory to process text chunk");
2590*a67afe4dSAndroid Build Coastguard Worker }
2591*a67afe4dSAndroid Build Coastguard Worker #endif
2592*a67afe4dSAndroid Build Coastguard Worker 
2593*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_zTXt_SUPPORTED
2594*a67afe4dSAndroid Build Coastguard Worker /* Note: this does not correctly handle chunks that are > 64K under DOS */
2595*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_zTXt(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2596*a67afe4dSAndroid Build Coastguard Worker png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2597*a67afe4dSAndroid Build Coastguard Worker {
2598*a67afe4dSAndroid Build Coastguard Worker    png_const_charp errmsg = NULL;
2599*a67afe4dSAndroid Build Coastguard Worker    png_bytep       buffer;
2600*a67afe4dSAndroid Build Coastguard Worker    png_uint_32     keyword_length;
2601*a67afe4dSAndroid Build Coastguard Worker 
2602*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_zTXt");
2603*a67afe4dSAndroid Build Coastguard Worker 
2604*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USER_LIMITS_SUPPORTED
2605*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_cache_max != 0)
2606*a67afe4dSAndroid Build Coastguard Worker    {
2607*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->user_chunk_cache_max == 1)
2608*a67afe4dSAndroid Build Coastguard Worker       {
2609*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2610*a67afe4dSAndroid Build Coastguard Worker          return;
2611*a67afe4dSAndroid Build Coastguard Worker       }
2612*a67afe4dSAndroid Build Coastguard Worker 
2613*a67afe4dSAndroid Build Coastguard Worker       if (--png_ptr->user_chunk_cache_max == 1)
2614*a67afe4dSAndroid Build Coastguard Worker       {
2615*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2616*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "no space in chunk cache");
2617*a67afe4dSAndroid Build Coastguard Worker          return;
2618*a67afe4dSAndroid Build Coastguard Worker       }
2619*a67afe4dSAndroid Build Coastguard Worker    }
2620*a67afe4dSAndroid Build Coastguard Worker #endif
2621*a67afe4dSAndroid Build Coastguard Worker 
2622*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2623*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2624*a67afe4dSAndroid Build Coastguard Worker 
2625*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2626*a67afe4dSAndroid Build Coastguard Worker       png_ptr->mode |= PNG_AFTER_IDAT;
2627*a67afe4dSAndroid Build Coastguard Worker 
2628*a67afe4dSAndroid Build Coastguard Worker    /* Note, "length" is sufficient here; we won't be adding
2629*a67afe4dSAndroid Build Coastguard Worker     * a null terminator later.
2630*a67afe4dSAndroid Build Coastguard Worker     */
2631*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2632*a67afe4dSAndroid Build Coastguard Worker 
2633*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
2634*a67afe4dSAndroid Build Coastguard Worker    {
2635*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2636*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2637*a67afe4dSAndroid Build Coastguard Worker       return;
2638*a67afe4dSAndroid Build Coastguard Worker    }
2639*a67afe4dSAndroid Build Coastguard Worker 
2640*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
2641*a67afe4dSAndroid Build Coastguard Worker 
2642*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2643*a67afe4dSAndroid Build Coastguard Worker       return;
2644*a67afe4dSAndroid Build Coastguard Worker 
2645*a67afe4dSAndroid Build Coastguard Worker    /* TODO: also check that the keyword contents match the spec! */
2646*a67afe4dSAndroid Build Coastguard Worker    for (keyword_length = 0;
2647*a67afe4dSAndroid Build Coastguard Worker       keyword_length < length && buffer[keyword_length] != 0;
2648*a67afe4dSAndroid Build Coastguard Worker       ++keyword_length)
2649*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop to find end of name */ ;
2650*a67afe4dSAndroid Build Coastguard Worker 
2651*a67afe4dSAndroid Build Coastguard Worker    if (keyword_length > 79 || keyword_length < 1)
2652*a67afe4dSAndroid Build Coastguard Worker       errmsg = "bad keyword";
2653*a67afe4dSAndroid Build Coastguard Worker 
2654*a67afe4dSAndroid Build Coastguard Worker    /* zTXt must have some LZ data after the keyword, although it may expand to
2655*a67afe4dSAndroid Build Coastguard Worker     * zero bytes; we need a '\0' at the end of the keyword, the compression type
2656*a67afe4dSAndroid Build Coastguard Worker     * then the LZ data:
2657*a67afe4dSAndroid Build Coastguard Worker     */
2658*a67afe4dSAndroid Build Coastguard Worker    else if (keyword_length + 3 > length)
2659*a67afe4dSAndroid Build Coastguard Worker       errmsg = "truncated";
2660*a67afe4dSAndroid Build Coastguard Worker 
2661*a67afe4dSAndroid Build Coastguard Worker    else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2662*a67afe4dSAndroid Build Coastguard Worker       errmsg = "unknown compression type";
2663*a67afe4dSAndroid Build Coastguard Worker 
2664*a67afe4dSAndroid Build Coastguard Worker    else
2665*a67afe4dSAndroid Build Coastguard Worker    {
2666*a67afe4dSAndroid Build Coastguard Worker       png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2667*a67afe4dSAndroid Build Coastguard Worker 
2668*a67afe4dSAndroid Build Coastguard Worker       /* TODO: at present png_decompress_chunk imposes a single application
2669*a67afe4dSAndroid Build Coastguard Worker        * level memory limit, this should be split to different values for iCCP
2670*a67afe4dSAndroid Build Coastguard Worker        * and text chunks.
2671*a67afe4dSAndroid Build Coastguard Worker        */
2672*a67afe4dSAndroid Build Coastguard Worker       if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2673*a67afe4dSAndroid Build Coastguard Worker           &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2674*a67afe4dSAndroid Build Coastguard Worker       {
2675*a67afe4dSAndroid Build Coastguard Worker          png_text text;
2676*a67afe4dSAndroid Build Coastguard Worker 
2677*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->read_buffer == NULL)
2678*a67afe4dSAndroid Build Coastguard Worker            errmsg="Read failure in png_handle_zTXt";
2679*a67afe4dSAndroid Build Coastguard Worker          else
2680*a67afe4dSAndroid Build Coastguard Worker          {
2681*a67afe4dSAndroid Build Coastguard Worker             /* It worked; png_ptr->read_buffer now looks like a tEXt chunk
2682*a67afe4dSAndroid Build Coastguard Worker              * except for the extra compression type byte and the fact that
2683*a67afe4dSAndroid Build Coastguard Worker              * it isn't necessarily '\0' terminated.
2684*a67afe4dSAndroid Build Coastguard Worker              */
2685*a67afe4dSAndroid Build Coastguard Worker             buffer = png_ptr->read_buffer;
2686*a67afe4dSAndroid Build Coastguard Worker             buffer[uncompressed_length+(keyword_length+2)] = 0;
2687*a67afe4dSAndroid Build Coastguard Worker 
2688*a67afe4dSAndroid Build Coastguard Worker             text.compression = PNG_TEXT_COMPRESSION_zTXt;
2689*a67afe4dSAndroid Build Coastguard Worker             text.key = (png_charp)buffer;
2690*a67afe4dSAndroid Build Coastguard Worker             text.text = (png_charp)(buffer + keyword_length+2);
2691*a67afe4dSAndroid Build Coastguard Worker             text.text_length = uncompressed_length;
2692*a67afe4dSAndroid Build Coastguard Worker             text.itxt_length = 0;
2693*a67afe4dSAndroid Build Coastguard Worker             text.lang = NULL;
2694*a67afe4dSAndroid Build Coastguard Worker             text.lang_key = NULL;
2695*a67afe4dSAndroid Build Coastguard Worker 
2696*a67afe4dSAndroid Build Coastguard Worker             if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2697*a67afe4dSAndroid Build Coastguard Worker                errmsg = "insufficient memory";
2698*a67afe4dSAndroid Build Coastguard Worker          }
2699*a67afe4dSAndroid Build Coastguard Worker       }
2700*a67afe4dSAndroid Build Coastguard Worker 
2701*a67afe4dSAndroid Build Coastguard Worker       else
2702*a67afe4dSAndroid Build Coastguard Worker          errmsg = png_ptr->zstream.msg;
2703*a67afe4dSAndroid Build Coastguard Worker    }
2704*a67afe4dSAndroid Build Coastguard Worker 
2705*a67afe4dSAndroid Build Coastguard Worker    if (errmsg != NULL)
2706*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, errmsg);
2707*a67afe4dSAndroid Build Coastguard Worker }
2708*a67afe4dSAndroid Build Coastguard Worker #endif
2709*a67afe4dSAndroid Build Coastguard Worker 
2710*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_iTXt_SUPPORTED
2711*a67afe4dSAndroid Build Coastguard Worker /* Note: this does not correctly handle chunks that are > 64K under DOS */
2712*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_iTXt(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length)2713*a67afe4dSAndroid Build Coastguard Worker png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2714*a67afe4dSAndroid Build Coastguard Worker {
2715*a67afe4dSAndroid Build Coastguard Worker    png_const_charp errmsg = NULL;
2716*a67afe4dSAndroid Build Coastguard Worker    png_bytep buffer;
2717*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 prefix_length;
2718*a67afe4dSAndroid Build Coastguard Worker 
2719*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_iTXt");
2720*a67afe4dSAndroid Build Coastguard Worker 
2721*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USER_LIMITS_SUPPORTED
2722*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_cache_max != 0)
2723*a67afe4dSAndroid Build Coastguard Worker    {
2724*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->user_chunk_cache_max == 1)
2725*a67afe4dSAndroid Build Coastguard Worker       {
2726*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2727*a67afe4dSAndroid Build Coastguard Worker          return;
2728*a67afe4dSAndroid Build Coastguard Worker       }
2729*a67afe4dSAndroid Build Coastguard Worker 
2730*a67afe4dSAndroid Build Coastguard Worker       if (--png_ptr->user_chunk_cache_max == 1)
2731*a67afe4dSAndroid Build Coastguard Worker       {
2732*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
2733*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "no space in chunk cache");
2734*a67afe4dSAndroid Build Coastguard Worker          return;
2735*a67afe4dSAndroid Build Coastguard Worker       }
2736*a67afe4dSAndroid Build Coastguard Worker    }
2737*a67afe4dSAndroid Build Coastguard Worker #endif
2738*a67afe4dSAndroid Build Coastguard Worker 
2739*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2740*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "missing IHDR");
2741*a67afe4dSAndroid Build Coastguard Worker 
2742*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2743*a67afe4dSAndroid Build Coastguard Worker       png_ptr->mode |= PNG_AFTER_IDAT;
2744*a67afe4dSAndroid Build Coastguard Worker 
2745*a67afe4dSAndroid Build Coastguard Worker    buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2746*a67afe4dSAndroid Build Coastguard Worker 
2747*a67afe4dSAndroid Build Coastguard Worker    if (buffer == NULL)
2748*a67afe4dSAndroid Build Coastguard Worker    {
2749*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2750*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "out of memory");
2751*a67afe4dSAndroid Build Coastguard Worker       return;
2752*a67afe4dSAndroid Build Coastguard Worker    }
2753*a67afe4dSAndroid Build Coastguard Worker 
2754*a67afe4dSAndroid Build Coastguard Worker    png_crc_read(png_ptr, buffer, length);
2755*a67afe4dSAndroid Build Coastguard Worker 
2756*a67afe4dSAndroid Build Coastguard Worker    if (png_crc_finish(png_ptr, 0) != 0)
2757*a67afe4dSAndroid Build Coastguard Worker       return;
2758*a67afe4dSAndroid Build Coastguard Worker 
2759*a67afe4dSAndroid Build Coastguard Worker    /* First the keyword. */
2760*a67afe4dSAndroid Build Coastguard Worker    for (prefix_length=0;
2761*a67afe4dSAndroid Build Coastguard Worker       prefix_length < length && buffer[prefix_length] != 0;
2762*a67afe4dSAndroid Build Coastguard Worker       ++prefix_length)
2763*a67afe4dSAndroid Build Coastguard Worker       /* Empty loop */ ;
2764*a67afe4dSAndroid Build Coastguard Worker 
2765*a67afe4dSAndroid Build Coastguard Worker    /* Perform a basic check on the keyword length here. */
2766*a67afe4dSAndroid Build Coastguard Worker    if (prefix_length > 79 || prefix_length < 1)
2767*a67afe4dSAndroid Build Coastguard Worker       errmsg = "bad keyword";
2768*a67afe4dSAndroid Build Coastguard Worker 
2769*a67afe4dSAndroid Build Coastguard Worker    /* Expect keyword, compression flag, compression type, language, translated
2770*a67afe4dSAndroid Build Coastguard Worker     * keyword (both may be empty but are 0 terminated) then the text, which may
2771*a67afe4dSAndroid Build Coastguard Worker     * be empty.
2772*a67afe4dSAndroid Build Coastguard Worker     */
2773*a67afe4dSAndroid Build Coastguard Worker    else if (prefix_length + 5 > length)
2774*a67afe4dSAndroid Build Coastguard Worker       errmsg = "truncated";
2775*a67afe4dSAndroid Build Coastguard Worker 
2776*a67afe4dSAndroid Build Coastguard Worker    else if (buffer[prefix_length+1] == 0 ||
2777*a67afe4dSAndroid Build Coastguard Worker       (buffer[prefix_length+1] == 1 &&
2778*a67afe4dSAndroid Build Coastguard Worker       buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2779*a67afe4dSAndroid Build Coastguard Worker    {
2780*a67afe4dSAndroid Build Coastguard Worker       int compressed = buffer[prefix_length+1] != 0;
2781*a67afe4dSAndroid Build Coastguard Worker       png_uint_32 language_offset, translated_keyword_offset;
2782*a67afe4dSAndroid Build Coastguard Worker       png_alloc_size_t uncompressed_length = 0;
2783*a67afe4dSAndroid Build Coastguard Worker 
2784*a67afe4dSAndroid Build Coastguard Worker       /* Now the language tag */
2785*a67afe4dSAndroid Build Coastguard Worker       prefix_length += 3;
2786*a67afe4dSAndroid Build Coastguard Worker       language_offset = prefix_length;
2787*a67afe4dSAndroid Build Coastguard Worker 
2788*a67afe4dSAndroid Build Coastguard Worker       for (; prefix_length < length && buffer[prefix_length] != 0;
2789*a67afe4dSAndroid Build Coastguard Worker          ++prefix_length)
2790*a67afe4dSAndroid Build Coastguard Worker          /* Empty loop */ ;
2791*a67afe4dSAndroid Build Coastguard Worker 
2792*a67afe4dSAndroid Build Coastguard Worker       /* WARNING: the length may be invalid here, this is checked below. */
2793*a67afe4dSAndroid Build Coastguard Worker       translated_keyword_offset = ++prefix_length;
2794*a67afe4dSAndroid Build Coastguard Worker 
2795*a67afe4dSAndroid Build Coastguard Worker       for (; prefix_length < length && buffer[prefix_length] != 0;
2796*a67afe4dSAndroid Build Coastguard Worker          ++prefix_length)
2797*a67afe4dSAndroid Build Coastguard Worker          /* Empty loop */ ;
2798*a67afe4dSAndroid Build Coastguard Worker 
2799*a67afe4dSAndroid Build Coastguard Worker       /* prefix_length should now be at the trailing '\0' of the translated
2800*a67afe4dSAndroid Build Coastguard Worker        * keyword, but it may already be over the end.  None of this arithmetic
2801*a67afe4dSAndroid Build Coastguard Worker        * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2802*a67afe4dSAndroid Build Coastguard Worker        * systems the available allocation may overflow.
2803*a67afe4dSAndroid Build Coastguard Worker        */
2804*a67afe4dSAndroid Build Coastguard Worker       ++prefix_length;
2805*a67afe4dSAndroid Build Coastguard Worker 
2806*a67afe4dSAndroid Build Coastguard Worker       if (compressed == 0 && prefix_length <= length)
2807*a67afe4dSAndroid Build Coastguard Worker          uncompressed_length = length - prefix_length;
2808*a67afe4dSAndroid Build Coastguard Worker 
2809*a67afe4dSAndroid Build Coastguard Worker       else if (compressed != 0 && prefix_length < length)
2810*a67afe4dSAndroid Build Coastguard Worker       {
2811*a67afe4dSAndroid Build Coastguard Worker          uncompressed_length = PNG_SIZE_MAX;
2812*a67afe4dSAndroid Build Coastguard Worker 
2813*a67afe4dSAndroid Build Coastguard Worker          /* TODO: at present png_decompress_chunk imposes a single application
2814*a67afe4dSAndroid Build Coastguard Worker           * level memory limit, this should be split to different values for
2815*a67afe4dSAndroid Build Coastguard Worker           * iCCP and text chunks.
2816*a67afe4dSAndroid Build Coastguard Worker           */
2817*a67afe4dSAndroid Build Coastguard Worker          if (png_decompress_chunk(png_ptr, length, prefix_length,
2818*a67afe4dSAndroid Build Coastguard Worker              &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2819*a67afe4dSAndroid Build Coastguard Worker             buffer = png_ptr->read_buffer;
2820*a67afe4dSAndroid Build Coastguard Worker 
2821*a67afe4dSAndroid Build Coastguard Worker          else
2822*a67afe4dSAndroid Build Coastguard Worker             errmsg = png_ptr->zstream.msg;
2823*a67afe4dSAndroid Build Coastguard Worker       }
2824*a67afe4dSAndroid Build Coastguard Worker 
2825*a67afe4dSAndroid Build Coastguard Worker       else
2826*a67afe4dSAndroid Build Coastguard Worker          errmsg = "truncated";
2827*a67afe4dSAndroid Build Coastguard Worker 
2828*a67afe4dSAndroid Build Coastguard Worker       if (errmsg == NULL)
2829*a67afe4dSAndroid Build Coastguard Worker       {
2830*a67afe4dSAndroid Build Coastguard Worker          png_text text;
2831*a67afe4dSAndroid Build Coastguard Worker 
2832*a67afe4dSAndroid Build Coastguard Worker          buffer[uncompressed_length+prefix_length] = 0;
2833*a67afe4dSAndroid Build Coastguard Worker 
2834*a67afe4dSAndroid Build Coastguard Worker          if (compressed == 0)
2835*a67afe4dSAndroid Build Coastguard Worker             text.compression = PNG_ITXT_COMPRESSION_NONE;
2836*a67afe4dSAndroid Build Coastguard Worker 
2837*a67afe4dSAndroid Build Coastguard Worker          else
2838*a67afe4dSAndroid Build Coastguard Worker             text.compression = PNG_ITXT_COMPRESSION_zTXt;
2839*a67afe4dSAndroid Build Coastguard Worker 
2840*a67afe4dSAndroid Build Coastguard Worker          text.key = (png_charp)buffer;
2841*a67afe4dSAndroid Build Coastguard Worker          text.lang = (png_charp)buffer + language_offset;
2842*a67afe4dSAndroid Build Coastguard Worker          text.lang_key = (png_charp)buffer + translated_keyword_offset;
2843*a67afe4dSAndroid Build Coastguard Worker          text.text = (png_charp)buffer + prefix_length;
2844*a67afe4dSAndroid Build Coastguard Worker          text.text_length = 0;
2845*a67afe4dSAndroid Build Coastguard Worker          text.itxt_length = uncompressed_length;
2846*a67afe4dSAndroid Build Coastguard Worker 
2847*a67afe4dSAndroid Build Coastguard Worker          if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2848*a67afe4dSAndroid Build Coastguard Worker             errmsg = "insufficient memory";
2849*a67afe4dSAndroid Build Coastguard Worker       }
2850*a67afe4dSAndroid Build Coastguard Worker    }
2851*a67afe4dSAndroid Build Coastguard Worker 
2852*a67afe4dSAndroid Build Coastguard Worker    else
2853*a67afe4dSAndroid Build Coastguard Worker       errmsg = "bad compression info";
2854*a67afe4dSAndroid Build Coastguard Worker 
2855*a67afe4dSAndroid Build Coastguard Worker    if (errmsg != NULL)
2856*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, errmsg);
2857*a67afe4dSAndroid Build Coastguard Worker }
2858*a67afe4dSAndroid Build Coastguard Worker #endif
2859*a67afe4dSAndroid Build Coastguard Worker 
2860*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2861*a67afe4dSAndroid Build Coastguard Worker /* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */
2862*a67afe4dSAndroid Build Coastguard Worker static int
png_cache_unknown_chunk(png_structrp png_ptr,png_uint_32 length)2863*a67afe4dSAndroid Build Coastguard Worker png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2864*a67afe4dSAndroid Build Coastguard Worker {
2865*a67afe4dSAndroid Build Coastguard Worker    png_alloc_size_t limit = PNG_SIZE_MAX;
2866*a67afe4dSAndroid Build Coastguard Worker 
2867*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->unknown_chunk.data != NULL)
2868*a67afe4dSAndroid Build Coastguard Worker    {
2869*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, png_ptr->unknown_chunk.data);
2870*a67afe4dSAndroid Build Coastguard Worker       png_ptr->unknown_chunk.data = NULL;
2871*a67afe4dSAndroid Build Coastguard Worker    }
2872*a67afe4dSAndroid Build Coastguard Worker 
2873*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_SET_USER_LIMITS_SUPPORTED
2874*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_malloc_max > 0 &&
2875*a67afe4dSAndroid Build Coastguard Worker        png_ptr->user_chunk_malloc_max < limit)
2876*a67afe4dSAndroid Build Coastguard Worker       limit = png_ptr->user_chunk_malloc_max;
2877*a67afe4dSAndroid Build Coastguard Worker 
2878*a67afe4dSAndroid Build Coastguard Worker #  elif PNG_USER_CHUNK_MALLOC_MAX > 0
2879*a67afe4dSAndroid Build Coastguard Worker    if (PNG_USER_CHUNK_MALLOC_MAX < limit)
2880*a67afe4dSAndroid Build Coastguard Worker       limit = PNG_USER_CHUNK_MALLOC_MAX;
2881*a67afe4dSAndroid Build Coastguard Worker #  endif
2882*a67afe4dSAndroid Build Coastguard Worker 
2883*a67afe4dSAndroid Build Coastguard Worker    if (length <= limit)
2884*a67afe4dSAndroid Build Coastguard Worker    {
2885*a67afe4dSAndroid Build Coastguard Worker       PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2886*a67afe4dSAndroid Build Coastguard Worker       /* The following is safe because of the PNG_SIZE_MAX init above */
2887*a67afe4dSAndroid Build Coastguard Worker       png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/;
2888*a67afe4dSAndroid Build Coastguard Worker       /* 'mode' is a flag array, only the bottom four bits matter here */
2889*a67afe4dSAndroid Build Coastguard Worker       png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/;
2890*a67afe4dSAndroid Build Coastguard Worker 
2891*a67afe4dSAndroid Build Coastguard Worker       if (length == 0)
2892*a67afe4dSAndroid Build Coastguard Worker          png_ptr->unknown_chunk.data = NULL;
2893*a67afe4dSAndroid Build Coastguard Worker 
2894*a67afe4dSAndroid Build Coastguard Worker       else
2895*a67afe4dSAndroid Build Coastguard Worker       {
2896*a67afe4dSAndroid Build Coastguard Worker          /* Do a 'warn' here - it is handled below. */
2897*a67afe4dSAndroid Build Coastguard Worker          png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2898*a67afe4dSAndroid Build Coastguard Worker              png_malloc_warn(png_ptr, length));
2899*a67afe4dSAndroid Build Coastguard Worker       }
2900*a67afe4dSAndroid Build Coastguard Worker    }
2901*a67afe4dSAndroid Build Coastguard Worker 
2902*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->unknown_chunk.data == NULL && length > 0)
2903*a67afe4dSAndroid Build Coastguard Worker    {
2904*a67afe4dSAndroid Build Coastguard Worker       /* This is benign because we clean up correctly */
2905*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
2906*a67afe4dSAndroid Build Coastguard Worker       png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits");
2907*a67afe4dSAndroid Build Coastguard Worker       return 0;
2908*a67afe4dSAndroid Build Coastguard Worker    }
2909*a67afe4dSAndroid Build Coastguard Worker 
2910*a67afe4dSAndroid Build Coastguard Worker    else
2911*a67afe4dSAndroid Build Coastguard Worker    {
2912*a67afe4dSAndroid Build Coastguard Worker       if (length > 0)
2913*a67afe4dSAndroid Build Coastguard Worker          png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2914*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, 0);
2915*a67afe4dSAndroid Build Coastguard Worker       return 1;
2916*a67afe4dSAndroid Build Coastguard Worker    }
2917*a67afe4dSAndroid Build Coastguard Worker }
2918*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_UNKNOWN_CHUNKS */
2919*a67afe4dSAndroid Build Coastguard Worker 
2920*a67afe4dSAndroid Build Coastguard Worker /* Handle an unknown, or known but disabled, chunk */
2921*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_handle_unknown(png_structrp png_ptr,png_inforp info_ptr,png_uint_32 length,int keep)2922*a67afe4dSAndroid Build Coastguard Worker png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2923*a67afe4dSAndroid Build Coastguard Worker     png_uint_32 length, int keep)
2924*a67afe4dSAndroid Build Coastguard Worker {
2925*a67afe4dSAndroid Build Coastguard Worker    int handled = 0; /* the chunk was handled */
2926*a67afe4dSAndroid Build Coastguard Worker 
2927*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_handle_unknown");
2928*a67afe4dSAndroid Build Coastguard Worker 
2929*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2930*a67afe4dSAndroid Build Coastguard Worker    /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2931*a67afe4dSAndroid Build Coastguard Worker     * the bug which meant that setting a non-default behavior for a specific
2932*a67afe4dSAndroid Build Coastguard Worker     * chunk would be ignored (the default was always used unless a user
2933*a67afe4dSAndroid Build Coastguard Worker     * callback was installed).
2934*a67afe4dSAndroid Build Coastguard Worker     *
2935*a67afe4dSAndroid Build Coastguard Worker     * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2936*a67afe4dSAndroid Build Coastguard Worker     * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2937*a67afe4dSAndroid Build Coastguard Worker     * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2938*a67afe4dSAndroid Build Coastguard Worker     * This is just an optimization to avoid multiple calls to the lookup
2939*a67afe4dSAndroid Build Coastguard Worker     * function.
2940*a67afe4dSAndroid Build Coastguard Worker     */
2941*a67afe4dSAndroid Build Coastguard Worker #  ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2942*a67afe4dSAndroid Build Coastguard Worker #     ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2943*a67afe4dSAndroid Build Coastguard Worker    keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2944*a67afe4dSAndroid Build Coastguard Worker #     endif
2945*a67afe4dSAndroid Build Coastguard Worker #  endif
2946*a67afe4dSAndroid Build Coastguard Worker 
2947*a67afe4dSAndroid Build Coastguard Worker    /* One of the following methods will read the chunk or skip it (at least one
2948*a67afe4dSAndroid Build Coastguard Worker     * of these is always defined because this is the only way to switch on
2949*a67afe4dSAndroid Build Coastguard Worker     * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2950*a67afe4dSAndroid Build Coastguard Worker     */
2951*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2952*a67afe4dSAndroid Build Coastguard Worker    /* The user callback takes precedence over the chunk keep value, but the
2953*a67afe4dSAndroid Build Coastguard Worker     * keep value is still required to validate a save of a critical chunk.
2954*a67afe4dSAndroid Build Coastguard Worker     */
2955*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->read_user_chunk_fn != NULL)
2956*a67afe4dSAndroid Build Coastguard Worker    {
2957*a67afe4dSAndroid Build Coastguard Worker       if (png_cache_unknown_chunk(png_ptr, length) != 0)
2958*a67afe4dSAndroid Build Coastguard Worker       {
2959*a67afe4dSAndroid Build Coastguard Worker          /* Callback to user unknown chunk handler */
2960*a67afe4dSAndroid Build Coastguard Worker          int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2961*a67afe4dSAndroid Build Coastguard Worker              &png_ptr->unknown_chunk);
2962*a67afe4dSAndroid Build Coastguard Worker 
2963*a67afe4dSAndroid Build Coastguard Worker          /* ret is:
2964*a67afe4dSAndroid Build Coastguard Worker           * negative: An error occurred; png_chunk_error will be called.
2965*a67afe4dSAndroid Build Coastguard Worker           *     zero: The chunk was not handled, the chunk will be discarded
2966*a67afe4dSAndroid Build Coastguard Worker           *           unless png_set_keep_unknown_chunks has been used to set
2967*a67afe4dSAndroid Build Coastguard Worker           *           a 'keep' behavior for this particular chunk, in which
2968*a67afe4dSAndroid Build Coastguard Worker           *           case that will be used.  A critical chunk will cause an
2969*a67afe4dSAndroid Build Coastguard Worker           *           error at this point unless it is to be saved.
2970*a67afe4dSAndroid Build Coastguard Worker           * positive: The chunk was handled, libpng will ignore/discard it.
2971*a67afe4dSAndroid Build Coastguard Worker           */
2972*a67afe4dSAndroid Build Coastguard Worker          if (ret < 0)
2973*a67afe4dSAndroid Build Coastguard Worker             png_chunk_error(png_ptr, "error in user chunk");
2974*a67afe4dSAndroid Build Coastguard Worker 
2975*a67afe4dSAndroid Build Coastguard Worker          else if (ret == 0)
2976*a67afe4dSAndroid Build Coastguard Worker          {
2977*a67afe4dSAndroid Build Coastguard Worker             /* If the keep value is 'default' or 'never' override it, but
2978*a67afe4dSAndroid Build Coastguard Worker              * still error out on critical chunks unless the keep value is
2979*a67afe4dSAndroid Build Coastguard Worker              * 'always'  While this is weird it is the behavior in 1.4.12.
2980*a67afe4dSAndroid Build Coastguard Worker              * A possible improvement would be to obey the value set for the
2981*a67afe4dSAndroid Build Coastguard Worker              * chunk, but this would be an API change that would probably
2982*a67afe4dSAndroid Build Coastguard Worker              * damage some applications.
2983*a67afe4dSAndroid Build Coastguard Worker              *
2984*a67afe4dSAndroid Build Coastguard Worker              * The png_app_warning below catches the case that matters, where
2985*a67afe4dSAndroid Build Coastguard Worker              * the application has not set specific save or ignore for this
2986*a67afe4dSAndroid Build Coastguard Worker              * chunk or global save or ignore.
2987*a67afe4dSAndroid Build Coastguard Worker              */
2988*a67afe4dSAndroid Build Coastguard Worker             if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2989*a67afe4dSAndroid Build Coastguard Worker             {
2990*a67afe4dSAndroid Build Coastguard Worker #              ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2991*a67afe4dSAndroid Build Coastguard Worker                if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2992*a67afe4dSAndroid Build Coastguard Worker                {
2993*a67afe4dSAndroid Build Coastguard Worker                   png_chunk_warning(png_ptr, "Saving unknown chunk:");
2994*a67afe4dSAndroid Build Coastguard Worker                   png_app_warning(png_ptr,
2995*a67afe4dSAndroid Build Coastguard Worker                       "forcing save of an unhandled chunk;"
2996*a67afe4dSAndroid Build Coastguard Worker                       " please call png_set_keep_unknown_chunks");
2997*a67afe4dSAndroid Build Coastguard Worker                       /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2998*a67afe4dSAndroid Build Coastguard Worker                }
2999*a67afe4dSAndroid Build Coastguard Worker #              endif
3000*a67afe4dSAndroid Build Coastguard Worker                keep = PNG_HANDLE_CHUNK_IF_SAFE;
3001*a67afe4dSAndroid Build Coastguard Worker             }
3002*a67afe4dSAndroid Build Coastguard Worker          }
3003*a67afe4dSAndroid Build Coastguard Worker 
3004*a67afe4dSAndroid Build Coastguard Worker          else /* chunk was handled */
3005*a67afe4dSAndroid Build Coastguard Worker          {
3006*a67afe4dSAndroid Build Coastguard Worker             handled = 1;
3007*a67afe4dSAndroid Build Coastguard Worker             /* Critical chunks can be safely discarded at this point. */
3008*a67afe4dSAndroid Build Coastguard Worker             keep = PNG_HANDLE_CHUNK_NEVER;
3009*a67afe4dSAndroid Build Coastguard Worker          }
3010*a67afe4dSAndroid Build Coastguard Worker       }
3011*a67afe4dSAndroid Build Coastguard Worker 
3012*a67afe4dSAndroid Build Coastguard Worker       else
3013*a67afe4dSAndroid Build Coastguard Worker          keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
3014*a67afe4dSAndroid Build Coastguard Worker    }
3015*a67afe4dSAndroid Build Coastguard Worker 
3016*a67afe4dSAndroid Build Coastguard Worker    else
3017*a67afe4dSAndroid Build Coastguard Worker    /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
3018*a67afe4dSAndroid Build Coastguard Worker #  endif /* READ_USER_CHUNKS */
3019*a67afe4dSAndroid Build Coastguard Worker 
3020*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
3021*a67afe4dSAndroid Build Coastguard Worker    {
3022*a67afe4dSAndroid Build Coastguard Worker       /* keep is currently just the per-chunk setting, if there was no
3023*a67afe4dSAndroid Build Coastguard Worker        * setting change it to the global default now (not that this may
3024*a67afe4dSAndroid Build Coastguard Worker        * still be AS_DEFAULT) then obtain the cache of the chunk if required,
3025*a67afe4dSAndroid Build Coastguard Worker        * if not simply skip the chunk.
3026*a67afe4dSAndroid Build Coastguard Worker        */
3027*a67afe4dSAndroid Build Coastguard Worker       if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
3028*a67afe4dSAndroid Build Coastguard Worker          keep = png_ptr->unknown_default;
3029*a67afe4dSAndroid Build Coastguard Worker 
3030*a67afe4dSAndroid Build Coastguard Worker       if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3031*a67afe4dSAndroid Build Coastguard Worker          (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3032*a67afe4dSAndroid Build Coastguard Worker           PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3033*a67afe4dSAndroid Build Coastguard Worker       {
3034*a67afe4dSAndroid Build Coastguard Worker          if (png_cache_unknown_chunk(png_ptr, length) == 0)
3035*a67afe4dSAndroid Build Coastguard Worker             keep = PNG_HANDLE_CHUNK_NEVER;
3036*a67afe4dSAndroid Build Coastguard Worker       }
3037*a67afe4dSAndroid Build Coastguard Worker 
3038*a67afe4dSAndroid Build Coastguard Worker       else
3039*a67afe4dSAndroid Build Coastguard Worker          png_crc_finish(png_ptr, length);
3040*a67afe4dSAndroid Build Coastguard Worker    }
3041*a67afe4dSAndroid Build Coastguard Worker #  else
3042*a67afe4dSAndroid Build Coastguard Worker #     ifndef PNG_READ_USER_CHUNKS_SUPPORTED
3043*a67afe4dSAndroid Build Coastguard Worker #        error no method to support READ_UNKNOWN_CHUNKS
3044*a67afe4dSAndroid Build Coastguard Worker #     endif
3045*a67afe4dSAndroid Build Coastguard Worker 
3046*a67afe4dSAndroid Build Coastguard Worker    {
3047*a67afe4dSAndroid Build Coastguard Worker       /* If here there is no read callback pointer set and no support is
3048*a67afe4dSAndroid Build Coastguard Worker        * compiled in to just save the unknown chunks, so simply skip this
3049*a67afe4dSAndroid Build Coastguard Worker        * chunk.  If 'keep' is something other than AS_DEFAULT or NEVER then
3050*a67afe4dSAndroid Build Coastguard Worker        * the app has erroneously asked for unknown chunk saving when there
3051*a67afe4dSAndroid Build Coastguard Worker        * is no support.
3052*a67afe4dSAndroid Build Coastguard Worker        */
3053*a67afe4dSAndroid Build Coastguard Worker       if (keep > PNG_HANDLE_CHUNK_NEVER)
3054*a67afe4dSAndroid Build Coastguard Worker          png_app_error(png_ptr, "no unknown chunk support available");
3055*a67afe4dSAndroid Build Coastguard Worker 
3056*a67afe4dSAndroid Build Coastguard Worker       png_crc_finish(png_ptr, length);
3057*a67afe4dSAndroid Build Coastguard Worker    }
3058*a67afe4dSAndroid Build Coastguard Worker #  endif
3059*a67afe4dSAndroid Build Coastguard Worker 
3060*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
3061*a67afe4dSAndroid Build Coastguard Worker    /* Now store the chunk in the chunk list if appropriate, and if the limits
3062*a67afe4dSAndroid Build Coastguard Worker     * permit it.
3063*a67afe4dSAndroid Build Coastguard Worker     */
3064*a67afe4dSAndroid Build Coastguard Worker    if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
3065*a67afe4dSAndroid Build Coastguard Worker       (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
3066*a67afe4dSAndroid Build Coastguard Worker        PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
3067*a67afe4dSAndroid Build Coastguard Worker    {
3068*a67afe4dSAndroid Build Coastguard Worker #     ifdef PNG_USER_LIMITS_SUPPORTED
3069*a67afe4dSAndroid Build Coastguard Worker       switch (png_ptr->user_chunk_cache_max)
3070*a67afe4dSAndroid Build Coastguard Worker       {
3071*a67afe4dSAndroid Build Coastguard Worker          case 2:
3072*a67afe4dSAndroid Build Coastguard Worker             png_ptr->user_chunk_cache_max = 1;
3073*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "no space in chunk cache");
3074*a67afe4dSAndroid Build Coastguard Worker             /* FALLTHROUGH */
3075*a67afe4dSAndroid Build Coastguard Worker          case 1:
3076*a67afe4dSAndroid Build Coastguard Worker             /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
3077*a67afe4dSAndroid Build Coastguard Worker              * chunk being skipped, now there will be a hard error below.
3078*a67afe4dSAndroid Build Coastguard Worker              */
3079*a67afe4dSAndroid Build Coastguard Worker             break;
3080*a67afe4dSAndroid Build Coastguard Worker 
3081*a67afe4dSAndroid Build Coastguard Worker          default: /* not at limit */
3082*a67afe4dSAndroid Build Coastguard Worker             --(png_ptr->user_chunk_cache_max);
3083*a67afe4dSAndroid Build Coastguard Worker             /* FALLTHROUGH */
3084*a67afe4dSAndroid Build Coastguard Worker          case 0: /* no limit */
3085*a67afe4dSAndroid Build Coastguard Worker #  endif /* USER_LIMITS */
3086*a67afe4dSAndroid Build Coastguard Worker             /* Here when the limit isn't reached or when limits are compiled
3087*a67afe4dSAndroid Build Coastguard Worker              * out; store the chunk.
3088*a67afe4dSAndroid Build Coastguard Worker              */
3089*a67afe4dSAndroid Build Coastguard Worker             png_set_unknown_chunks(png_ptr, info_ptr,
3090*a67afe4dSAndroid Build Coastguard Worker                 &png_ptr->unknown_chunk, 1);
3091*a67afe4dSAndroid Build Coastguard Worker             handled = 1;
3092*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_USER_LIMITS_SUPPORTED
3093*a67afe4dSAndroid Build Coastguard Worker             break;
3094*a67afe4dSAndroid Build Coastguard Worker       }
3095*a67afe4dSAndroid Build Coastguard Worker #  endif
3096*a67afe4dSAndroid Build Coastguard Worker    }
3097*a67afe4dSAndroid Build Coastguard Worker #  else /* no store support: the chunk must be handled by the user callback */
3098*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(info_ptr)
3099*a67afe4dSAndroid Build Coastguard Worker #  endif
3100*a67afe4dSAndroid Build Coastguard Worker 
3101*a67afe4dSAndroid Build Coastguard Worker    /* Regardless of the error handling below the cached data (if any) can be
3102*a67afe4dSAndroid Build Coastguard Worker     * freed now.  Notice that the data is not freed if there is a png_error, but
3103*a67afe4dSAndroid Build Coastguard Worker     * it will be freed by destroy_read_struct.
3104*a67afe4dSAndroid Build Coastguard Worker     */
3105*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->unknown_chunk.data != NULL)
3106*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, png_ptr->unknown_chunk.data);
3107*a67afe4dSAndroid Build Coastguard Worker    png_ptr->unknown_chunk.data = NULL;
3108*a67afe4dSAndroid Build Coastguard Worker 
3109*a67afe4dSAndroid Build Coastguard Worker #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
3110*a67afe4dSAndroid Build Coastguard Worker    /* There is no support to read an unknown chunk, so just skip it. */
3111*a67afe4dSAndroid Build Coastguard Worker    png_crc_finish(png_ptr, length);
3112*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(info_ptr)
3113*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(keep)
3114*a67afe4dSAndroid Build Coastguard Worker #endif /* !READ_UNKNOWN_CHUNKS */
3115*a67afe4dSAndroid Build Coastguard Worker 
3116*a67afe4dSAndroid Build Coastguard Worker    /* Check for unhandled critical chunks */
3117*a67afe4dSAndroid Build Coastguard Worker    if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
3118*a67afe4dSAndroid Build Coastguard Worker       png_chunk_error(png_ptr, "unhandled critical chunk");
3119*a67afe4dSAndroid Build Coastguard Worker }
3120*a67afe4dSAndroid Build Coastguard Worker 
3121*a67afe4dSAndroid Build Coastguard Worker /* This function is called to verify that a chunk name is valid.
3122*a67afe4dSAndroid Build Coastguard Worker  * This function can't have the "critical chunk check" incorporated
3123*a67afe4dSAndroid Build Coastguard Worker  * into it, since in the future we will need to be able to call user
3124*a67afe4dSAndroid Build Coastguard Worker  * functions to handle unknown critical chunks after we check that
3125*a67afe4dSAndroid Build Coastguard Worker  * the chunk name itself is valid.
3126*a67afe4dSAndroid Build Coastguard Worker  */
3127*a67afe4dSAndroid Build Coastguard Worker 
3128*a67afe4dSAndroid Build Coastguard Worker /* Bit hacking: the test for an invalid byte in the 4 byte chunk name is:
3129*a67afe4dSAndroid Build Coastguard Worker  *
3130*a67afe4dSAndroid Build Coastguard Worker  * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
3131*a67afe4dSAndroid Build Coastguard Worker  */
3132*a67afe4dSAndroid Build Coastguard Worker 
3133*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_check_chunk_name(png_const_structrp png_ptr,png_uint_32 chunk_name)3134*a67afe4dSAndroid Build Coastguard Worker png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name)
3135*a67afe4dSAndroid Build Coastguard Worker {
3136*a67afe4dSAndroid Build Coastguard Worker    int i;
3137*a67afe4dSAndroid Build Coastguard Worker    png_uint_32 cn=chunk_name;
3138*a67afe4dSAndroid Build Coastguard Worker 
3139*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_check_chunk_name");
3140*a67afe4dSAndroid Build Coastguard Worker 
3141*a67afe4dSAndroid Build Coastguard Worker    for (i=1; i<=4; ++i)
3142*a67afe4dSAndroid Build Coastguard Worker    {
3143*a67afe4dSAndroid Build Coastguard Worker       int c = cn & 0xff;
3144*a67afe4dSAndroid Build Coastguard Worker 
3145*a67afe4dSAndroid Build Coastguard Worker       if (c < 65 || c > 122 || (c > 90 && c < 97))
3146*a67afe4dSAndroid Build Coastguard Worker          png_chunk_error(png_ptr, "invalid chunk type");
3147*a67afe4dSAndroid Build Coastguard Worker 
3148*a67afe4dSAndroid Build Coastguard Worker       cn >>= 8;
3149*a67afe4dSAndroid Build Coastguard Worker    }
3150*a67afe4dSAndroid Build Coastguard Worker }
3151*a67afe4dSAndroid Build Coastguard Worker 
3152*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_check_chunk_length(png_const_structrp png_ptr,png_uint_32 length)3153*a67afe4dSAndroid Build Coastguard Worker png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length)
3154*a67afe4dSAndroid Build Coastguard Worker {
3155*a67afe4dSAndroid Build Coastguard Worker    png_alloc_size_t limit = PNG_UINT_31_MAX;
3156*a67afe4dSAndroid Build Coastguard Worker 
3157*a67afe4dSAndroid Build Coastguard Worker # ifdef PNG_SET_USER_LIMITS_SUPPORTED
3158*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->user_chunk_malloc_max > 0 &&
3159*a67afe4dSAndroid Build Coastguard Worker        png_ptr->user_chunk_malloc_max < limit)
3160*a67afe4dSAndroid Build Coastguard Worker       limit = png_ptr->user_chunk_malloc_max;
3161*a67afe4dSAndroid Build Coastguard Worker # elif PNG_USER_CHUNK_MALLOC_MAX > 0
3162*a67afe4dSAndroid Build Coastguard Worker    if (PNG_USER_CHUNK_MALLOC_MAX < limit)
3163*a67afe4dSAndroid Build Coastguard Worker       limit = PNG_USER_CHUNK_MALLOC_MAX;
3164*a67afe4dSAndroid Build Coastguard Worker # endif
3165*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->chunk_name == png_IDAT)
3166*a67afe4dSAndroid Build Coastguard Worker    {
3167*a67afe4dSAndroid Build Coastguard Worker       png_alloc_size_t idat_limit = PNG_UINT_31_MAX;
3168*a67afe4dSAndroid Build Coastguard Worker       size_t row_factor =
3169*a67afe4dSAndroid Build Coastguard Worker          (size_t)png_ptr->width
3170*a67afe4dSAndroid Build Coastguard Worker          * (size_t)png_ptr->channels
3171*a67afe4dSAndroid Build Coastguard Worker          * (png_ptr->bit_depth > 8? 2: 1)
3172*a67afe4dSAndroid Build Coastguard Worker          + 1
3173*a67afe4dSAndroid Build Coastguard Worker          + (png_ptr->interlaced? 6: 0);
3174*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->height > PNG_UINT_32_MAX/row_factor)
3175*a67afe4dSAndroid Build Coastguard Worker          idat_limit = PNG_UINT_31_MAX;
3176*a67afe4dSAndroid Build Coastguard Worker       else
3177*a67afe4dSAndroid Build Coastguard Worker          idat_limit = png_ptr->height * row_factor;
3178*a67afe4dSAndroid Build Coastguard Worker       row_factor = row_factor > 32566? 32566 : row_factor;
3179*a67afe4dSAndroid Build Coastguard Worker       idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */
3180*a67afe4dSAndroid Build Coastguard Worker       idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX;
3181*a67afe4dSAndroid Build Coastguard Worker       limit = limit < idat_limit? idat_limit : limit;
3182*a67afe4dSAndroid Build Coastguard Worker    }
3183*a67afe4dSAndroid Build Coastguard Worker 
3184*a67afe4dSAndroid Build Coastguard Worker    if (length > limit)
3185*a67afe4dSAndroid Build Coastguard Worker    {
3186*a67afe4dSAndroid Build Coastguard Worker       png_debug2(0," length = %lu, limit = %lu",
3187*a67afe4dSAndroid Build Coastguard Worker          (unsigned long)length,(unsigned long)limit);
3188*a67afe4dSAndroid Build Coastguard Worker       png_benign_error(png_ptr, "chunk data is too large");
3189*a67afe4dSAndroid Build Coastguard Worker    }
3190*a67afe4dSAndroid Build Coastguard Worker }
3191*a67afe4dSAndroid Build Coastguard Worker 
3192*a67afe4dSAndroid Build Coastguard Worker /* Combines the row recently read in with the existing pixels in the row.  This
3193*a67afe4dSAndroid Build Coastguard Worker  * routine takes care of alpha and transparency if requested.  This routine also
3194*a67afe4dSAndroid Build Coastguard Worker  * handles the two methods of progressive display of interlaced images,
3195*a67afe4dSAndroid Build Coastguard Worker  * depending on the 'display' value; if 'display' is true then the whole row
3196*a67afe4dSAndroid Build Coastguard Worker  * (dp) is filled from the start by replicating the available pixels.  If
3197*a67afe4dSAndroid Build Coastguard Worker  * 'display' is false only those pixels present in the pass are filled in.
3198*a67afe4dSAndroid Build Coastguard Worker  */
3199*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_combine_row(png_const_structrp png_ptr,png_bytep dp,int display)3200*a67afe4dSAndroid Build Coastguard Worker png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display)
3201*a67afe4dSAndroid Build Coastguard Worker {
3202*a67afe4dSAndroid Build Coastguard Worker    unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3203*a67afe4dSAndroid Build Coastguard Worker    png_const_bytep sp = png_ptr->row_buf + 1;
3204*a67afe4dSAndroid Build Coastguard Worker    png_alloc_size_t row_width = png_ptr->width;
3205*a67afe4dSAndroid Build Coastguard Worker    unsigned int pass = png_ptr->pass;
3206*a67afe4dSAndroid Build Coastguard Worker    png_bytep end_ptr = 0;
3207*a67afe4dSAndroid Build Coastguard Worker    png_byte end_byte = 0;
3208*a67afe4dSAndroid Build Coastguard Worker    unsigned int end_mask;
3209*a67afe4dSAndroid Build Coastguard Worker 
3210*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_combine_row");
3211*a67afe4dSAndroid Build Coastguard Worker 
3212*a67afe4dSAndroid Build Coastguard Worker    /* Added in 1.5.6: it should not be possible to enter this routine until at
3213*a67afe4dSAndroid Build Coastguard Worker     * least one row has been read from the PNG data and transformed.
3214*a67afe4dSAndroid Build Coastguard Worker     */
3215*a67afe4dSAndroid Build Coastguard Worker    if (pixel_depth == 0)
3216*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "internal row logic error");
3217*a67afe4dSAndroid Build Coastguard Worker 
3218*a67afe4dSAndroid Build Coastguard Worker    /* Added in 1.5.4: the pixel depth should match the information returned by
3219*a67afe4dSAndroid Build Coastguard Worker     * any call to png_read_update_info at this point.  Do not continue if we got
3220*a67afe4dSAndroid Build Coastguard Worker     * this wrong.
3221*a67afe4dSAndroid Build Coastguard Worker     */
3222*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3223*a67afe4dSAndroid Build Coastguard Worker           PNG_ROWBYTES(pixel_depth, row_width))
3224*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "internal row size calculation error");
3225*a67afe4dSAndroid Build Coastguard Worker 
3226*a67afe4dSAndroid Build Coastguard Worker    /* Don't expect this to ever happen: */
3227*a67afe4dSAndroid Build Coastguard Worker    if (row_width == 0)
3228*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "internal row width error");
3229*a67afe4dSAndroid Build Coastguard Worker 
3230*a67afe4dSAndroid Build Coastguard Worker    /* Preserve the last byte in cases where only part of it will be overwritten,
3231*a67afe4dSAndroid Build Coastguard Worker     * the multiply below may overflow, we don't care because ANSI-C guarantees
3232*a67afe4dSAndroid Build Coastguard Worker     * we get the low bits.
3233*a67afe4dSAndroid Build Coastguard Worker     */
3234*a67afe4dSAndroid Build Coastguard Worker    end_mask = (pixel_depth * row_width) & 7;
3235*a67afe4dSAndroid Build Coastguard Worker    if (end_mask != 0)
3236*a67afe4dSAndroid Build Coastguard Worker    {
3237*a67afe4dSAndroid Build Coastguard Worker       /* end_ptr == NULL is a flag to say do nothing */
3238*a67afe4dSAndroid Build Coastguard Worker       end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3239*a67afe4dSAndroid Build Coastguard Worker       end_byte = *end_ptr;
3240*a67afe4dSAndroid Build Coastguard Worker #     ifdef PNG_READ_PACKSWAP_SUPPORTED
3241*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3242*a67afe4dSAndroid Build Coastguard Worker          /* little-endian byte */
3243*a67afe4dSAndroid Build Coastguard Worker          end_mask = (unsigned int)(0xff << end_mask);
3244*a67afe4dSAndroid Build Coastguard Worker 
3245*a67afe4dSAndroid Build Coastguard Worker       else /* big-endian byte */
3246*a67afe4dSAndroid Build Coastguard Worker #     endif
3247*a67afe4dSAndroid Build Coastguard Worker       end_mask = 0xff >> end_mask;
3248*a67afe4dSAndroid Build Coastguard Worker       /* end_mask is now the bits to *keep* from the destination row */
3249*a67afe4dSAndroid Build Coastguard Worker    }
3250*a67afe4dSAndroid Build Coastguard Worker 
3251*a67afe4dSAndroid Build Coastguard Worker    /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3252*a67afe4dSAndroid Build Coastguard Worker     * will also happen if interlacing isn't supported or if the application
3253*a67afe4dSAndroid Build Coastguard Worker     * does not call png_set_interlace_handling().  In the latter cases the
3254*a67afe4dSAndroid Build Coastguard Worker     * caller just gets a sequence of the unexpanded rows from each interlace
3255*a67afe4dSAndroid Build Coastguard Worker     * pass.
3256*a67afe4dSAndroid Build Coastguard Worker     */
3257*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_INTERLACING_SUPPORTED
3258*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->interlaced != 0 &&
3259*a67afe4dSAndroid Build Coastguard Worker        (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3260*a67afe4dSAndroid Build Coastguard Worker        pass < 6 && (display == 0 ||
3261*a67afe4dSAndroid Build Coastguard Worker        /* The following copies everything for 'display' on passes 0, 2 and 4. */
3262*a67afe4dSAndroid Build Coastguard Worker        (display == 1 && (pass & 1) != 0)))
3263*a67afe4dSAndroid Build Coastguard Worker    {
3264*a67afe4dSAndroid Build Coastguard Worker       /* Narrow images may have no bits in a pass; the caller should handle
3265*a67afe4dSAndroid Build Coastguard Worker        * this, but this test is cheap:
3266*a67afe4dSAndroid Build Coastguard Worker        */
3267*a67afe4dSAndroid Build Coastguard Worker       if (row_width <= PNG_PASS_START_COL(pass))
3268*a67afe4dSAndroid Build Coastguard Worker          return;
3269*a67afe4dSAndroid Build Coastguard Worker 
3270*a67afe4dSAndroid Build Coastguard Worker       if (pixel_depth < 8)
3271*a67afe4dSAndroid Build Coastguard Worker       {
3272*a67afe4dSAndroid Build Coastguard Worker          /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3273*a67afe4dSAndroid Build Coastguard Worker           * into 32 bits, then a single loop over the bytes using the four byte
3274*a67afe4dSAndroid Build Coastguard Worker           * values in the 32-bit mask can be used.  For the 'display' option the
3275*a67afe4dSAndroid Build Coastguard Worker           * expanded mask may also not require any masking within a byte.  To
3276*a67afe4dSAndroid Build Coastguard Worker           * make this work the PACKSWAP option must be taken into account - it
3277*a67afe4dSAndroid Build Coastguard Worker           * simply requires the pixels to be reversed in each byte.
3278*a67afe4dSAndroid Build Coastguard Worker           *
3279*a67afe4dSAndroid Build Coastguard Worker           * The 'regular' case requires a mask for each of the first 6 passes,
3280*a67afe4dSAndroid Build Coastguard Worker           * the 'display' case does a copy for the even passes in the range
3281*a67afe4dSAndroid Build Coastguard Worker           * 0..6.  This has already been handled in the test above.
3282*a67afe4dSAndroid Build Coastguard Worker           *
3283*a67afe4dSAndroid Build Coastguard Worker           * The masks are arranged as four bytes with the first byte to use in
3284*a67afe4dSAndroid Build Coastguard Worker           * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3285*a67afe4dSAndroid Build Coastguard Worker           * not) of the pixels in each byte.
3286*a67afe4dSAndroid Build Coastguard Worker           *
3287*a67afe4dSAndroid Build Coastguard Worker           * NOTE: the whole of this logic depends on the caller of this function
3288*a67afe4dSAndroid Build Coastguard Worker           * only calling it on rows appropriate to the pass.  This function only
3289*a67afe4dSAndroid Build Coastguard Worker           * understands the 'x' logic; the 'y' logic is handled by the caller.
3290*a67afe4dSAndroid Build Coastguard Worker           *
3291*a67afe4dSAndroid Build Coastguard Worker           * The following defines allow generation of compile time constant bit
3292*a67afe4dSAndroid Build Coastguard Worker           * masks for each pixel depth and each possibility of swapped or not
3293*a67afe4dSAndroid Build Coastguard Worker           * swapped bytes.  Pass 'p' is in the range 0..6; 'x', a pixel index,
3294*a67afe4dSAndroid Build Coastguard Worker           * is in the range 0..7; and the result is 1 if the pixel is to be
3295*a67afe4dSAndroid Build Coastguard Worker           * copied in the pass, 0 if not.  'S' is for the sparkle method, 'B'
3296*a67afe4dSAndroid Build Coastguard Worker           * for the block method.
3297*a67afe4dSAndroid Build Coastguard Worker           *
3298*a67afe4dSAndroid Build Coastguard Worker           * With some compilers a compile time expression of the general form:
3299*a67afe4dSAndroid Build Coastguard Worker           *
3300*a67afe4dSAndroid Build Coastguard Worker           *    (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3301*a67afe4dSAndroid Build Coastguard Worker           *
3302*a67afe4dSAndroid Build Coastguard Worker           * Produces warnings with values of 'shift' in the range 33 to 63
3303*a67afe4dSAndroid Build Coastguard Worker           * because the right hand side of the ?: expression is evaluated by
3304*a67afe4dSAndroid Build Coastguard Worker           * the compiler even though it isn't used.  Microsoft Visual C (various
3305*a67afe4dSAndroid Build Coastguard Worker           * versions) and the Intel C compiler are known to do this.  To avoid
3306*a67afe4dSAndroid Build Coastguard Worker           * this the following macros are used in 1.5.6.  This is a temporary
3307*a67afe4dSAndroid Build Coastguard Worker           * solution to avoid destabilizing the code during the release process.
3308*a67afe4dSAndroid Build Coastguard Worker           */
3309*a67afe4dSAndroid Build Coastguard Worker #        if PNG_USE_COMPILE_TIME_MASKS
3310*a67afe4dSAndroid Build Coastguard Worker #           define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3311*a67afe4dSAndroid Build Coastguard Worker #           define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3312*a67afe4dSAndroid Build Coastguard Worker #        else
3313*a67afe4dSAndroid Build Coastguard Worker #           define PNG_LSR(x,s) ((x)>>(s))
3314*a67afe4dSAndroid Build Coastguard Worker #           define PNG_LSL(x,s) ((x)<<(s))
3315*a67afe4dSAndroid Build Coastguard Worker #        endif
3316*a67afe4dSAndroid Build Coastguard Worker #        define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3317*a67afe4dSAndroid Build Coastguard Worker            PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3318*a67afe4dSAndroid Build Coastguard Worker #        define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3319*a67afe4dSAndroid Build Coastguard Worker            PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3320*a67afe4dSAndroid Build Coastguard Worker 
3321*a67afe4dSAndroid Build Coastguard Worker          /* Return a mask for pass 'p' pixel 'x' at depth 'd'.  The mask is
3322*a67afe4dSAndroid Build Coastguard Worker           * little endian - the first pixel is at bit 0 - however the extra
3323*a67afe4dSAndroid Build Coastguard Worker           * parameter 's' can be set to cause the mask position to be swapped
3324*a67afe4dSAndroid Build Coastguard Worker           * within each byte, to match the PNG format.  This is done by XOR of
3325*a67afe4dSAndroid Build Coastguard Worker           * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3326*a67afe4dSAndroid Build Coastguard Worker           */
3327*a67afe4dSAndroid Build Coastguard Worker #        define PIXEL_MASK(p,x,d,s) \
3328*a67afe4dSAndroid Build Coastguard Worker             (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3329*a67afe4dSAndroid Build Coastguard Worker 
3330*a67afe4dSAndroid Build Coastguard Worker          /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3331*a67afe4dSAndroid Build Coastguard Worker           */
3332*a67afe4dSAndroid Build Coastguard Worker #        define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3333*a67afe4dSAndroid Build Coastguard Worker #        define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3334*a67afe4dSAndroid Build Coastguard Worker 
3335*a67afe4dSAndroid Build Coastguard Worker          /* Combine 8 of these to get the full mask.  For the 1-bpp and 2-bpp
3336*a67afe4dSAndroid Build Coastguard Worker           * cases the result needs replicating, for the 4-bpp case the above
3337*a67afe4dSAndroid Build Coastguard Worker           * generates a full 32 bits.
3338*a67afe4dSAndroid Build Coastguard Worker           */
3339*a67afe4dSAndroid Build Coastguard Worker #        define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3340*a67afe4dSAndroid Build Coastguard Worker 
3341*a67afe4dSAndroid Build Coastguard Worker #        define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3342*a67afe4dSAndroid Build Coastguard Worker             S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3343*a67afe4dSAndroid Build Coastguard Worker             S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3344*a67afe4dSAndroid Build Coastguard Worker 
3345*a67afe4dSAndroid Build Coastguard Worker #        define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3346*a67afe4dSAndroid Build Coastguard Worker             B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3347*a67afe4dSAndroid Build Coastguard Worker             B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3348*a67afe4dSAndroid Build Coastguard Worker 
3349*a67afe4dSAndroid Build Coastguard Worker #if PNG_USE_COMPILE_TIME_MASKS
3350*a67afe4dSAndroid Build Coastguard Worker          /* Utility macros to construct all the masks for a depth/swap
3351*a67afe4dSAndroid Build Coastguard Worker           * combination.  The 's' parameter says whether the format is PNG
3352*a67afe4dSAndroid Build Coastguard Worker           * (big endian bytes) or not.  Only the three odd-numbered passes are
3353*a67afe4dSAndroid Build Coastguard Worker           * required for the display/block algorithm.
3354*a67afe4dSAndroid Build Coastguard Worker           */
3355*a67afe4dSAndroid Build Coastguard Worker #        define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3356*a67afe4dSAndroid Build Coastguard Worker             S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3357*a67afe4dSAndroid Build Coastguard Worker 
3358*a67afe4dSAndroid Build Coastguard Worker #        define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3359*a67afe4dSAndroid Build Coastguard Worker 
3360*a67afe4dSAndroid Build Coastguard Worker #        define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3361*a67afe4dSAndroid Build Coastguard Worker 
3362*a67afe4dSAndroid Build Coastguard Worker          /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3363*a67afe4dSAndroid Build Coastguard Worker           * then pass:
3364*a67afe4dSAndroid Build Coastguard Worker           */
3365*a67afe4dSAndroid Build Coastguard Worker          static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3366*a67afe4dSAndroid Build Coastguard Worker          {
3367*a67afe4dSAndroid Build Coastguard Worker             /* Little-endian byte masks for PACKSWAP */
3368*a67afe4dSAndroid Build Coastguard Worker             { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3369*a67afe4dSAndroid Build Coastguard Worker             /* Normal (big-endian byte) masks - PNG format */
3370*a67afe4dSAndroid Build Coastguard Worker             { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3371*a67afe4dSAndroid Build Coastguard Worker          };
3372*a67afe4dSAndroid Build Coastguard Worker 
3373*a67afe4dSAndroid Build Coastguard Worker          /* display_mask has only three entries for the odd passes, so index by
3374*a67afe4dSAndroid Build Coastguard Worker           * pass>>1.
3375*a67afe4dSAndroid Build Coastguard Worker           */
3376*a67afe4dSAndroid Build Coastguard Worker          static const png_uint_32 display_mask[2][3][3] =
3377*a67afe4dSAndroid Build Coastguard Worker          {
3378*a67afe4dSAndroid Build Coastguard Worker             /* Little-endian byte masks for PACKSWAP */
3379*a67afe4dSAndroid Build Coastguard Worker             { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3380*a67afe4dSAndroid Build Coastguard Worker             /* Normal (big-endian byte) masks - PNG format */
3381*a67afe4dSAndroid Build Coastguard Worker             { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3382*a67afe4dSAndroid Build Coastguard Worker          };
3383*a67afe4dSAndroid Build Coastguard Worker 
3384*a67afe4dSAndroid Build Coastguard Worker #        define MASK(pass,depth,display,png)\
3385*a67afe4dSAndroid Build Coastguard Worker             ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3386*a67afe4dSAndroid Build Coastguard Worker                row_mask[png][DEPTH_INDEX(depth)][pass])
3387*a67afe4dSAndroid Build Coastguard Worker 
3388*a67afe4dSAndroid Build Coastguard Worker #else /* !PNG_USE_COMPILE_TIME_MASKS */
3389*a67afe4dSAndroid Build Coastguard Worker          /* This is the runtime alternative: it seems unlikely that this will
3390*a67afe4dSAndroid Build Coastguard Worker           * ever be either smaller or faster than the compile time approach.
3391*a67afe4dSAndroid Build Coastguard Worker           */
3392*a67afe4dSAndroid Build Coastguard Worker #        define MASK(pass,depth,display,png)\
3393*a67afe4dSAndroid Build Coastguard Worker             ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3394*a67afe4dSAndroid Build Coastguard Worker #endif /* !USE_COMPILE_TIME_MASKS */
3395*a67afe4dSAndroid Build Coastguard Worker 
3396*a67afe4dSAndroid Build Coastguard Worker          /* Use the appropriate mask to copy the required bits.  In some cases
3397*a67afe4dSAndroid Build Coastguard Worker           * the byte mask will be 0 or 0xff; optimize these cases.  row_width is
3398*a67afe4dSAndroid Build Coastguard Worker           * the number of pixels, but the code copies bytes, so it is necessary
3399*a67afe4dSAndroid Build Coastguard Worker           * to special case the end.
3400*a67afe4dSAndroid Build Coastguard Worker           */
3401*a67afe4dSAndroid Build Coastguard Worker          png_uint_32 pixels_per_byte = 8 / pixel_depth;
3402*a67afe4dSAndroid Build Coastguard Worker          png_uint_32 mask;
3403*a67afe4dSAndroid Build Coastguard Worker 
3404*a67afe4dSAndroid Build Coastguard Worker #        ifdef PNG_READ_PACKSWAP_SUPPORTED
3405*a67afe4dSAndroid Build Coastguard Worker          if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3406*a67afe4dSAndroid Build Coastguard Worker             mask = MASK(pass, pixel_depth, display, 0);
3407*a67afe4dSAndroid Build Coastguard Worker 
3408*a67afe4dSAndroid Build Coastguard Worker          else
3409*a67afe4dSAndroid Build Coastguard Worker #        endif
3410*a67afe4dSAndroid Build Coastguard Worker          mask = MASK(pass, pixel_depth, display, 1);
3411*a67afe4dSAndroid Build Coastguard Worker 
3412*a67afe4dSAndroid Build Coastguard Worker          for (;;)
3413*a67afe4dSAndroid Build Coastguard Worker          {
3414*a67afe4dSAndroid Build Coastguard Worker             png_uint_32 m;
3415*a67afe4dSAndroid Build Coastguard Worker 
3416*a67afe4dSAndroid Build Coastguard Worker             /* It doesn't matter in the following if png_uint_32 has more than
3417*a67afe4dSAndroid Build Coastguard Worker              * 32 bits because the high bits always match those in m<<24; it is,
3418*a67afe4dSAndroid Build Coastguard Worker              * however, essential to use OR here, not +, because of this.
3419*a67afe4dSAndroid Build Coastguard Worker              */
3420*a67afe4dSAndroid Build Coastguard Worker             m = mask;
3421*a67afe4dSAndroid Build Coastguard Worker             mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3422*a67afe4dSAndroid Build Coastguard Worker             m &= 0xff;
3423*a67afe4dSAndroid Build Coastguard Worker 
3424*a67afe4dSAndroid Build Coastguard Worker             if (m != 0) /* something to copy */
3425*a67afe4dSAndroid Build Coastguard Worker             {
3426*a67afe4dSAndroid Build Coastguard Worker                if (m != 0xff)
3427*a67afe4dSAndroid Build Coastguard Worker                   *dp = (png_byte)((*dp & ~m) | (*sp & m));
3428*a67afe4dSAndroid Build Coastguard Worker                else
3429*a67afe4dSAndroid Build Coastguard Worker                   *dp = *sp;
3430*a67afe4dSAndroid Build Coastguard Worker             }
3431*a67afe4dSAndroid Build Coastguard Worker 
3432*a67afe4dSAndroid Build Coastguard Worker             /* NOTE: this may overwrite the last byte with garbage if the image
3433*a67afe4dSAndroid Build Coastguard Worker              * is not an exact number of bytes wide; libpng has always done
3434*a67afe4dSAndroid Build Coastguard Worker              * this.
3435*a67afe4dSAndroid Build Coastguard Worker              */
3436*a67afe4dSAndroid Build Coastguard Worker             if (row_width <= pixels_per_byte)
3437*a67afe4dSAndroid Build Coastguard Worker                break; /* May need to restore part of the last byte */
3438*a67afe4dSAndroid Build Coastguard Worker 
3439*a67afe4dSAndroid Build Coastguard Worker             row_width -= pixels_per_byte;
3440*a67afe4dSAndroid Build Coastguard Worker             ++dp;
3441*a67afe4dSAndroid Build Coastguard Worker             ++sp;
3442*a67afe4dSAndroid Build Coastguard Worker          }
3443*a67afe4dSAndroid Build Coastguard Worker       }
3444*a67afe4dSAndroid Build Coastguard Worker 
3445*a67afe4dSAndroid Build Coastguard Worker       else /* pixel_depth >= 8 */
3446*a67afe4dSAndroid Build Coastguard Worker       {
3447*a67afe4dSAndroid Build Coastguard Worker          unsigned int bytes_to_copy, bytes_to_jump;
3448*a67afe4dSAndroid Build Coastguard Worker 
3449*a67afe4dSAndroid Build Coastguard Worker          /* Validate the depth - it must be a multiple of 8 */
3450*a67afe4dSAndroid Build Coastguard Worker          if (pixel_depth & 7)
3451*a67afe4dSAndroid Build Coastguard Worker             png_error(png_ptr, "invalid user transform pixel depth");
3452*a67afe4dSAndroid Build Coastguard Worker 
3453*a67afe4dSAndroid Build Coastguard Worker          pixel_depth >>= 3; /* now in bytes */
3454*a67afe4dSAndroid Build Coastguard Worker          row_width *= pixel_depth;
3455*a67afe4dSAndroid Build Coastguard Worker 
3456*a67afe4dSAndroid Build Coastguard Worker          /* Regardless of pass number the Adam 7 interlace always results in a
3457*a67afe4dSAndroid Build Coastguard Worker           * fixed number of pixels to copy then to skip.  There may be a
3458*a67afe4dSAndroid Build Coastguard Worker           * different number of pixels to skip at the start though.
3459*a67afe4dSAndroid Build Coastguard Worker           */
3460*a67afe4dSAndroid Build Coastguard Worker          {
3461*a67afe4dSAndroid Build Coastguard Worker             unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3462*a67afe4dSAndroid Build Coastguard Worker 
3463*a67afe4dSAndroid Build Coastguard Worker             row_width -= offset;
3464*a67afe4dSAndroid Build Coastguard Worker             dp += offset;
3465*a67afe4dSAndroid Build Coastguard Worker             sp += offset;
3466*a67afe4dSAndroid Build Coastguard Worker          }
3467*a67afe4dSAndroid Build Coastguard Worker 
3468*a67afe4dSAndroid Build Coastguard Worker          /* Work out the bytes to copy. */
3469*a67afe4dSAndroid Build Coastguard Worker          if (display != 0)
3470*a67afe4dSAndroid Build Coastguard Worker          {
3471*a67afe4dSAndroid Build Coastguard Worker             /* When doing the 'block' algorithm the pixel in the pass gets
3472*a67afe4dSAndroid Build Coastguard Worker              * replicated to adjacent pixels.  This is why the even (0,2,4,6)
3473*a67afe4dSAndroid Build Coastguard Worker              * passes are skipped above - the entire expanded row is copied.
3474*a67afe4dSAndroid Build Coastguard Worker              */
3475*a67afe4dSAndroid Build Coastguard Worker             bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3476*a67afe4dSAndroid Build Coastguard Worker 
3477*a67afe4dSAndroid Build Coastguard Worker             /* But don't allow this number to exceed the actual row width. */
3478*a67afe4dSAndroid Build Coastguard Worker             if (bytes_to_copy > row_width)
3479*a67afe4dSAndroid Build Coastguard Worker                bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3480*a67afe4dSAndroid Build Coastguard Worker          }
3481*a67afe4dSAndroid Build Coastguard Worker 
3482*a67afe4dSAndroid Build Coastguard Worker          else /* normal row; Adam7 only ever gives us one pixel to copy. */
3483*a67afe4dSAndroid Build Coastguard Worker             bytes_to_copy = pixel_depth;
3484*a67afe4dSAndroid Build Coastguard Worker 
3485*a67afe4dSAndroid Build Coastguard Worker          /* In Adam7 there is a constant offset between where the pixels go. */
3486*a67afe4dSAndroid Build Coastguard Worker          bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3487*a67afe4dSAndroid Build Coastguard Worker 
3488*a67afe4dSAndroid Build Coastguard Worker          /* And simply copy these bytes.  Some optimization is possible here,
3489*a67afe4dSAndroid Build Coastguard Worker           * depending on the value of 'bytes_to_copy'.  Special case the low
3490*a67afe4dSAndroid Build Coastguard Worker           * byte counts, which we know to be frequent.
3491*a67afe4dSAndroid Build Coastguard Worker           *
3492*a67afe4dSAndroid Build Coastguard Worker           * Notice that these cases all 'return' rather than 'break' - this
3493*a67afe4dSAndroid Build Coastguard Worker           * avoids an unnecessary test on whether to restore the last byte
3494*a67afe4dSAndroid Build Coastguard Worker           * below.
3495*a67afe4dSAndroid Build Coastguard Worker           */
3496*a67afe4dSAndroid Build Coastguard Worker          switch (bytes_to_copy)
3497*a67afe4dSAndroid Build Coastguard Worker          {
3498*a67afe4dSAndroid Build Coastguard Worker             case 1:
3499*a67afe4dSAndroid Build Coastguard Worker                for (;;)
3500*a67afe4dSAndroid Build Coastguard Worker                {
3501*a67afe4dSAndroid Build Coastguard Worker                   *dp = *sp;
3502*a67afe4dSAndroid Build Coastguard Worker 
3503*a67afe4dSAndroid Build Coastguard Worker                   if (row_width <= bytes_to_jump)
3504*a67afe4dSAndroid Build Coastguard Worker                      return;
3505*a67afe4dSAndroid Build Coastguard Worker 
3506*a67afe4dSAndroid Build Coastguard Worker                   dp += bytes_to_jump;
3507*a67afe4dSAndroid Build Coastguard Worker                   sp += bytes_to_jump;
3508*a67afe4dSAndroid Build Coastguard Worker                   row_width -= bytes_to_jump;
3509*a67afe4dSAndroid Build Coastguard Worker                }
3510*a67afe4dSAndroid Build Coastguard Worker 
3511*a67afe4dSAndroid Build Coastguard Worker             case 2:
3512*a67afe4dSAndroid Build Coastguard Worker                /* There is a possibility of a partial copy at the end here; this
3513*a67afe4dSAndroid Build Coastguard Worker                 * slows the code down somewhat.
3514*a67afe4dSAndroid Build Coastguard Worker                 */
3515*a67afe4dSAndroid Build Coastguard Worker                do
3516*a67afe4dSAndroid Build Coastguard Worker                {
3517*a67afe4dSAndroid Build Coastguard Worker                   dp[0] = sp[0]; dp[1] = sp[1];
3518*a67afe4dSAndroid Build Coastguard Worker 
3519*a67afe4dSAndroid Build Coastguard Worker                   if (row_width <= bytes_to_jump)
3520*a67afe4dSAndroid Build Coastguard Worker                      return;
3521*a67afe4dSAndroid Build Coastguard Worker 
3522*a67afe4dSAndroid Build Coastguard Worker                   sp += bytes_to_jump;
3523*a67afe4dSAndroid Build Coastguard Worker                   dp += bytes_to_jump;
3524*a67afe4dSAndroid Build Coastguard Worker                   row_width -= bytes_to_jump;
3525*a67afe4dSAndroid Build Coastguard Worker                }
3526*a67afe4dSAndroid Build Coastguard Worker                while (row_width > 1);
3527*a67afe4dSAndroid Build Coastguard Worker 
3528*a67afe4dSAndroid Build Coastguard Worker                /* And there can only be one byte left at this point: */
3529*a67afe4dSAndroid Build Coastguard Worker                *dp = *sp;
3530*a67afe4dSAndroid Build Coastguard Worker                return;
3531*a67afe4dSAndroid Build Coastguard Worker 
3532*a67afe4dSAndroid Build Coastguard Worker             case 3:
3533*a67afe4dSAndroid Build Coastguard Worker                /* This can only be the RGB case, so each copy is exactly one
3534*a67afe4dSAndroid Build Coastguard Worker                 * pixel and it is not necessary to check for a partial copy.
3535*a67afe4dSAndroid Build Coastguard Worker                 */
3536*a67afe4dSAndroid Build Coastguard Worker                for (;;)
3537*a67afe4dSAndroid Build Coastguard Worker                {
3538*a67afe4dSAndroid Build Coastguard Worker                   dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3539*a67afe4dSAndroid Build Coastguard Worker 
3540*a67afe4dSAndroid Build Coastguard Worker                   if (row_width <= bytes_to_jump)
3541*a67afe4dSAndroid Build Coastguard Worker                      return;
3542*a67afe4dSAndroid Build Coastguard Worker 
3543*a67afe4dSAndroid Build Coastguard Worker                   sp += bytes_to_jump;
3544*a67afe4dSAndroid Build Coastguard Worker                   dp += bytes_to_jump;
3545*a67afe4dSAndroid Build Coastguard Worker                   row_width -= bytes_to_jump;
3546*a67afe4dSAndroid Build Coastguard Worker                }
3547*a67afe4dSAndroid Build Coastguard Worker 
3548*a67afe4dSAndroid Build Coastguard Worker             default:
3549*a67afe4dSAndroid Build Coastguard Worker #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3550*a67afe4dSAndroid Build Coastguard Worker                /* Check for double byte alignment and, if possible, use a
3551*a67afe4dSAndroid Build Coastguard Worker                 * 16-bit copy.  Don't attempt this for narrow images - ones that
3552*a67afe4dSAndroid Build Coastguard Worker                 * are less than an interlace panel wide.  Don't attempt it for
3553*a67afe4dSAndroid Build Coastguard Worker                 * wide bytes_to_copy either - use the memcpy there.
3554*a67afe4dSAndroid Build Coastguard Worker                 */
3555*a67afe4dSAndroid Build Coastguard Worker                if (bytes_to_copy < 16 /*else use memcpy*/ &&
3556*a67afe4dSAndroid Build Coastguard Worker                    png_isaligned(dp, png_uint_16) &&
3557*a67afe4dSAndroid Build Coastguard Worker                    png_isaligned(sp, png_uint_16) &&
3558*a67afe4dSAndroid Build Coastguard Worker                    bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3559*a67afe4dSAndroid Build Coastguard Worker                    bytes_to_jump % (sizeof (png_uint_16)) == 0)
3560*a67afe4dSAndroid Build Coastguard Worker                {
3561*a67afe4dSAndroid Build Coastguard Worker                   /* Everything is aligned for png_uint_16 copies, but try for
3562*a67afe4dSAndroid Build Coastguard Worker                    * png_uint_32 first.
3563*a67afe4dSAndroid Build Coastguard Worker                    */
3564*a67afe4dSAndroid Build Coastguard Worker                   if (png_isaligned(dp, png_uint_32) &&
3565*a67afe4dSAndroid Build Coastguard Worker                       png_isaligned(sp, png_uint_32) &&
3566*a67afe4dSAndroid Build Coastguard Worker                       bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3567*a67afe4dSAndroid Build Coastguard Worker                       bytes_to_jump % (sizeof (png_uint_32)) == 0)
3568*a67afe4dSAndroid Build Coastguard Worker                   {
3569*a67afe4dSAndroid Build Coastguard Worker                      png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3570*a67afe4dSAndroid Build Coastguard Worker                      png_const_uint_32p sp32 = png_aligncastconst(
3571*a67afe4dSAndroid Build Coastguard Worker                          png_const_uint_32p, sp);
3572*a67afe4dSAndroid Build Coastguard Worker                      size_t skip = (bytes_to_jump-bytes_to_copy) /
3573*a67afe4dSAndroid Build Coastguard Worker                          (sizeof (png_uint_32));
3574*a67afe4dSAndroid Build Coastguard Worker 
3575*a67afe4dSAndroid Build Coastguard Worker                      do
3576*a67afe4dSAndroid Build Coastguard Worker                      {
3577*a67afe4dSAndroid Build Coastguard Worker                         size_t c = bytes_to_copy;
3578*a67afe4dSAndroid Build Coastguard Worker                         do
3579*a67afe4dSAndroid Build Coastguard Worker                         {
3580*a67afe4dSAndroid Build Coastguard Worker                            *dp32++ = *sp32++;
3581*a67afe4dSAndroid Build Coastguard Worker                            c -= (sizeof (png_uint_32));
3582*a67afe4dSAndroid Build Coastguard Worker                         }
3583*a67afe4dSAndroid Build Coastguard Worker                         while (c > 0);
3584*a67afe4dSAndroid Build Coastguard Worker 
3585*a67afe4dSAndroid Build Coastguard Worker                         if (row_width <= bytes_to_jump)
3586*a67afe4dSAndroid Build Coastguard Worker                            return;
3587*a67afe4dSAndroid Build Coastguard Worker 
3588*a67afe4dSAndroid Build Coastguard Worker                         dp32 += skip;
3589*a67afe4dSAndroid Build Coastguard Worker                         sp32 += skip;
3590*a67afe4dSAndroid Build Coastguard Worker                         row_width -= bytes_to_jump;
3591*a67afe4dSAndroid Build Coastguard Worker                      }
3592*a67afe4dSAndroid Build Coastguard Worker                      while (bytes_to_copy <= row_width);
3593*a67afe4dSAndroid Build Coastguard Worker 
3594*a67afe4dSAndroid Build Coastguard Worker                      /* Get to here when the row_width truncates the final copy.
3595*a67afe4dSAndroid Build Coastguard Worker                       * There will be 1-3 bytes left to copy, so don't try the
3596*a67afe4dSAndroid Build Coastguard Worker                       * 16-bit loop below.
3597*a67afe4dSAndroid Build Coastguard Worker                       */
3598*a67afe4dSAndroid Build Coastguard Worker                      dp = (png_bytep)dp32;
3599*a67afe4dSAndroid Build Coastguard Worker                      sp = (png_const_bytep)sp32;
3600*a67afe4dSAndroid Build Coastguard Worker                      do
3601*a67afe4dSAndroid Build Coastguard Worker                         *dp++ = *sp++;
3602*a67afe4dSAndroid Build Coastguard Worker                      while (--row_width > 0);
3603*a67afe4dSAndroid Build Coastguard Worker                      return;
3604*a67afe4dSAndroid Build Coastguard Worker                   }
3605*a67afe4dSAndroid Build Coastguard Worker 
3606*a67afe4dSAndroid Build Coastguard Worker                   /* Else do it in 16-bit quantities, but only if the size is
3607*a67afe4dSAndroid Build Coastguard Worker                    * not too large.
3608*a67afe4dSAndroid Build Coastguard Worker                    */
3609*a67afe4dSAndroid Build Coastguard Worker                   else
3610*a67afe4dSAndroid Build Coastguard Worker                   {
3611*a67afe4dSAndroid Build Coastguard Worker                      png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3612*a67afe4dSAndroid Build Coastguard Worker                      png_const_uint_16p sp16 = png_aligncastconst(
3613*a67afe4dSAndroid Build Coastguard Worker                         png_const_uint_16p, sp);
3614*a67afe4dSAndroid Build Coastguard Worker                      size_t skip = (bytes_to_jump-bytes_to_copy) /
3615*a67afe4dSAndroid Build Coastguard Worker                         (sizeof (png_uint_16));
3616*a67afe4dSAndroid Build Coastguard Worker 
3617*a67afe4dSAndroid Build Coastguard Worker                      do
3618*a67afe4dSAndroid Build Coastguard Worker                      {
3619*a67afe4dSAndroid Build Coastguard Worker                         size_t c = bytes_to_copy;
3620*a67afe4dSAndroid Build Coastguard Worker                         do
3621*a67afe4dSAndroid Build Coastguard Worker                         {
3622*a67afe4dSAndroid Build Coastguard Worker                            *dp16++ = *sp16++;
3623*a67afe4dSAndroid Build Coastguard Worker                            c -= (sizeof (png_uint_16));
3624*a67afe4dSAndroid Build Coastguard Worker                         }
3625*a67afe4dSAndroid Build Coastguard Worker                         while (c > 0);
3626*a67afe4dSAndroid Build Coastguard Worker 
3627*a67afe4dSAndroid Build Coastguard Worker                         if (row_width <= bytes_to_jump)
3628*a67afe4dSAndroid Build Coastguard Worker                            return;
3629*a67afe4dSAndroid Build Coastguard Worker 
3630*a67afe4dSAndroid Build Coastguard Worker                         dp16 += skip;
3631*a67afe4dSAndroid Build Coastguard Worker                         sp16 += skip;
3632*a67afe4dSAndroid Build Coastguard Worker                         row_width -= bytes_to_jump;
3633*a67afe4dSAndroid Build Coastguard Worker                      }
3634*a67afe4dSAndroid Build Coastguard Worker                      while (bytes_to_copy <= row_width);
3635*a67afe4dSAndroid Build Coastguard Worker 
3636*a67afe4dSAndroid Build Coastguard Worker                      /* End of row - 1 byte left, bytes_to_copy > row_width: */
3637*a67afe4dSAndroid Build Coastguard Worker                      dp = (png_bytep)dp16;
3638*a67afe4dSAndroid Build Coastguard Worker                      sp = (png_const_bytep)sp16;
3639*a67afe4dSAndroid Build Coastguard Worker                      do
3640*a67afe4dSAndroid Build Coastguard Worker                         *dp++ = *sp++;
3641*a67afe4dSAndroid Build Coastguard Worker                      while (--row_width > 0);
3642*a67afe4dSAndroid Build Coastguard Worker                      return;
3643*a67afe4dSAndroid Build Coastguard Worker                   }
3644*a67afe4dSAndroid Build Coastguard Worker                }
3645*a67afe4dSAndroid Build Coastguard Worker #endif /* ALIGN_TYPE code */
3646*a67afe4dSAndroid Build Coastguard Worker 
3647*a67afe4dSAndroid Build Coastguard Worker                /* The true default - use a memcpy: */
3648*a67afe4dSAndroid Build Coastguard Worker                for (;;)
3649*a67afe4dSAndroid Build Coastguard Worker                {
3650*a67afe4dSAndroid Build Coastguard Worker                   memcpy(dp, sp, bytes_to_copy);
3651*a67afe4dSAndroid Build Coastguard Worker 
3652*a67afe4dSAndroid Build Coastguard Worker                   if (row_width <= bytes_to_jump)
3653*a67afe4dSAndroid Build Coastguard Worker                      return;
3654*a67afe4dSAndroid Build Coastguard Worker 
3655*a67afe4dSAndroid Build Coastguard Worker                   sp += bytes_to_jump;
3656*a67afe4dSAndroid Build Coastguard Worker                   dp += bytes_to_jump;
3657*a67afe4dSAndroid Build Coastguard Worker                   row_width -= bytes_to_jump;
3658*a67afe4dSAndroid Build Coastguard Worker                   if (bytes_to_copy > row_width)
3659*a67afe4dSAndroid Build Coastguard Worker                      bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3660*a67afe4dSAndroid Build Coastguard Worker                }
3661*a67afe4dSAndroid Build Coastguard Worker          }
3662*a67afe4dSAndroid Build Coastguard Worker 
3663*a67afe4dSAndroid Build Coastguard Worker          /* NOT REACHED*/
3664*a67afe4dSAndroid Build Coastguard Worker       } /* pixel_depth >= 8 */
3665*a67afe4dSAndroid Build Coastguard Worker 
3666*a67afe4dSAndroid Build Coastguard Worker       /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3667*a67afe4dSAndroid Build Coastguard Worker    }
3668*a67afe4dSAndroid Build Coastguard Worker    else
3669*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_INTERLACING */
3670*a67afe4dSAndroid Build Coastguard Worker 
3671*a67afe4dSAndroid Build Coastguard Worker    /* If here then the switch above wasn't used so just memcpy the whole row
3672*a67afe4dSAndroid Build Coastguard Worker     * from the temporary row buffer (notice that this overwrites the end of the
3673*a67afe4dSAndroid Build Coastguard Worker     * destination row if it is a partial byte.)
3674*a67afe4dSAndroid Build Coastguard Worker     */
3675*a67afe4dSAndroid Build Coastguard Worker    memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3676*a67afe4dSAndroid Build Coastguard Worker 
3677*a67afe4dSAndroid Build Coastguard Worker    /* Restore the overwritten bits from the last byte if necessary. */
3678*a67afe4dSAndroid Build Coastguard Worker    if (end_ptr != NULL)
3679*a67afe4dSAndroid Build Coastguard Worker       *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3680*a67afe4dSAndroid Build Coastguard Worker }
3681*a67afe4dSAndroid Build Coastguard Worker 
3682*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_INTERLACING_SUPPORTED
3683*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_do_read_interlace(png_row_infop row_info,png_bytep row,int pass,png_uint_32 transformations)3684*a67afe4dSAndroid Build Coastguard Worker png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
3685*a67afe4dSAndroid Build Coastguard Worker     png_uint_32 transformations /* Because these may affect the byte layout */)
3686*a67afe4dSAndroid Build Coastguard Worker {
3687*a67afe4dSAndroid Build Coastguard Worker    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3688*a67afe4dSAndroid Build Coastguard Worker    /* Offset to next interlace block */
3689*a67afe4dSAndroid Build Coastguard Worker    static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3690*a67afe4dSAndroid Build Coastguard Worker 
3691*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_do_read_interlace");
3692*a67afe4dSAndroid Build Coastguard Worker    if (row != NULL && row_info != NULL)
3693*a67afe4dSAndroid Build Coastguard Worker    {
3694*a67afe4dSAndroid Build Coastguard Worker       png_uint_32 final_width;
3695*a67afe4dSAndroid Build Coastguard Worker 
3696*a67afe4dSAndroid Build Coastguard Worker       final_width = row_info->width * png_pass_inc[pass];
3697*a67afe4dSAndroid Build Coastguard Worker 
3698*a67afe4dSAndroid Build Coastguard Worker       switch (row_info->pixel_depth)
3699*a67afe4dSAndroid Build Coastguard Worker       {
3700*a67afe4dSAndroid Build Coastguard Worker          case 1:
3701*a67afe4dSAndroid Build Coastguard Worker          {
3702*a67afe4dSAndroid Build Coastguard Worker             png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3703*a67afe4dSAndroid Build Coastguard Worker             png_bytep dp = row + (size_t)((final_width - 1) >> 3);
3704*a67afe4dSAndroid Build Coastguard Worker             unsigned int sshift, dshift;
3705*a67afe4dSAndroid Build Coastguard Worker             unsigned int s_start, s_end;
3706*a67afe4dSAndroid Build Coastguard Worker             int s_inc;
3707*a67afe4dSAndroid Build Coastguard Worker             int jstop = (int)png_pass_inc[pass];
3708*a67afe4dSAndroid Build Coastguard Worker             png_byte v;
3709*a67afe4dSAndroid Build Coastguard Worker             png_uint_32 i;
3710*a67afe4dSAndroid Build Coastguard Worker             int j;
3711*a67afe4dSAndroid Build Coastguard Worker 
3712*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_PACKSWAP_SUPPORTED
3713*a67afe4dSAndroid Build Coastguard Worker             if ((transformations & PNG_PACKSWAP) != 0)
3714*a67afe4dSAndroid Build Coastguard Worker             {
3715*a67afe4dSAndroid Build Coastguard Worker                 sshift = ((row_info->width + 7) & 0x07);
3716*a67afe4dSAndroid Build Coastguard Worker                 dshift = ((final_width + 7) & 0x07);
3717*a67afe4dSAndroid Build Coastguard Worker                 s_start = 7;
3718*a67afe4dSAndroid Build Coastguard Worker                 s_end = 0;
3719*a67afe4dSAndroid Build Coastguard Worker                 s_inc = -1;
3720*a67afe4dSAndroid Build Coastguard Worker             }
3721*a67afe4dSAndroid Build Coastguard Worker 
3722*a67afe4dSAndroid Build Coastguard Worker             else
3723*a67afe4dSAndroid Build Coastguard Worker #endif
3724*a67afe4dSAndroid Build Coastguard Worker             {
3725*a67afe4dSAndroid Build Coastguard Worker                 sshift = 7 - ((row_info->width + 7) & 0x07);
3726*a67afe4dSAndroid Build Coastguard Worker                 dshift = 7 - ((final_width + 7) & 0x07);
3727*a67afe4dSAndroid Build Coastguard Worker                 s_start = 0;
3728*a67afe4dSAndroid Build Coastguard Worker                 s_end = 7;
3729*a67afe4dSAndroid Build Coastguard Worker                 s_inc = 1;
3730*a67afe4dSAndroid Build Coastguard Worker             }
3731*a67afe4dSAndroid Build Coastguard Worker 
3732*a67afe4dSAndroid Build Coastguard Worker             for (i = 0; i < row_info->width; i++)
3733*a67afe4dSAndroid Build Coastguard Worker             {
3734*a67afe4dSAndroid Build Coastguard Worker                v = (png_byte)((*sp >> sshift) & 0x01);
3735*a67afe4dSAndroid Build Coastguard Worker                for (j = 0; j < jstop; j++)
3736*a67afe4dSAndroid Build Coastguard Worker                {
3737*a67afe4dSAndroid Build Coastguard Worker                   unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3738*a67afe4dSAndroid Build Coastguard Worker                   tmp |= (unsigned int)(v << dshift);
3739*a67afe4dSAndroid Build Coastguard Worker                   *dp = (png_byte)(tmp & 0xff);
3740*a67afe4dSAndroid Build Coastguard Worker 
3741*a67afe4dSAndroid Build Coastguard Worker                   if (dshift == s_end)
3742*a67afe4dSAndroid Build Coastguard Worker                   {
3743*a67afe4dSAndroid Build Coastguard Worker                      dshift = s_start;
3744*a67afe4dSAndroid Build Coastguard Worker                      dp--;
3745*a67afe4dSAndroid Build Coastguard Worker                   }
3746*a67afe4dSAndroid Build Coastguard Worker 
3747*a67afe4dSAndroid Build Coastguard Worker                   else
3748*a67afe4dSAndroid Build Coastguard Worker                      dshift = (unsigned int)((int)dshift + s_inc);
3749*a67afe4dSAndroid Build Coastguard Worker                }
3750*a67afe4dSAndroid Build Coastguard Worker 
3751*a67afe4dSAndroid Build Coastguard Worker                if (sshift == s_end)
3752*a67afe4dSAndroid Build Coastguard Worker                {
3753*a67afe4dSAndroid Build Coastguard Worker                   sshift = s_start;
3754*a67afe4dSAndroid Build Coastguard Worker                   sp--;
3755*a67afe4dSAndroid Build Coastguard Worker                }
3756*a67afe4dSAndroid Build Coastguard Worker 
3757*a67afe4dSAndroid Build Coastguard Worker                else
3758*a67afe4dSAndroid Build Coastguard Worker                   sshift = (unsigned int)((int)sshift + s_inc);
3759*a67afe4dSAndroid Build Coastguard Worker             }
3760*a67afe4dSAndroid Build Coastguard Worker             break;
3761*a67afe4dSAndroid Build Coastguard Worker          }
3762*a67afe4dSAndroid Build Coastguard Worker 
3763*a67afe4dSAndroid Build Coastguard Worker          case 2:
3764*a67afe4dSAndroid Build Coastguard Worker          {
3765*a67afe4dSAndroid Build Coastguard Worker             png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3766*a67afe4dSAndroid Build Coastguard Worker             png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3767*a67afe4dSAndroid Build Coastguard Worker             unsigned int sshift, dshift;
3768*a67afe4dSAndroid Build Coastguard Worker             unsigned int s_start, s_end;
3769*a67afe4dSAndroid Build Coastguard Worker             int s_inc;
3770*a67afe4dSAndroid Build Coastguard Worker             int jstop = (int)png_pass_inc[pass];
3771*a67afe4dSAndroid Build Coastguard Worker             png_uint_32 i;
3772*a67afe4dSAndroid Build Coastguard Worker 
3773*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_PACKSWAP_SUPPORTED
3774*a67afe4dSAndroid Build Coastguard Worker             if ((transformations & PNG_PACKSWAP) != 0)
3775*a67afe4dSAndroid Build Coastguard Worker             {
3776*a67afe4dSAndroid Build Coastguard Worker                sshift = (((row_info->width + 3) & 0x03) << 1);
3777*a67afe4dSAndroid Build Coastguard Worker                dshift = (((final_width + 3) & 0x03) << 1);
3778*a67afe4dSAndroid Build Coastguard Worker                s_start = 6;
3779*a67afe4dSAndroid Build Coastguard Worker                s_end = 0;
3780*a67afe4dSAndroid Build Coastguard Worker                s_inc = -2;
3781*a67afe4dSAndroid Build Coastguard Worker             }
3782*a67afe4dSAndroid Build Coastguard Worker 
3783*a67afe4dSAndroid Build Coastguard Worker             else
3784*a67afe4dSAndroid Build Coastguard Worker #endif
3785*a67afe4dSAndroid Build Coastguard Worker             {
3786*a67afe4dSAndroid Build Coastguard Worker                sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3787*a67afe4dSAndroid Build Coastguard Worker                dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3788*a67afe4dSAndroid Build Coastguard Worker                s_start = 0;
3789*a67afe4dSAndroid Build Coastguard Worker                s_end = 6;
3790*a67afe4dSAndroid Build Coastguard Worker                s_inc = 2;
3791*a67afe4dSAndroid Build Coastguard Worker             }
3792*a67afe4dSAndroid Build Coastguard Worker 
3793*a67afe4dSAndroid Build Coastguard Worker             for (i = 0; i < row_info->width; i++)
3794*a67afe4dSAndroid Build Coastguard Worker             {
3795*a67afe4dSAndroid Build Coastguard Worker                png_byte v;
3796*a67afe4dSAndroid Build Coastguard Worker                int j;
3797*a67afe4dSAndroid Build Coastguard Worker 
3798*a67afe4dSAndroid Build Coastguard Worker                v = (png_byte)((*sp >> sshift) & 0x03);
3799*a67afe4dSAndroid Build Coastguard Worker                for (j = 0; j < jstop; j++)
3800*a67afe4dSAndroid Build Coastguard Worker                {
3801*a67afe4dSAndroid Build Coastguard Worker                   unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3802*a67afe4dSAndroid Build Coastguard Worker                   tmp |= (unsigned int)(v << dshift);
3803*a67afe4dSAndroid Build Coastguard Worker                   *dp = (png_byte)(tmp & 0xff);
3804*a67afe4dSAndroid Build Coastguard Worker 
3805*a67afe4dSAndroid Build Coastguard Worker                   if (dshift == s_end)
3806*a67afe4dSAndroid Build Coastguard Worker                   {
3807*a67afe4dSAndroid Build Coastguard Worker                      dshift = s_start;
3808*a67afe4dSAndroid Build Coastguard Worker                      dp--;
3809*a67afe4dSAndroid Build Coastguard Worker                   }
3810*a67afe4dSAndroid Build Coastguard Worker 
3811*a67afe4dSAndroid Build Coastguard Worker                   else
3812*a67afe4dSAndroid Build Coastguard Worker                      dshift = (unsigned int)((int)dshift + s_inc);
3813*a67afe4dSAndroid Build Coastguard Worker                }
3814*a67afe4dSAndroid Build Coastguard Worker 
3815*a67afe4dSAndroid Build Coastguard Worker                if (sshift == s_end)
3816*a67afe4dSAndroid Build Coastguard Worker                {
3817*a67afe4dSAndroid Build Coastguard Worker                   sshift = s_start;
3818*a67afe4dSAndroid Build Coastguard Worker                   sp--;
3819*a67afe4dSAndroid Build Coastguard Worker                }
3820*a67afe4dSAndroid Build Coastguard Worker 
3821*a67afe4dSAndroid Build Coastguard Worker                else
3822*a67afe4dSAndroid Build Coastguard Worker                   sshift = (unsigned int)((int)sshift + s_inc);
3823*a67afe4dSAndroid Build Coastguard Worker             }
3824*a67afe4dSAndroid Build Coastguard Worker             break;
3825*a67afe4dSAndroid Build Coastguard Worker          }
3826*a67afe4dSAndroid Build Coastguard Worker 
3827*a67afe4dSAndroid Build Coastguard Worker          case 4:
3828*a67afe4dSAndroid Build Coastguard Worker          {
3829*a67afe4dSAndroid Build Coastguard Worker             png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
3830*a67afe4dSAndroid Build Coastguard Worker             png_bytep dp = row + (size_t)((final_width - 1) >> 1);
3831*a67afe4dSAndroid Build Coastguard Worker             unsigned int sshift, dshift;
3832*a67afe4dSAndroid Build Coastguard Worker             unsigned int s_start, s_end;
3833*a67afe4dSAndroid Build Coastguard Worker             int s_inc;
3834*a67afe4dSAndroid Build Coastguard Worker             png_uint_32 i;
3835*a67afe4dSAndroid Build Coastguard Worker             int jstop = (int)png_pass_inc[pass];
3836*a67afe4dSAndroid Build Coastguard Worker 
3837*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_PACKSWAP_SUPPORTED
3838*a67afe4dSAndroid Build Coastguard Worker             if ((transformations & PNG_PACKSWAP) != 0)
3839*a67afe4dSAndroid Build Coastguard Worker             {
3840*a67afe4dSAndroid Build Coastguard Worker                sshift = (((row_info->width + 1) & 0x01) << 2);
3841*a67afe4dSAndroid Build Coastguard Worker                dshift = (((final_width + 1) & 0x01) << 2);
3842*a67afe4dSAndroid Build Coastguard Worker                s_start = 4;
3843*a67afe4dSAndroid Build Coastguard Worker                s_end = 0;
3844*a67afe4dSAndroid Build Coastguard Worker                s_inc = -4;
3845*a67afe4dSAndroid Build Coastguard Worker             }
3846*a67afe4dSAndroid Build Coastguard Worker 
3847*a67afe4dSAndroid Build Coastguard Worker             else
3848*a67afe4dSAndroid Build Coastguard Worker #endif
3849*a67afe4dSAndroid Build Coastguard Worker             {
3850*a67afe4dSAndroid Build Coastguard Worker                sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
3851*a67afe4dSAndroid Build Coastguard Worker                dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
3852*a67afe4dSAndroid Build Coastguard Worker                s_start = 0;
3853*a67afe4dSAndroid Build Coastguard Worker                s_end = 4;
3854*a67afe4dSAndroid Build Coastguard Worker                s_inc = 4;
3855*a67afe4dSAndroid Build Coastguard Worker             }
3856*a67afe4dSAndroid Build Coastguard Worker 
3857*a67afe4dSAndroid Build Coastguard Worker             for (i = 0; i < row_info->width; i++)
3858*a67afe4dSAndroid Build Coastguard Worker             {
3859*a67afe4dSAndroid Build Coastguard Worker                png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
3860*a67afe4dSAndroid Build Coastguard Worker                int j;
3861*a67afe4dSAndroid Build Coastguard Worker 
3862*a67afe4dSAndroid Build Coastguard Worker                for (j = 0; j < jstop; j++)
3863*a67afe4dSAndroid Build Coastguard Worker                {
3864*a67afe4dSAndroid Build Coastguard Worker                   unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3865*a67afe4dSAndroid Build Coastguard Worker                   tmp |= (unsigned int)(v << dshift);
3866*a67afe4dSAndroid Build Coastguard Worker                   *dp = (png_byte)(tmp & 0xff);
3867*a67afe4dSAndroid Build Coastguard Worker 
3868*a67afe4dSAndroid Build Coastguard Worker                   if (dshift == s_end)
3869*a67afe4dSAndroid Build Coastguard Worker                   {
3870*a67afe4dSAndroid Build Coastguard Worker                      dshift = s_start;
3871*a67afe4dSAndroid Build Coastguard Worker                      dp--;
3872*a67afe4dSAndroid Build Coastguard Worker                   }
3873*a67afe4dSAndroid Build Coastguard Worker 
3874*a67afe4dSAndroid Build Coastguard Worker                   else
3875*a67afe4dSAndroid Build Coastguard Worker                      dshift = (unsigned int)((int)dshift + s_inc);
3876*a67afe4dSAndroid Build Coastguard Worker                }
3877*a67afe4dSAndroid Build Coastguard Worker 
3878*a67afe4dSAndroid Build Coastguard Worker                if (sshift == s_end)
3879*a67afe4dSAndroid Build Coastguard Worker                {
3880*a67afe4dSAndroid Build Coastguard Worker                   sshift = s_start;
3881*a67afe4dSAndroid Build Coastguard Worker                   sp--;
3882*a67afe4dSAndroid Build Coastguard Worker                }
3883*a67afe4dSAndroid Build Coastguard Worker 
3884*a67afe4dSAndroid Build Coastguard Worker                else
3885*a67afe4dSAndroid Build Coastguard Worker                   sshift = (unsigned int)((int)sshift + s_inc);
3886*a67afe4dSAndroid Build Coastguard Worker             }
3887*a67afe4dSAndroid Build Coastguard Worker             break;
3888*a67afe4dSAndroid Build Coastguard Worker          }
3889*a67afe4dSAndroid Build Coastguard Worker 
3890*a67afe4dSAndroid Build Coastguard Worker          default:
3891*a67afe4dSAndroid Build Coastguard Worker          {
3892*a67afe4dSAndroid Build Coastguard Worker             size_t pixel_bytes = (row_info->pixel_depth >> 3);
3893*a67afe4dSAndroid Build Coastguard Worker 
3894*a67afe4dSAndroid Build Coastguard Worker             png_bytep sp = row + (size_t)(row_info->width - 1)
3895*a67afe4dSAndroid Build Coastguard Worker                 * pixel_bytes;
3896*a67afe4dSAndroid Build Coastguard Worker 
3897*a67afe4dSAndroid Build Coastguard Worker             png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
3898*a67afe4dSAndroid Build Coastguard Worker 
3899*a67afe4dSAndroid Build Coastguard Worker             int jstop = (int)png_pass_inc[pass];
3900*a67afe4dSAndroid Build Coastguard Worker             png_uint_32 i;
3901*a67afe4dSAndroid Build Coastguard Worker 
3902*a67afe4dSAndroid Build Coastguard Worker             for (i = 0; i < row_info->width; i++)
3903*a67afe4dSAndroid Build Coastguard Worker             {
3904*a67afe4dSAndroid Build Coastguard Worker                png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
3905*a67afe4dSAndroid Build Coastguard Worker                int j;
3906*a67afe4dSAndroid Build Coastguard Worker 
3907*a67afe4dSAndroid Build Coastguard Worker                memcpy(v, sp, pixel_bytes);
3908*a67afe4dSAndroid Build Coastguard Worker 
3909*a67afe4dSAndroid Build Coastguard Worker                for (j = 0; j < jstop; j++)
3910*a67afe4dSAndroid Build Coastguard Worker                {
3911*a67afe4dSAndroid Build Coastguard Worker                   memcpy(dp, v, pixel_bytes);
3912*a67afe4dSAndroid Build Coastguard Worker                   dp -= pixel_bytes;
3913*a67afe4dSAndroid Build Coastguard Worker                }
3914*a67afe4dSAndroid Build Coastguard Worker 
3915*a67afe4dSAndroid Build Coastguard Worker                sp -= pixel_bytes;
3916*a67afe4dSAndroid Build Coastguard Worker             }
3917*a67afe4dSAndroid Build Coastguard Worker             break;
3918*a67afe4dSAndroid Build Coastguard Worker          }
3919*a67afe4dSAndroid Build Coastguard Worker       }
3920*a67afe4dSAndroid Build Coastguard Worker 
3921*a67afe4dSAndroid Build Coastguard Worker       row_info->width = final_width;
3922*a67afe4dSAndroid Build Coastguard Worker       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3923*a67afe4dSAndroid Build Coastguard Worker    }
3924*a67afe4dSAndroid Build Coastguard Worker #ifndef PNG_READ_PACKSWAP_SUPPORTED
3925*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(transformations)  /* Silence compiler warning */
3926*a67afe4dSAndroid Build Coastguard Worker #endif
3927*a67afe4dSAndroid Build Coastguard Worker }
3928*a67afe4dSAndroid Build Coastguard Worker #endif /* READ_INTERLACING */
3929*a67afe4dSAndroid Build Coastguard Worker 
3930*a67afe4dSAndroid Build Coastguard Worker static void
png_read_filter_row_sub(png_row_infop row_info,png_bytep row,png_const_bytep prev_row)3931*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3932*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row)
3933*a67afe4dSAndroid Build Coastguard Worker {
3934*a67afe4dSAndroid Build Coastguard Worker    size_t i;
3935*a67afe4dSAndroid Build Coastguard Worker    size_t istop = row_info->rowbytes;
3936*a67afe4dSAndroid Build Coastguard Worker    unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3937*a67afe4dSAndroid Build Coastguard Worker    png_bytep rp = row + bpp;
3938*a67afe4dSAndroid Build Coastguard Worker 
3939*a67afe4dSAndroid Build Coastguard Worker    PNG_UNUSED(prev_row)
3940*a67afe4dSAndroid Build Coastguard Worker 
3941*a67afe4dSAndroid Build Coastguard Worker    for (i = bpp; i < istop; i++)
3942*a67afe4dSAndroid Build Coastguard Worker    {
3943*a67afe4dSAndroid Build Coastguard Worker       *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff);
3944*a67afe4dSAndroid Build Coastguard Worker       rp++;
3945*a67afe4dSAndroid Build Coastguard Worker    }
3946*a67afe4dSAndroid Build Coastguard Worker }
3947*a67afe4dSAndroid Build Coastguard Worker 
3948*a67afe4dSAndroid Build Coastguard Worker static void
png_read_filter_row_up(png_row_infop row_info,png_bytep row,png_const_bytep prev_row)3949*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3950*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row)
3951*a67afe4dSAndroid Build Coastguard Worker {
3952*a67afe4dSAndroid Build Coastguard Worker    size_t i;
3953*a67afe4dSAndroid Build Coastguard Worker    size_t istop = row_info->rowbytes;
3954*a67afe4dSAndroid Build Coastguard Worker    png_bytep rp = row;
3955*a67afe4dSAndroid Build Coastguard Worker    png_const_bytep pp = prev_row;
3956*a67afe4dSAndroid Build Coastguard Worker 
3957*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < istop; i++)
3958*a67afe4dSAndroid Build Coastguard Worker    {
3959*a67afe4dSAndroid Build Coastguard Worker       *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
3960*a67afe4dSAndroid Build Coastguard Worker       rp++;
3961*a67afe4dSAndroid Build Coastguard Worker    }
3962*a67afe4dSAndroid Build Coastguard Worker }
3963*a67afe4dSAndroid Build Coastguard Worker 
3964*a67afe4dSAndroid Build Coastguard Worker static void
png_read_filter_row_avg(png_row_infop row_info,png_bytep row,png_const_bytep prev_row)3965*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
3966*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row)
3967*a67afe4dSAndroid Build Coastguard Worker {
3968*a67afe4dSAndroid Build Coastguard Worker    size_t i;
3969*a67afe4dSAndroid Build Coastguard Worker    png_bytep rp = row;
3970*a67afe4dSAndroid Build Coastguard Worker    png_const_bytep pp = prev_row;
3971*a67afe4dSAndroid Build Coastguard Worker    unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3972*a67afe4dSAndroid Build Coastguard Worker    size_t istop = row_info->rowbytes - bpp;
3973*a67afe4dSAndroid Build Coastguard Worker 
3974*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < bpp; i++)
3975*a67afe4dSAndroid Build Coastguard Worker    {
3976*a67afe4dSAndroid Build Coastguard Worker       *rp = (png_byte)(((int)(*rp) +
3977*a67afe4dSAndroid Build Coastguard Worker          ((int)(*pp++) / 2 )) & 0xff);
3978*a67afe4dSAndroid Build Coastguard Worker 
3979*a67afe4dSAndroid Build Coastguard Worker       rp++;
3980*a67afe4dSAndroid Build Coastguard Worker    }
3981*a67afe4dSAndroid Build Coastguard Worker 
3982*a67afe4dSAndroid Build Coastguard Worker    for (i = 0; i < istop; i++)
3983*a67afe4dSAndroid Build Coastguard Worker    {
3984*a67afe4dSAndroid Build Coastguard Worker       *rp = (png_byte)(((int)(*rp) +
3985*a67afe4dSAndroid Build Coastguard Worker          (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
3986*a67afe4dSAndroid Build Coastguard Worker 
3987*a67afe4dSAndroid Build Coastguard Worker       rp++;
3988*a67afe4dSAndroid Build Coastguard Worker    }
3989*a67afe4dSAndroid Build Coastguard Worker }
3990*a67afe4dSAndroid Build Coastguard Worker 
3991*a67afe4dSAndroid Build Coastguard Worker static void
png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info,png_bytep row,png_const_bytep prev_row)3992*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
3993*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row)
3994*a67afe4dSAndroid Build Coastguard Worker {
3995*a67afe4dSAndroid Build Coastguard Worker    png_bytep rp_end = row + row_info->rowbytes;
3996*a67afe4dSAndroid Build Coastguard Worker    int a, c;
3997*a67afe4dSAndroid Build Coastguard Worker 
3998*a67afe4dSAndroid Build Coastguard Worker    /* First pixel/byte */
3999*a67afe4dSAndroid Build Coastguard Worker    c = *prev_row++;
4000*a67afe4dSAndroid Build Coastguard Worker    a = *row + c;
4001*a67afe4dSAndroid Build Coastguard Worker    *row++ = (png_byte)a;
4002*a67afe4dSAndroid Build Coastguard Worker 
4003*a67afe4dSAndroid Build Coastguard Worker    /* Remainder */
4004*a67afe4dSAndroid Build Coastguard Worker    while (row < rp_end)
4005*a67afe4dSAndroid Build Coastguard Worker    {
4006*a67afe4dSAndroid Build Coastguard Worker       int b, pa, pb, pc, p;
4007*a67afe4dSAndroid Build Coastguard Worker 
4008*a67afe4dSAndroid Build Coastguard Worker       a &= 0xff; /* From previous iteration or start */
4009*a67afe4dSAndroid Build Coastguard Worker       b = *prev_row++;
4010*a67afe4dSAndroid Build Coastguard Worker 
4011*a67afe4dSAndroid Build Coastguard Worker       p = b - c;
4012*a67afe4dSAndroid Build Coastguard Worker       pc = a - c;
4013*a67afe4dSAndroid Build Coastguard Worker 
4014*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USE_ABS
4015*a67afe4dSAndroid Build Coastguard Worker       pa = abs(p);
4016*a67afe4dSAndroid Build Coastguard Worker       pb = abs(pc);
4017*a67afe4dSAndroid Build Coastguard Worker       pc = abs(p + pc);
4018*a67afe4dSAndroid Build Coastguard Worker #else
4019*a67afe4dSAndroid Build Coastguard Worker       pa = p < 0 ? -p : p;
4020*a67afe4dSAndroid Build Coastguard Worker       pb = pc < 0 ? -pc : pc;
4021*a67afe4dSAndroid Build Coastguard Worker       pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4022*a67afe4dSAndroid Build Coastguard Worker #endif
4023*a67afe4dSAndroid Build Coastguard Worker 
4024*a67afe4dSAndroid Build Coastguard Worker       /* Find the best predictor, the least of pa, pb, pc favoring the earlier
4025*a67afe4dSAndroid Build Coastguard Worker        * ones in the case of a tie.
4026*a67afe4dSAndroid Build Coastguard Worker        */
4027*a67afe4dSAndroid Build Coastguard Worker       if (pb < pa)
4028*a67afe4dSAndroid Build Coastguard Worker       {
4029*a67afe4dSAndroid Build Coastguard Worker          pa = pb; a = b;
4030*a67afe4dSAndroid Build Coastguard Worker       }
4031*a67afe4dSAndroid Build Coastguard Worker       if (pc < pa) a = c;
4032*a67afe4dSAndroid Build Coastguard Worker 
4033*a67afe4dSAndroid Build Coastguard Worker       /* Calculate the current pixel in a, and move the previous row pixel to c
4034*a67afe4dSAndroid Build Coastguard Worker        * for the next time round the loop
4035*a67afe4dSAndroid Build Coastguard Worker        */
4036*a67afe4dSAndroid Build Coastguard Worker       c = b;
4037*a67afe4dSAndroid Build Coastguard Worker       a += *row;
4038*a67afe4dSAndroid Build Coastguard Worker       *row++ = (png_byte)a;
4039*a67afe4dSAndroid Build Coastguard Worker    }
4040*a67afe4dSAndroid Build Coastguard Worker }
4041*a67afe4dSAndroid Build Coastguard Worker 
4042*a67afe4dSAndroid Build Coastguard Worker static void
png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info,png_bytep row,png_const_bytep prev_row)4043*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4044*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row)
4045*a67afe4dSAndroid Build Coastguard Worker {
4046*a67afe4dSAndroid Build Coastguard Worker    unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4047*a67afe4dSAndroid Build Coastguard Worker    png_bytep rp_end = row + bpp;
4048*a67afe4dSAndroid Build Coastguard Worker 
4049*a67afe4dSAndroid Build Coastguard Worker    /* Process the first pixel in the row completely (this is the same as 'up'
4050*a67afe4dSAndroid Build Coastguard Worker     * because there is only one candidate predictor for the first row).
4051*a67afe4dSAndroid Build Coastguard Worker     */
4052*a67afe4dSAndroid Build Coastguard Worker    while (row < rp_end)
4053*a67afe4dSAndroid Build Coastguard Worker    {
4054*a67afe4dSAndroid Build Coastguard Worker       int a = *row + *prev_row++;
4055*a67afe4dSAndroid Build Coastguard Worker       *row++ = (png_byte)a;
4056*a67afe4dSAndroid Build Coastguard Worker    }
4057*a67afe4dSAndroid Build Coastguard Worker 
4058*a67afe4dSAndroid Build Coastguard Worker    /* Remainder */
4059*a67afe4dSAndroid Build Coastguard Worker    rp_end = rp_end + (row_info->rowbytes - bpp);
4060*a67afe4dSAndroid Build Coastguard Worker 
4061*a67afe4dSAndroid Build Coastguard Worker    while (row < rp_end)
4062*a67afe4dSAndroid Build Coastguard Worker    {
4063*a67afe4dSAndroid Build Coastguard Worker       int a, b, c, pa, pb, pc, p;
4064*a67afe4dSAndroid Build Coastguard Worker 
4065*a67afe4dSAndroid Build Coastguard Worker       c = *(prev_row - bpp);
4066*a67afe4dSAndroid Build Coastguard Worker       a = *(row - bpp);
4067*a67afe4dSAndroid Build Coastguard Worker       b = *prev_row++;
4068*a67afe4dSAndroid Build Coastguard Worker 
4069*a67afe4dSAndroid Build Coastguard Worker       p = b - c;
4070*a67afe4dSAndroid Build Coastguard Worker       pc = a - c;
4071*a67afe4dSAndroid Build Coastguard Worker 
4072*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_USE_ABS
4073*a67afe4dSAndroid Build Coastguard Worker       pa = abs(p);
4074*a67afe4dSAndroid Build Coastguard Worker       pb = abs(pc);
4075*a67afe4dSAndroid Build Coastguard Worker       pc = abs(p + pc);
4076*a67afe4dSAndroid Build Coastguard Worker #else
4077*a67afe4dSAndroid Build Coastguard Worker       pa = p < 0 ? -p : p;
4078*a67afe4dSAndroid Build Coastguard Worker       pb = pc < 0 ? -pc : pc;
4079*a67afe4dSAndroid Build Coastguard Worker       pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4080*a67afe4dSAndroid Build Coastguard Worker #endif
4081*a67afe4dSAndroid Build Coastguard Worker 
4082*a67afe4dSAndroid Build Coastguard Worker       if (pb < pa)
4083*a67afe4dSAndroid Build Coastguard Worker       {
4084*a67afe4dSAndroid Build Coastguard Worker          pa = pb; a = b;
4085*a67afe4dSAndroid Build Coastguard Worker       }
4086*a67afe4dSAndroid Build Coastguard Worker       if (pc < pa) a = c;
4087*a67afe4dSAndroid Build Coastguard Worker 
4088*a67afe4dSAndroid Build Coastguard Worker       a += *row;
4089*a67afe4dSAndroid Build Coastguard Worker       *row++ = (png_byte)a;
4090*a67afe4dSAndroid Build Coastguard Worker    }
4091*a67afe4dSAndroid Build Coastguard Worker }
4092*a67afe4dSAndroid Build Coastguard Worker 
4093*a67afe4dSAndroid Build Coastguard Worker static void
png_init_filter_functions(png_structrp pp)4094*a67afe4dSAndroid Build Coastguard Worker png_init_filter_functions(png_structrp pp)
4095*a67afe4dSAndroid Build Coastguard Worker    /* This function is called once for every PNG image (except for PNG images
4096*a67afe4dSAndroid Build Coastguard Worker     * that only use PNG_FILTER_VALUE_NONE for all rows) to set the
4097*a67afe4dSAndroid Build Coastguard Worker     * implementations required to reverse the filtering of PNG rows.  Reversing
4098*a67afe4dSAndroid Build Coastguard Worker     * the filter is the first transformation performed on the row data.  It is
4099*a67afe4dSAndroid Build Coastguard Worker     * performed in place, therefore an implementation can be selected based on
4100*a67afe4dSAndroid Build Coastguard Worker     * the image pixel format.  If the implementation depends on image width then
4101*a67afe4dSAndroid Build Coastguard Worker     * take care to ensure that it works correctly if the image is interlaced -
4102*a67afe4dSAndroid Build Coastguard Worker     * interlacing causes the actual row width to vary.
4103*a67afe4dSAndroid Build Coastguard Worker     */
4104*a67afe4dSAndroid Build Coastguard Worker {
4105*a67afe4dSAndroid Build Coastguard Worker    unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4106*a67afe4dSAndroid Build Coastguard Worker 
4107*a67afe4dSAndroid Build Coastguard Worker    pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4108*a67afe4dSAndroid Build Coastguard Worker    pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4109*a67afe4dSAndroid Build Coastguard Worker    pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4110*a67afe4dSAndroid Build Coastguard Worker    if (bpp == 1)
4111*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4112*a67afe4dSAndroid Build Coastguard Worker          png_read_filter_row_paeth_1byte_pixel;
4113*a67afe4dSAndroid Build Coastguard Worker    else
4114*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4115*a67afe4dSAndroid Build Coastguard Worker          png_read_filter_row_paeth_multibyte_pixel;
4116*a67afe4dSAndroid Build Coastguard Worker 
4117*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_FILTER_OPTIMIZATIONS
4118*a67afe4dSAndroid Build Coastguard Worker    /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to
4119*a67afe4dSAndroid Build Coastguard Worker     * call to install hardware optimizations for the above functions; simply
4120*a67afe4dSAndroid Build Coastguard Worker     * replace whatever elements of the pp->read_filter[] array with a hardware
4121*a67afe4dSAndroid Build Coastguard Worker     * specific (or, for that matter, generic) optimization.
4122*a67afe4dSAndroid Build Coastguard Worker     *
4123*a67afe4dSAndroid Build Coastguard Worker     * To see an example of this examine what configure.ac does when
4124*a67afe4dSAndroid Build Coastguard Worker     * --enable-arm-neon is specified on the command line.
4125*a67afe4dSAndroid Build Coastguard Worker     */
4126*a67afe4dSAndroid Build Coastguard Worker    PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4127*a67afe4dSAndroid Build Coastguard Worker #endif
4128*a67afe4dSAndroid Build Coastguard Worker }
4129*a67afe4dSAndroid Build Coastguard Worker 
4130*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_filter_row(png_structrp pp,png_row_infop row_info,png_bytep row,png_const_bytep prev_row,int filter)4131*a67afe4dSAndroid Build Coastguard Worker png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4132*a67afe4dSAndroid Build Coastguard Worker     png_const_bytep prev_row, int filter)
4133*a67afe4dSAndroid Build Coastguard Worker {
4134*a67afe4dSAndroid Build Coastguard Worker    /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
4135*a67afe4dSAndroid Build Coastguard Worker     * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
4136*a67afe4dSAndroid Build Coastguard Worker     * implementations.  See png_init_filter_functions above.
4137*a67afe4dSAndroid Build Coastguard Worker     */
4138*a67afe4dSAndroid Build Coastguard Worker    if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4139*a67afe4dSAndroid Build Coastguard Worker    {
4140*a67afe4dSAndroid Build Coastguard Worker       if (pp->read_filter[0] == NULL)
4141*a67afe4dSAndroid Build Coastguard Worker          png_init_filter_functions(pp);
4142*a67afe4dSAndroid Build Coastguard Worker 
4143*a67afe4dSAndroid Build Coastguard Worker       pp->read_filter[filter-1](row_info, row, prev_row);
4144*a67afe4dSAndroid Build Coastguard Worker    }
4145*a67afe4dSAndroid Build Coastguard Worker }
4146*a67afe4dSAndroid Build Coastguard Worker 
4147*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4148*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_IDAT_data(png_structrp png_ptr,png_bytep output,png_alloc_size_t avail_out)4149*a67afe4dSAndroid Build Coastguard Worker png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4150*a67afe4dSAndroid Build Coastguard Worker     png_alloc_size_t avail_out)
4151*a67afe4dSAndroid Build Coastguard Worker {
4152*a67afe4dSAndroid Build Coastguard Worker    /* Loop reading IDATs and decompressing the result into output[avail_out] */
4153*a67afe4dSAndroid Build Coastguard Worker    png_ptr->zstream.next_out = output;
4154*a67afe4dSAndroid Build Coastguard Worker    png_ptr->zstream.avail_out = 0; /* safety: set below */
4155*a67afe4dSAndroid Build Coastguard Worker 
4156*a67afe4dSAndroid Build Coastguard Worker    if (output == NULL)
4157*a67afe4dSAndroid Build Coastguard Worker       avail_out = 0;
4158*a67afe4dSAndroid Build Coastguard Worker 
4159*a67afe4dSAndroid Build Coastguard Worker    do
4160*a67afe4dSAndroid Build Coastguard Worker    {
4161*a67afe4dSAndroid Build Coastguard Worker       int ret;
4162*a67afe4dSAndroid Build Coastguard Worker       png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4163*a67afe4dSAndroid Build Coastguard Worker 
4164*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->zstream.avail_in == 0)
4165*a67afe4dSAndroid Build Coastguard Worker       {
4166*a67afe4dSAndroid Build Coastguard Worker          uInt avail_in;
4167*a67afe4dSAndroid Build Coastguard Worker          png_bytep buffer;
4168*a67afe4dSAndroid Build Coastguard Worker 
4169*a67afe4dSAndroid Build Coastguard Worker          while (png_ptr->idat_size == 0)
4170*a67afe4dSAndroid Build Coastguard Worker          {
4171*a67afe4dSAndroid Build Coastguard Worker             png_crc_finish(png_ptr, 0);
4172*a67afe4dSAndroid Build Coastguard Worker 
4173*a67afe4dSAndroid Build Coastguard Worker             png_ptr->idat_size = png_read_chunk_header(png_ptr);
4174*a67afe4dSAndroid Build Coastguard Worker             /* This is an error even in the 'check' case because the code just
4175*a67afe4dSAndroid Build Coastguard Worker              * consumed a non-IDAT header.
4176*a67afe4dSAndroid Build Coastguard Worker              */
4177*a67afe4dSAndroid Build Coastguard Worker             if (png_ptr->chunk_name != png_IDAT)
4178*a67afe4dSAndroid Build Coastguard Worker                png_error(png_ptr, "Not enough image data");
4179*a67afe4dSAndroid Build Coastguard Worker          }
4180*a67afe4dSAndroid Build Coastguard Worker 
4181*a67afe4dSAndroid Build Coastguard Worker          avail_in = png_ptr->IDAT_read_size;
4182*a67afe4dSAndroid Build Coastguard Worker 
4183*a67afe4dSAndroid Build Coastguard Worker          if (avail_in > png_ptr->idat_size)
4184*a67afe4dSAndroid Build Coastguard Worker             avail_in = (uInt)png_ptr->idat_size;
4185*a67afe4dSAndroid Build Coastguard Worker 
4186*a67afe4dSAndroid Build Coastguard Worker          /* A PNG with a gradually increasing IDAT size will defeat this attempt
4187*a67afe4dSAndroid Build Coastguard Worker           * to minimize memory usage by causing lots of re-allocs, but
4188*a67afe4dSAndroid Build Coastguard Worker           * realistically doing IDAT_read_size re-allocs is not likely to be a
4189*a67afe4dSAndroid Build Coastguard Worker           * big problem.
4190*a67afe4dSAndroid Build Coastguard Worker           */
4191*a67afe4dSAndroid Build Coastguard Worker          buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
4192*a67afe4dSAndroid Build Coastguard Worker 
4193*a67afe4dSAndroid Build Coastguard Worker          png_crc_read(png_ptr, buffer, avail_in);
4194*a67afe4dSAndroid Build Coastguard Worker          png_ptr->idat_size -= avail_in;
4195*a67afe4dSAndroid Build Coastguard Worker 
4196*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.next_in = buffer;
4197*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.avail_in = avail_in;
4198*a67afe4dSAndroid Build Coastguard Worker       }
4199*a67afe4dSAndroid Build Coastguard Worker 
4200*a67afe4dSAndroid Build Coastguard Worker       /* And set up the output side. */
4201*a67afe4dSAndroid Build Coastguard Worker       if (output != NULL) /* standard read */
4202*a67afe4dSAndroid Build Coastguard Worker       {
4203*a67afe4dSAndroid Build Coastguard Worker          uInt out = ZLIB_IO_MAX;
4204*a67afe4dSAndroid Build Coastguard Worker 
4205*a67afe4dSAndroid Build Coastguard Worker          if (out > avail_out)
4206*a67afe4dSAndroid Build Coastguard Worker             out = (uInt)avail_out;
4207*a67afe4dSAndroid Build Coastguard Worker 
4208*a67afe4dSAndroid Build Coastguard Worker          avail_out -= out;
4209*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.avail_out = out;
4210*a67afe4dSAndroid Build Coastguard Worker       }
4211*a67afe4dSAndroid Build Coastguard Worker 
4212*a67afe4dSAndroid Build Coastguard Worker       else /* after last row, checking for end */
4213*a67afe4dSAndroid Build Coastguard Worker       {
4214*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.next_out = tmpbuf;
4215*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.avail_out = (sizeof tmpbuf);
4216*a67afe4dSAndroid Build Coastguard Worker       }
4217*a67afe4dSAndroid Build Coastguard Worker 
4218*a67afe4dSAndroid Build Coastguard Worker       /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4219*a67afe4dSAndroid Build Coastguard Worker        * process.  If the LZ stream is truncated the sequential reader will
4220*a67afe4dSAndroid Build Coastguard Worker        * terminally damage the stream, above, by reading the chunk header of the
4221*a67afe4dSAndroid Build Coastguard Worker        * following chunk (it then exits with png_error).
4222*a67afe4dSAndroid Build Coastguard Worker        *
4223*a67afe4dSAndroid Build Coastguard Worker        * TODO: deal more elegantly with truncated IDAT lists.
4224*a67afe4dSAndroid Build Coastguard Worker        */
4225*a67afe4dSAndroid Build Coastguard Worker       ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4226*a67afe4dSAndroid Build Coastguard Worker 
4227*a67afe4dSAndroid Build Coastguard Worker       /* Take the unconsumed output back. */
4228*a67afe4dSAndroid Build Coastguard Worker       if (output != NULL)
4229*a67afe4dSAndroid Build Coastguard Worker          avail_out += png_ptr->zstream.avail_out;
4230*a67afe4dSAndroid Build Coastguard Worker 
4231*a67afe4dSAndroid Build Coastguard Worker       else /* avail_out counts the extra bytes */
4232*a67afe4dSAndroid Build Coastguard Worker          avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4233*a67afe4dSAndroid Build Coastguard Worker 
4234*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_out = 0;
4235*a67afe4dSAndroid Build Coastguard Worker 
4236*a67afe4dSAndroid Build Coastguard Worker       if (ret == Z_STREAM_END)
4237*a67afe4dSAndroid Build Coastguard Worker       {
4238*a67afe4dSAndroid Build Coastguard Worker          /* Do this for safety; we won't read any more into this row. */
4239*a67afe4dSAndroid Build Coastguard Worker          png_ptr->zstream.next_out = NULL;
4240*a67afe4dSAndroid Build Coastguard Worker 
4241*a67afe4dSAndroid Build Coastguard Worker          png_ptr->mode |= PNG_AFTER_IDAT;
4242*a67afe4dSAndroid Build Coastguard Worker          png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4243*a67afe4dSAndroid Build Coastguard Worker 
4244*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4245*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, "Extra compressed data");
4246*a67afe4dSAndroid Build Coastguard Worker          break;
4247*a67afe4dSAndroid Build Coastguard Worker       }
4248*a67afe4dSAndroid Build Coastguard Worker 
4249*a67afe4dSAndroid Build Coastguard Worker       if (ret != Z_OK)
4250*a67afe4dSAndroid Build Coastguard Worker       {
4251*a67afe4dSAndroid Build Coastguard Worker          png_zstream_error(png_ptr, ret);
4252*a67afe4dSAndroid Build Coastguard Worker 
4253*a67afe4dSAndroid Build Coastguard Worker          if (output != NULL)
4254*a67afe4dSAndroid Build Coastguard Worker             png_chunk_error(png_ptr, png_ptr->zstream.msg);
4255*a67afe4dSAndroid Build Coastguard Worker 
4256*a67afe4dSAndroid Build Coastguard Worker          else /* checking */
4257*a67afe4dSAndroid Build Coastguard Worker          {
4258*a67afe4dSAndroid Build Coastguard Worker             png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4259*a67afe4dSAndroid Build Coastguard Worker             return;
4260*a67afe4dSAndroid Build Coastguard Worker          }
4261*a67afe4dSAndroid Build Coastguard Worker       }
4262*a67afe4dSAndroid Build Coastguard Worker    } while (avail_out > 0);
4263*a67afe4dSAndroid Build Coastguard Worker 
4264*a67afe4dSAndroid Build Coastguard Worker    if (avail_out > 0)
4265*a67afe4dSAndroid Build Coastguard Worker    {
4266*a67afe4dSAndroid Build Coastguard Worker       /* The stream ended before the image; this is the same as too few IDATs so
4267*a67afe4dSAndroid Build Coastguard Worker        * should be handled the same way.
4268*a67afe4dSAndroid Build Coastguard Worker        */
4269*a67afe4dSAndroid Build Coastguard Worker       if (output != NULL)
4270*a67afe4dSAndroid Build Coastguard Worker          png_error(png_ptr, "Not enough image data");
4271*a67afe4dSAndroid Build Coastguard Worker 
4272*a67afe4dSAndroid Build Coastguard Worker       else /* the deflate stream contained extra data */
4273*a67afe4dSAndroid Build Coastguard Worker          png_chunk_benign_error(png_ptr, "Too much image data");
4274*a67afe4dSAndroid Build Coastguard Worker    }
4275*a67afe4dSAndroid Build Coastguard Worker }
4276*a67afe4dSAndroid Build Coastguard Worker 
4277*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_finish_IDAT(png_structrp png_ptr)4278*a67afe4dSAndroid Build Coastguard Worker png_read_finish_IDAT(png_structrp png_ptr)
4279*a67afe4dSAndroid Build Coastguard Worker {
4280*a67afe4dSAndroid Build Coastguard Worker    /* We don't need any more data and the stream should have ended, however the
4281*a67afe4dSAndroid Build Coastguard Worker     * LZ end code may actually not have been processed.  In this case we must
4282*a67afe4dSAndroid Build Coastguard Worker     * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4283*a67afe4dSAndroid Build Coastguard Worker     * may still remain to be consumed.
4284*a67afe4dSAndroid Build Coastguard Worker     */
4285*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4286*a67afe4dSAndroid Build Coastguard Worker    {
4287*a67afe4dSAndroid Build Coastguard Worker       /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4288*a67afe4dSAndroid Build Coastguard Worker        * the compressed stream, but the stream may be damaged too, so even after
4289*a67afe4dSAndroid Build Coastguard Worker        * this call we may need to terminate the zstream ownership.
4290*a67afe4dSAndroid Build Coastguard Worker        */
4291*a67afe4dSAndroid Build Coastguard Worker       png_read_IDAT_data(png_ptr, NULL, 0);
4292*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_out = NULL; /* safety */
4293*a67afe4dSAndroid Build Coastguard Worker 
4294*a67afe4dSAndroid Build Coastguard Worker       /* Now clear everything out for safety; the following may not have been
4295*a67afe4dSAndroid Build Coastguard Worker        * done.
4296*a67afe4dSAndroid Build Coastguard Worker        */
4297*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4298*a67afe4dSAndroid Build Coastguard Worker       {
4299*a67afe4dSAndroid Build Coastguard Worker          png_ptr->mode |= PNG_AFTER_IDAT;
4300*a67afe4dSAndroid Build Coastguard Worker          png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4301*a67afe4dSAndroid Build Coastguard Worker       }
4302*a67afe4dSAndroid Build Coastguard Worker    }
4303*a67afe4dSAndroid Build Coastguard Worker 
4304*a67afe4dSAndroid Build Coastguard Worker    /* If the zstream has not been released do it now *and* terminate the reading
4305*a67afe4dSAndroid Build Coastguard Worker     * of the final IDAT chunk.
4306*a67afe4dSAndroid Build Coastguard Worker     */
4307*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->zowner == png_IDAT)
4308*a67afe4dSAndroid Build Coastguard Worker    {
4309*a67afe4dSAndroid Build Coastguard Worker       /* Always do this; the pointers otherwise point into the read buffer. */
4310*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.next_in = NULL;
4311*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zstream.avail_in = 0;
4312*a67afe4dSAndroid Build Coastguard Worker 
4313*a67afe4dSAndroid Build Coastguard Worker       /* Now we no longer own the zstream. */
4314*a67afe4dSAndroid Build Coastguard Worker       png_ptr->zowner = 0;
4315*a67afe4dSAndroid Build Coastguard Worker 
4316*a67afe4dSAndroid Build Coastguard Worker       /* The slightly weird semantics of the sequential IDAT reading is that we
4317*a67afe4dSAndroid Build Coastguard Worker        * are always in or at the end of an IDAT chunk, so we always need to do a
4318*a67afe4dSAndroid Build Coastguard Worker        * crc_finish here.  If idat_size is non-zero we also need to read the
4319*a67afe4dSAndroid Build Coastguard Worker        * spurious bytes at the end of the chunk now.
4320*a67afe4dSAndroid Build Coastguard Worker        */
4321*a67afe4dSAndroid Build Coastguard Worker       (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4322*a67afe4dSAndroid Build Coastguard Worker    }
4323*a67afe4dSAndroid Build Coastguard Worker }
4324*a67afe4dSAndroid Build Coastguard Worker 
4325*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_finish_row(png_structrp png_ptr)4326*a67afe4dSAndroid Build Coastguard Worker png_read_finish_row(png_structrp png_ptr)
4327*a67afe4dSAndroid Build Coastguard Worker {
4328*a67afe4dSAndroid Build Coastguard Worker    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4329*a67afe4dSAndroid Build Coastguard Worker 
4330*a67afe4dSAndroid Build Coastguard Worker    /* Start of interlace block */
4331*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4332*a67afe4dSAndroid Build Coastguard Worker 
4333*a67afe4dSAndroid Build Coastguard Worker    /* Offset to next interlace block */
4334*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4335*a67afe4dSAndroid Build Coastguard Worker 
4336*a67afe4dSAndroid Build Coastguard Worker    /* Start of interlace block in the y direction */
4337*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4338*a67afe4dSAndroid Build Coastguard Worker 
4339*a67afe4dSAndroid Build Coastguard Worker    /* Offset to next interlace block in the y direction */
4340*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4341*a67afe4dSAndroid Build Coastguard Worker 
4342*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_read_finish_row");
4343*a67afe4dSAndroid Build Coastguard Worker    png_ptr->row_number++;
4344*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->row_number < png_ptr->num_rows)
4345*a67afe4dSAndroid Build Coastguard Worker       return;
4346*a67afe4dSAndroid Build Coastguard Worker 
4347*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->interlaced != 0)
4348*a67afe4dSAndroid Build Coastguard Worker    {
4349*a67afe4dSAndroid Build Coastguard Worker       png_ptr->row_number = 0;
4350*a67afe4dSAndroid Build Coastguard Worker 
4351*a67afe4dSAndroid Build Coastguard Worker       /* TO DO: don't do this if prev_row isn't needed (requires
4352*a67afe4dSAndroid Build Coastguard Worker        * read-ahead of the next row's filter byte.
4353*a67afe4dSAndroid Build Coastguard Worker        */
4354*a67afe4dSAndroid Build Coastguard Worker       memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4355*a67afe4dSAndroid Build Coastguard Worker 
4356*a67afe4dSAndroid Build Coastguard Worker       do
4357*a67afe4dSAndroid Build Coastguard Worker       {
4358*a67afe4dSAndroid Build Coastguard Worker          png_ptr->pass++;
4359*a67afe4dSAndroid Build Coastguard Worker 
4360*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->pass >= 7)
4361*a67afe4dSAndroid Build Coastguard Worker             break;
4362*a67afe4dSAndroid Build Coastguard Worker 
4363*a67afe4dSAndroid Build Coastguard Worker          png_ptr->iwidth = (png_ptr->width +
4364*a67afe4dSAndroid Build Coastguard Worker             png_pass_inc[png_ptr->pass] - 1 -
4365*a67afe4dSAndroid Build Coastguard Worker             png_pass_start[png_ptr->pass]) /
4366*a67afe4dSAndroid Build Coastguard Worker             png_pass_inc[png_ptr->pass];
4367*a67afe4dSAndroid Build Coastguard Worker 
4368*a67afe4dSAndroid Build Coastguard Worker          if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4369*a67afe4dSAndroid Build Coastguard Worker          {
4370*a67afe4dSAndroid Build Coastguard Worker             png_ptr->num_rows = (png_ptr->height +
4371*a67afe4dSAndroid Build Coastguard Worker                 png_pass_yinc[png_ptr->pass] - 1 -
4372*a67afe4dSAndroid Build Coastguard Worker                 png_pass_ystart[png_ptr->pass]) /
4373*a67afe4dSAndroid Build Coastguard Worker                 png_pass_yinc[png_ptr->pass];
4374*a67afe4dSAndroid Build Coastguard Worker          }
4375*a67afe4dSAndroid Build Coastguard Worker 
4376*a67afe4dSAndroid Build Coastguard Worker          else  /* if (png_ptr->transformations & PNG_INTERLACE) */
4377*a67afe4dSAndroid Build Coastguard Worker             break; /* libpng deinterlacing sees every row */
4378*a67afe4dSAndroid Build Coastguard Worker 
4379*a67afe4dSAndroid Build Coastguard Worker       } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4380*a67afe4dSAndroid Build Coastguard Worker 
4381*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->pass < 7)
4382*a67afe4dSAndroid Build Coastguard Worker          return;
4383*a67afe4dSAndroid Build Coastguard Worker    }
4384*a67afe4dSAndroid Build Coastguard Worker 
4385*a67afe4dSAndroid Build Coastguard Worker    /* Here after at the end of the last row of the last pass. */
4386*a67afe4dSAndroid Build Coastguard Worker    png_read_finish_IDAT(png_ptr);
4387*a67afe4dSAndroid Build Coastguard Worker }
4388*a67afe4dSAndroid Build Coastguard Worker #endif /* SEQUENTIAL_READ */
4389*a67afe4dSAndroid Build Coastguard Worker 
4390*a67afe4dSAndroid Build Coastguard Worker void /* PRIVATE */
png_read_start_row(png_structrp png_ptr)4391*a67afe4dSAndroid Build Coastguard Worker png_read_start_row(png_structrp png_ptr)
4392*a67afe4dSAndroid Build Coastguard Worker {
4393*a67afe4dSAndroid Build Coastguard Worker    /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4394*a67afe4dSAndroid Build Coastguard Worker 
4395*a67afe4dSAndroid Build Coastguard Worker    /* Start of interlace block */
4396*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4397*a67afe4dSAndroid Build Coastguard Worker 
4398*a67afe4dSAndroid Build Coastguard Worker    /* Offset to next interlace block */
4399*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4400*a67afe4dSAndroid Build Coastguard Worker 
4401*a67afe4dSAndroid Build Coastguard Worker    /* Start of interlace block in the y direction */
4402*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4403*a67afe4dSAndroid Build Coastguard Worker 
4404*a67afe4dSAndroid Build Coastguard Worker    /* Offset to next interlace block in the y direction */
4405*a67afe4dSAndroid Build Coastguard Worker    static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4406*a67afe4dSAndroid Build Coastguard Worker 
4407*a67afe4dSAndroid Build Coastguard Worker    unsigned int max_pixel_depth;
4408*a67afe4dSAndroid Build Coastguard Worker    size_t row_bytes;
4409*a67afe4dSAndroid Build Coastguard Worker 
4410*a67afe4dSAndroid Build Coastguard Worker    png_debug(1, "in png_read_start_row");
4411*a67afe4dSAndroid Build Coastguard Worker 
4412*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4413*a67afe4dSAndroid Build Coastguard Worker    png_init_read_transformations(png_ptr);
4414*a67afe4dSAndroid Build Coastguard Worker #endif
4415*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->interlaced != 0)
4416*a67afe4dSAndroid Build Coastguard Worker    {
4417*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4418*a67afe4dSAndroid Build Coastguard Worker          png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4419*a67afe4dSAndroid Build Coastguard Worker              png_pass_ystart[0]) / png_pass_yinc[0];
4420*a67afe4dSAndroid Build Coastguard Worker 
4421*a67afe4dSAndroid Build Coastguard Worker       else
4422*a67afe4dSAndroid Build Coastguard Worker          png_ptr->num_rows = png_ptr->height;
4423*a67afe4dSAndroid Build Coastguard Worker 
4424*a67afe4dSAndroid Build Coastguard Worker       png_ptr->iwidth = (png_ptr->width +
4425*a67afe4dSAndroid Build Coastguard Worker           png_pass_inc[png_ptr->pass] - 1 -
4426*a67afe4dSAndroid Build Coastguard Worker           png_pass_start[png_ptr->pass]) /
4427*a67afe4dSAndroid Build Coastguard Worker           png_pass_inc[png_ptr->pass];
4428*a67afe4dSAndroid Build Coastguard Worker    }
4429*a67afe4dSAndroid Build Coastguard Worker 
4430*a67afe4dSAndroid Build Coastguard Worker    else
4431*a67afe4dSAndroid Build Coastguard Worker    {
4432*a67afe4dSAndroid Build Coastguard Worker       png_ptr->num_rows = png_ptr->height;
4433*a67afe4dSAndroid Build Coastguard Worker       png_ptr->iwidth = png_ptr->width;
4434*a67afe4dSAndroid Build Coastguard Worker    }
4435*a67afe4dSAndroid Build Coastguard Worker 
4436*a67afe4dSAndroid Build Coastguard Worker    max_pixel_depth = (unsigned int)png_ptr->pixel_depth;
4437*a67afe4dSAndroid Build Coastguard Worker 
4438*a67afe4dSAndroid Build Coastguard Worker    /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4439*a67afe4dSAndroid Build Coastguard Worker     * calculations to calculate the final pixel depth, then
4440*a67afe4dSAndroid Build Coastguard Worker     * png_do_read_transforms actually does the transforms.  This means that the
4441*a67afe4dSAndroid Build Coastguard Worker     * code which effectively calculates this value is actually repeated in three
4442*a67afe4dSAndroid Build Coastguard Worker     * separate places.  They must all match.  Innocent changes to the order of
4443*a67afe4dSAndroid Build Coastguard Worker     * transformations can and will break libpng in a way that causes memory
4444*a67afe4dSAndroid Build Coastguard Worker     * overwrites.
4445*a67afe4dSAndroid Build Coastguard Worker     *
4446*a67afe4dSAndroid Build Coastguard Worker     * TODO: fix this.
4447*a67afe4dSAndroid Build Coastguard Worker     */
4448*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_PACK_SUPPORTED
4449*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4450*a67afe4dSAndroid Build Coastguard Worker       max_pixel_depth = 8;
4451*a67afe4dSAndroid Build Coastguard Worker #endif
4452*a67afe4dSAndroid Build Coastguard Worker 
4453*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_EXPAND_SUPPORTED
4454*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & PNG_EXPAND) != 0)
4455*a67afe4dSAndroid Build Coastguard Worker    {
4456*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4457*a67afe4dSAndroid Build Coastguard Worker       {
4458*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->num_trans != 0)
4459*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 32;
4460*a67afe4dSAndroid Build Coastguard Worker 
4461*a67afe4dSAndroid Build Coastguard Worker          else
4462*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 24;
4463*a67afe4dSAndroid Build Coastguard Worker       }
4464*a67afe4dSAndroid Build Coastguard Worker 
4465*a67afe4dSAndroid Build Coastguard Worker       else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4466*a67afe4dSAndroid Build Coastguard Worker       {
4467*a67afe4dSAndroid Build Coastguard Worker          if (max_pixel_depth < 8)
4468*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 8;
4469*a67afe4dSAndroid Build Coastguard Worker 
4470*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->num_trans != 0)
4471*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth *= 2;
4472*a67afe4dSAndroid Build Coastguard Worker       }
4473*a67afe4dSAndroid Build Coastguard Worker 
4474*a67afe4dSAndroid Build Coastguard Worker       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4475*a67afe4dSAndroid Build Coastguard Worker       {
4476*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->num_trans != 0)
4477*a67afe4dSAndroid Build Coastguard Worker          {
4478*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth *= 4;
4479*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth /= 3;
4480*a67afe4dSAndroid Build Coastguard Worker          }
4481*a67afe4dSAndroid Build Coastguard Worker       }
4482*a67afe4dSAndroid Build Coastguard Worker    }
4483*a67afe4dSAndroid Build Coastguard Worker #endif
4484*a67afe4dSAndroid Build Coastguard Worker 
4485*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_EXPAND_16_SUPPORTED
4486*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4487*a67afe4dSAndroid Build Coastguard Worker    {
4488*a67afe4dSAndroid Build Coastguard Worker #  ifdef PNG_READ_EXPAND_SUPPORTED
4489*a67afe4dSAndroid Build Coastguard Worker       /* In fact it is an error if it isn't supported, but checking is
4490*a67afe4dSAndroid Build Coastguard Worker        * the safe way.
4491*a67afe4dSAndroid Build Coastguard Worker        */
4492*a67afe4dSAndroid Build Coastguard Worker       if ((png_ptr->transformations & PNG_EXPAND) != 0)
4493*a67afe4dSAndroid Build Coastguard Worker       {
4494*a67afe4dSAndroid Build Coastguard Worker          if (png_ptr->bit_depth < 16)
4495*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth *= 2;
4496*a67afe4dSAndroid Build Coastguard Worker       }
4497*a67afe4dSAndroid Build Coastguard Worker       else
4498*a67afe4dSAndroid Build Coastguard Worker #  endif
4499*a67afe4dSAndroid Build Coastguard Worker       png_ptr->transformations &= ~PNG_EXPAND_16;
4500*a67afe4dSAndroid Build Coastguard Worker    }
4501*a67afe4dSAndroid Build Coastguard Worker #endif
4502*a67afe4dSAndroid Build Coastguard Worker 
4503*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_FILLER_SUPPORTED
4504*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4505*a67afe4dSAndroid Build Coastguard Worker    {
4506*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4507*a67afe4dSAndroid Build Coastguard Worker       {
4508*a67afe4dSAndroid Build Coastguard Worker          if (max_pixel_depth <= 8)
4509*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 16;
4510*a67afe4dSAndroid Build Coastguard Worker 
4511*a67afe4dSAndroid Build Coastguard Worker          else
4512*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 32;
4513*a67afe4dSAndroid Build Coastguard Worker       }
4514*a67afe4dSAndroid Build Coastguard Worker 
4515*a67afe4dSAndroid Build Coastguard Worker       else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4516*a67afe4dSAndroid Build Coastguard Worker          png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4517*a67afe4dSAndroid Build Coastguard Worker       {
4518*a67afe4dSAndroid Build Coastguard Worker          if (max_pixel_depth <= 32)
4519*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 32;
4520*a67afe4dSAndroid Build Coastguard Worker 
4521*a67afe4dSAndroid Build Coastguard Worker          else
4522*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 64;
4523*a67afe4dSAndroid Build Coastguard Worker       }
4524*a67afe4dSAndroid Build Coastguard Worker    }
4525*a67afe4dSAndroid Build Coastguard Worker #endif
4526*a67afe4dSAndroid Build Coastguard Worker 
4527*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4528*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4529*a67afe4dSAndroid Build Coastguard Worker    {
4530*a67afe4dSAndroid Build Coastguard Worker       if (
4531*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_EXPAND_SUPPORTED
4532*a67afe4dSAndroid Build Coastguard Worker           (png_ptr->num_trans != 0 &&
4533*a67afe4dSAndroid Build Coastguard Worker           (png_ptr->transformations & PNG_EXPAND) != 0) ||
4534*a67afe4dSAndroid Build Coastguard Worker #endif
4535*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_READ_FILLER_SUPPORTED
4536*a67afe4dSAndroid Build Coastguard Worker           (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4537*a67afe4dSAndroid Build Coastguard Worker #endif
4538*a67afe4dSAndroid Build Coastguard Worker           png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4539*a67afe4dSAndroid Build Coastguard Worker       {
4540*a67afe4dSAndroid Build Coastguard Worker          if (max_pixel_depth <= 16)
4541*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 32;
4542*a67afe4dSAndroid Build Coastguard Worker 
4543*a67afe4dSAndroid Build Coastguard Worker          else
4544*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 64;
4545*a67afe4dSAndroid Build Coastguard Worker       }
4546*a67afe4dSAndroid Build Coastguard Worker 
4547*a67afe4dSAndroid Build Coastguard Worker       else
4548*a67afe4dSAndroid Build Coastguard Worker       {
4549*a67afe4dSAndroid Build Coastguard Worker          if (max_pixel_depth <= 8)
4550*a67afe4dSAndroid Build Coastguard Worker          {
4551*a67afe4dSAndroid Build Coastguard Worker             if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4552*a67afe4dSAndroid Build Coastguard Worker                max_pixel_depth = 32;
4553*a67afe4dSAndroid Build Coastguard Worker 
4554*a67afe4dSAndroid Build Coastguard Worker             else
4555*a67afe4dSAndroid Build Coastguard Worker                max_pixel_depth = 24;
4556*a67afe4dSAndroid Build Coastguard Worker          }
4557*a67afe4dSAndroid Build Coastguard Worker 
4558*a67afe4dSAndroid Build Coastguard Worker          else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4559*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 64;
4560*a67afe4dSAndroid Build Coastguard Worker 
4561*a67afe4dSAndroid Build Coastguard Worker          else
4562*a67afe4dSAndroid Build Coastguard Worker             max_pixel_depth = 48;
4563*a67afe4dSAndroid Build Coastguard Worker       }
4564*a67afe4dSAndroid Build Coastguard Worker    }
4565*a67afe4dSAndroid Build Coastguard Worker #endif
4566*a67afe4dSAndroid Build Coastguard Worker 
4567*a67afe4dSAndroid Build Coastguard Worker #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4568*a67afe4dSAndroid Build Coastguard Worker defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4569*a67afe4dSAndroid Build Coastguard Worker    if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4570*a67afe4dSAndroid Build Coastguard Worker    {
4571*a67afe4dSAndroid Build Coastguard Worker       unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4572*a67afe4dSAndroid Build Coastguard Worker          png_ptr->user_transform_channels;
4573*a67afe4dSAndroid Build Coastguard Worker 
4574*a67afe4dSAndroid Build Coastguard Worker       if (user_pixel_depth > max_pixel_depth)
4575*a67afe4dSAndroid Build Coastguard Worker          max_pixel_depth = user_pixel_depth;
4576*a67afe4dSAndroid Build Coastguard Worker    }
4577*a67afe4dSAndroid Build Coastguard Worker #endif
4578*a67afe4dSAndroid Build Coastguard Worker 
4579*a67afe4dSAndroid Build Coastguard Worker    /* This value is stored in png_struct and double checked in the row read
4580*a67afe4dSAndroid Build Coastguard Worker     * code.
4581*a67afe4dSAndroid Build Coastguard Worker     */
4582*a67afe4dSAndroid Build Coastguard Worker    png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4583*a67afe4dSAndroid Build Coastguard Worker    png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4584*a67afe4dSAndroid Build Coastguard Worker 
4585*a67afe4dSAndroid Build Coastguard Worker    /* Align the width on the next larger 8 pixels.  Mainly used
4586*a67afe4dSAndroid Build Coastguard Worker     * for interlacing
4587*a67afe4dSAndroid Build Coastguard Worker     */
4588*a67afe4dSAndroid Build Coastguard Worker    row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4589*a67afe4dSAndroid Build Coastguard Worker    /* Calculate the maximum bytes needed, adding a byte and a pixel
4590*a67afe4dSAndroid Build Coastguard Worker     * for safety's sake
4591*a67afe4dSAndroid Build Coastguard Worker     */
4592*a67afe4dSAndroid Build Coastguard Worker    row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4593*a67afe4dSAndroid Build Coastguard Worker        1 + ((max_pixel_depth + 7) >> 3U);
4594*a67afe4dSAndroid Build Coastguard Worker 
4595*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_MAX_MALLOC_64K
4596*a67afe4dSAndroid Build Coastguard Worker    if (row_bytes > (png_uint_32)65536L)
4597*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "This image requires a row greater than 64KB");
4598*a67afe4dSAndroid Build Coastguard Worker #endif
4599*a67afe4dSAndroid Build Coastguard Worker 
4600*a67afe4dSAndroid Build Coastguard Worker    if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4601*a67afe4dSAndroid Build Coastguard Worker    {
4602*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, png_ptr->big_row_buf);
4603*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, png_ptr->big_prev_row);
4604*a67afe4dSAndroid Build Coastguard Worker 
4605*a67afe4dSAndroid Build Coastguard Worker       if (png_ptr->interlaced != 0)
4606*a67afe4dSAndroid Build Coastguard Worker          png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4607*a67afe4dSAndroid Build Coastguard Worker              row_bytes + 48);
4608*a67afe4dSAndroid Build Coastguard Worker 
4609*a67afe4dSAndroid Build Coastguard Worker       else
4610*a67afe4dSAndroid Build Coastguard Worker          png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4611*a67afe4dSAndroid Build Coastguard Worker 
4612*a67afe4dSAndroid Build Coastguard Worker       png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4613*a67afe4dSAndroid Build Coastguard Worker 
4614*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4615*a67afe4dSAndroid Build Coastguard Worker       /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4616*a67afe4dSAndroid Build Coastguard Worker        * of padding before and after row_buf; treat prev_row similarly.
4617*a67afe4dSAndroid Build Coastguard Worker        * NOTE: the alignment is to the start of the pixels, one beyond the start
4618*a67afe4dSAndroid Build Coastguard Worker        * of the buffer, because of the filter byte.  Prior to libpng 1.5.6 this
4619*a67afe4dSAndroid Build Coastguard Worker        * was incorrect; the filter byte was aligned, which had the exact
4620*a67afe4dSAndroid Build Coastguard Worker        * opposite effect of that intended.
4621*a67afe4dSAndroid Build Coastguard Worker        */
4622*a67afe4dSAndroid Build Coastguard Worker       {
4623*a67afe4dSAndroid Build Coastguard Worker          png_bytep temp = png_ptr->big_row_buf + 32;
4624*a67afe4dSAndroid Build Coastguard Worker          size_t extra = (size_t)temp & 0x0f;
4625*a67afe4dSAndroid Build Coastguard Worker          png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4626*a67afe4dSAndroid Build Coastguard Worker 
4627*a67afe4dSAndroid Build Coastguard Worker          temp = png_ptr->big_prev_row + 32;
4628*a67afe4dSAndroid Build Coastguard Worker          extra = (size_t)temp & 0x0f;
4629*a67afe4dSAndroid Build Coastguard Worker          png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4630*a67afe4dSAndroid Build Coastguard Worker       }
4631*a67afe4dSAndroid Build Coastguard Worker #else
4632*a67afe4dSAndroid Build Coastguard Worker       /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4633*a67afe4dSAndroid Build Coastguard Worker       png_ptr->row_buf = png_ptr->big_row_buf + 31;
4634*a67afe4dSAndroid Build Coastguard Worker       png_ptr->prev_row = png_ptr->big_prev_row + 31;
4635*a67afe4dSAndroid Build Coastguard Worker #endif
4636*a67afe4dSAndroid Build Coastguard Worker       png_ptr->old_big_row_buf_size = row_bytes + 48;
4637*a67afe4dSAndroid Build Coastguard Worker    }
4638*a67afe4dSAndroid Build Coastguard Worker 
4639*a67afe4dSAndroid Build Coastguard Worker #ifdef PNG_MAX_MALLOC_64K
4640*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->rowbytes > 65535)
4641*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "This image requires a row greater than 64KB");
4642*a67afe4dSAndroid Build Coastguard Worker 
4643*a67afe4dSAndroid Build Coastguard Worker #endif
4644*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4645*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, "Row has too many bytes to allocate in memory");
4646*a67afe4dSAndroid Build Coastguard Worker 
4647*a67afe4dSAndroid Build Coastguard Worker    memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4648*a67afe4dSAndroid Build Coastguard Worker 
4649*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "width = %u,", png_ptr->width);
4650*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "height = %u,", png_ptr->height);
4651*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4652*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4653*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4654*a67afe4dSAndroid Build Coastguard Worker    png_debug1(3, "irowbytes = %lu",
4655*a67afe4dSAndroid Build Coastguard Worker        (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4656*a67afe4dSAndroid Build Coastguard Worker 
4657*a67afe4dSAndroid Build Coastguard Worker    /* The sequential reader needs a buffer for IDAT, but the progressive reader
4658*a67afe4dSAndroid Build Coastguard Worker     * does not, so free the read buffer now regardless; the sequential reader
4659*a67afe4dSAndroid Build Coastguard Worker     * reallocates it on demand.
4660*a67afe4dSAndroid Build Coastguard Worker     */
4661*a67afe4dSAndroid Build Coastguard Worker    if (png_ptr->read_buffer != NULL)
4662*a67afe4dSAndroid Build Coastguard Worker    {
4663*a67afe4dSAndroid Build Coastguard Worker       png_bytep buffer = png_ptr->read_buffer;
4664*a67afe4dSAndroid Build Coastguard Worker 
4665*a67afe4dSAndroid Build Coastguard Worker       png_ptr->read_buffer_size = 0;
4666*a67afe4dSAndroid Build Coastguard Worker       png_ptr->read_buffer = NULL;
4667*a67afe4dSAndroid Build Coastguard Worker       png_free(png_ptr, buffer);
4668*a67afe4dSAndroid Build Coastguard Worker    }
4669*a67afe4dSAndroid Build Coastguard Worker 
4670*a67afe4dSAndroid Build Coastguard Worker    /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4671*a67afe4dSAndroid Build Coastguard Worker     * value from the stream (note that this will result in a fatal error if the
4672*a67afe4dSAndroid Build Coastguard Worker     * IDAT stream has a bogus deflate header window_bits value, but this should
4673*a67afe4dSAndroid Build Coastguard Worker     * not be happening any longer!)
4674*a67afe4dSAndroid Build Coastguard Worker     */
4675*a67afe4dSAndroid Build Coastguard Worker    if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4676*a67afe4dSAndroid Build Coastguard Worker       png_error(png_ptr, png_ptr->zstream.msg);
4677*a67afe4dSAndroid Build Coastguard Worker 
4678*a67afe4dSAndroid Build Coastguard Worker    png_ptr->flags |= PNG_FLAG_ROW_INIT;
4679*a67afe4dSAndroid Build Coastguard Worker }
4680*a67afe4dSAndroid Build Coastguard Worker #endif /* READ */
4681