istereo

annotate libs/libpng/pngpread.c @ 26:862a3329a8f0

wohooo, added a shitload of code from zlib/libpng/libjpeg. When the good lord was raining shared libraries the iphone held a fucking umbrella...
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 06:28:38 +0300
parents
children
rev   line source
nuclear@26 1
nuclear@26 2 /* pngpread.c - read a png file in push mode
nuclear@26 3 *
nuclear@26 4 * Last changed in libpng 1.2.32 [September 18, 2008]
nuclear@26 5 * For conditions of distribution and use, see copyright notice in png.h
nuclear@26 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
nuclear@26 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
nuclear@26 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
nuclear@26 9 */
nuclear@26 10
nuclear@26 11 #define PNG_INTERNAL
nuclear@26 12 #include "png.h"
nuclear@26 13 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
nuclear@26 14
nuclear@26 15 /* push model modes */
nuclear@26 16 #define PNG_READ_SIG_MODE 0
nuclear@26 17 #define PNG_READ_CHUNK_MODE 1
nuclear@26 18 #define PNG_READ_IDAT_MODE 2
nuclear@26 19 #define PNG_SKIP_MODE 3
nuclear@26 20 #define PNG_READ_tEXt_MODE 4
nuclear@26 21 #define PNG_READ_zTXt_MODE 5
nuclear@26 22 #define PNG_READ_DONE_MODE 6
nuclear@26 23 #define PNG_READ_iTXt_MODE 7
nuclear@26 24 #define PNG_ERROR_MODE 8
nuclear@26 25
nuclear@26 26 void PNGAPI
nuclear@26 27 png_process_data(png_structp png_ptr, png_infop info_ptr,
nuclear@26 28 png_bytep buffer, png_size_t buffer_size)
nuclear@26 29 {
nuclear@26 30 if (png_ptr == NULL || info_ptr == NULL) return;
nuclear@26 31 png_push_restore_buffer(png_ptr, buffer, buffer_size);
nuclear@26 32
nuclear@26 33 while (png_ptr->buffer_size)
nuclear@26 34 {
nuclear@26 35 png_process_some_data(png_ptr, info_ptr);
nuclear@26 36 }
nuclear@26 37 }
nuclear@26 38
nuclear@26 39 /* What we do with the incoming data depends on what we were previously
nuclear@26 40 * doing before we ran out of data...
nuclear@26 41 */
nuclear@26 42 void /* PRIVATE */
nuclear@26 43 png_process_some_data(png_structp png_ptr, png_infop info_ptr)
nuclear@26 44 {
nuclear@26 45 if (png_ptr == NULL) return;
nuclear@26 46 switch (png_ptr->process_mode)
nuclear@26 47 {
nuclear@26 48 case PNG_READ_SIG_MODE:
nuclear@26 49 {
nuclear@26 50 png_push_read_sig(png_ptr, info_ptr);
nuclear@26 51 break;
nuclear@26 52 }
nuclear@26 53 case PNG_READ_CHUNK_MODE:
nuclear@26 54 {
nuclear@26 55 png_push_read_chunk(png_ptr, info_ptr);
nuclear@26 56 break;
nuclear@26 57 }
nuclear@26 58 case PNG_READ_IDAT_MODE:
nuclear@26 59 {
nuclear@26 60 png_push_read_IDAT(png_ptr);
nuclear@26 61 break;
nuclear@26 62 }
nuclear@26 63 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 64 case PNG_READ_tEXt_MODE:
nuclear@26 65 {
nuclear@26 66 png_push_read_tEXt(png_ptr, info_ptr);
nuclear@26 67 break;
nuclear@26 68 }
nuclear@26 69 #endif
nuclear@26 70 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 71 case PNG_READ_zTXt_MODE:
nuclear@26 72 {
nuclear@26 73 png_push_read_zTXt(png_ptr, info_ptr);
nuclear@26 74 break;
nuclear@26 75 }
nuclear@26 76 #endif
nuclear@26 77 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 78 case PNG_READ_iTXt_MODE:
nuclear@26 79 {
nuclear@26 80 png_push_read_iTXt(png_ptr, info_ptr);
nuclear@26 81 break;
nuclear@26 82 }
nuclear@26 83 #endif
nuclear@26 84 case PNG_SKIP_MODE:
nuclear@26 85 {
nuclear@26 86 png_push_crc_finish(png_ptr);
nuclear@26 87 break;
nuclear@26 88 }
nuclear@26 89 default:
nuclear@26 90 {
nuclear@26 91 png_ptr->buffer_size = 0;
nuclear@26 92 break;
nuclear@26 93 }
nuclear@26 94 }
nuclear@26 95 }
nuclear@26 96
nuclear@26 97 /* Read any remaining signature bytes from the stream and compare them with
nuclear@26 98 * the correct PNG signature. It is possible that this routine is called
nuclear@26 99 * with bytes already read from the signature, either because they have been
nuclear@26 100 * checked by the calling application, or because of multiple calls to this
nuclear@26 101 * routine.
nuclear@26 102 */
nuclear@26 103 void /* PRIVATE */
nuclear@26 104 png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
nuclear@26 105 {
nuclear@26 106 png_size_t num_checked = png_ptr->sig_bytes,
nuclear@26 107 num_to_check = 8 - num_checked;
nuclear@26 108
nuclear@26 109 if (png_ptr->buffer_size < num_to_check)
nuclear@26 110 {
nuclear@26 111 num_to_check = png_ptr->buffer_size;
nuclear@26 112 }
nuclear@26 113
nuclear@26 114 png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
nuclear@26 115 num_to_check);
nuclear@26 116 png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
nuclear@26 117
nuclear@26 118 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
nuclear@26 119 {
nuclear@26 120 if (num_checked < 4 &&
nuclear@26 121 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
nuclear@26 122 png_error(png_ptr, "Not a PNG file");
nuclear@26 123 else
nuclear@26 124 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
nuclear@26 125 }
nuclear@26 126 else
nuclear@26 127 {
nuclear@26 128 if (png_ptr->sig_bytes >= 8)
nuclear@26 129 {
nuclear@26 130 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
nuclear@26 131 }
nuclear@26 132 }
nuclear@26 133 }
nuclear@26 134
nuclear@26 135 void /* PRIVATE */
nuclear@26 136 png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
nuclear@26 137 {
nuclear@26 138 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 139 PNG_CONST PNG_IHDR;
nuclear@26 140 PNG_CONST PNG_IDAT;
nuclear@26 141 PNG_CONST PNG_IEND;
nuclear@26 142 PNG_CONST PNG_PLTE;
nuclear@26 143 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 144 PNG_CONST PNG_bKGD;
nuclear@26 145 #endif
nuclear@26 146 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 147 PNG_CONST PNG_cHRM;
nuclear@26 148 #endif
nuclear@26 149 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 150 PNG_CONST PNG_gAMA;
nuclear@26 151 #endif
nuclear@26 152 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 153 PNG_CONST PNG_hIST;
nuclear@26 154 #endif
nuclear@26 155 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 156 PNG_CONST PNG_iCCP;
nuclear@26 157 #endif
nuclear@26 158 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 159 PNG_CONST PNG_iTXt;
nuclear@26 160 #endif
nuclear@26 161 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 162 PNG_CONST PNG_oFFs;
nuclear@26 163 #endif
nuclear@26 164 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 165 PNG_CONST PNG_pCAL;
nuclear@26 166 #endif
nuclear@26 167 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 168 PNG_CONST PNG_pHYs;
nuclear@26 169 #endif
nuclear@26 170 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 171 PNG_CONST PNG_sBIT;
nuclear@26 172 #endif
nuclear@26 173 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 174 PNG_CONST PNG_sCAL;
nuclear@26 175 #endif
nuclear@26 176 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 177 PNG_CONST PNG_sRGB;
nuclear@26 178 #endif
nuclear@26 179 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 180 PNG_CONST PNG_sPLT;
nuclear@26 181 #endif
nuclear@26 182 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 183 PNG_CONST PNG_tEXt;
nuclear@26 184 #endif
nuclear@26 185 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 186 PNG_CONST PNG_tIME;
nuclear@26 187 #endif
nuclear@26 188 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 189 PNG_CONST PNG_tRNS;
nuclear@26 190 #endif
nuclear@26 191 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 192 PNG_CONST PNG_zTXt;
nuclear@26 193 #endif
nuclear@26 194 #endif /* PNG_USE_LOCAL_ARRAYS */
nuclear@26 195 /* First we make sure we have enough data for the 4 byte chunk name
nuclear@26 196 * and the 4 byte chunk length before proceeding with decoding the
nuclear@26 197 * chunk data. To fully decode each of these chunks, we also make
nuclear@26 198 * sure we have enough data in the buffer for the 4 byte CRC at the
nuclear@26 199 * end of every chunk (except IDAT, which is handled separately).
nuclear@26 200 */
nuclear@26 201 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
nuclear@26 202 {
nuclear@26 203 png_byte chunk_length[4];
nuclear@26 204
nuclear@26 205 if (png_ptr->buffer_size < 8)
nuclear@26 206 {
nuclear@26 207 png_push_save_buffer(png_ptr);
nuclear@26 208 return;
nuclear@26 209 }
nuclear@26 210
nuclear@26 211 png_push_fill_buffer(png_ptr, chunk_length, 4);
nuclear@26 212 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
nuclear@26 213 png_reset_crc(png_ptr);
nuclear@26 214 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
nuclear@26 215 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
nuclear@26 216 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
nuclear@26 217 }
nuclear@26 218
nuclear@26 219 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 220 if (png_ptr->mode & PNG_AFTER_IDAT)
nuclear@26 221 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
nuclear@26 222
nuclear@26 223 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
nuclear@26 224 {
nuclear@26 225 if (png_ptr->push_length != 13)
nuclear@26 226 png_error(png_ptr, "Invalid IHDR length");
nuclear@26 227 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 228 {
nuclear@26 229 png_push_save_buffer(png_ptr);
nuclear@26 230 return;
nuclear@26 231 }
nuclear@26 232 png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 233 }
nuclear@26 234 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
nuclear@26 235 {
nuclear@26 236 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 237 {
nuclear@26 238 png_push_save_buffer(png_ptr);
nuclear@26 239 return;
nuclear@26 240 }
nuclear@26 241 png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 242
nuclear@26 243 png_ptr->process_mode = PNG_READ_DONE_MODE;
nuclear@26 244 png_push_have_end(png_ptr, info_ptr);
nuclear@26 245 }
nuclear@26 246 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
nuclear@26 247 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
nuclear@26 248 {
nuclear@26 249 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 250 {
nuclear@26 251 png_push_save_buffer(png_ptr);
nuclear@26 252 return;
nuclear@26 253 }
nuclear@26 254 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 255 png_ptr->mode |= PNG_HAVE_IDAT;
nuclear@26 256 png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 257 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
nuclear@26 258 png_ptr->mode |= PNG_HAVE_PLTE;
nuclear@26 259 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 260 {
nuclear@26 261 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@26 262 png_error(png_ptr, "Missing IHDR before IDAT");
nuclear@26 263 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
nuclear@26 264 !(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@26 265 png_error(png_ptr, "Missing PLTE before IDAT");
nuclear@26 266 }
nuclear@26 267 }
nuclear@26 268 #endif
nuclear@26 269 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
nuclear@26 270 {
nuclear@26 271 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 272 {
nuclear@26 273 png_push_save_buffer(png_ptr);
nuclear@26 274 return;
nuclear@26 275 }
nuclear@26 276 png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 277 }
nuclear@26 278 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 279 {
nuclear@26 280 /* If we reach an IDAT chunk, this means we have read all of the
nuclear@26 281 * header chunks, and we can start reading the image (or if this
nuclear@26 282 * is called after the image has been read - we have an error).
nuclear@26 283 */
nuclear@26 284 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@26 285 png_error(png_ptr, "Missing IHDR before IDAT");
nuclear@26 286 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
nuclear@26 287 !(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@26 288 png_error(png_ptr, "Missing PLTE before IDAT");
nuclear@26 289
nuclear@26 290 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@26 291 {
nuclear@26 292 if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
nuclear@26 293 if (png_ptr->push_length == 0)
nuclear@26 294 return;
nuclear@26 295
nuclear@26 296 if (png_ptr->mode & PNG_AFTER_IDAT)
nuclear@26 297 png_error(png_ptr, "Too many IDAT's found");
nuclear@26 298 }
nuclear@26 299
nuclear@26 300 png_ptr->idat_size = png_ptr->push_length;
nuclear@26 301 png_ptr->mode |= PNG_HAVE_IDAT;
nuclear@26 302 png_ptr->process_mode = PNG_READ_IDAT_MODE;
nuclear@26 303 png_push_have_info(png_ptr, info_ptr);
nuclear@26 304 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
nuclear@26 305 png_ptr->zstream.next_out = png_ptr->row_buf;
nuclear@26 306 return;
nuclear@26 307 }
nuclear@26 308 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@26 309 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
nuclear@26 310 {
nuclear@26 311 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 312 {
nuclear@26 313 png_push_save_buffer(png_ptr);
nuclear@26 314 return;
nuclear@26 315 }
nuclear@26 316 png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 317 }
nuclear@26 318 #endif
nuclear@26 319 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@26 320 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
nuclear@26 321 {
nuclear@26 322 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 323 {
nuclear@26 324 png_push_save_buffer(png_ptr);
nuclear@26 325 return;
nuclear@26 326 }
nuclear@26 327 png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 328 }
nuclear@26 329 #endif
nuclear@26 330 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@26 331 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
nuclear@26 332 {
nuclear@26 333 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 334 {
nuclear@26 335 png_push_save_buffer(png_ptr);
nuclear@26 336 return;
nuclear@26 337 }
nuclear@26 338 png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 339 }
nuclear@26 340 #endif
nuclear@26 341 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@26 342 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
nuclear@26 343 {
nuclear@26 344 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 345 {
nuclear@26 346 png_push_save_buffer(png_ptr);
nuclear@26 347 return;
nuclear@26 348 }
nuclear@26 349 png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 350 }
nuclear@26 351 #endif
nuclear@26 352 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@26 353 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
nuclear@26 354 {
nuclear@26 355 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 356 {
nuclear@26 357 png_push_save_buffer(png_ptr);
nuclear@26 358 return;
nuclear@26 359 }
nuclear@26 360 png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 361 }
nuclear@26 362 #endif
nuclear@26 363 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@26 364 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
nuclear@26 365 {
nuclear@26 366 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 367 {
nuclear@26 368 png_push_save_buffer(png_ptr);
nuclear@26 369 return;
nuclear@26 370 }
nuclear@26 371 png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 372 }
nuclear@26 373 #endif
nuclear@26 374 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@26 375 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
nuclear@26 376 {
nuclear@26 377 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 378 {
nuclear@26 379 png_push_save_buffer(png_ptr);
nuclear@26 380 return;
nuclear@26 381 }
nuclear@26 382 png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 383 }
nuclear@26 384 #endif
nuclear@26 385 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@26 386 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
nuclear@26 387 {
nuclear@26 388 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 389 {
nuclear@26 390 png_push_save_buffer(png_ptr);
nuclear@26 391 return;
nuclear@26 392 }
nuclear@26 393 png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 394 }
nuclear@26 395 #endif
nuclear@26 396 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@26 397 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
nuclear@26 398 {
nuclear@26 399 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 400 {
nuclear@26 401 png_push_save_buffer(png_ptr);
nuclear@26 402 return;
nuclear@26 403 }
nuclear@26 404 png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 405 }
nuclear@26 406 #endif
nuclear@26 407 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@26 408 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
nuclear@26 409 {
nuclear@26 410 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 411 {
nuclear@26 412 png_push_save_buffer(png_ptr);
nuclear@26 413 return;
nuclear@26 414 }
nuclear@26 415 png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 416 }
nuclear@26 417 #endif
nuclear@26 418 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@26 419 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
nuclear@26 420 {
nuclear@26 421 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 422 {
nuclear@26 423 png_push_save_buffer(png_ptr);
nuclear@26 424 return;
nuclear@26 425 }
nuclear@26 426 png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 427 }
nuclear@26 428 #endif
nuclear@26 429 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@26 430 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
nuclear@26 431 {
nuclear@26 432 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 433 {
nuclear@26 434 png_push_save_buffer(png_ptr);
nuclear@26 435 return;
nuclear@26 436 }
nuclear@26 437 png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 438 }
nuclear@26 439 #endif
nuclear@26 440 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@26 441 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
nuclear@26 442 {
nuclear@26 443 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 444 {
nuclear@26 445 png_push_save_buffer(png_ptr);
nuclear@26 446 return;
nuclear@26 447 }
nuclear@26 448 png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 449 }
nuclear@26 450 #endif
nuclear@26 451 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@26 452 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
nuclear@26 453 {
nuclear@26 454 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 455 {
nuclear@26 456 png_push_save_buffer(png_ptr);
nuclear@26 457 return;
nuclear@26 458 }
nuclear@26 459 png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 460 }
nuclear@26 461 #endif
nuclear@26 462 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 463 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
nuclear@26 464 {
nuclear@26 465 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 466 {
nuclear@26 467 png_push_save_buffer(png_ptr);
nuclear@26 468 return;
nuclear@26 469 }
nuclear@26 470 png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 471 }
nuclear@26 472 #endif
nuclear@26 473 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 474 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
nuclear@26 475 {
nuclear@26 476 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 477 {
nuclear@26 478 png_push_save_buffer(png_ptr);
nuclear@26 479 return;
nuclear@26 480 }
nuclear@26 481 png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 482 }
nuclear@26 483 #endif
nuclear@26 484 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 485 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
nuclear@26 486 {
nuclear@26 487 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 488 {
nuclear@26 489 png_push_save_buffer(png_ptr);
nuclear@26 490 return;
nuclear@26 491 }
nuclear@26 492 png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 493 }
nuclear@26 494 #endif
nuclear@26 495 else
nuclear@26 496 {
nuclear@26 497 if (png_ptr->push_length + 4 > png_ptr->buffer_size)
nuclear@26 498 {
nuclear@26 499 png_push_save_buffer(png_ptr);
nuclear@26 500 return;
nuclear@26 501 }
nuclear@26 502 png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
nuclear@26 503 }
nuclear@26 504
nuclear@26 505 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
nuclear@26 506 }
nuclear@26 507
nuclear@26 508 void /* PRIVATE */
nuclear@26 509 png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
nuclear@26 510 {
nuclear@26 511 png_ptr->process_mode = PNG_SKIP_MODE;
nuclear@26 512 png_ptr->skip_length = skip;
nuclear@26 513 }
nuclear@26 514
nuclear@26 515 void /* PRIVATE */
nuclear@26 516 png_push_crc_finish(png_structp png_ptr)
nuclear@26 517 {
nuclear@26 518 if (png_ptr->skip_length && png_ptr->save_buffer_size)
nuclear@26 519 {
nuclear@26 520 png_size_t save_size;
nuclear@26 521
nuclear@26 522 if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
nuclear@26 523 save_size = (png_size_t)png_ptr->skip_length;
nuclear@26 524 else
nuclear@26 525 save_size = png_ptr->save_buffer_size;
nuclear@26 526
nuclear@26 527 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
nuclear@26 528
nuclear@26 529 png_ptr->skip_length -= save_size;
nuclear@26 530 png_ptr->buffer_size -= save_size;
nuclear@26 531 png_ptr->save_buffer_size -= save_size;
nuclear@26 532 png_ptr->save_buffer_ptr += save_size;
nuclear@26 533 }
nuclear@26 534 if (png_ptr->skip_length && png_ptr->current_buffer_size)
nuclear@26 535 {
nuclear@26 536 png_size_t save_size;
nuclear@26 537
nuclear@26 538 if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
nuclear@26 539 save_size = (png_size_t)png_ptr->skip_length;
nuclear@26 540 else
nuclear@26 541 save_size = png_ptr->current_buffer_size;
nuclear@26 542
nuclear@26 543 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
nuclear@26 544
nuclear@26 545 png_ptr->skip_length -= save_size;
nuclear@26 546 png_ptr->buffer_size -= save_size;
nuclear@26 547 png_ptr->current_buffer_size -= save_size;
nuclear@26 548 png_ptr->current_buffer_ptr += save_size;
nuclear@26 549 }
nuclear@26 550 if (!png_ptr->skip_length)
nuclear@26 551 {
nuclear@26 552 if (png_ptr->buffer_size < 4)
nuclear@26 553 {
nuclear@26 554 png_push_save_buffer(png_ptr);
nuclear@26 555 return;
nuclear@26 556 }
nuclear@26 557
nuclear@26 558 png_crc_finish(png_ptr, 0);
nuclear@26 559 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
nuclear@26 560 }
nuclear@26 561 }
nuclear@26 562
nuclear@26 563 void PNGAPI
nuclear@26 564 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
nuclear@26 565 {
nuclear@26 566 png_bytep ptr;
nuclear@26 567
nuclear@26 568 if (png_ptr == NULL) return;
nuclear@26 569 ptr = buffer;
nuclear@26 570 if (png_ptr->save_buffer_size)
nuclear@26 571 {
nuclear@26 572 png_size_t save_size;
nuclear@26 573
nuclear@26 574 if (length < png_ptr->save_buffer_size)
nuclear@26 575 save_size = length;
nuclear@26 576 else
nuclear@26 577 save_size = png_ptr->save_buffer_size;
nuclear@26 578
nuclear@26 579 png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
nuclear@26 580 length -= save_size;
nuclear@26 581 ptr += save_size;
nuclear@26 582 png_ptr->buffer_size -= save_size;
nuclear@26 583 png_ptr->save_buffer_size -= save_size;
nuclear@26 584 png_ptr->save_buffer_ptr += save_size;
nuclear@26 585 }
nuclear@26 586 if (length && png_ptr->current_buffer_size)
nuclear@26 587 {
nuclear@26 588 png_size_t save_size;
nuclear@26 589
nuclear@26 590 if (length < png_ptr->current_buffer_size)
nuclear@26 591 save_size = length;
nuclear@26 592 else
nuclear@26 593 save_size = png_ptr->current_buffer_size;
nuclear@26 594
nuclear@26 595 png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
nuclear@26 596 png_ptr->buffer_size -= save_size;
nuclear@26 597 png_ptr->current_buffer_size -= save_size;
nuclear@26 598 png_ptr->current_buffer_ptr += save_size;
nuclear@26 599 }
nuclear@26 600 }
nuclear@26 601
nuclear@26 602 void /* PRIVATE */
nuclear@26 603 png_push_save_buffer(png_structp png_ptr)
nuclear@26 604 {
nuclear@26 605 if (png_ptr->save_buffer_size)
nuclear@26 606 {
nuclear@26 607 if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
nuclear@26 608 {
nuclear@26 609 png_size_t i, istop;
nuclear@26 610 png_bytep sp;
nuclear@26 611 png_bytep dp;
nuclear@26 612
nuclear@26 613 istop = png_ptr->save_buffer_size;
nuclear@26 614 for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
nuclear@26 615 i < istop; i++, sp++, dp++)
nuclear@26 616 {
nuclear@26 617 *dp = *sp;
nuclear@26 618 }
nuclear@26 619 }
nuclear@26 620 }
nuclear@26 621 if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
nuclear@26 622 png_ptr->save_buffer_max)
nuclear@26 623 {
nuclear@26 624 png_size_t new_max;
nuclear@26 625 png_bytep old_buffer;
nuclear@26 626
nuclear@26 627 if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
nuclear@26 628 (png_ptr->current_buffer_size + 256))
nuclear@26 629 {
nuclear@26 630 png_error(png_ptr, "Potential overflow of save_buffer");
nuclear@26 631 }
nuclear@26 632 new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
nuclear@26 633 old_buffer = png_ptr->save_buffer;
nuclear@26 634 png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
nuclear@26 635 (png_uint_32)new_max);
nuclear@26 636 png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
nuclear@26 637 png_free(png_ptr, old_buffer);
nuclear@26 638 png_ptr->save_buffer_max = new_max;
nuclear@26 639 }
nuclear@26 640 if (png_ptr->current_buffer_size)
nuclear@26 641 {
nuclear@26 642 png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
nuclear@26 643 png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
nuclear@26 644 png_ptr->save_buffer_size += png_ptr->current_buffer_size;
nuclear@26 645 png_ptr->current_buffer_size = 0;
nuclear@26 646 }
nuclear@26 647 png_ptr->save_buffer_ptr = png_ptr->save_buffer;
nuclear@26 648 png_ptr->buffer_size = 0;
nuclear@26 649 }
nuclear@26 650
nuclear@26 651 void /* PRIVATE */
nuclear@26 652 png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
nuclear@26 653 png_size_t buffer_length)
nuclear@26 654 {
nuclear@26 655 png_ptr->current_buffer = buffer;
nuclear@26 656 png_ptr->current_buffer_size = buffer_length;
nuclear@26 657 png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
nuclear@26 658 png_ptr->current_buffer_ptr = png_ptr->current_buffer;
nuclear@26 659 }
nuclear@26 660
nuclear@26 661 void /* PRIVATE */
nuclear@26 662 png_push_read_IDAT(png_structp png_ptr)
nuclear@26 663 {
nuclear@26 664 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 665 PNG_CONST PNG_IDAT;
nuclear@26 666 #endif
nuclear@26 667 if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
nuclear@26 668 {
nuclear@26 669 png_byte chunk_length[4];
nuclear@26 670
nuclear@26 671 if (png_ptr->buffer_size < 8)
nuclear@26 672 {
nuclear@26 673 png_push_save_buffer(png_ptr);
nuclear@26 674 return;
nuclear@26 675 }
nuclear@26 676
nuclear@26 677 png_push_fill_buffer(png_ptr, chunk_length, 4);
nuclear@26 678 png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
nuclear@26 679 png_reset_crc(png_ptr);
nuclear@26 680 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
nuclear@26 681 png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
nuclear@26 682
nuclear@26 683 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@26 684 {
nuclear@26 685 png_ptr->process_mode = PNG_READ_CHUNK_MODE;
nuclear@26 686 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
nuclear@26 687 png_error(png_ptr, "Not enough compressed data");
nuclear@26 688 return;
nuclear@26 689 }
nuclear@26 690
nuclear@26 691 png_ptr->idat_size = png_ptr->push_length;
nuclear@26 692 }
nuclear@26 693 if (png_ptr->idat_size && png_ptr->save_buffer_size)
nuclear@26 694 {
nuclear@26 695 png_size_t save_size;
nuclear@26 696
nuclear@26 697 if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
nuclear@26 698 {
nuclear@26 699 save_size = (png_size_t)png_ptr->idat_size;
nuclear@26 700 /* check for overflow */
nuclear@26 701 if ((png_uint_32)save_size != png_ptr->idat_size)
nuclear@26 702 png_error(png_ptr, "save_size overflowed in pngpread");
nuclear@26 703 }
nuclear@26 704 else
nuclear@26 705 save_size = png_ptr->save_buffer_size;
nuclear@26 706
nuclear@26 707 png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
nuclear@26 708 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
nuclear@26 709 png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
nuclear@26 710 png_ptr->idat_size -= save_size;
nuclear@26 711 png_ptr->buffer_size -= save_size;
nuclear@26 712 png_ptr->save_buffer_size -= save_size;
nuclear@26 713 png_ptr->save_buffer_ptr += save_size;
nuclear@26 714 }
nuclear@26 715 if (png_ptr->idat_size && png_ptr->current_buffer_size)
nuclear@26 716 {
nuclear@26 717 png_size_t save_size;
nuclear@26 718
nuclear@26 719 if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
nuclear@26 720 {
nuclear@26 721 save_size = (png_size_t)png_ptr->idat_size;
nuclear@26 722 /* check for overflow */
nuclear@26 723 if ((png_uint_32)save_size != png_ptr->idat_size)
nuclear@26 724 png_error(png_ptr, "save_size overflowed in pngpread");
nuclear@26 725 }
nuclear@26 726 else
nuclear@26 727 save_size = png_ptr->current_buffer_size;
nuclear@26 728
nuclear@26 729 png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
nuclear@26 730 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
nuclear@26 731 png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
nuclear@26 732
nuclear@26 733 png_ptr->idat_size -= save_size;
nuclear@26 734 png_ptr->buffer_size -= save_size;
nuclear@26 735 png_ptr->current_buffer_size -= save_size;
nuclear@26 736 png_ptr->current_buffer_ptr += save_size;
nuclear@26 737 }
nuclear@26 738 if (!png_ptr->idat_size)
nuclear@26 739 {
nuclear@26 740 if (png_ptr->buffer_size < 4)
nuclear@26 741 {
nuclear@26 742 png_push_save_buffer(png_ptr);
nuclear@26 743 return;
nuclear@26 744 }
nuclear@26 745
nuclear@26 746 png_crc_finish(png_ptr, 0);
nuclear@26 747 png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
nuclear@26 748 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@26 749 }
nuclear@26 750 }
nuclear@26 751
nuclear@26 752 void /* PRIVATE */
nuclear@26 753 png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
nuclear@26 754 png_size_t buffer_length)
nuclear@26 755 {
nuclear@26 756 int ret;
nuclear@26 757
nuclear@26 758 if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length)
nuclear@26 759 png_error(png_ptr, "Extra compression data");
nuclear@26 760
nuclear@26 761 png_ptr->zstream.next_in = buffer;
nuclear@26 762 png_ptr->zstream.avail_in = (uInt)buffer_length;
nuclear@26 763 for (;;)
nuclear@26 764 {
nuclear@26 765 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
nuclear@26 766 if (ret != Z_OK)
nuclear@26 767 {
nuclear@26 768 if (ret == Z_STREAM_END)
nuclear@26 769 {
nuclear@26 770 if (png_ptr->zstream.avail_in)
nuclear@26 771 png_error(png_ptr, "Extra compressed data");
nuclear@26 772 if (!(png_ptr->zstream.avail_out))
nuclear@26 773 {
nuclear@26 774 png_push_process_row(png_ptr);
nuclear@26 775 }
nuclear@26 776
nuclear@26 777 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@26 778 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
nuclear@26 779 break;
nuclear@26 780 }
nuclear@26 781 else if (ret == Z_BUF_ERROR)
nuclear@26 782 break;
nuclear@26 783 else
nuclear@26 784 png_error(png_ptr, "Decompression Error");
nuclear@26 785 }
nuclear@26 786 if (!(png_ptr->zstream.avail_out))
nuclear@26 787 {
nuclear@26 788 if ((
nuclear@26 789 #if defined(PNG_READ_INTERLACING_SUPPORTED)
nuclear@26 790 png_ptr->interlaced && png_ptr->pass > 6) ||
nuclear@26 791 (!png_ptr->interlaced &&
nuclear@26 792 #endif
nuclear@26 793 png_ptr->row_number == png_ptr->num_rows))
nuclear@26 794 {
nuclear@26 795 if (png_ptr->zstream.avail_in)
nuclear@26 796 png_warning(png_ptr, "Too much data in IDAT chunks");
nuclear@26 797 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
nuclear@26 798 break;
nuclear@26 799 }
nuclear@26 800 png_push_process_row(png_ptr);
nuclear@26 801 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
nuclear@26 802 png_ptr->zstream.next_out = png_ptr->row_buf;
nuclear@26 803 }
nuclear@26 804 else
nuclear@26 805 break;
nuclear@26 806 }
nuclear@26 807 }
nuclear@26 808
nuclear@26 809 void /* PRIVATE */
nuclear@26 810 png_push_process_row(png_structp png_ptr)
nuclear@26 811 {
nuclear@26 812 png_ptr->row_info.color_type = png_ptr->color_type;
nuclear@26 813 png_ptr->row_info.width = png_ptr->iwidth;
nuclear@26 814 png_ptr->row_info.channels = png_ptr->channels;
nuclear@26 815 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
nuclear@26 816 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
nuclear@26 817
nuclear@26 818 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
nuclear@26 819 png_ptr->row_info.width);
nuclear@26 820
nuclear@26 821 png_read_filter_row(png_ptr, &(png_ptr->row_info),
nuclear@26 822 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
nuclear@26 823 (int)(png_ptr->row_buf[0]));
nuclear@26 824
nuclear@26 825 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
nuclear@26 826 png_ptr->rowbytes + 1);
nuclear@26 827
nuclear@26 828 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
nuclear@26 829 png_do_read_transformations(png_ptr);
nuclear@26 830
nuclear@26 831 #if defined(PNG_READ_INTERLACING_SUPPORTED)
nuclear@26 832 /* blow up interlaced rows to full size */
nuclear@26 833 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
nuclear@26 834 {
nuclear@26 835 if (png_ptr->pass < 6)
nuclear@26 836 /* old interface (pre-1.0.9):
nuclear@26 837 png_do_read_interlace(&(png_ptr->row_info),
nuclear@26 838 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
nuclear@26 839 */
nuclear@26 840 png_do_read_interlace(png_ptr);
nuclear@26 841
nuclear@26 842 switch (png_ptr->pass)
nuclear@26 843 {
nuclear@26 844 case 0:
nuclear@26 845 {
nuclear@26 846 int i;
nuclear@26 847 for (i = 0; i < 8 && png_ptr->pass == 0; i++)
nuclear@26 848 {
nuclear@26 849 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 850 png_read_push_finish_row(png_ptr); /* updates png_ptr->pass */
nuclear@26 851 }
nuclear@26 852 if (png_ptr->pass == 2) /* pass 1 might be empty */
nuclear@26 853 {
nuclear@26 854 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
nuclear@26 855 {
nuclear@26 856 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 857 png_read_push_finish_row(png_ptr);
nuclear@26 858 }
nuclear@26 859 }
nuclear@26 860 if (png_ptr->pass == 4 && png_ptr->height <= 4)
nuclear@26 861 {
nuclear@26 862 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
nuclear@26 863 {
nuclear@26 864 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 865 png_read_push_finish_row(png_ptr);
nuclear@26 866 }
nuclear@26 867 }
nuclear@26 868 if (png_ptr->pass == 6 && png_ptr->height <= 4)
nuclear@26 869 {
nuclear@26 870 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 871 png_read_push_finish_row(png_ptr);
nuclear@26 872 }
nuclear@26 873 break;
nuclear@26 874 }
nuclear@26 875 case 1:
nuclear@26 876 {
nuclear@26 877 int i;
nuclear@26 878 for (i = 0; i < 8 && png_ptr->pass == 1; i++)
nuclear@26 879 {
nuclear@26 880 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 881 png_read_push_finish_row(png_ptr);
nuclear@26 882 }
nuclear@26 883 if (png_ptr->pass == 2) /* skip top 4 generated rows */
nuclear@26 884 {
nuclear@26 885 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
nuclear@26 886 {
nuclear@26 887 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 888 png_read_push_finish_row(png_ptr);
nuclear@26 889 }
nuclear@26 890 }
nuclear@26 891 break;
nuclear@26 892 }
nuclear@26 893 case 2:
nuclear@26 894 {
nuclear@26 895 int i;
nuclear@26 896 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
nuclear@26 897 {
nuclear@26 898 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 899 png_read_push_finish_row(png_ptr);
nuclear@26 900 }
nuclear@26 901 for (i = 0; i < 4 && png_ptr->pass == 2; i++)
nuclear@26 902 {
nuclear@26 903 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 904 png_read_push_finish_row(png_ptr);
nuclear@26 905 }
nuclear@26 906 if (png_ptr->pass == 4) /* pass 3 might be empty */
nuclear@26 907 {
nuclear@26 908 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
nuclear@26 909 {
nuclear@26 910 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 911 png_read_push_finish_row(png_ptr);
nuclear@26 912 }
nuclear@26 913 }
nuclear@26 914 break;
nuclear@26 915 }
nuclear@26 916 case 3:
nuclear@26 917 {
nuclear@26 918 int i;
nuclear@26 919 for (i = 0; i < 4 && png_ptr->pass == 3; i++)
nuclear@26 920 {
nuclear@26 921 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 922 png_read_push_finish_row(png_ptr);
nuclear@26 923 }
nuclear@26 924 if (png_ptr->pass == 4) /* skip top two generated rows */
nuclear@26 925 {
nuclear@26 926 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
nuclear@26 927 {
nuclear@26 928 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 929 png_read_push_finish_row(png_ptr);
nuclear@26 930 }
nuclear@26 931 }
nuclear@26 932 break;
nuclear@26 933 }
nuclear@26 934 case 4:
nuclear@26 935 {
nuclear@26 936 int i;
nuclear@26 937 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
nuclear@26 938 {
nuclear@26 939 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 940 png_read_push_finish_row(png_ptr);
nuclear@26 941 }
nuclear@26 942 for (i = 0; i < 2 && png_ptr->pass == 4; i++)
nuclear@26 943 {
nuclear@26 944 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 945 png_read_push_finish_row(png_ptr);
nuclear@26 946 }
nuclear@26 947 if (png_ptr->pass == 6) /* pass 5 might be empty */
nuclear@26 948 {
nuclear@26 949 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 950 png_read_push_finish_row(png_ptr);
nuclear@26 951 }
nuclear@26 952 break;
nuclear@26 953 }
nuclear@26 954 case 5:
nuclear@26 955 {
nuclear@26 956 int i;
nuclear@26 957 for (i = 0; i < 2 && png_ptr->pass == 5; i++)
nuclear@26 958 {
nuclear@26 959 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 960 png_read_push_finish_row(png_ptr);
nuclear@26 961 }
nuclear@26 962 if (png_ptr->pass == 6) /* skip top generated row */
nuclear@26 963 {
nuclear@26 964 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 965 png_read_push_finish_row(png_ptr);
nuclear@26 966 }
nuclear@26 967 break;
nuclear@26 968 }
nuclear@26 969 case 6:
nuclear@26 970 {
nuclear@26 971 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 972 png_read_push_finish_row(png_ptr);
nuclear@26 973 if (png_ptr->pass != 6)
nuclear@26 974 break;
nuclear@26 975 png_push_have_row(png_ptr, png_bytep_NULL);
nuclear@26 976 png_read_push_finish_row(png_ptr);
nuclear@26 977 }
nuclear@26 978 }
nuclear@26 979 }
nuclear@26 980 else
nuclear@26 981 #endif
nuclear@26 982 {
nuclear@26 983 png_push_have_row(png_ptr, png_ptr->row_buf + 1);
nuclear@26 984 png_read_push_finish_row(png_ptr);
nuclear@26 985 }
nuclear@26 986 }
nuclear@26 987
nuclear@26 988 void /* PRIVATE */
nuclear@26 989 png_read_push_finish_row(png_structp png_ptr)
nuclear@26 990 {
nuclear@26 991 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 992 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@26 993
nuclear@26 994 /* start of interlace block */
nuclear@26 995 PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
nuclear@26 996
nuclear@26 997 /* offset to next interlace block */
nuclear@26 998 PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@26 999
nuclear@26 1000 /* start of interlace block in the y direction */
nuclear@26 1001 PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
nuclear@26 1002
nuclear@26 1003 /* offset to next interlace block in the y direction */
nuclear@26 1004 PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
nuclear@26 1005
nuclear@26 1006 /* Height of interlace block. This is not currently used - if you need
nuclear@26 1007 * it, uncomment it here and in png.h
nuclear@26 1008 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
nuclear@26 1009 */
nuclear@26 1010 #endif
nuclear@26 1011
nuclear@26 1012 png_ptr->row_number++;
nuclear@26 1013 if (png_ptr->row_number < png_ptr->num_rows)
nuclear@26 1014 return;
nuclear@26 1015
nuclear@26 1016 if (png_ptr->interlaced)
nuclear@26 1017 {
nuclear@26 1018 png_ptr->row_number = 0;
nuclear@26 1019 png_memset_check(png_ptr, png_ptr->prev_row, 0,
nuclear@26 1020 png_ptr->rowbytes + 1);
nuclear@26 1021 do
nuclear@26 1022 {
nuclear@26 1023 png_ptr->pass++;
nuclear@26 1024 if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
nuclear@26 1025 (png_ptr->pass == 3 && png_ptr->width < 3) ||
nuclear@26 1026 (png_ptr->pass == 5 && png_ptr->width < 2))
nuclear@26 1027 png_ptr->pass++;
nuclear@26 1028
nuclear@26 1029 if (png_ptr->pass > 7)
nuclear@26 1030 png_ptr->pass--;
nuclear@26 1031 if (png_ptr->pass >= 7)
nuclear@26 1032 break;
nuclear@26 1033
nuclear@26 1034 png_ptr->iwidth = (png_ptr->width +
nuclear@26 1035 png_pass_inc[png_ptr->pass] - 1 -
nuclear@26 1036 png_pass_start[png_ptr->pass]) /
nuclear@26 1037 png_pass_inc[png_ptr->pass];
nuclear@26 1038
nuclear@26 1039 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
nuclear@26 1040 png_ptr->iwidth) + 1;
nuclear@26 1041
nuclear@26 1042 if (png_ptr->transformations & PNG_INTERLACE)
nuclear@26 1043 break;
nuclear@26 1044
nuclear@26 1045 png_ptr->num_rows = (png_ptr->height +
nuclear@26 1046 png_pass_yinc[png_ptr->pass] - 1 -
nuclear@26 1047 png_pass_ystart[png_ptr->pass]) /
nuclear@26 1048 png_pass_yinc[png_ptr->pass];
nuclear@26 1049
nuclear@26 1050 } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
nuclear@26 1051 }
nuclear@26 1052 }
nuclear@26 1053
nuclear@26 1054 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@26 1055 void /* PRIVATE */
nuclear@26 1056 png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
nuclear@26 1057 length)
nuclear@26 1058 {
nuclear@26 1059 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
nuclear@26 1060 {
nuclear@26 1061 png_error(png_ptr, "Out of place tEXt");
nuclear@26 1062 info_ptr = info_ptr; /* to quiet some compiler warnings */
nuclear@26 1063 }
nuclear@26 1064
nuclear@26 1065 #ifdef PNG_MAX_MALLOC_64K
nuclear@26 1066 png_ptr->skip_length = 0; /* This may not be necessary */
nuclear@26 1067
nuclear@26 1068 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
nuclear@26 1069 {
nuclear@26 1070 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
nuclear@26 1071 png_ptr->skip_length = length - (png_uint_32)65535L;
nuclear@26 1072 length = (png_uint_32)65535L;
nuclear@26 1073 }
nuclear@26 1074 #endif
nuclear@26 1075
nuclear@26 1076 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
nuclear@26 1077 (png_uint_32)(length + 1));
nuclear@26 1078 png_ptr->current_text[length] = '\0';
nuclear@26 1079 png_ptr->current_text_ptr = png_ptr->current_text;
nuclear@26 1080 png_ptr->current_text_size = (png_size_t)length;
nuclear@26 1081 png_ptr->current_text_left = (png_size_t)length;
nuclear@26 1082 png_ptr->process_mode = PNG_READ_tEXt_MODE;
nuclear@26 1083 }
nuclear@26 1084
nuclear@26 1085 void /* PRIVATE */
nuclear@26 1086 png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
nuclear@26 1087 {
nuclear@26 1088 if (png_ptr->buffer_size && png_ptr->current_text_left)
nuclear@26 1089 {
nuclear@26 1090 png_size_t text_size;
nuclear@26 1091
nuclear@26 1092 if (png_ptr->buffer_size < png_ptr->current_text_left)
nuclear@26 1093 text_size = png_ptr->buffer_size;
nuclear@26 1094 else
nuclear@26 1095 text_size = png_ptr->current_text_left;
nuclear@26 1096 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
nuclear@26 1097 png_ptr->current_text_left -= text_size;
nuclear@26 1098 png_ptr->current_text_ptr += text_size;
nuclear@26 1099 }
nuclear@26 1100 if (!(png_ptr->current_text_left))
nuclear@26 1101 {
nuclear@26 1102 png_textp text_ptr;
nuclear@26 1103 png_charp text;
nuclear@26 1104 png_charp key;
nuclear@26 1105 int ret;
nuclear@26 1106
nuclear@26 1107 if (png_ptr->buffer_size < 4)
nuclear@26 1108 {
nuclear@26 1109 png_push_save_buffer(png_ptr);
nuclear@26 1110 return;
nuclear@26 1111 }
nuclear@26 1112
nuclear@26 1113 png_push_crc_finish(png_ptr);
nuclear@26 1114
nuclear@26 1115 #if defined(PNG_MAX_MALLOC_64K)
nuclear@26 1116 if (png_ptr->skip_length)
nuclear@26 1117 return;
nuclear@26 1118 #endif
nuclear@26 1119
nuclear@26 1120 key = png_ptr->current_text;
nuclear@26 1121
nuclear@26 1122 for (text = key; *text; text++)
nuclear@26 1123 /* empty loop */ ;
nuclear@26 1124
nuclear@26 1125 if (text < key + png_ptr->current_text_size)
nuclear@26 1126 text++;
nuclear@26 1127
nuclear@26 1128 text_ptr = (png_textp)png_malloc(png_ptr,
nuclear@26 1129 (png_uint_32)png_sizeof(png_text));
nuclear@26 1130 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
nuclear@26 1131 text_ptr->key = key;
nuclear@26 1132 #ifdef PNG_iTXt_SUPPORTED
nuclear@26 1133 text_ptr->lang = NULL;
nuclear@26 1134 text_ptr->lang_key = NULL;
nuclear@26 1135 #endif
nuclear@26 1136 text_ptr->text = text;
nuclear@26 1137
nuclear@26 1138 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@26 1139
nuclear@26 1140 png_free(png_ptr, key);
nuclear@26 1141 png_free(png_ptr, text_ptr);
nuclear@26 1142 png_ptr->current_text = NULL;
nuclear@26 1143
nuclear@26 1144 if (ret)
nuclear@26 1145 png_warning(png_ptr, "Insufficient memory to store text chunk.");
nuclear@26 1146 }
nuclear@26 1147 }
nuclear@26 1148 #endif
nuclear@26 1149
nuclear@26 1150 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@26 1151 void /* PRIVATE */
nuclear@26 1152 png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
nuclear@26 1153 length)
nuclear@26 1154 {
nuclear@26 1155 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
nuclear@26 1156 {
nuclear@26 1157 png_error(png_ptr, "Out of place zTXt");
nuclear@26 1158 info_ptr = info_ptr; /* to quiet some compiler warnings */
nuclear@26 1159 }
nuclear@26 1160
nuclear@26 1161 #ifdef PNG_MAX_MALLOC_64K
nuclear@26 1162 /* We can't handle zTXt chunks > 64K, since we don't have enough space
nuclear@26 1163 * to be able to store the uncompressed data. Actually, the threshold
nuclear@26 1164 * is probably around 32K, but it isn't as definite as 64K is.
nuclear@26 1165 */
nuclear@26 1166 if (length > (png_uint_32)65535L)
nuclear@26 1167 {
nuclear@26 1168 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
nuclear@26 1169 png_push_crc_skip(png_ptr, length);
nuclear@26 1170 return;
nuclear@26 1171 }
nuclear@26 1172 #endif
nuclear@26 1173
nuclear@26 1174 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
nuclear@26 1175 (png_uint_32)(length + 1));
nuclear@26 1176 png_ptr->current_text[length] = '\0';
nuclear@26 1177 png_ptr->current_text_ptr = png_ptr->current_text;
nuclear@26 1178 png_ptr->current_text_size = (png_size_t)length;
nuclear@26 1179 png_ptr->current_text_left = (png_size_t)length;
nuclear@26 1180 png_ptr->process_mode = PNG_READ_zTXt_MODE;
nuclear@26 1181 }
nuclear@26 1182
nuclear@26 1183 void /* PRIVATE */
nuclear@26 1184 png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
nuclear@26 1185 {
nuclear@26 1186 if (png_ptr->buffer_size && png_ptr->current_text_left)
nuclear@26 1187 {
nuclear@26 1188 png_size_t text_size;
nuclear@26 1189
nuclear@26 1190 if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
nuclear@26 1191 text_size = png_ptr->buffer_size;
nuclear@26 1192 else
nuclear@26 1193 text_size = png_ptr->current_text_left;
nuclear@26 1194 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
nuclear@26 1195 png_ptr->current_text_left -= text_size;
nuclear@26 1196 png_ptr->current_text_ptr += text_size;
nuclear@26 1197 }
nuclear@26 1198 if (!(png_ptr->current_text_left))
nuclear@26 1199 {
nuclear@26 1200 png_textp text_ptr;
nuclear@26 1201 png_charp text;
nuclear@26 1202 png_charp key;
nuclear@26 1203 int ret;
nuclear@26 1204 png_size_t text_size, key_size;
nuclear@26 1205
nuclear@26 1206 if (png_ptr->buffer_size < 4)
nuclear@26 1207 {
nuclear@26 1208 png_push_save_buffer(png_ptr);
nuclear@26 1209 return;
nuclear@26 1210 }
nuclear@26 1211
nuclear@26 1212 png_push_crc_finish(png_ptr);
nuclear@26 1213
nuclear@26 1214 key = png_ptr->current_text;
nuclear@26 1215
nuclear@26 1216 for (text = key; *text; text++)
nuclear@26 1217 /* empty loop */ ;
nuclear@26 1218
nuclear@26 1219 /* zTXt can't have zero text */
nuclear@26 1220 if (text >= key + png_ptr->current_text_size)
nuclear@26 1221 {
nuclear@26 1222 png_ptr->current_text = NULL;
nuclear@26 1223 png_free(png_ptr, key);
nuclear@26 1224 return;
nuclear@26 1225 }
nuclear@26 1226
nuclear@26 1227 text++;
nuclear@26 1228
nuclear@26 1229 if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */
nuclear@26 1230 {
nuclear@26 1231 png_ptr->current_text = NULL;
nuclear@26 1232 png_free(png_ptr, key);
nuclear@26 1233 return;
nuclear@26 1234 }
nuclear@26 1235
nuclear@26 1236 text++;
nuclear@26 1237
nuclear@26 1238 png_ptr->zstream.next_in = (png_bytep )text;
nuclear@26 1239 png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
nuclear@26 1240 (text - key));
nuclear@26 1241 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@26 1242 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@26 1243
nuclear@26 1244 key_size = text - key;
nuclear@26 1245 text_size = 0;
nuclear@26 1246 text = NULL;
nuclear@26 1247 ret = Z_STREAM_END;
nuclear@26 1248
nuclear@26 1249 while (png_ptr->zstream.avail_in)
nuclear@26 1250 {
nuclear@26 1251 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
nuclear@26 1252 if (ret != Z_OK && ret != Z_STREAM_END)
nuclear@26 1253 {
nuclear@26 1254 inflateReset(&png_ptr->zstream);
nuclear@26 1255 png_ptr->zstream.avail_in = 0;
nuclear@26 1256 png_ptr->current_text = NULL;
nuclear@26 1257 png_free(png_ptr, key);
nuclear@26 1258 png_free(png_ptr, text);
nuclear@26 1259 return;
nuclear@26 1260 }
nuclear@26 1261 if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
nuclear@26 1262 {
nuclear@26 1263 if (text == NULL)
nuclear@26 1264 {
nuclear@26 1265 text = (png_charp)png_malloc(png_ptr,
nuclear@26 1266 (png_uint_32)(png_ptr->zbuf_size
nuclear@26 1267 - png_ptr->zstream.avail_out + key_size + 1));
nuclear@26 1268 png_memcpy(text + key_size, png_ptr->zbuf,
nuclear@26 1269 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
nuclear@26 1270 png_memcpy(text, key, key_size);
nuclear@26 1271 text_size = key_size + png_ptr->zbuf_size -
nuclear@26 1272 png_ptr->zstream.avail_out;
nuclear@26 1273 *(text + text_size) = '\0';
nuclear@26 1274 }
nuclear@26 1275 else
nuclear@26 1276 {
nuclear@26 1277 png_charp tmp;
nuclear@26 1278
nuclear@26 1279 tmp = text;
nuclear@26 1280 text = (png_charp)png_malloc(png_ptr, text_size +
nuclear@26 1281 (png_uint_32)(png_ptr->zbuf_size
nuclear@26 1282 - png_ptr->zstream.avail_out + 1));
nuclear@26 1283 png_memcpy(text, tmp, text_size);
nuclear@26 1284 png_free(png_ptr, tmp);
nuclear@26 1285 png_memcpy(text + text_size, png_ptr->zbuf,
nuclear@26 1286 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
nuclear@26 1287 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
nuclear@26 1288 *(text + text_size) = '\0';
nuclear@26 1289 }
nuclear@26 1290 if (ret != Z_STREAM_END)
nuclear@26 1291 {
nuclear@26 1292 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@26 1293 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@26 1294 }
nuclear@26 1295 }
nuclear@26 1296 else
nuclear@26 1297 {
nuclear@26 1298 break;
nuclear@26 1299 }
nuclear@26 1300
nuclear@26 1301 if (ret == Z_STREAM_END)
nuclear@26 1302 break;
nuclear@26 1303 }
nuclear@26 1304
nuclear@26 1305 inflateReset(&png_ptr->zstream);
nuclear@26 1306 png_ptr->zstream.avail_in = 0;
nuclear@26 1307
nuclear@26 1308 if (ret != Z_STREAM_END)
nuclear@26 1309 {
nuclear@26 1310 png_ptr->current_text = NULL;
nuclear@26 1311 png_free(png_ptr, key);
nuclear@26 1312 png_free(png_ptr, text);
nuclear@26 1313 return;
nuclear@26 1314 }
nuclear@26 1315
nuclear@26 1316 png_ptr->current_text = NULL;
nuclear@26 1317 png_free(png_ptr, key);
nuclear@26 1318 key = text;
nuclear@26 1319 text += key_size;
nuclear@26 1320
nuclear@26 1321 text_ptr = (png_textp)png_malloc(png_ptr,
nuclear@26 1322 (png_uint_32)png_sizeof(png_text));
nuclear@26 1323 text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
nuclear@26 1324 text_ptr->key = key;
nuclear@26 1325 #ifdef PNG_iTXt_SUPPORTED
nuclear@26 1326 text_ptr->lang = NULL;
nuclear@26 1327 text_ptr->lang_key = NULL;
nuclear@26 1328 #endif
nuclear@26 1329 text_ptr->text = text;
nuclear@26 1330
nuclear@26 1331 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@26 1332
nuclear@26 1333 png_free(png_ptr, key);
nuclear@26 1334 png_free(png_ptr, text_ptr);
nuclear@26 1335
nuclear@26 1336 if (ret)
nuclear@26 1337 png_warning(png_ptr, "Insufficient memory to store text chunk.");
nuclear@26 1338 }
nuclear@26 1339 }
nuclear@26 1340 #endif
nuclear@26 1341
nuclear@26 1342 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@26 1343 void /* PRIVATE */
nuclear@26 1344 png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
nuclear@26 1345 length)
nuclear@26 1346 {
nuclear@26 1347 if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
nuclear@26 1348 {
nuclear@26 1349 png_error(png_ptr, "Out of place iTXt");
nuclear@26 1350 info_ptr = info_ptr; /* to quiet some compiler warnings */
nuclear@26 1351 }
nuclear@26 1352
nuclear@26 1353 #ifdef PNG_MAX_MALLOC_64K
nuclear@26 1354 png_ptr->skip_length = 0; /* This may not be necessary */
nuclear@26 1355
nuclear@26 1356 if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
nuclear@26 1357 {
nuclear@26 1358 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
nuclear@26 1359 png_ptr->skip_length = length - (png_uint_32)65535L;
nuclear@26 1360 length = (png_uint_32)65535L;
nuclear@26 1361 }
nuclear@26 1362 #endif
nuclear@26 1363
nuclear@26 1364 png_ptr->current_text = (png_charp)png_malloc(png_ptr,
nuclear@26 1365 (png_uint_32)(length + 1));
nuclear@26 1366 png_ptr->current_text[length] = '\0';
nuclear@26 1367 png_ptr->current_text_ptr = png_ptr->current_text;
nuclear@26 1368 png_ptr->current_text_size = (png_size_t)length;
nuclear@26 1369 png_ptr->current_text_left = (png_size_t)length;
nuclear@26 1370 png_ptr->process_mode = PNG_READ_iTXt_MODE;
nuclear@26 1371 }
nuclear@26 1372
nuclear@26 1373 void /* PRIVATE */
nuclear@26 1374 png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
nuclear@26 1375 {
nuclear@26 1376
nuclear@26 1377 if (png_ptr->buffer_size && png_ptr->current_text_left)
nuclear@26 1378 {
nuclear@26 1379 png_size_t text_size;
nuclear@26 1380
nuclear@26 1381 if (png_ptr->buffer_size < png_ptr->current_text_left)
nuclear@26 1382 text_size = png_ptr->buffer_size;
nuclear@26 1383 else
nuclear@26 1384 text_size = png_ptr->current_text_left;
nuclear@26 1385 png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
nuclear@26 1386 png_ptr->current_text_left -= text_size;
nuclear@26 1387 png_ptr->current_text_ptr += text_size;
nuclear@26 1388 }
nuclear@26 1389 if (!(png_ptr->current_text_left))
nuclear@26 1390 {
nuclear@26 1391 png_textp text_ptr;
nuclear@26 1392 png_charp key;
nuclear@26 1393 int comp_flag;
nuclear@26 1394 png_charp lang;
nuclear@26 1395 png_charp lang_key;
nuclear@26 1396 png_charp text;
nuclear@26 1397 int ret;
nuclear@26 1398
nuclear@26 1399 if (png_ptr->buffer_size < 4)
nuclear@26 1400 {
nuclear@26 1401 png_push_save_buffer(png_ptr);
nuclear@26 1402 return;
nuclear@26 1403 }
nuclear@26 1404
nuclear@26 1405 png_push_crc_finish(png_ptr);
nuclear@26 1406
nuclear@26 1407 #if defined(PNG_MAX_MALLOC_64K)
nuclear@26 1408 if (png_ptr->skip_length)
nuclear@26 1409 return;
nuclear@26 1410 #endif
nuclear@26 1411
nuclear@26 1412 key = png_ptr->current_text;
nuclear@26 1413
nuclear@26 1414 for (lang = key; *lang; lang++)
nuclear@26 1415 /* empty loop */ ;
nuclear@26 1416
nuclear@26 1417 if (lang < key + png_ptr->current_text_size - 3)
nuclear@26 1418 lang++;
nuclear@26 1419
nuclear@26 1420 comp_flag = *lang++;
nuclear@26 1421 lang++; /* skip comp_type, always zero */
nuclear@26 1422
nuclear@26 1423 for (lang_key = lang; *lang_key; lang_key++)
nuclear@26 1424 /* empty loop */ ;
nuclear@26 1425 lang_key++; /* skip NUL separator */
nuclear@26 1426
nuclear@26 1427 text=lang_key;
nuclear@26 1428 if (lang_key < key + png_ptr->current_text_size - 1)
nuclear@26 1429 {
nuclear@26 1430 for (; *text; text++)
nuclear@26 1431 /* empty loop */ ;
nuclear@26 1432 }
nuclear@26 1433
nuclear@26 1434 if (text < key + png_ptr->current_text_size)
nuclear@26 1435 text++;
nuclear@26 1436
nuclear@26 1437 text_ptr = (png_textp)png_malloc(png_ptr,
nuclear@26 1438 (png_uint_32)png_sizeof(png_text));
nuclear@26 1439 text_ptr->compression = comp_flag + 2;
nuclear@26 1440 text_ptr->key = key;
nuclear@26 1441 text_ptr->lang = lang;
nuclear@26 1442 text_ptr->lang_key = lang_key;
nuclear@26 1443 text_ptr->text = text;
nuclear@26 1444 text_ptr->text_length = 0;
nuclear@26 1445 text_ptr->itxt_length = png_strlen(text);
nuclear@26 1446
nuclear@26 1447 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@26 1448
nuclear@26 1449 png_ptr->current_text = NULL;
nuclear@26 1450
nuclear@26 1451 png_free(png_ptr, text_ptr);
nuclear@26 1452 if (ret)
nuclear@26 1453 png_warning(png_ptr, "Insufficient memory to store iTXt chunk.");
nuclear@26 1454 }
nuclear@26 1455 }
nuclear@26 1456 #endif
nuclear@26 1457
nuclear@26 1458 /* This function is called when we haven't found a handler for this
nuclear@26 1459 * chunk. If there isn't a problem with the chunk itself (ie a bad chunk
nuclear@26 1460 * name or a critical chunk), the chunk is (currently) silently ignored.
nuclear@26 1461 */
nuclear@26 1462 void /* PRIVATE */
nuclear@26 1463 png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
nuclear@26 1464 length)
nuclear@26 1465 {
nuclear@26 1466 png_uint_32 skip = 0;
nuclear@26 1467
nuclear@26 1468 if (!(png_ptr->chunk_name[0] & 0x20))
nuclear@26 1469 {
nuclear@26 1470 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@26 1471 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
nuclear@26 1472 PNG_HANDLE_CHUNK_ALWAYS
nuclear@26 1473 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
nuclear@26 1474 && png_ptr->read_user_chunk_fn == NULL
nuclear@26 1475 #endif
nuclear@26 1476 )
nuclear@26 1477 #endif
nuclear@26 1478 png_chunk_error(png_ptr, "unknown critical chunk");
nuclear@26 1479
nuclear@26 1480 info_ptr = info_ptr; /* to quiet some compiler warnings */
nuclear@26 1481 }
nuclear@26 1482
nuclear@26 1483 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@26 1484 if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
nuclear@26 1485 {
nuclear@26 1486 #ifdef PNG_MAX_MALLOC_64K
nuclear@26 1487 if (length > (png_uint_32)65535L)
nuclear@26 1488 {
nuclear@26 1489 png_warning(png_ptr, "unknown chunk too large to fit in memory");
nuclear@26 1490 skip = length - (png_uint_32)65535L;
nuclear@26 1491 length = (png_uint_32)65535L;
nuclear@26 1492 }
nuclear@26 1493 #endif
nuclear@26 1494 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
nuclear@26 1495 (png_charp)png_ptr->chunk_name,
nuclear@26 1496 png_sizeof(png_ptr->unknown_chunk.name));
nuclear@26 1497 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
nuclear@26 1498 = '\0';
nuclear@26 1499
nuclear@26 1500 png_ptr->unknown_chunk.size = (png_size_t)length;
nuclear@26 1501 if (length == 0)
nuclear@26 1502 png_ptr->unknown_chunk.data = NULL;
nuclear@26 1503 else
nuclear@26 1504 {
nuclear@26 1505 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
nuclear@26 1506 (png_uint_32)length);
nuclear@26 1507 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
nuclear@26 1508 }
nuclear@26 1509 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
nuclear@26 1510 if (png_ptr->read_user_chunk_fn != NULL)
nuclear@26 1511 {
nuclear@26 1512 /* callback to user unknown chunk handler */
nuclear@26 1513 int ret;
nuclear@26 1514 ret = (*(png_ptr->read_user_chunk_fn))
nuclear@26 1515 (png_ptr, &png_ptr->unknown_chunk);
nuclear@26 1516 if (ret < 0)
nuclear@26 1517 png_chunk_error(png_ptr, "error in user chunk");
nuclear@26 1518 if (ret == 0)
nuclear@26 1519 {
nuclear@26 1520 if (!(png_ptr->chunk_name[0] & 0x20))
nuclear@26 1521 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
nuclear@26 1522 PNG_HANDLE_CHUNK_ALWAYS)
nuclear@26 1523 png_chunk_error(png_ptr, "unknown critical chunk");
nuclear@26 1524 png_set_unknown_chunks(png_ptr, info_ptr,
nuclear@26 1525 &png_ptr->unknown_chunk, 1);
nuclear@26 1526 }
nuclear@26 1527 }
nuclear@26 1528 else
nuclear@26 1529 #endif
nuclear@26 1530 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
nuclear@26 1531 png_free(png_ptr, png_ptr->unknown_chunk.data);
nuclear@26 1532 png_ptr->unknown_chunk.data = NULL;
nuclear@26 1533 }
nuclear@26 1534 else
nuclear@26 1535 #endif
nuclear@26 1536 skip=length;
nuclear@26 1537 png_push_crc_skip(png_ptr, skip);
nuclear@26 1538 }
nuclear@26 1539
nuclear@26 1540 void /* PRIVATE */
nuclear@26 1541 png_push_have_info(png_structp png_ptr, png_infop info_ptr)
nuclear@26 1542 {
nuclear@26 1543 if (png_ptr->info_fn != NULL)
nuclear@26 1544 (*(png_ptr->info_fn))(png_ptr, info_ptr);
nuclear@26 1545 }
nuclear@26 1546
nuclear@26 1547 void /* PRIVATE */
nuclear@26 1548 png_push_have_end(png_structp png_ptr, png_infop info_ptr)
nuclear@26 1549 {
nuclear@26 1550 if (png_ptr->end_fn != NULL)
nuclear@26 1551 (*(png_ptr->end_fn))(png_ptr, info_ptr);
nuclear@26 1552 }
nuclear@26 1553
nuclear@26 1554 void /* PRIVATE */
nuclear@26 1555 png_push_have_row(png_structp png_ptr, png_bytep row)
nuclear@26 1556 {
nuclear@26 1557 if (png_ptr->row_fn != NULL)
nuclear@26 1558 (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
nuclear@26 1559 (int)png_ptr->pass);
nuclear@26 1560 }
nuclear@26 1561
nuclear@26 1562 void PNGAPI
nuclear@26 1563 png_progressive_combine_row (png_structp png_ptr,
nuclear@26 1564 png_bytep old_row, png_bytep new_row)
nuclear@26 1565 {
nuclear@26 1566 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@26 1567 PNG_CONST int FARDATA png_pass_dsp_mask[7] =
nuclear@26 1568 {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
nuclear@26 1569 #endif
nuclear@26 1570 if (png_ptr == NULL) return;
nuclear@26 1571 if (new_row != NULL) /* new_row must == png_ptr->row_buf here. */
nuclear@26 1572 png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
nuclear@26 1573 }
nuclear@26 1574
nuclear@26 1575 void PNGAPI
nuclear@26 1576 png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
nuclear@26 1577 png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
nuclear@26 1578 png_progressive_end_ptr end_fn)
nuclear@26 1579 {
nuclear@26 1580 if (png_ptr == NULL) return;
nuclear@26 1581 png_ptr->info_fn = info_fn;
nuclear@26 1582 png_ptr->row_fn = row_fn;
nuclear@26 1583 png_ptr->end_fn = end_fn;
nuclear@26 1584
nuclear@26 1585 png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
nuclear@26 1586 }
nuclear@26 1587
nuclear@26 1588 png_voidp PNGAPI
nuclear@26 1589 png_get_progressive_ptr(png_structp png_ptr)
nuclear@26 1590 {
nuclear@26 1591 if (png_ptr == NULL) return (NULL);
nuclear@26 1592 return png_ptr->io_ptr;
nuclear@26 1593 }
nuclear@26 1594 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */