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