18#ifdef PNG_READ_SUPPORTED
23#define LZ77Min (2U+5U+4U)
25#ifdef PNG_READ_INTERLACING_SUPPORTED
29static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
31static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
33static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
35static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
41png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
43 png_uint_32 uval = png_get_uint_32(buf);
45 if (uval > PNG_UINT_31_MAX)
46 png_error(png_ptr,
"PNG unsigned integer out of range");
51#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED
63png_get_uint_32)(png_const_bytep buf)
66 ((png_uint_32)(*(buf )) << 24) +
67 ((png_uint_32)(*(buf + 1)) << 16) +
68 ((png_uint_32)(*(buf + 2)) << 8) +
69 ((png_uint_32)(*(buf + 3)) ) ;
80png_get_int_32)(png_const_bytep buf)
82 png_uint_32 uval = png_get_uint_32(buf);
83 if ((uval & 0x80000000) == 0)
84 return (png_int_32)uval;
86 uval = (uval ^ 0xffffffff) + 1;
87 if ((uval & 0x80000000) == 0)
88 return -(png_int_32)uval;
98png_get_uint_16)(png_const_bytep buf)
106 ((
unsigned int)(*buf) << 8) +
107 ((
unsigned int)(*(buf + 1)));
109 return (png_uint_16)val;
116png_read_sig(png_structrp png_ptr, png_inforp info_ptr)
118 size_t num_checked, num_to_check;
121 if (png_ptr->sig_bytes >= 8)
124 num_checked = png_ptr->sig_bytes;
125 num_to_check = 8 - num_checked;
127#ifdef PNG_IO_STATE_SUPPORTED
128 png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
132 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
133 png_ptr->sig_bytes = 8;
135 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
137 if (num_checked < 4 &&
138 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4) != 0)
139 png_error(png_ptr,
"Not a PNG file");
141 png_error(png_ptr,
"PNG file corrupted by ASCII conversion");
144 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
153check_chunk_name(png_uint_32 name)
161 name &= ~PNG_U32(32,32,0,32);
162 t = (name & ~0x1f1f1f1fU) ^ 0x40404040U;
167 name -= PNG_U32(65,65,65,65);
173 name -= PNG_U32(25,25,25,26);
176 return (t & 0xe0e0e0e0U) == 0U;
183png_read_chunk_header(png_structrp png_ptr)
186 png_uint_32 chunk_name, length;
188#ifdef PNG_IO_STATE_SUPPORTED
189 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
196 png_read_data(png_ptr, buf, 8);
197 length = png_get_uint_31(png_ptr, buf);
198 png_ptr->chunk_name = chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
201 png_reset_crc(png_ptr);
202 png_calculate_crc(png_ptr, buf + 4, 4);
204 png_debug2(0,
"Reading chunk typeid = 0x%lx, length = %lu",
205 (
unsigned long)png_ptr->chunk_name, (
unsigned long)length);
211 png_chunk_error(png_ptr,
"bad header (invalid length)");
214 if (!check_chunk_name(chunk_name))
215 png_chunk_error(png_ptr,
"bad header (invalid type)");
217#ifdef PNG_IO_STATE_SUPPORTED
218 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
226png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
231 png_read_data(png_ptr, buf, length);
232 png_calculate_crc(png_ptr, buf, length);
239png_crc_error(png_structrp png_ptr,
int handle_as_ancillary)
241 png_byte crc_bytes[4];
271 if (handle_as_ancillary || PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
273 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
274 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
280 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
284#ifdef PNG_IO_STATE_SUPPORTED
285 png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
289 png_read_data(png_ptr, crc_bytes, 4);
293 crc = png_get_uint_32(crc_bytes);
294 return crc != png_ptr->crc;
312png_crc_finish_critical(png_structrp png_ptr, png_uint_32 skip,
313 int handle_as_ancillary)
321 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
323 len = (
sizeof tmpbuf);
328 png_crc_read(png_ptr, tmpbuf, len);
336 if (handle_as_ancillary &&
337 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
338 handle_as_ancillary = 0;
342 if (png_crc_error(png_ptr, handle_as_ancillary) != 0)
345 if (handle_as_ancillary || PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
346 (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
347 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
348 png_chunk_warning(png_ptr,
"CRC error");
351 png_chunk_error(png_ptr,
"CRC error");
360png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
362 return png_crc_finish_critical(png_ptr, skip, 0);
365#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\
366 defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\
367 defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\
368 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_eXIf_SUPPORTED) ||\
369 defined(PNG_SEQUENTIAL_READ_SUPPORTED)
376png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size)
378 png_bytep buffer = png_ptr->read_buffer;
380 if (new_size > png_chunk_max(png_ptr))
return NULL;
382 if (buffer != NULL && new_size > png_ptr->read_buffer_size)
384 png_ptr->read_buffer = NULL;
385 png_ptr->read_buffer_size = 0;
386 png_free(png_ptr, buffer);
392 buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size));
396# ifndef PNG_NO_MEMZERO
397 memset(buffer, 0, new_size);
399 png_ptr->read_buffer = buffer;
400 png_ptr->read_buffer_size = new_size;
414png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
416 if (png_ptr->zowner != 0)
420 PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner);
425 (void)png_safecat(msg, (
sizeof msg), 4,
" using zstream");
427 png_chunk_warning(png_ptr, msg);
430 png_chunk_error(png_ptr, msg);
449#if ZLIB_VERNUM >= 0x1240
452# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW)
453 if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) ==
457 png_ptr->zstream_start = 0;
462 png_ptr->zstream_start = 1;
471 png_ptr->zstream.next_in = NULL;
472 png_ptr->zstream.avail_in = 0;
473 png_ptr->zstream.next_out = NULL;
474 png_ptr->zstream.avail_out = 0;
476 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
478#if ZLIB_VERNUM >= 0x1240
479 ret = inflateReset2(&png_ptr->zstream, window_bits);
481 ret = inflateReset(&png_ptr->zstream);
487#if ZLIB_VERNUM >= 0x1240
488 ret = inflateInit2(&png_ptr->zstream, window_bits);
490 ret = inflateInit(&png_ptr->zstream);
494 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
497#ifdef PNG_DISABLE_ADLER32_CHECK_SUPPORTED
498 if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON)
500 ret = inflateValidate(&png_ptr->zstream, 0);
504 png_ptr->zowner = owner;
507 png_zstream_error(png_ptr, ret);
517#if ZLIB_VERNUM >= 0x1240
525png_zlib_inflate(png_structrp png_ptr,
int flush)
527 if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0)
529 if ((*png_ptr->zstream.next_in >> 4) > 7)
531 png_ptr->zstream.msg =
"invalid window size (libpng)";
535 png_ptr->zstream_start = 0;
538 return inflate(&png_ptr->zstream, flush);
542#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED
543#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED)
556png_inflate(png_structrp png_ptr, png_uint_32 owner,
int finish,
557 png_const_bytep input, png_uint_32p input_size_ptr,
558 png_bytep output, png_alloc_size_t *output_size_ptr)
560 if (png_ptr->zowner == owner)
563 png_alloc_size_t avail_out = *output_size_ptr;
564 png_uint_32 avail_in = *input_size_ptr;
575 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
577 png_ptr->zstream.avail_in = 0;
578 png_ptr->zstream.avail_out = 0;
584 png_ptr->zstream.next_out = output;
589 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
599 avail_in += png_ptr->zstream.avail_in;
603 if (avail_in < avail)
604 avail = (uInt)avail_in;
607 png_ptr->zstream.avail_in = avail;
610 avail_out += png_ptr->zstream.avail_out;
619 png_ptr->zstream.next_out = local_buffer;
620 if ((
sizeof local_buffer) < avail)
621 avail = (
sizeof local_buffer);
624 if (avail_out < avail)
625 avail = (uInt)avail_out;
627 png_ptr->zstream.avail_out = avail;
636 ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH :
637 (finish ? Z_FINISH : Z_SYNC_FLUSH));
638 }
while (ret == Z_OK);
642 png_ptr->zstream.next_out = NULL;
645 avail_in += png_ptr->zstream.avail_in;
646 avail_out += png_ptr->zstream.avail_out;
652 *output_size_ptr -= avail_out;
655 *input_size_ptr -= avail_in;
658 png_zstream_error(png_ptr, ret);
668 png_ptr->zstream.msg = PNGZ_MSG_CAST(
"zstream unclaimed");
669 return Z_STREAM_ERROR;
681png_decompress_chunk(png_structrp png_ptr,
682 png_uint_32 chunklength, png_uint_32 prefix_size,
683 png_alloc_size_t *newlength ,
693 png_alloc_size_t limit = png_chunk_max(png_ptr);
695 if (limit >= prefix_size + (terminate != 0))
699 limit -= prefix_size + (terminate != 0);
701 if (limit < *newlength)
705 ret = png_inflate_claim(png_ptr, png_ptr->chunk_name);
709 png_uint_32 lzsize = chunklength - prefix_size;
711 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1,
712 png_ptr->read_buffer + prefix_size, &lzsize,
715 if (ret == Z_STREAM_END)
724 if (inflateReset(&png_ptr->zstream) == Z_OK)
731 png_alloc_size_t new_size = *newlength;
732 png_alloc_size_t buffer_size = prefix_size + new_size +
734 png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr,
739 memset(text, 0, buffer_size);
741 ret = png_inflate(png_ptr, png_ptr->chunk_name, 1,
742 png_ptr->read_buffer + prefix_size, &lzsize,
743 text + prefix_size, newlength);
745 if (ret == Z_STREAM_END)
747 if (new_size == *newlength)
750 text[prefix_size + *newlength] = 0;
753 memcpy(text, png_ptr->read_buffer, prefix_size);
756 png_bytep old_ptr = png_ptr->read_buffer;
758 png_ptr->read_buffer = text;
759 png_ptr->read_buffer_size = buffer_size;
772 ret = PNG_UNEXPECTED_ZLIB_RETURN;
776 else if (ret == Z_OK)
777 ret = PNG_UNEXPECTED_ZLIB_RETURN;
782 png_free(png_ptr, text);
787 if (ret == Z_STREAM_END &&
788 chunklength - prefix_size != lzsize)
789 png_chunk_benign_error(png_ptr,
"extra compressed data");
796 png_zstream_error(png_ptr, Z_MEM_ERROR);
803 png_zstream_error(png_ptr, ret);
804 ret = PNG_UNEXPECTED_ZLIB_RETURN;
808 else if (ret == Z_OK)
809 ret = PNG_UNEXPECTED_ZLIB_RETURN;
815 else if (ret == Z_STREAM_END)
816 ret = PNG_UNEXPECTED_ZLIB_RETURN;
824 png_zstream_error(png_ptr, Z_MEM_ERROR);
831#ifdef PNG_READ_iCCP_SUPPORTED
836png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size,
837 png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size,
840 if (png_ptr->zowner == png_ptr->chunk_name)
845 png_ptr->zstream.next_out = next_out;
846 png_ptr->zstream.avail_out = 0;
850 if (png_ptr->zstream.avail_in == 0)
852 if (read_size > *chunk_bytes)
853 read_size = (uInt)*chunk_bytes;
854 *chunk_bytes -= read_size;
857 png_crc_read(png_ptr, read_buffer, read_size);
859 png_ptr->zstream.next_in = read_buffer;
860 png_ptr->zstream.avail_in = read_size;
863 if (png_ptr->zstream.avail_out == 0)
865 uInt avail = ZLIB_IO_MAX;
866 if (avail > *out_size)
867 avail = (uInt)*out_size;
870 png_ptr->zstream.avail_out = avail;
877 ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ?
878 Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH));
880 while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0));
882 *out_size += png_ptr->zstream.avail_out;
883 png_ptr->zstream.avail_out = 0;
886 png_zstream_error(png_ptr, ret);
892 png_ptr->zstream.msg = PNGZ_MSG_CAST(
"zstream unclaimed");
893 return Z_STREAM_ERROR;
900static png_handle_result_code
901png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
904 png_uint_32 width, height;
905 int bit_depth, color_type, compression_type, filter_type;
908 png_debug(1,
"in png_handle_IHDR");
912 png_ptr->mode |= PNG_HAVE_IHDR;
914 png_crc_read(png_ptr, buf, 13);
915 png_crc_finish(png_ptr, 0);
917 width = png_get_uint_31(png_ptr, buf);
918 height = png_get_uint_31(png_ptr, buf + 4);
921 compression_type = buf[10];
922 filter_type = buf[11];
923 interlace_type = buf[12];
926 png_ptr->width = width;
927 png_ptr->height = height;
928 png_ptr->bit_depth = (png_byte)bit_depth;
929 png_ptr->interlaced = (png_byte)interlace_type;
930 png_ptr->color_type = (png_byte)color_type;
931#ifdef PNG_MNG_FEATURES_SUPPORTED
932 png_ptr->filter_type = (png_byte)filter_type;
934 png_ptr->compression_type = (png_byte)compression_type;
937 switch (png_ptr->color_type)
940 case PNG_COLOR_TYPE_GRAY:
941 case PNG_COLOR_TYPE_PALETTE:
942 png_ptr->channels = 1;
945 case PNG_COLOR_TYPE_RGB:
946 png_ptr->channels = 3;
949 case PNG_COLOR_TYPE_GRAY_ALPHA:
950 png_ptr->channels = 2;
953 case PNG_COLOR_TYPE_RGB_ALPHA:
954 png_ptr->channels = 4;
959 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
960 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
961 png_debug1(3,
"bit_depth = %d", png_ptr->bit_depth);
962 png_debug1(3,
"channels = %d", png_ptr->channels);
963 png_debug1(3,
"rowbytes = %lu", (
unsigned long)png_ptr->rowbytes);
968 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
969 color_type, interlace_type, compression_type, filter_type);
980static png_handle_result_code
981png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
983 png_const_charp errmsg = NULL;
985 png_debug(1,
"in png_handle_PLTE");
992 if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
993 errmsg =
"duplicate";
995 else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
996 errmsg =
"out of place";
998 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
999 errmsg =
"ignored in grayscale PNG";
1001 else if (length > 3*PNG_MAX_PALETTE_LENGTH || (length % 3) != 0)
1015 else if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE &&
1016 (png_has_chunk(png_ptr, tRNS) || png_has_chunk(png_ptr, bKGD)))
1017 errmsg =
"out of place";
1026 const unsigned max_palette_length =
1027 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
1028 1U << png_ptr->bit_depth : PNG_MAX_PALETTE_LENGTH;
1033 const unsigned num = (length > 3U*max_palette_length) ?
1034 max_palette_length : (unsigned)length / 3U;
1037 png_byte buf[3*PNG_MAX_PALETTE_LENGTH];
1038 png_color palette[PNG_MAX_PALETTE_LENGTH];
1041 png_crc_read(png_ptr, buf, num*3U);
1042 png_crc_finish_critical(png_ptr, length - 3U*num,
1044 png_ptr->color_type != PNG_COLOR_TYPE_PALETTE);
1046 for (i = 0U, j = 0U; i < num; i++)
1048 palette[i].red = buf[j++];
1049 palette[i].green = buf[j++];
1050 palette[i].blue = buf[j++];
1054 png_ptr->mode |= PNG_HAVE_PLTE;
1069 png_set_PLTE(png_ptr, info_ptr, palette, num);
1074 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1076 png_crc_finish(png_ptr, length);
1077 png_chunk_error(png_ptr, errmsg);
1082 png_crc_finish_critical(png_ptr, length, 1);
1083 png_chunk_benign_error(png_ptr, errmsg);
1089 return errmsg != NULL ? handled_error : handled_error;
1095#define png_handle_IDAT NULL
1097static png_handle_result_code
1098png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1100 png_debug(1,
"in png_handle_IEND");
1102 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1105 png_chunk_benign_error(png_ptr,
"invalid");
1107 png_crc_finish_critical(png_ptr, length, 1);
1110 PNG_UNUSED(info_ptr)
1113#ifdef PNG_READ_gAMA_SUPPORTED
1114static png_handle_result_code
1115png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1120 png_debug(1,
"in png_handle_gAMA");
1122 png_crc_read(png_ptr, buf, 4);
1124 if (png_crc_finish(png_ptr, 0) != 0)
1125 return handled_error;
1127 ugamma = png_get_uint_32(buf);
1129 if (ugamma > PNG_UINT_31_MAX)
1131 png_chunk_benign_error(png_ptr,
"invalid");
1132 return handled_error;
1135 png_set_gAMA_fixed(png_ptr, info_ptr, (png_fixed_point)ugamma);
1137#ifdef PNG_READ_GAMMA_SUPPORTED
1141 if (png_ptr->chunk_gamma == 0)
1142 png_ptr->chunk_gamma = (png_fixed_point)ugamma;
1149# define png_handle_gAMA NULL
1152#ifdef PNG_READ_sBIT_SUPPORTED
1153static png_handle_result_code
1154png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1156 unsigned int truelen, i;
1157 png_byte sample_depth;
1160 png_debug(1,
"in png_handle_sBIT");
1162 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1170 truelen = png_ptr->channels;
1171 sample_depth = png_ptr->bit_depth;
1174 if (length != truelen)
1176 png_crc_finish(png_ptr, length);
1177 png_chunk_benign_error(png_ptr,
"bad length");
1178 return handled_error;
1181 buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1182 png_crc_read(png_ptr, buf, truelen);
1184 if (png_crc_finish(png_ptr, 0) != 0)
1185 return handled_error;
1187 for (i=0; i<truelen; ++i)
1189 if (buf[i] == 0 || buf[i] > sample_depth)
1191 png_chunk_benign_error(png_ptr,
"invalid");
1192 return handled_error;
1196 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1198 png_ptr->sig_bit.red = buf[0];
1199 png_ptr->sig_bit.green = buf[1];
1200 png_ptr->sig_bit.blue = buf[2];
1201 png_ptr->sig_bit.alpha = buf[3];
1206 png_ptr->sig_bit.gray = buf[0];
1207 png_ptr->sig_bit.red = buf[0];
1208 png_ptr->sig_bit.green = buf[0];
1209 png_ptr->sig_bit.blue = buf[0];
1210 png_ptr->sig_bit.alpha = buf[1];
1213 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1217# define png_handle_sBIT NULL
1220#ifdef PNG_READ_cHRM_SUPPORTED
1222png_get_int_32_checked(png_const_bytep buf,
int *error)
1224 png_uint_32 uval = png_get_uint_32(buf);
1225 if ((uval & 0x80000000) == 0)
1226 return (png_int_32)uval;
1228 uval = (uval ^ 0xffffffff) + 1;
1229 if ((uval & 0x80000000) == 0)
1230 return -(png_int_32)uval;
1239static png_handle_result_code
1240png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1246 png_debug(1,
"in png_handle_cHRM");
1248 png_crc_read(png_ptr, buf, 32);
1250 if (png_crc_finish(png_ptr, 0) != 0)
1251 return handled_error;
1253 xy.whitex = png_get_int_32_checked(buf + 0, &error);
1254 xy.whitey = png_get_int_32_checked(buf + 4, &error);
1255 xy.redx = png_get_int_32_checked(buf + 8, &error);
1256 xy.redy = png_get_int_32_checked(buf + 12, &error);
1257 xy.greenx = png_get_int_32_checked(buf + 16, &error);
1258 xy.greeny = png_get_int_32_checked(buf + 20, &error);
1259 xy.bluex = png_get_int_32_checked(buf + 24, &error);
1260 xy.bluey = png_get_int_32_checked(buf + 28, &error);
1264 png_chunk_benign_error(png_ptr,
"invalid");
1265 return handled_error;
1272 png_set_cHRM_fixed(png_ptr, info_ptr, xy.whitex, xy.whitey, xy.redx, xy.redy,
1273 xy.greenx, xy.greeny, xy.bluex, xy.bluey);
1276# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1280 if (!png_has_chunk(png_ptr, mDCV))
1282 png_ptr->chromaticities = xy;
1290# define png_handle_cHRM NULL
1293#ifdef PNG_READ_sRGB_SUPPORTED
1294static png_handle_result_code
1295png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1299 png_debug(1,
"in png_handle_sRGB");
1301 png_crc_read(png_ptr, &intent, 1);
1303 if (png_crc_finish(png_ptr, 0) != 0)
1304 return handled_error;
1313 png_chunk_benign_error(png_ptr,
"invalid");
1314 return handled_error;
1317 png_set_sRGB(png_ptr, info_ptr, intent);
1322#ifdef PNG_READ_GAMMA_SUPPORTED
1327 if (!png_has_chunk(png_ptr, cICP) || png_ptr->chunk_gamma == 0)
1328 png_ptr->chunk_gamma = PNG_GAMMA_sRGB_INVERSE;
1335# define png_handle_sRGB NULL
1338#ifdef PNG_READ_iCCP_SUPPORTED
1339static png_handle_result_code
1340png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1343 png_const_charp errmsg = NULL;
1346 png_debug(1,
"in png_handle_iCCP");
1353 uInt read_length, keyword_length;
1360 if (read_length > length)
1361 read_length = (uInt)length;
1363 png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1364 length -= read_length;
1366 if (length < LZ77Min)
1368 png_crc_finish(png_ptr, length);
1369 png_chunk_benign_error(png_ptr,
"too short");
1370 return handled_error;
1374 while (keyword_length < 80 && keyword_length < read_length &&
1375 keyword[keyword_length] != 0)
1379 if (keyword_length >= 1 && keyword_length <= 79)
1384 if (keyword_length+1 < read_length &&
1385 keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1387 read_length -= keyword_length+2;
1389 if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1391 Byte profile_header[132]={0};
1392 Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1393 png_alloc_size_t size = (
sizeof profile_header);
1395 png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1396 png_ptr->zstream.avail_in = read_length;
1397 (void)png_inflate_read(png_ptr, local_buffer,
1398 (
sizeof local_buffer), &length, profile_header, &size,
1405 png_uint_32 profile_length = png_get_uint_32(profile_header);
1407 if (png_icc_check_length(png_ptr, keyword, profile_length) !=
1413 if (png_icc_check_header(png_ptr, keyword, profile_length,
1414 profile_header, png_ptr->color_type) != 0)
1421 png_uint_32 tag_count =
1422 png_get_uint_32(profile_header + 128);
1423 png_bytep profile = png_read_buffer(png_ptr,
1426 if (profile != NULL)
1428 memcpy(profile, profile_header,
1429 (
sizeof profile_header));
1431 size = 12 * tag_count;
1433 (void)png_inflate_read(png_ptr, local_buffer,
1434 (
sizeof local_buffer), &length,
1435 profile + (
sizeof profile_header), &size, 0);
1442 if (png_icc_check_tag_table(png_ptr,
1443 keyword, profile_length, profile) != 0)
1448 size = profile_length - (
sizeof profile_header)
1451 (void)png_inflate_read(png_ptr, local_buffer,
1452 (
sizeof local_buffer), &length,
1453 profile + (
sizeof profile_header) +
1454 12 * tag_count, &size, 1);
1456 if (length > 0 && !(png_ptr->flags &
1457 PNG_FLAG_BENIGN_ERRORS_WARN))
1458 errmsg =
"extra compressed data";
1468 png_chunk_warning(png_ptr,
1469 "extra compressed data");
1472 png_crc_finish(png_ptr, length);
1476 if (info_ptr != NULL)
1478 png_free_data(png_ptr, info_ptr,
1481 info_ptr->iccp_name = png_voidcast(
char*,
1482 png_malloc_base(png_ptr,
1484 if (info_ptr->iccp_name != NULL)
1486 memcpy(info_ptr->iccp_name, keyword,
1488 info_ptr->iccp_proflen =
1490 info_ptr->iccp_profile = profile;
1491 png_ptr->read_buffer = NULL;
1492 info_ptr->free_me |= PNG_FREE_ICCP;
1493 info_ptr->valid |= PNG_INFO_iCCP;
1497 errmsg =
"out of memory";
1507 png_ptr->zowner = 0;
1512 errmsg = png_ptr->zstream.msg;
1517 errmsg = png_ptr->zstream.msg;
1521 errmsg =
"out of memory";
1531 errmsg = png_ptr->zstream.msg;
1534 png_ptr->zowner = 0;
1538 errmsg = png_ptr->zstream.msg;
1542 errmsg =
"bad compression method";
1546 errmsg =
"bad keyword";
1551 png_crc_finish(png_ptr, length);
1554 png_chunk_benign_error(png_ptr, errmsg);
1556 return handled_error;
1559# define png_handle_iCCP NULL
1562#ifdef PNG_READ_sPLT_SUPPORTED
1563static png_handle_result_code
1564png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1567 png_bytep entry_start, buffer;
1568 png_sPLT_t new_palette;
1570 png_uint_32 data_length;
1572 png_uint_32 skip = 0;
1576 png_debug(1,
"in png_handle_sPLT");
1578#ifdef PNG_USER_LIMITS_SUPPORTED
1579 if (png_ptr->user_chunk_cache_max != 0)
1581 if (png_ptr->user_chunk_cache_max == 1)
1583 png_crc_finish(png_ptr, length);
1584 return handled_error;
1587 if (--png_ptr->user_chunk_cache_max == 1)
1589 png_warning(png_ptr,
"No space in chunk cache for sPLT");
1590 png_crc_finish(png_ptr, length);
1591 return handled_error;
1596 buffer = png_read_buffer(png_ptr, length+1);
1599 png_crc_finish(png_ptr, length);
1600 png_chunk_benign_error(png_ptr,
"out of memory");
1601 return handled_error;
1609 png_crc_read(png_ptr, buffer, length);
1611 if (png_crc_finish(png_ptr, skip) != 0)
1612 return handled_error;
1616 for (entry_start = buffer; *entry_start; entry_start++)
1622 if (length < 2U || entry_start > buffer + (length - 2U))
1624 png_warning(png_ptr,
"malformed sPLT chunk");
1625 return handled_error;
1628 new_palette.depth = *entry_start++;
1629 entry_size = (new_palette.depth == 8 ? 6 : 10);
1633 data_length = length - (png_uint_32)(entry_start - buffer);
1636 if ((data_length % (
unsigned int)entry_size) != 0)
1638 png_warning(png_ptr,
"sPLT chunk has bad length");
1639 return handled_error;
1642 dl = (png_uint_32)(data_length / (
unsigned int)entry_size);
1643 max_dl = PNG_SIZE_MAX / (
sizeof (png_sPLT_entry));
1647 png_warning(png_ptr,
"sPLT chunk too long");
1648 return handled_error;
1651 new_palette.nentries = (png_int_32)(data_length / (
unsigned int)entry_size);
1653 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr,
1654 (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry)));
1656 if (new_palette.entries == NULL)
1658 png_warning(png_ptr,
"sPLT chunk requires too much memory");
1659 return handled_error;
1662 for (i = 0; i < new_palette.nentries; i++)
1664 pp = new_palette.entries + i;
1666 if (new_palette.depth == 8)
1668 pp->red = *entry_start++;
1669 pp->green = *entry_start++;
1670 pp->blue = *entry_start++;
1671 pp->alpha = *entry_start++;
1676 pp->red = png_get_uint_16(entry_start); entry_start += 2;
1677 pp->green = png_get_uint_16(entry_start); entry_start += 2;
1678 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1679 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1682 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1686 new_palette.name = (png_charp)buffer;
1688 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1690 png_free(png_ptr, new_palette.entries);
1694# define png_handle_sPLT NULL
1697#ifdef PNG_READ_tRNS_SUPPORTED
1698static png_handle_result_code
1699png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1701 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1703 png_debug(1,
"in png_handle_tRNS");
1705 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1711 png_crc_finish(png_ptr, length);
1712 png_chunk_benign_error(png_ptr,
"invalid");
1713 return handled_error;
1716 png_crc_read(png_ptr, buf, 2);
1717 png_ptr->num_trans = 1;
1718 png_ptr->trans_color.gray = png_get_uint_16(buf);
1721 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1727 png_crc_finish(png_ptr, length);
1728 png_chunk_benign_error(png_ptr,
"invalid");
1729 return handled_error;
1732 png_crc_read(png_ptr, buf, length);
1733 png_ptr->num_trans = 1;
1734 png_ptr->trans_color.red = png_get_uint_16(buf);
1735 png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1736 png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1739 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1741 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1743 png_crc_finish(png_ptr, length);
1744 png_chunk_benign_error(png_ptr,
"out of place");
1745 return handled_error;
1748 if (length > (
unsigned int) png_ptr->num_palette ||
1749 length > (
unsigned int) PNG_MAX_PALETTE_LENGTH ||
1752 png_crc_finish(png_ptr, length);
1753 png_chunk_benign_error(png_ptr,
"invalid");
1754 return handled_error;
1757 png_crc_read(png_ptr, readbuf, length);
1758 png_ptr->num_trans = (png_uint_16)length;
1763 png_crc_finish(png_ptr, length);
1764 png_chunk_benign_error(png_ptr,
"invalid with alpha channel");
1765 return handled_error;
1768 if (png_crc_finish(png_ptr, 0) != 0)
1770 png_ptr->num_trans = 0;
1771 return handled_error;
1778 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1779 &(png_ptr->trans_color));
1783# define png_handle_tRNS NULL
1786#ifdef PNG_READ_bKGD_SUPPORTED
1787static png_handle_result_code
1788png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1790 unsigned int truelen;
1792 png_color_16 background;
1794 png_debug(1,
"in png_handle_bKGD");
1796 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1798 if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1800 png_crc_finish(png_ptr, length);
1801 png_chunk_benign_error(png_ptr,
"out of place");
1802 return handled_error;
1808 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1814 if (length != truelen)
1816 png_crc_finish(png_ptr, length);
1817 png_chunk_benign_error(png_ptr,
"invalid");
1818 return handled_error;
1821 png_crc_read(png_ptr, buf, truelen);
1823 if (png_crc_finish(png_ptr, 0) != 0)
1824 return handled_error;
1831 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1833 background.index = buf[0];
1835 if (info_ptr != NULL && info_ptr->num_palette != 0)
1837 if (buf[0] >= info_ptr->num_palette)
1839 png_chunk_benign_error(png_ptr,
"invalid index");
1840 return handled_error;
1843 background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1844 background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1845 background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1849 background.red = background.green = background.blue = 0;
1851 background.gray = 0;
1854 else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1856 if (png_ptr->bit_depth <= 8)
1858 if (buf[0] != 0 || buf[1] >= (
unsigned int)(1 << png_ptr->bit_depth))
1860 png_chunk_benign_error(png_ptr,
"invalid gray level");
1861 return handled_error;
1865 background.index = 0;
1869 background.gray = png_get_uint_16(buf);
1874 if (png_ptr->bit_depth <= 8)
1876 if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0)
1878 png_chunk_benign_error(png_ptr,
"invalid color");
1879 return handled_error;
1883 background.index = 0;
1884 background.red = png_get_uint_16(buf);
1885 background.green = png_get_uint_16(buf + 2);
1886 background.blue = png_get_uint_16(buf + 4);
1887 background.gray = 0;
1890 png_set_bKGD(png_ptr, info_ptr, &background);
1894# define png_handle_bKGD NULL
1897#ifdef PNG_READ_cICP_SUPPORTED
1898static png_handle_result_code
1899png_handle_cICP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1903 png_debug(1,
"in png_handle_cICP");
1905 png_crc_read(png_ptr, buf, 4);
1907 if (png_crc_finish(png_ptr, 0) != 0)
1908 return handled_error;
1910 png_set_cICP(png_ptr, info_ptr, buf[0], buf[1], buf[2], buf[3]);
1913# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1914 if (!png_has_chunk(png_ptr, mDCV))
1920#ifdef PNG_READ_GAMMA_SUPPORTED
1933# define png_handle_cICP NULL
1936#ifdef PNG_READ_cLLI_SUPPORTED
1937static png_handle_result_code
1938png_handle_cLLI(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1942 png_debug(1,
"in png_handle_cLLI");
1944 png_crc_read(png_ptr, buf, 8);
1946 if (png_crc_finish(png_ptr, 0) != 0)
1947 return handled_error;
1950 png_set_cLLI_fixed(png_ptr, info_ptr, png_get_uint_32(buf),
1951 png_get_uint_32(buf+4));
1956# define png_handle_cLLI NULL
1959#ifdef PNG_READ_mDCV_SUPPORTED
1960static png_handle_result_code
1961png_handle_mDCV(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
1966 png_debug(1,
"in png_handle_mDCV");
1968 png_crc_read(png_ptr, buf, 24);
1970 if (png_crc_finish(png_ptr, 0) != 0)
1971 return handled_error;
1984 chromaticities.redx = png_get_uint_16(buf+ 0U) << 1;
1985 chromaticities.redy = png_get_uint_16(buf+ 2U) << 1;
1986 chromaticities.greenx = png_get_uint_16(buf+ 4U) << 1;
1987 chromaticities.greeny = png_get_uint_16(buf+ 6U) << 1;
1988 chromaticities.bluex = png_get_uint_16(buf+ 8U) << 1;
1989 chromaticities.bluey = png_get_uint_16(buf+10U) << 1;
1990 chromaticities.whitex = png_get_uint_16(buf+12U) << 1;
1991 chromaticities.whitey = png_get_uint_16(buf+14U) << 1;
1993 png_set_mDCV_fixed(png_ptr, info_ptr,
1994 chromaticities.whitex, chromaticities.whitey,
1995 chromaticities.redx, chromaticities.redy,
1996 chromaticities.greenx, chromaticities.greeny,
1997 chromaticities.bluex, chromaticities.bluey,
1998 png_get_uint_32(buf+16U),
1999 png_get_uint_32(buf+20U));
2002# ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2003 png_ptr->chromaticities = chromaticities;
2010# define png_handle_mDCV NULL
2013#ifdef PNG_READ_eXIf_SUPPORTED
2014static png_handle_result_code
2015png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2017 png_bytep buffer = NULL;
2019 png_debug(1,
"in png_handle_eXIf");
2021 buffer = png_read_buffer(png_ptr, length);
2025 png_crc_finish(png_ptr, length);
2026 png_chunk_benign_error(png_ptr,
"out of memory");
2027 return handled_error;
2030 png_crc_read(png_ptr, buffer, length);
2032 if (png_crc_finish(png_ptr, 0) != 0)
2033 return handled_error;
2040 png_uint_32 header = png_get_uint_32(buffer);
2043 if (header != 0x49492A00 && header != 0x4D4D002A)
2045 png_chunk_benign_error(png_ptr,
"invalid");
2046 return handled_error;
2050 png_set_eXIf_1(png_ptr, info_ptr, length, buffer);
2054# define png_handle_eXIf NULL
2057#ifdef PNG_READ_hIST_SUPPORTED
2058static png_handle_result_code
2059png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2061 unsigned int num, i;
2062 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
2064 png_debug(1,
"in png_handle_hIST");
2072 num = (
unsigned int)length / 2 ;
2074 if (length != num * 2 ||
2075 num != (
unsigned int)png_ptr->num_palette ||
2076 num > (
unsigned int)PNG_MAX_PALETTE_LENGTH)
2078 png_crc_finish(png_ptr, length);
2079 png_chunk_benign_error(png_ptr,
"invalid");
2080 return handled_error;
2083 for (i = 0; i < num; i++)
2087 png_crc_read(png_ptr, buf, 2);
2088 readbuf[i] = png_get_uint_16(buf);
2091 if (png_crc_finish(png_ptr, 0) != 0)
2092 return handled_error;
2094 png_set_hIST(png_ptr, info_ptr, readbuf);
2098# define png_handle_hIST NULL
2101#ifdef PNG_READ_pHYs_SUPPORTED
2102static png_handle_result_code
2103png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2106 png_uint_32 res_x, res_y;
2109 png_debug(1,
"in png_handle_pHYs");
2111 png_crc_read(png_ptr, buf, 9);
2113 if (png_crc_finish(png_ptr, 0) != 0)
2114 return handled_error;
2116 res_x = png_get_uint_32(buf);
2117 res_y = png_get_uint_32(buf + 4);
2119 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2124# define png_handle_pHYs NULL
2127#ifdef PNG_READ_oFFs_SUPPORTED
2128static png_handle_result_code
2129png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2132 png_int_32 offset_x, offset_y;
2135 png_debug(1,
"in png_handle_oFFs");
2137 png_crc_read(png_ptr, buf, 9);
2139 if (png_crc_finish(png_ptr, 0) != 0)
2140 return handled_error;
2142 offset_x = png_get_int_32(buf);
2143 offset_y = png_get_int_32(buf + 4);
2145 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2150# define png_handle_oFFs NULL
2153#ifdef PNG_READ_pCAL_SUPPORTED
2155static png_handle_result_code
2156png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2159 png_byte type, nparams;
2160 png_bytep buffer, buf, units, endptr;
2164 png_debug(1,
"in png_handle_pCAL");
2165 png_debug1(2,
"Allocating and reading pCAL chunk data (%u bytes)",
2168 buffer = png_read_buffer(png_ptr, length+1);
2172 png_crc_finish(png_ptr, length);
2173 png_chunk_benign_error(png_ptr,
"out of memory");
2174 return handled_error;
2177 png_crc_read(png_ptr, buffer, length);
2179 if (png_crc_finish(png_ptr, 0) != 0)
2180 return handled_error;
2184 png_debug(3,
"Finding end of pCAL purpose string");
2185 for (buf = buffer; *buf; buf++)
2188 endptr = buffer + length;
2193 if (endptr - buf <= 12)
2195 png_chunk_benign_error(png_ptr,
"invalid");
2196 return handled_error;
2199 png_debug(3,
"Reading pCAL X0, X1, type, nparams, and units");
2200 X0 = png_get_int_32((png_bytep)buf+1);
2201 X1 = png_get_int_32((png_bytep)buf+5);
2206 png_debug(3,
"Checking pCAL equation type and number of parameters");
2210 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2211 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2212 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2213 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2215 png_chunk_benign_error(png_ptr,
"invalid parameter count");
2216 return handled_error;
2219 else if (type >= PNG_EQUATION_LAST)
2221 png_chunk_benign_error(png_ptr,
"unrecognized equation type");
2224 for (buf = units; *buf; buf++)
2227 png_debug(3,
"Allocating pCAL parameters array");
2229 params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2230 nparams * (
sizeof (png_charp))));
2234 png_chunk_benign_error(png_ptr,
"out of memory");
2235 return handled_error;
2239 for (i = 0; i < nparams; i++)
2243 png_debug1(3,
"Reading pCAL parameter %d", i);
2245 for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2251 png_free(png_ptr, params);
2252 png_chunk_benign_error(png_ptr,
"invalid data");
2253 return handled_error;
2257 png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2258 (png_charp)units, params);
2266 png_free(png_ptr, params);
2270# define png_handle_pCAL NULL
2273#ifdef PNG_READ_sCAL_SUPPORTED
2275static png_handle_result_code
2276png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2282 png_debug(1,
"in png_handle_sCAL");
2283 png_debug1(2,
"Allocating and reading sCAL chunk data (%u bytes)",
2286 buffer = png_read_buffer(png_ptr, length+1);
2290 png_crc_finish(png_ptr, length);
2291 png_chunk_benign_error(png_ptr,
"out of memory");
2292 return handled_error;
2295 png_crc_read(png_ptr, buffer, length);
2298 if (png_crc_finish(png_ptr, 0) != 0)
2299 return handled_error;
2302 if (buffer[0] != 1 && buffer[0] != 2)
2304 png_chunk_benign_error(png_ptr,
"invalid unit");
2305 return handled_error;
2314 if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2315 i >= length || buffer[i++] != 0)
2316 png_chunk_benign_error(png_ptr,
"bad width format");
2318 else if (PNG_FP_IS_POSITIVE(state) == 0)
2319 png_chunk_benign_error(png_ptr,
"non-positive width");
2326 if (png_check_fp_number((png_const_charp)buffer, length,
2327 &state, &i) == 0 || i != length)
2328 png_chunk_benign_error(png_ptr,
"bad height format");
2330 else if (PNG_FP_IS_POSITIVE(state) == 0)
2331 png_chunk_benign_error(png_ptr,
"non-positive height");
2336 png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2337 (png_charp)buffer+1, (png_charp)buffer+heighti);
2342 return handled_error;
2345# define png_handle_sCAL NULL
2348#ifdef PNG_READ_tIME_SUPPORTED
2349static png_handle_result_code
2350png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2355 png_debug(1,
"in png_handle_tIME");
2361 if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2362 png_ptr->mode |= PNG_AFTER_IDAT;
2364 png_crc_read(png_ptr, buf, 7);
2366 if (png_crc_finish(png_ptr, 0) != 0)
2367 return handled_error;
2369 mod_time.second = buf[6];
2370 mod_time.minute = buf[5];
2371 mod_time.hour = buf[4];
2372 mod_time.day = buf[3];
2373 mod_time.month = buf[2];
2374 mod_time.year = png_get_uint_16(buf);
2376 png_set_tIME(png_ptr, info_ptr, &mod_time);
2381# define png_handle_tIME NULL
2384#ifdef PNG_READ_tEXt_SUPPORTED
2386static png_handle_result_code
2387png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2393 png_uint_32 skip = 0;
2395 png_debug(1,
"in png_handle_tEXt");
2397#ifdef PNG_USER_LIMITS_SUPPORTED
2398 if (png_ptr->user_chunk_cache_max != 0)
2400 if (png_ptr->user_chunk_cache_max == 1)
2402 png_crc_finish(png_ptr, length);
2403 return handled_error;
2406 if (--png_ptr->user_chunk_cache_max == 1)
2408 png_crc_finish(png_ptr, length);
2409 png_chunk_benign_error(png_ptr,
"no space in chunk cache");
2410 return handled_error;
2415 buffer = png_read_buffer(png_ptr, length+1);
2419 png_crc_finish(png_ptr, length);
2420 png_chunk_benign_error(png_ptr,
"out of memory");
2421 return handled_error;
2424 png_crc_read(png_ptr, buffer, length);
2426 if (png_crc_finish(png_ptr, skip) != 0)
2427 return handled_error;
2429 key = (png_charp)buffer;
2432 for (text = key; *text; text++)
2435 if (text != key + length)
2438 text_info.compression = PNG_TEXT_COMPRESSION_NONE;
2439 text_info.key = key;
2440 text_info.lang = NULL;
2441 text_info.lang_key = NULL;
2442 text_info.itxt_length = 0;
2443 text_info.text = text;
2444 text_info.text_length = strlen(text);
2446 if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) == 0)
2449 png_chunk_benign_error(png_ptr,
"out of memory");
2450 return handled_error;
2453# define png_handle_tEXt NULL
2456#ifdef PNG_READ_zTXt_SUPPORTED
2458static png_handle_result_code
2459png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2461 png_const_charp errmsg = NULL;
2463 png_uint_32 keyword_length;
2465 png_debug(1,
"in png_handle_zTXt");
2467#ifdef PNG_USER_LIMITS_SUPPORTED
2468 if (png_ptr->user_chunk_cache_max != 0)
2470 if (png_ptr->user_chunk_cache_max == 1)
2472 png_crc_finish(png_ptr, length);
2473 return handled_error;
2476 if (--png_ptr->user_chunk_cache_max == 1)
2478 png_crc_finish(png_ptr, length);
2479 png_chunk_benign_error(png_ptr,
"no space in chunk cache");
2480 return handled_error;
2489 buffer = png_read_buffer(png_ptr, length);
2493 png_crc_finish(png_ptr, length);
2494 png_chunk_benign_error(png_ptr,
"out of memory");
2495 return handled_error;
2498 png_crc_read(png_ptr, buffer, length);
2500 if (png_crc_finish(png_ptr, 0) != 0)
2501 return handled_error;
2504 for (keyword_length = 0;
2505 keyword_length < length && buffer[keyword_length] != 0;
2509 if (keyword_length > 79 || keyword_length < 1)
2510 errmsg =
"bad keyword";
2516 else if (keyword_length + 3 > length)
2517 errmsg =
"truncated";
2519 else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2520 errmsg =
"unknown compression type";
2524 png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2530 if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2531 &uncompressed_length, 1) == Z_STREAM_END)
2535 if (png_ptr->read_buffer == NULL)
2536 errmsg=
"Read failure in png_handle_zTXt";
2543 buffer = png_ptr->read_buffer;
2544 buffer[uncompressed_length+(keyword_length+2)] = 0;
2546 text.compression = PNG_TEXT_COMPRESSION_zTXt;
2547 text.key = (png_charp)buffer;
2548 text.text = (png_charp)(buffer + keyword_length+2);
2549 text.text_length = uncompressed_length;
2550 text.itxt_length = 0;
2552 text.lang_key = NULL;
2554 if (png_set_text_2(png_ptr, info_ptr, &text, 1) == 0)
2557 errmsg =
"out of memory";
2562 errmsg = png_ptr->zstream.msg;
2565 png_chunk_benign_error(png_ptr, errmsg);
2566 return handled_error;
2569# define png_handle_zTXt NULL
2572#ifdef PNG_READ_iTXt_SUPPORTED
2574static png_handle_result_code
2575png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
2577 png_const_charp errmsg = NULL;
2579 png_uint_32 prefix_length;
2581 png_debug(1,
"in png_handle_iTXt");
2583#ifdef PNG_USER_LIMITS_SUPPORTED
2584 if (png_ptr->user_chunk_cache_max != 0)
2586 if (png_ptr->user_chunk_cache_max == 1)
2588 png_crc_finish(png_ptr, length);
2589 return handled_error;
2592 if (--png_ptr->user_chunk_cache_max == 1)
2594 png_crc_finish(png_ptr, length);
2595 png_chunk_benign_error(png_ptr,
"no space in chunk cache");
2596 return handled_error;
2601 buffer = png_read_buffer(png_ptr, length+1);
2605 png_crc_finish(png_ptr, length);
2606 png_chunk_benign_error(png_ptr,
"out of memory");
2607 return handled_error;
2610 png_crc_read(png_ptr, buffer, length);
2612 if (png_crc_finish(png_ptr, 0) != 0)
2613 return handled_error;
2616 for (prefix_length=0;
2617 prefix_length < length && buffer[prefix_length] != 0;
2622 if (prefix_length > 79 || prefix_length < 1)
2623 errmsg =
"bad keyword";
2629 else if (prefix_length + 5 > length)
2630 errmsg =
"truncated";
2632 else if (buffer[prefix_length+1] == 0 ||
2633 (buffer[prefix_length+1] == 1 &&
2634 buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2636 int compressed = buffer[prefix_length+1] != 0;
2637 png_uint_32 language_offset, translated_keyword_offset;
2638 png_alloc_size_t uncompressed_length = 0;
2642 language_offset = prefix_length;
2644 for (; prefix_length < length && buffer[prefix_length] != 0;
2649 translated_keyword_offset = ++prefix_length;
2651 for (; prefix_length < length && buffer[prefix_length] != 0;
2662 if (compressed == 0 && prefix_length <= length)
2663 uncompressed_length = length - prefix_length;
2665 else if (compressed != 0 && prefix_length < length)
2667 uncompressed_length = PNG_SIZE_MAX;
2673 if (png_decompress_chunk(png_ptr, length, prefix_length,
2674 &uncompressed_length, 1) == Z_STREAM_END)
2675 buffer = png_ptr->read_buffer;
2678 errmsg = png_ptr->zstream.msg;
2682 errmsg =
"truncated";
2688 buffer[uncompressed_length+prefix_length] = 0;
2690 if (compressed == 0)
2691 text.compression = PNG_ITXT_COMPRESSION_NONE;
2694 text.compression = PNG_ITXT_COMPRESSION_zTXt;
2696 text.key = (png_charp)buffer;
2697 text.lang = (png_charp)buffer + language_offset;
2698 text.lang_key = (png_charp)buffer + translated_keyword_offset;
2699 text.text = (png_charp)buffer + prefix_length;
2700 text.text_length = 0;
2701 text.itxt_length = uncompressed_length;
2703 if (png_set_text_2(png_ptr, info_ptr, &text, 1) == 0)
2706 errmsg =
"out of memory";
2711 errmsg =
"bad compression info";
2714 png_chunk_benign_error(png_ptr, errmsg);
2715 return handled_error;
2718# define png_handle_iTXt NULL
2721#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2724png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
2726 const png_alloc_size_t limit = png_chunk_max(png_ptr);
2728 if (png_ptr->unknown_chunk.data != NULL)
2730 png_free(png_ptr, png_ptr->unknown_chunk.data);
2731 png_ptr->unknown_chunk.data = NULL;
2734 if (length <= limit)
2736 PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name);
2738 png_ptr->unknown_chunk.size = (size_t)length;
2740 png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode;
2743 png_ptr->unknown_chunk.data = NULL;
2748 png_ptr->unknown_chunk.data = png_voidcast(png_bytep,
2749 png_malloc_warn(png_ptr, length));
2753 if (png_ptr->unknown_chunk.data == NULL && length > 0)
2756 png_crc_finish(png_ptr, length);
2757 png_chunk_benign_error(png_ptr,
"unknown chunk exceeds memory limits");
2764 png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length);
2765 png_crc_finish(png_ptr, 0);
2772png_handle_result_code
2773png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr,
2774 png_uint_32 length,
int keep)
2776 png_handle_result_code handled = handled_discarded;
2778 png_debug(1,
"in png_handle_unknown");
2780#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2792# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2793# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2794 keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2802# ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2806 if (png_ptr->read_user_chunk_fn != NULL)
2808 if (png_cache_unknown_chunk(png_ptr, length) != 0)
2811 int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2812 &png_ptr->unknown_chunk);
2824 png_chunk_error(png_ptr,
"error in user chunk");
2839 if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2841# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2842 if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2844 png_chunk_warning(png_ptr,
"Saving unknown chunk:");
2845 png_app_warning(png_ptr,
2846 "forcing save of an unhandled chunk;"
2847 " please call png_set_keep_unknown_chunks");
2851 keep = PNG_HANDLE_CHUNK_IF_SAFE;
2857 handled = handled_ok;
2859 keep = PNG_HANDLE_CHUNK_NEVER;
2864 keep = PNG_HANDLE_CHUNK_NEVER;
2871# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2878 if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2879 keep = png_ptr->unknown_default;
2881 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2882 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2883 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2885 if (png_cache_unknown_chunk(png_ptr, length) == 0)
2886 keep = PNG_HANDLE_CHUNK_NEVER;
2890 png_crc_finish(png_ptr, length);
2893# ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2894# error no method to support READ_UNKNOWN_CHUNKS
2904 if (keep > PNG_HANDLE_CHUNK_NEVER)
2905 png_app_error(png_ptr,
"no unknown chunk support available");
2907 png_crc_finish(png_ptr, length);
2911# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2915 if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2916 (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2917 PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2919# ifdef PNG_USER_LIMITS_SUPPORTED
2920 switch (png_ptr->user_chunk_cache_max)
2923 png_ptr->user_chunk_cache_max = 1;
2924 png_chunk_benign_error(png_ptr,
"no space in chunk cache");
2933 --(png_ptr->user_chunk_cache_max);
2940 png_set_unknown_chunks(png_ptr, info_ptr,
2941 &png_ptr->unknown_chunk, 1);
2942 handled = handled_saved;
2943# ifdef PNG_USER_LIMITS_SUPPORTED
2949 PNG_UNUSED(info_ptr)
2956 if (png_ptr->unknown_chunk.data != NULL)
2957 png_free(png_ptr, png_ptr->unknown_chunk.data);
2958 png_ptr->unknown_chunk.data = NULL;
2962 png_crc_finish(png_ptr, length);
2963 PNG_UNUSED(info_ptr)
2968 if (handled < handled_saved && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2969 png_chunk_error(png_ptr,
"unhandled critical chunk");
2983#define png_handle_acTL NULL
2984#define png_handle_fcTL NULL
2985#define png_handle_fdAT NULL
3000 png_handle_result_code (*handler)(
3001 png_structrp, png_inforp, png_uint_32 length);
3011 png_uint_32 max_length :12;
3012 png_uint_32 min_length :8;
3017 png_uint_32 pos_before :4;
3018 png_uint_32 pos_after :4;
3022 png_uint_32 multiple :1;
3025read_chunks[PNG_INDEX_unknown] =
3034# define NoCheck 0x801U
3035# define Limit 0x802U
3036# define LKMin 3U+LZ77Min
3038#define hIHDR PNG_HAVE_IHDR
3039#define hPLTE PNG_HAVE_PLTE
3040#define hIDAT PNG_HAVE_IDAT
3047#define hCOL (PNG_HAVE_PLTE|PNG_HAVE_IDAT)
3049#define aIDAT PNG_AFTER_IDAT
3053# define CDIHDR 13U, 13U, hIHDR, 0, 0
3054# define CDPLTE NoCheck, 0U, 0, hIHDR, 1
3058# define CDIDAT NoCheck, 0U, aIDAT, hIHDR, 1
3059# define CDIEND NoCheck, 0U, 0, aIDAT, 0
3061# define CDtRNS 256U, 0U, hIDAT, hIHDR, 0
3062# define CDcHRM 32U, 32U, hCOL, hIHDR, 0
3063# define CDgAMA 4U, 4U, hCOL, hIHDR, 0
3064# define CDiCCP NoCheck, LKMin, hCOL, hIHDR, 0
3065# define CDsBIT 4U, 1U, hCOL, hIHDR, 0
3066# define CDsRGB 1U, 1U, hCOL, hIHDR, 0
3067# define CDcICP 4U, 4U, hCOL, hIHDR, 0
3068# define CDmDCV 24U, 24U, hCOL, hIHDR, 0
3069# define CDeXIf Limit, 4U, 0, hIHDR, 0
3070# define CDcLLI 8U, 8U, hCOL, hIHDR, 0
3071# define CDtEXt NoCheck, 2U, 0, hIHDR, 1
3073# define CDzTXt Limit, LKMin, 0, hIHDR, 1
3074# define CDiTXt NoCheck, 6U, 0, hIHDR, 1
3076# define CDbKGD 6U, 1U, hIDAT, hIHDR, 0
3077# define CDhIST 1024U, 0U, hPLTE, hIHDR, 0
3078# define CDpHYs 9U, 9U, hIDAT, hIHDR, 0
3079# define CDsPLT NoCheck, 3U, hIDAT, hIHDR, 1
3081# define CDtIME 7U, 7U, 0, hIHDR, 0
3082# define CDacTL 8U, 8U, hIDAT, hIHDR, 0
3083# define CDfcTL 25U, 26U, 0, hIHDR, 1
3084# define CDfdAT Limit, 4U, hIDAT, hIHDR, 1
3086# define CDoFFs 9U, 9U, hIDAT, hIHDR, 0
3087# define CDpCAL NoCheck, 14U, hIDAT, hIHDR, 0
3089# define CDsCAL Limit, 4U, hIDAT, hIHDR, 0
3092# define PNG_CHUNK(cHNK, index) { png_handle_ ## cHNK, CD ## cHNK },
3099png_chunk_index_from_name(png_uint_32 chunk_name)
3107# define PNG_CHUNK(cHNK, index)\
3108 case png_ ## cHNK: return PNG_INDEX_ ## cHNK;
3114 default:
return PNG_INDEX_unknown;
3118png_handle_result_code
3119png_handle_chunk(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
3124 const png_uint_32 chunk_name = png_ptr->chunk_name;
3125 const png_index chunk_index = png_chunk_index_from_name(chunk_name);
3127 png_handle_result_code handled = handled_error;
3128 png_const_charp errmsg = NULL;
3135 if (chunk_index == PNG_INDEX_unknown ||
3136 read_chunks[chunk_index].handler == NULL)
3138 handled = png_handle_unknown(
3139 png_ptr, info_ptr, length, PNG_HANDLE_CHUNK_AS_DEFAULT);
3145 else if (chunk_index != PNG_INDEX_IHDR &&
3146 (png_ptr->mode & PNG_HAVE_IHDR) == 0)
3147 png_chunk_error(png_ptr,
"missing IHDR");
3150 else if (((png_ptr->mode & read_chunks[chunk_index].pos_before) != 0) ||
3151 ((png_ptr->mode & read_chunks[chunk_index].pos_after) !=
3152 read_chunks[chunk_index].pos_after))
3154 errmsg =
"out of place";
3160 else if (read_chunks[chunk_index].multiple == 0 &&
3161 png_file_has_chunk(png_ptr, chunk_index))
3163 errmsg =
"duplicate";
3166 else if (length < read_chunks[chunk_index].min_length)
3167 errmsg =
"too short";
3177 unsigned max_length = read_chunks[chunk_index].max_length;
3186 if (length <= png_chunk_max(png_ptr))
3189 errmsg =
"length exceeds libpng limit";
3193 if (length <= max_length)
3196 errmsg =
"too long";
3201 handled = read_chunks[chunk_index].handler(
3202 png_ptr, info_ptr, length);
3212 if (PNG_CHUNK_CRITICAL(chunk_name))
3213 png_chunk_error(png_ptr, errmsg);
3217 png_crc_finish(png_ptr, length);
3218 png_chunk_benign_error(png_ptr, errmsg);
3222 else if (handled >= handled_saved)
3224 if (chunk_index != PNG_INDEX_unknown)
3225 png_file_add_chunk(png_ptr, chunk_index);
3239png_combine_row(png_const_structrp png_ptr, png_bytep dp,
int display)
3241 unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3242 png_const_bytep sp = png_ptr->row_buf + 1;
3243 png_alloc_size_t row_width = png_ptr->width;
3244 unsigned int pass = png_ptr->pass;
3245 png_bytep end_ptr = 0;
3246 png_byte end_byte = 0;
3247 unsigned int end_mask;
3249 png_debug(1,
"in png_combine_row");
3254 if (pixel_depth == 0)
3255 png_error(png_ptr,
"internal row logic error");
3261 if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3262 PNG_ROWBYTES(pixel_depth, row_width))
3263 png_error(png_ptr,
"internal row size calculation error");
3267 png_error(png_ptr,
"internal row width error");
3273 end_mask = (pixel_depth * row_width) & 7;
3277 end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3278 end_byte = *end_ptr;
3279# ifdef PNG_READ_PACKSWAP_SUPPORTED
3280 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3282 end_mask = (
unsigned int)(0xff << end_mask);
3286 end_mask = 0xff >> end_mask;
3296#ifdef PNG_READ_INTERLACING_SUPPORTED
3297 if (png_ptr->interlaced != 0 &&
3298 (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3299 pass < 6 && (display == 0 ||
3301 (display == 1 && (pass & 1) != 0)))
3306 if (row_width <= PNG_PASS_START_COL(pass))
3309 if (pixel_depth < 8)
3348# if PNG_USE_COMPILE_TIME_MASKS
3349# define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3350# define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3352# define PNG_LSR(x,s) ((x)>>(s))
3353# define PNG_LSL(x,s) ((x)<<(s))
3355# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3356 PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3357# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3358 PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3366# define PIXEL_MASK(p,x,d,s) \
3367 (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3371# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3372# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3378# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3380# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3381 S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3382 S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3384# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3385 B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3386 B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3388#if PNG_USE_COMPILE_TIME_MASKS
3394# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3395 S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3397# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3399# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3404 static const png_uint_32 row_mask[2][3][6] =
3407 { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3409 { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3415 static const png_uint_32 display_mask[2][3][3] =
3418 { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3420 { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3423# define MASK(pass,depth,display,png)\
3424 ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3425 row_mask[png][DEPTH_INDEX(depth)][pass])
3431# define MASK(pass,depth,display,png)\
3432 ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3440 png_uint_32 pixels_per_byte = 8 / pixel_depth;
3443# ifdef PNG_READ_PACKSWAP_SUPPORTED
3444 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3445 mask = MASK(pass, pixel_depth, display, 0);
3449 mask = MASK(pass, pixel_depth, display, 1);
3460 mask = (m >> 8) | (m << 24);
3466 *dp = (png_byte)((*dp & ~m) | (*sp & m));
3475 if (row_width <= pixels_per_byte)
3478 row_width -= pixels_per_byte;
3486 unsigned int bytes_to_copy, bytes_to_jump;
3489 if (pixel_depth & 7)
3490 png_error(png_ptr,
"invalid user transform pixel depth");
3493 row_width *= pixel_depth;
3500 unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3502 row_width -= offset;
3514 bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3517 if (bytes_to_copy > row_width)
3518 bytes_to_copy = (
unsigned int)row_width;
3522 bytes_to_copy = pixel_depth;
3525 bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3535 switch (bytes_to_copy)
3542 if (row_width <= bytes_to_jump)
3545 dp += bytes_to_jump;
3546 sp += bytes_to_jump;
3547 row_width -= bytes_to_jump;
3556 dp[0] = sp[0]; dp[1] = sp[1];
3558 if (row_width <= bytes_to_jump)
3561 sp += bytes_to_jump;
3562 dp += bytes_to_jump;
3563 row_width -= bytes_to_jump;
3565 while (row_width > 1);
3577 dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2];
3579 if (row_width <= bytes_to_jump)
3582 sp += bytes_to_jump;
3583 dp += bytes_to_jump;
3584 row_width -= bytes_to_jump;
3588#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3594 if (bytes_to_copy < 16 &&
3595 png_isaligned(dp, png_uint_16) &&
3596 png_isaligned(sp, png_uint_16) &&
3597 bytes_to_copy % (
sizeof (png_uint_16)) == 0 &&
3598 bytes_to_jump % (
sizeof (png_uint_16)) == 0)
3603 if (png_isaligned(dp, png_uint_32) &&
3604 png_isaligned(sp, png_uint_32) &&
3605 bytes_to_copy % (
sizeof (png_uint_32)) == 0 &&
3606 bytes_to_jump % (
sizeof (png_uint_32)) == 0)
3608 png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
3609 png_const_uint_32p sp32 = png_aligncastconst(
3610 png_const_uint_32p, sp);
3611 size_t skip = (bytes_to_jump-bytes_to_copy) /
3612 (
sizeof (png_uint_32));
3616 size_t c = bytes_to_copy;
3620 c -= (
sizeof (png_uint_32));
3624 if (row_width <= bytes_to_jump)
3629 row_width -= bytes_to_jump;
3631 while (bytes_to_copy <= row_width);
3637 dp = (png_bytep)dp32;
3638 sp = (png_const_bytep)sp32;
3641 while (--row_width > 0);
3650 png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
3651 png_const_uint_16p sp16 = png_aligncastconst(
3652 png_const_uint_16p, sp);
3653 size_t skip = (bytes_to_jump-bytes_to_copy) /
3654 (
sizeof (png_uint_16));
3658 size_t c = bytes_to_copy;
3662 c -= (
sizeof (png_uint_16));
3666 if (row_width <= bytes_to_jump)
3671 row_width -= bytes_to_jump;
3673 while (bytes_to_copy <= row_width);
3676 dp = (png_bytep)dp16;
3677 sp = (png_const_bytep)sp16;
3680 while (--row_width > 0);
3689 memcpy(dp, sp, bytes_to_copy);
3691 if (row_width <= bytes_to_jump)
3694 sp += bytes_to_jump;
3695 dp += bytes_to_jump;
3696 row_width -= bytes_to_jump;
3697 if (bytes_to_copy > row_width)
3698 bytes_to_copy = (
unsigned int)row_width;
3714 memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3717 if (end_ptr != NULL)
3718 *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3721#ifdef PNG_READ_INTERLACING_SUPPORTED
3723png_do_read_interlace(png_row_infop row_info, png_bytep row,
int pass,
3724 png_uint_32 transformations )
3726 png_debug(1,
"in png_do_read_interlace");
3727 if (row != NULL && row_info != NULL)
3729 png_uint_32 final_width;
3731 final_width = row_info->width * png_pass_inc[pass];
3733 switch (row_info->pixel_depth)
3737 png_bytep sp = row + (size_t)((row_info->width - 1) >> 3);
3738 png_bytep dp = row + (size_t)((final_width - 1) >> 3);
3739 unsigned int sshift, dshift;
3740 unsigned int s_start, s_end;
3742 int jstop = (int)png_pass_inc[pass];
3747#ifdef PNG_READ_PACKSWAP_SUPPORTED
3748 if ((transformations & PNG_PACKSWAP) != 0)
3750 sshift = ((row_info->width + 7) & 0x07);
3751 dshift = ((final_width + 7) & 0x07);
3760 sshift = 7 - ((row_info->width + 7) & 0x07);
3761 dshift = 7 - ((final_width + 7) & 0x07);
3767 for (i = 0; i < row_info->width; i++)
3769 v = (png_byte)((*sp >> sshift) & 0x01);
3770 for (j = 0; j < jstop; j++)
3772 unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3773 tmp |= (
unsigned int)(v << dshift);
3774 *dp = (png_byte)(tmp & 0xff);
3776 if (dshift == s_end)
3783 dshift = (
unsigned int)((
int)dshift + s_inc);
3786 if (sshift == s_end)
3793 sshift = (
unsigned int)((
int)sshift + s_inc);
3800 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3801 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3802 unsigned int sshift, dshift;
3803 unsigned int s_start, s_end;
3805 int jstop = (int)png_pass_inc[pass];
3808#ifdef PNG_READ_PACKSWAP_SUPPORTED
3809 if ((transformations & PNG_PACKSWAP) != 0)
3811 sshift = (((row_info->width + 3) & 0x03) << 1);
3812 dshift = (((final_width + 3) & 0x03) << 1);
3821 sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1);
3822 dshift = ((3 - ((final_width + 3) & 0x03)) << 1);
3828 for (i = 0; i < row_info->width; i++)
3833 v = (png_byte)((*sp >> sshift) & 0x03);
3834 for (j = 0; j < jstop; j++)
3836 unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3837 tmp |= (
unsigned int)(v << dshift);
3838 *dp = (png_byte)(tmp & 0xff);
3840 if (dshift == s_end)
3847 dshift = (
unsigned int)((
int)dshift + s_inc);
3850 if (sshift == s_end)
3857 sshift = (
unsigned int)((
int)sshift + s_inc);
3864 png_bytep sp = row + (size_t)((row_info->width - 1) >> 1);
3865 png_bytep dp = row + (size_t)((final_width - 1) >> 1);
3866 unsigned int sshift, dshift;
3867 unsigned int s_start, s_end;
3870 int jstop = (int)png_pass_inc[pass];
3872#ifdef PNG_READ_PACKSWAP_SUPPORTED
3873 if ((transformations & PNG_PACKSWAP) != 0)
3875 sshift = (((row_info->width + 1) & 0x01) << 2);
3876 dshift = (((final_width + 1) & 0x01) << 2);
3885 sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2);
3886 dshift = ((1 - ((final_width + 1) & 0x01)) << 2);
3892 for (i = 0; i < row_info->width; i++)
3894 png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
3897 for (j = 0; j < jstop; j++)
3899 unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3900 tmp |= (
unsigned int)(v << dshift);
3901 *dp = (png_byte)(tmp & 0xff);
3903 if (dshift == s_end)
3910 dshift = (
unsigned int)((
int)dshift + s_inc);
3913 if (sshift == s_end)
3920 sshift = (
unsigned int)((
int)sshift + s_inc);
3927 size_t pixel_bytes = (row_info->pixel_depth >> 3);
3929 png_bytep sp = row + (size_t)(row_info->width - 1)
3932 png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes;
3934 int jstop = (int)png_pass_inc[pass];
3937 for (i = 0; i < row_info->width; i++)
3942 memcpy(v, sp, pixel_bytes);
3944 for (j = 0; j < jstop; j++)
3946 memcpy(dp, v, pixel_bytes);
3956 row_info->width = final_width;
3957 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3959#ifndef PNG_READ_PACKSWAP_SUPPORTED
3960 PNG_UNUSED(transformations)
3966png_read_filter_row_sub(png_row_infop row_info, png_bytep row,
3967 png_const_bytep prev_row)
3970 size_t istop = row_info->rowbytes;
3971 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
3972 png_bytep rp = row + bpp;
3974 PNG_UNUSED(prev_row)
3976 for (i = bpp; i < istop; i++)
3978 *rp = (png_byte)(((
int)(*rp) + (
int)(*(rp-bpp))) & 0xff);
3984png_read_filter_row_up(png_row_infop row_info, png_bytep row,
3985 png_const_bytep prev_row)
3988 size_t istop = row_info->rowbytes;
3990 png_const_bytep pp = prev_row;
3992 for (i = 0; i < istop; i++)
3994 *rp = (png_byte)(((
int)(*rp) + (
int)(*pp++)) & 0xff);
4000png_read_filter_row_avg(png_row_infop row_info, png_bytep row,
4001 png_const_bytep prev_row)
4005 png_const_bytep pp = prev_row;
4006 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4007 size_t istop = row_info->rowbytes - bpp;
4009 for (i = 0; i < bpp; i++)
4011 *rp = (png_byte)(((
int)(*rp) +
4012 ((
int)(*pp++) / 2 )) & 0xff);
4017 for (i = 0; i < istop; i++)
4019 *rp = (png_byte)(((
int)(*rp) +
4020 (
int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff);
4027png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row,
4028 png_const_bytep prev_row)
4030 png_bytep rp_end = row + row_info->rowbytes;
4036 *row++ = (png_byte)a;
4039 while (row < rp_end)
4041 int b, pa, pb, pc, p;
4054 pa = p < 0 ? -p : p;
4055 pb = pc < 0 ? -pc : pc;
4056 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4073 *row++ = (png_byte)a;
4078png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row,
4079 png_const_bytep prev_row)
4081 unsigned int bpp = (row_info->pixel_depth + 7) >> 3;
4082 png_bytep rp_end = row + bpp;
4087 while (row < rp_end)
4089 int a = *row + *prev_row++;
4090 *row++ = (png_byte)a;
4094 rp_end = rp_end + (row_info->rowbytes - bpp);
4096 while (row < rp_end)
4098 int a, b, c, pa, pb, pc, p;
4100 c = *(prev_row - bpp);
4112 pa = p < 0 ? -p : p;
4113 pb = pc < 0 ? -pc : pc;
4114 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
4124 *row++ = (png_byte)a;
4129png_init_filter_functions(png_structrp pp)
4140 unsigned int bpp = (pp->pixel_depth + 7) >> 3;
4142 pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub;
4143 pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up;
4144 pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg;
4146 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4147 png_read_filter_row_paeth_1byte_pixel;
4149 pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
4150 png_read_filter_row_paeth_multibyte_pixel;
4152#ifdef PNG_FILTER_OPTIMIZATIONS
4161 PNG_FILTER_OPTIMIZATIONS(pp, bpp);
4166png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row,
4167 png_const_bytep prev_row,
int filter)
4173 if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
4175 if (pp->read_filter[0] == NULL)
4176 png_init_filter_functions(pp);
4178 pp->read_filter[filter-1](row_info, row, prev_row);
4182#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
4184png_read_IDAT_data(png_structrp png_ptr, png_bytep output,
4185 png_alloc_size_t avail_out)
4188 png_ptr->zstream.next_out = output;
4189 png_ptr->zstream.avail_out = 0;
4197 png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
4199 if (png_ptr->zstream.avail_in == 0)
4204 while (png_ptr->idat_size == 0)
4206 png_crc_finish(png_ptr, 0);
4208 png_ptr->idat_size = png_read_chunk_header(png_ptr);
4212 if (png_ptr->chunk_name != png_IDAT)
4213 png_error(png_ptr,
"Not enough image data");
4216 avail_in = png_ptr->IDAT_read_size;
4218 if (avail_in > png_chunk_max(png_ptr))
4219 avail_in = (uInt)png_chunk_max(png_ptr);
4221 if (avail_in > png_ptr->idat_size)
4222 avail_in = (uInt)png_ptr->idat_size;
4231 buffer = png_read_buffer(png_ptr, avail_in);
4234 png_chunk_error(png_ptr,
"out of memory");
4236 png_crc_read(png_ptr, buffer, avail_in);
4237 png_ptr->idat_size -= avail_in;
4239 png_ptr->zstream.next_in = buffer;
4240 png_ptr->zstream.avail_in = avail_in;
4246 uInt out = ZLIB_IO_MAX;
4248 if (out > avail_out)
4249 out = (uInt)avail_out;
4252 png_ptr->zstream.avail_out = out;
4257 png_ptr->zstream.next_out = tmpbuf;
4258 png_ptr->zstream.avail_out = (
sizeof tmpbuf);
4268 ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH);
4272 avail_out += png_ptr->zstream.avail_out;
4275 avail_out += (
sizeof tmpbuf) - png_ptr->zstream.avail_out;
4277 png_ptr->zstream.avail_out = 0;
4279 if (ret == Z_STREAM_END)
4282 png_ptr->zstream.next_out = NULL;
4284 png_ptr->mode |= PNG_AFTER_IDAT;
4285 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4287 if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4288 png_chunk_benign_error(png_ptr,
"Extra compressed data");
4294 png_zstream_error(png_ptr, ret);
4297 png_chunk_error(png_ptr, png_ptr->zstream.msg);
4301 png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4305 }
while (avail_out > 0);
4313 png_error(png_ptr,
"Not enough image data");
4316 png_chunk_benign_error(png_ptr,
"Too much image data");
4321png_read_finish_IDAT(png_structrp png_ptr)
4328 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4334 png_read_IDAT_data(png_ptr, NULL, 0);
4335 png_ptr->zstream.next_out = NULL;
4340 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4342 png_ptr->mode |= PNG_AFTER_IDAT;
4343 png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4350 if (png_ptr->zowner == png_IDAT)
4353 png_ptr->zstream.next_in = NULL;
4354 png_ptr->zstream.avail_in = 0;
4357 png_ptr->zowner = 0;
4364 (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4369png_read_finish_row(png_structrp png_ptr)
4371 png_debug(1,
"in png_read_finish_row");
4372 png_ptr->row_number++;
4373 if (png_ptr->row_number < png_ptr->num_rows)
4376 if (png_ptr->interlaced != 0)
4378 png_ptr->row_number = 0;
4383 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4389 if (png_ptr->pass >= 7)
4392 png_ptr->iwidth = (png_ptr->width +
4393 png_pass_inc[png_ptr->pass] - 1 -
4394 png_pass_start[png_ptr->pass]) /
4395 png_pass_inc[png_ptr->pass];
4397 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4399 png_ptr->num_rows = (png_ptr->height +
4400 png_pass_yinc[png_ptr->pass] - 1 -
4401 png_pass_ystart[png_ptr->pass]) /
4402 png_pass_yinc[png_ptr->pass];
4408 }
while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4410 if (png_ptr->pass < 7)
4415 png_read_finish_IDAT(png_ptr);
4420png_read_start_row(png_structrp png_ptr)
4422 unsigned int max_pixel_depth;
4425 png_debug(1,
"in png_read_start_row");
4427#ifdef PNG_READ_TRANSFORMS_SUPPORTED
4428 png_init_read_transformations(png_ptr);
4430 if (png_ptr->interlaced != 0)
4432 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4433 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4434 png_pass_ystart[0]) / png_pass_yinc[0];
4437 png_ptr->num_rows = png_ptr->height;
4439 png_ptr->iwidth = (png_ptr->width +
4440 png_pass_inc[png_ptr->pass] - 1 -
4441 png_pass_start[png_ptr->pass]) /
4442 png_pass_inc[png_ptr->pass];
4447 png_ptr->num_rows = png_ptr->height;
4448 png_ptr->iwidth = png_ptr->width;
4451 max_pixel_depth = (
unsigned int)png_ptr->pixel_depth;
4463#ifdef PNG_READ_PACK_SUPPORTED
4464 if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4465 max_pixel_depth = 8;
4468#ifdef PNG_READ_EXPAND_SUPPORTED
4469 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4471 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4473 if (png_ptr->num_trans != 0)
4474 max_pixel_depth = 32;
4477 max_pixel_depth = 24;
4480 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4482 if (max_pixel_depth < 8)
4483 max_pixel_depth = 8;
4485 if (png_ptr->num_trans != 0)
4486 max_pixel_depth *= 2;
4489 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4491 if (png_ptr->num_trans != 0)
4493 max_pixel_depth *= 4;
4494 max_pixel_depth /= 3;
4500#ifdef PNG_READ_EXPAND_16_SUPPORTED
4501 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4503# ifdef PNG_READ_EXPAND_SUPPORTED
4507 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4509 if (png_ptr->bit_depth < 16)
4510 max_pixel_depth *= 2;
4514 png_ptr->transformations &= ~PNG_EXPAND_16;
4518#ifdef PNG_READ_FILLER_SUPPORTED
4519 if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4521 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4523 if (max_pixel_depth <= 8)
4524 max_pixel_depth = 16;
4527 max_pixel_depth = 32;
4530 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4531 png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4533 if (max_pixel_depth <= 32)
4534 max_pixel_depth = 32;
4537 max_pixel_depth = 64;
4542#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4543 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4546#ifdef PNG_READ_EXPAND_SUPPORTED
4547 (png_ptr->num_trans != 0 &&
4548 (png_ptr->transformations & PNG_EXPAND) != 0) ||
4550#ifdef PNG_READ_FILLER_SUPPORTED
4551 (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4553 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
4555 if (max_pixel_depth <= 16)
4556 max_pixel_depth = 32;
4559 max_pixel_depth = 64;
4564 if (max_pixel_depth <= 8)
4566 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4567 max_pixel_depth = 32;
4570 max_pixel_depth = 24;
4573 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4574 max_pixel_depth = 64;
4577 max_pixel_depth = 48;
4582#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4583defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4584 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4586 unsigned int user_pixel_depth = png_ptr->user_transform_depth *
4587 png_ptr->user_transform_channels;
4589 if (user_pixel_depth > max_pixel_depth)
4590 max_pixel_depth = user_pixel_depth;
4597 png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4598 png_ptr->transformed_pixel_depth = 0;
4603 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4607 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4608 1 + ((max_pixel_depth + 7) >> 3U);
4610#ifdef PNG_MAX_MALLOC_64K
4611 if (row_bytes > (png_uint_32)65536L)
4612 png_error(png_ptr,
"This image requires a row greater than 64KB");
4615 if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4617 png_free(png_ptr, png_ptr->big_row_buf);
4618 png_free(png_ptr, png_ptr->big_prev_row);
4620 if (png_ptr->interlaced != 0)
4621 png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4625 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4627 png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4629#ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4638 png_bytep temp = png_ptr->big_row_buf + 32;
4639 size_t extra = (size_t)temp & 0x0f;
4640 png_ptr->row_buf = temp - extra - 1;
4642 temp = png_ptr->big_prev_row + 32;
4643 extra = (size_t)temp & 0x0f;
4644 png_ptr->prev_row = temp - extra - 1;
4648 png_ptr->row_buf = png_ptr->big_row_buf + 31;
4649 png_ptr->prev_row = png_ptr->big_prev_row + 31;
4651 png_ptr->old_big_row_buf_size = row_bytes + 48;
4654#ifdef PNG_MAX_MALLOC_64K
4655 if (png_ptr->rowbytes > 65535)
4656 png_error(png_ptr,
"This image requires a row greater than 64KB");
4659 if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4660 png_error(png_ptr,
"Row has too many bytes to allocate in memory");
4662 memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4664 png_debug1(3,
"width = %u,", png_ptr->width);
4665 png_debug1(3,
"height = %u,", png_ptr->height);
4666 png_debug1(3,
"iwidth = %u,", png_ptr->iwidth);
4667 png_debug1(3,
"num_rows = %u,", png_ptr->num_rows);
4668 png_debug1(3,
"rowbytes = %lu,", (
unsigned long)png_ptr->rowbytes);
4669 png_debug1(3,
"irowbytes = %lu",
4670 (
unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4676 if (png_ptr->read_buffer != NULL)
4678 png_bytep buffer = png_ptr->read_buffer;
4680 png_ptr->read_buffer_size = 0;
4681 png_ptr->read_buffer = NULL;
4682 png_free(png_ptr, buffer);
4690 if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4691 png_error(png_ptr, png_ptr->zstream.msg);
4693 png_ptr->flags |= PNG_FLAG_ROW_INIT;