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