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