annotate lib/libpng/pngrutil.c @ 2296:6e178010fc29

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