pngpread.c 47.3 KB
Newer Older
G
Guy Schalnat 已提交
1 2

/* pngpread.c - read a png file in push mode
3
 *
4
 * Last changed in libpng 1.5.0 [July 30, 2010]
5
 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
 *
9
 * This code is released under the libpng license.
10
 * For conditions of distribution and use, see the disclaimer
11
 * and license in png.h
12
 */
G
Guy Schalnat 已提交
13

14
#include "pngpriv.h"
G
Guy Schalnat 已提交
15

16 17
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED

18
/* Push model modes */
19 20 21 22 23 24 25 26 27 28
#define PNG_READ_SIG_MODE   0
#define PNG_READ_CHUNK_MODE 1
#define PNG_READ_IDAT_MODE  2
#define PNG_SKIP_MODE       3
#define PNG_READ_tEXt_MODE  4
#define PNG_READ_zTXt_MODE  5
#define PNG_READ_DONE_MODE  6
#define PNG_READ_iTXt_MODE  7
#define PNG_ERROR_MODE      8

29
void PNGAPI
A
Andreas Dilger 已提交
30
png_process_data(png_structp png_ptr, png_infop info_ptr,
31
    png_bytep buffer, png_size_t buffer_size)
G
Guy Schalnat 已提交
32
{
33 34 35
   if (png_ptr == NULL || info_ptr == NULL)
      return;

G
Guy Schalnat 已提交
36
   png_push_restore_buffer(png_ptr, buffer, buffer_size);
G
Guy Schalnat 已提交
37

G
Guy Schalnat 已提交
38 39
   while (png_ptr->buffer_size)
   {
A
Andreas Dilger 已提交
40
      png_process_some_data(png_ptr, info_ptr);
G
Guy Schalnat 已提交
41
   }
G
Guy Schalnat 已提交
42 43
}

A
Andreas Dilger 已提交
44 45 46
/* What we do with the incoming data depends on what we were previously
 * doing before we ran out of data...
 */
47
void /* PRIVATE */
A
Andreas Dilger 已提交
48
png_process_some_data(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
49
{
50 51 52
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
53 54 55 56
   switch (png_ptr->process_mode)
   {
      case PNG_READ_SIG_MODE:
      {
A
Andreas Dilger 已提交
57
         png_push_read_sig(png_ptr, info_ptr);
G
Guy Schalnat 已提交
58 59
         break;
      }
60

G
Guy Schalnat 已提交
61 62
      case PNG_READ_CHUNK_MODE:
      {
A
Andreas Dilger 已提交
63
         png_push_read_chunk(png_ptr, info_ptr);
G
Guy Schalnat 已提交
64 65
         break;
      }
66

G
Guy Schalnat 已提交
67 68
      case PNG_READ_IDAT_MODE:
      {
G
Guy Schalnat 已提交
69
         png_push_read_IDAT(png_ptr);
G
Guy Schalnat 已提交
70 71
         break;
      }
72

73
#ifdef PNG_READ_tEXt_SUPPORTED
G
Guy Schalnat 已提交
74 75
      case PNG_READ_tEXt_MODE:
      {
A
Andreas Dilger 已提交
76
         png_push_read_tEXt(png_ptr, info_ptr);
G
Guy Schalnat 已提交
77 78
         break;
      }
79

G
Guy Schalnat 已提交
80
#endif
81
#ifdef PNG_READ_zTXt_SUPPORTED
G
Guy Schalnat 已提交
82 83
      case PNG_READ_zTXt_MODE:
      {
A
Andreas Dilger 已提交
84
         png_push_read_zTXt(png_ptr, info_ptr);
G
Guy Schalnat 已提交
85 86
         break;
      }
87

88
#endif
89
#ifdef PNG_READ_iTXt_SUPPORTED
90 91 92 93 94
      case PNG_READ_iTXt_MODE:
      {
         png_push_read_iTXt(png_ptr, info_ptr);
         break;
      }
95

G
Guy Schalnat 已提交
96 97 98
#endif
      case PNG_SKIP_MODE:
      {
A
Andreas Dilger 已提交
99
         png_push_crc_finish(png_ptr);
G
Guy Schalnat 已提交
100 101
         break;
      }
102

G
Guy Schalnat 已提交
103 104 105 106 107 108
      default:
      {
         png_ptr->buffer_size = 0;
         break;
      }
   }
G
Guy Schalnat 已提交
109 110
}

A
Andreas Dilger 已提交
111 112
/* Read any remaining signature bytes from the stream and compare them with
 * the correct PNG signature.  It is possible that this routine is called
113 114 115
 * with bytes already read from the signature, either because they have been
 * checked by the calling application, or because of multiple calls to this
 * routine.
A
Andreas Dilger 已提交
116
 */
117
void /* PRIVATE */
A
Andreas Dilger 已提交
118
png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
119
{
A
Andreas Dilger 已提交
120 121
   png_size_t num_checked = png_ptr->sig_bytes,
             num_to_check = 8 - num_checked;
G
Guy Schalnat 已提交
122

A
Andreas Dilger 已提交
123
   if (png_ptr->buffer_size < num_to_check)
G
Guy Schalnat 已提交
124
   {
A
Andreas Dilger 已提交
125
      num_to_check = png_ptr->buffer_size;
G
Guy Schalnat 已提交
126 127
   }

128
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
129
       num_to_check);
130
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
G
Guy Schalnat 已提交
131

A
Andreas Dilger 已提交
132
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
G
Guy Schalnat 已提交
133
   {
A
Andreas Dilger 已提交
134 135 136
      if (num_checked < 4 &&
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
         png_error(png_ptr, "Not a PNG file");
137

A
Andreas Dilger 已提交
138 139
      else
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
G
Guy Schalnat 已提交
140 141 142
   }
   else
   {
A
Andreas Dilger 已提交
143 144 145 146
      if (png_ptr->sig_bytes >= 8)
      {
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
      }
G
Guy Schalnat 已提交
147
   }
G
Guy Schalnat 已提交
148 149
}

150
void /* PRIVATE */
A
Andreas Dilger 已提交
151
png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
152
{
153 154 155 156
      PNG_IHDR;
      PNG_IDAT;
      PNG_IEND;
      PNG_PLTE;
157
#ifdef PNG_READ_bKGD_SUPPORTED
158
      PNG_bKGD;
159
#endif
160
#ifdef PNG_READ_cHRM_SUPPORTED
161
      PNG_cHRM;
162
#endif
163
#ifdef PNG_READ_gAMA_SUPPORTED
164
      PNG_gAMA;
165
#endif
166
#ifdef PNG_READ_hIST_SUPPORTED
167
      PNG_hIST;
168
#endif
169
#ifdef PNG_READ_iCCP_SUPPORTED
170
      PNG_iCCP;
171
#endif
172
#ifdef PNG_READ_iTXt_SUPPORTED
173
      PNG_iTXt;
174
#endif
175
#ifdef PNG_READ_oFFs_SUPPORTED
176
      PNG_oFFs;
177
#endif
178
#ifdef PNG_READ_pCAL_SUPPORTED
179
      PNG_pCAL;
180
#endif
181
#ifdef PNG_READ_pHYs_SUPPORTED
182
      PNG_pHYs;
183
#endif
184
#ifdef PNG_READ_sBIT_SUPPORTED
185
      PNG_sBIT;
186
#endif
187
#ifdef PNG_READ_sCAL_SUPPORTED
188
      PNG_sCAL;
189
#endif
190
#ifdef PNG_READ_sRGB_SUPPORTED
191
      PNG_sRGB;
192
#endif
193
#ifdef PNG_READ_sPLT_SUPPORTED
194
      PNG_sPLT;
195
#endif
196
#ifdef PNG_READ_tEXt_SUPPORTED
197
      PNG_tEXt;
198
#endif
199
#ifdef PNG_READ_tIME_SUPPORTED
200
      PNG_tIME;
201
#endif
202
#ifdef PNG_READ_tRNS_SUPPORTED
203
      PNG_tRNS;
204
#endif
205
#ifdef PNG_READ_zTXt_SUPPORTED
206
      PNG_zTXt;
207
#endif
208

A
Andreas Dilger 已提交
209
   /* First we make sure we have enough data for the 4 byte chunk name
210 211 212 213 214
    * and the 4 byte chunk length before proceeding with decoding the
    * chunk data.  To fully decode each of these chunks, we also make
    * sure we have enough data in the buffer for the 4 byte CRC at the
    * end of every chunk (except IDAT, which is handled separately).
    */
215
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
G
Guy Schalnat 已提交
216
   {
A
Andreas Dilger 已提交
217
      png_byte chunk_length[4];
G
Guy Schalnat 已提交
218 219 220 221 222 223 224

      if (png_ptr->buffer_size < 8)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
225
      png_push_fill_buffer(png_ptr, chunk_length, 4);
226
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
G
Guy Schalnat 已提交
227
      png_reset_crc(png_ptr);
A
Andreas Dilger 已提交
228
      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
229
      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
230
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
G
Guy Schalnat 已提交
231 232
   }

233
   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
234 235
      if (png_ptr->mode & PNG_AFTER_IDAT)
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
236

A
Andreas Dilger 已提交
237
   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
G
Guy Schalnat 已提交
238
   {
239 240
      if (png_ptr->push_length != 13)
         png_error(png_ptr, "Invalid IHDR length");
241

242 243 244
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
245
         return;
246
      }
247

A
Andreas Dilger 已提交
248
      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
249
   }
250

251 252
   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
   {
253 254 255
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
256
         return;
257
      }
258

259 260 261 262 263
      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);

      png_ptr->process_mode = PNG_READ_DONE_MODE;
      png_push_have_end(png_ptr, info_ptr);
   }
264

265 266 267
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
   {
268 269 270
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
271
         return;
272
      }
273

274 275
      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
         png_ptr->mode |= PNG_HAVE_IDAT;
276

277
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
278

279 280
      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
         png_ptr->mode |= PNG_HAVE_PLTE;
281

282 283 284 285
      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
      {
         if (!(png_ptr->mode & PNG_HAVE_IHDR))
            png_error(png_ptr, "Missing IHDR before IDAT");
286

287
         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
288
             !(png_ptr->mode & PNG_HAVE_PLTE))
289 290 291
            png_error(png_ptr, "Missing PLTE before IDAT");
      }
   }
292

293
#endif
A
Andreas Dilger 已提交
294
   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
G
Guy Schalnat 已提交
295
   {
296 297 298
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
A
Andreas Dilger 已提交
299
         return;
300
      }
A
Andreas Dilger 已提交
301
      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
302
   }
303

304
   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
G
Guy Schalnat 已提交
305
   {
A
Andreas Dilger 已提交
306
      /* If we reach an IDAT chunk, this means we have read all of the
307 308 309
       * header chunks, and we can start reading the image (or if this
       * is called after the image has been read - we have an error).
       */
310 311 312 313 314 315 316

      if (!(png_ptr->mode & PNG_HAVE_IHDR))
         png_error(png_ptr, "Missing IHDR before IDAT");

      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
          !(png_ptr->mode & PNG_HAVE_PLTE))
         png_error(png_ptr, "Missing PLTE before IDAT");
317

A
Andreas Dilger 已提交
318 319
      if (png_ptr->mode & PNG_HAVE_IDAT)
      {
320
         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
321 322
            if (png_ptr->push_length == 0)
               return;
A
Andreas Dilger 已提交
323 324

         if (png_ptr->mode & PNG_AFTER_IDAT)
325
            png_benign_error(png_ptr, "Too many IDATs found");
A
Andreas Dilger 已提交
326 327
      }

G
Guy Schalnat 已提交
328
      png_ptr->idat_size = png_ptr->push_length;
A
Andreas Dilger 已提交
329
      png_ptr->mode |= PNG_HAVE_IDAT;
G
Guy Schalnat 已提交
330
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
A
Andreas Dilger 已提交
331
      png_push_have_info(png_ptr, info_ptr);
332 333 334
      png_ptr->zstream.avail_out =
          (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
          png_ptr->iwidth) + 1;
A
Andreas Dilger 已提交
335
      png_ptr->zstream.next_out = png_ptr->row_buf;
G
Guy Schalnat 已提交
336 337
      return;
   }
338

339
#ifdef PNG_READ_gAMA_SUPPORTED
A
Andreas Dilger 已提交
340
   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
G
Guy Schalnat 已提交
341
   {
342 343 344
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
345
         return;
346
      }
347

A
Andreas Dilger 已提交
348
      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
349
   }
350

G
Guy Schalnat 已提交
351
#endif
352
#ifdef PNG_READ_sBIT_SUPPORTED
A
Andreas Dilger 已提交
353
   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
G
Guy Schalnat 已提交
354
   {
355 356 357
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
358
         return;
359
      }
360

A
Andreas Dilger 已提交
361
      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
362
   }
363

G
Guy Schalnat 已提交
364
#endif
365
#ifdef PNG_READ_cHRM_SUPPORTED
A
Andreas Dilger 已提交
366
   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
G
Guy Schalnat 已提交
367
   {
368 369 370
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
371
         return;
372
      }
373

A
Andreas Dilger 已提交
374
      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
375
   }
376

G
Guy Schalnat 已提交
377
#endif
378
#ifdef PNG_READ_sRGB_SUPPORTED
379 380
   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
   {
381 382 383
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
384
         return;
385
      }
386

387 388
      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
   }
389

390
#endif
391
#ifdef PNG_READ_iCCP_SUPPORTED
392 393
   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
   {
394 395 396
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
397
         return;
398
      }
399

400 401
      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
   }
402

403
#endif
404
#ifdef PNG_READ_sPLT_SUPPORTED
405 406
   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
   {
407 408 409
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
410
         return;
411
      }
412

413 414
      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
   }
415

416
#endif
417
#ifdef PNG_READ_tRNS_SUPPORTED
A
Andreas Dilger 已提交
418
   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
G
Guy Schalnat 已提交
419
   {
420 421 422
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
423
         return;
424
      }
425

A
Andreas Dilger 已提交
426
      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
427
   }
428

G
Guy Schalnat 已提交
429
#endif
430
#ifdef PNG_READ_bKGD_SUPPORTED
A
Andreas Dilger 已提交
431
   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
G
Guy Schalnat 已提交
432
   {
433 434 435
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
436
         return;
437
      }
438

A
Andreas Dilger 已提交
439
      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
440
   }
441

G
Guy Schalnat 已提交
442
#endif
443
#ifdef PNG_READ_hIST_SUPPORTED
A
Andreas Dilger 已提交
444
   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
G
Guy Schalnat 已提交
445
   {
446 447 448
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
449
         return;
450
      }
451

A
Andreas Dilger 已提交
452
      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
453
   }
454

G
Guy Schalnat 已提交
455
#endif
456
#ifdef PNG_READ_pHYs_SUPPORTED
A
Andreas Dilger 已提交
457
   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
G
Guy Schalnat 已提交
458 459 460 461 462 463
   {
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
         return;
      }
464

A
Andreas Dilger 已提交
465
      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
466
   }
467

G
Guy Schalnat 已提交
468
#endif
469
#ifdef PNG_READ_oFFs_SUPPORTED
A
Andreas Dilger 已提交
470
   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
G
Guy Schalnat 已提交
471
   {
472 473 474
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
475
         return;
476
      }
477

A
Andreas Dilger 已提交
478
      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
479
   }
G
Guy Schalnat 已提交
480
#endif
481

482
#ifdef PNG_READ_pCAL_SUPPORTED
A
Andreas Dilger 已提交
483 484
   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
   {
485 486 487
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
A
Andreas Dilger 已提交
488
         return;
489
      }
490

A
Andreas Dilger 已提交
491 492
      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
493

A
Andreas Dilger 已提交
494
#endif
495
#ifdef PNG_READ_sCAL_SUPPORTED
496 497
   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
   {
498 499 500
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
501
         return;
502
      }
503

504 505
      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
   }
506

507
#endif
508
#ifdef PNG_READ_tIME_SUPPORTED
A
Andreas Dilger 已提交
509
   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
G
Guy Schalnat 已提交
510
   {
511 512 513
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
G
Guy Schalnat 已提交
514
         return;
515
      }
516

A
Andreas Dilger 已提交
517
      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
518
   }
519

G
Guy Schalnat 已提交
520
#endif
521
#ifdef PNG_READ_tEXt_SUPPORTED
A
Andreas Dilger 已提交
522
   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
G
Guy Schalnat 已提交
523
   {
524 525 526
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
527
         return;
528
      }
529

A
Andreas Dilger 已提交
530
      png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
531
   }
532

G
Guy Schalnat 已提交
533
#endif
534
#ifdef PNG_READ_zTXt_SUPPORTED
A
Andreas Dilger 已提交
535
   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
G
Guy Schalnat 已提交
536
   {
537 538 539
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
540
         return;
541
      }
542

A
Andreas Dilger 已提交
543
      png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
544
   }
545

546
#endif
547
#ifdef PNG_READ_iTXt_SUPPORTED
548
   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
549
   {
550 551 552
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
553
         return;
554
      }
555

556 557
      png_push_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
   }
558

G
Guy Schalnat 已提交
559
#endif
G
Guy Schalnat 已提交
560 561
   else
   {
562 563 564
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
      {
         png_push_save_buffer(png_ptr);
565
         return;
566
      }
A
Andreas Dilger 已提交
567
      png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
G
Guy Schalnat 已提交
568
   }
G
Guy Schalnat 已提交
569

570
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
G
Guy Schalnat 已提交
571 572
}

573
void /* PRIVATE */
A
Andreas Dilger 已提交
574
png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
G
Guy Schalnat 已提交
575
{
G
Guy Schalnat 已提交
576
   png_ptr->process_mode = PNG_SKIP_MODE;
A
Andreas Dilger 已提交
577
   png_ptr->skip_length = skip;
G
Guy Schalnat 已提交
578 579
}

580
void /* PRIVATE */
A
Andreas Dilger 已提交
581
png_push_crc_finish(png_structp png_ptr)
G
Guy Schalnat 已提交
582
{
G
Guy Schalnat 已提交
583 584
   if (png_ptr->skip_length && png_ptr->save_buffer_size)
   {
A
Andreas Dilger 已提交
585
      png_size_t save_size;
G
Guy Schalnat 已提交
586

A
Andreas Dilger 已提交
587 588
      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
         save_size = (png_size_t)png_ptr->skip_length;
589

G
Guy Schalnat 已提交
590 591 592 593 594 595 596 597
      else
         save_size = png_ptr->save_buffer_size;

      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);

      png_ptr->skip_length -= save_size;
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
598
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
599 600 601
   }
   if (png_ptr->skip_length && png_ptr->current_buffer_size)
   {
A
Andreas Dilger 已提交
602
      png_size_t save_size;
G
Guy Schalnat 已提交
603

A
Andreas Dilger 已提交
604 605
      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
         save_size = (png_size_t)png_ptr->skip_length;
606

G
Guy Schalnat 已提交
607 608 609 610 611 612 613 614
      else
         save_size = png_ptr->current_buffer_size;

      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);

      png_ptr->skip_length -= save_size;
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
615
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
616 617 618 619 620 621 622 623
   }
   if (!png_ptr->skip_length)
   {
      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }
A
Andreas Dilger 已提交
624 625

      png_crc_finish(png_ptr, 0);
A
Andreas Dilger 已提交
626
      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
G
Guy Schalnat 已提交
627
   }
G
Guy Schalnat 已提交
628 629
}

630
void PNGCBAPI
A
Andreas Dilger 已提交
631
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
G
Guy Schalnat 已提交
632
{
G
Guy Schalnat 已提交
633 634
   png_bytep ptr;

635 636 637
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
638 639 640
   ptr = buffer;
   if (png_ptr->save_buffer_size)
   {
A
Andreas Dilger 已提交
641
      png_size_t save_size;
G
Guy Schalnat 已提交
642 643 644

      if (length < png_ptr->save_buffer_size)
         save_size = length;
645

G
Guy Schalnat 已提交
646 647 648
      else
         save_size = png_ptr->save_buffer_size;

A
Andreas Dilger 已提交
649
      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
G
Guy Schalnat 已提交
650
      length -= save_size;
A
Andreas Dilger 已提交
651
      ptr += save_size;
G
Guy Schalnat 已提交
652 653
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
654
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
655 656 657
   }
   if (length && png_ptr->current_buffer_size)
   {
A
Andreas Dilger 已提交
658
      png_size_t save_size;
G
Guy Schalnat 已提交
659 660 661

      if (length < png_ptr->current_buffer_size)
         save_size = length;
662

G
Guy Schalnat 已提交
663 664 665
      else
         save_size = png_ptr->current_buffer_size;

A
Andreas Dilger 已提交
666
      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
G
Guy Schalnat 已提交
667 668
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
669
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
670
   }
G
Guy Schalnat 已提交
671 672
}

673
void /* PRIVATE */
G
Guy Schalnat 已提交
674 675
png_push_save_buffer(png_structp png_ptr)
{
G
Guy Schalnat 已提交
676 677 678 679
   if (png_ptr->save_buffer_size)
   {
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
      {
680
         png_size_t i, istop;
G
Guy Schalnat 已提交
681 682 683
         png_bytep sp;
         png_bytep dp;

684
         istop = png_ptr->save_buffer_size;
G
Guy Schalnat 已提交
685
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
686
             i < istop; i++, sp++, dp++)
G
Guy Schalnat 已提交
687 688 689 690 691
         {
            *dp = *sp;
         }
      }
   }
692
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
693
       png_ptr->save_buffer_max)
G
Guy Schalnat 已提交
694
   {
A
Andreas Dilger 已提交
695
      png_size_t new_max;
G
Guy Schalnat 已提交
696 697
      png_bytep old_buffer;

698
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
699
          (png_ptr->current_buffer_size + 256))
700
      {
701
         png_error(png_ptr, "Potential overflow of save_buffer");
702
      }
703

704
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
G
Guy Schalnat 已提交
705
      old_buffer = png_ptr->save_buffer;
706
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
707
          (png_size_t)new_max);
708

709 710
      if (png_ptr->save_buffer == NULL)
      {
711 712
         png_free(png_ptr, old_buffer);
         png_error(png_ptr, "Insufficient memory for save_buffer");
713
      }
714

715 716 717
      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
      png_free(png_ptr, old_buffer);
      png_ptr->save_buffer_max = new_max;
G
Guy Schalnat 已提交
718 719 720
   }
   if (png_ptr->current_buffer_size)
   {
A
Andreas Dilger 已提交
721 722
      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
G
Guy Schalnat 已提交
723 724 725 726 727
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
      png_ptr->current_buffer_size = 0;
   }
   png_ptr->save_buffer_ptr = png_ptr->save_buffer;
   png_ptr->buffer_size = 0;
G
Guy Schalnat 已提交
728 729
}

730
void /* PRIVATE */
G
Guy Schalnat 已提交
731
png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
A
Andreas Dilger 已提交
732
   png_size_t buffer_length)
G
Guy Schalnat 已提交
733
{
G
Guy Schalnat 已提交
734 735 736 737
   png_ptr->current_buffer = buffer;
   png_ptr->current_buffer_size = buffer_length;
   png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
   png_ptr->current_buffer_ptr = png_ptr->current_buffer;
G
Guy Schalnat 已提交
738 739
}

740
void /* PRIVATE */
G
Guy Schalnat 已提交
741
png_push_read_IDAT(png_structp png_ptr)
G
Guy Schalnat 已提交
742
{
743
   PNG_IDAT;
744
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
G
Guy Schalnat 已提交
745
   {
A
Andreas Dilger 已提交
746
      png_byte chunk_length[4];
G
Guy Schalnat 已提交
747 748 749 750 751 752 753

      if (png_ptr->buffer_size < 8)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
754
      png_push_fill_buffer(png_ptr, chunk_length, 4);
755
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
G
Guy Schalnat 已提交
756
      png_reset_crc(png_ptr);
A
Andreas Dilger 已提交
757
      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
758
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
A
Andreas Dilger 已提交
759

760
      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
G
Guy Schalnat 已提交
761
      {
A
Andreas Dilger 已提交
762
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
763

G
Guy Schalnat 已提交
764
         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
G
Guy Schalnat 已提交
765
            png_error(png_ptr, "Not enough compressed data");
766

G
Guy Schalnat 已提交
767 768 769 770 771 772 773
         return;
      }

      png_ptr->idat_size = png_ptr->push_length;
   }
   if (png_ptr->idat_size && png_ptr->save_buffer_size)
   {
A
Andreas Dilger 已提交
774
      png_size_t save_size;
G
Guy Schalnat 已提交
775

A
Andreas Dilger 已提交
776
      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
777 778
      {
         save_size = (png_size_t)png_ptr->idat_size;
779

780
         /* Check for overflow */
781
         if ((png_uint_32)save_size != png_ptr->idat_size)
782 783
            png_error(png_ptr, "save_size overflowed in pngpread");
      }
784

G
Guy Schalnat 已提交
785 786 787 788
      else
         save_size = png_ptr->save_buffer_size;

      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
789

790
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
791

G
Guy Schalnat 已提交
792 793 794
      png_ptr->idat_size -= save_size;
      png_ptr->buffer_size -= save_size;
      png_ptr->save_buffer_size -= save_size;
A
Andreas Dilger 已提交
795
      png_ptr->save_buffer_ptr += save_size;
G
Guy Schalnat 已提交
796 797 798
   }
   if (png_ptr->idat_size && png_ptr->current_buffer_size)
   {
A
Andreas Dilger 已提交
799
      png_size_t save_size;
G
Guy Schalnat 已提交
800

A
Andreas Dilger 已提交
801
      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
802 803
      {
         save_size = (png_size_t)png_ptr->idat_size;
804

805
         /* Check for overflow */
806
         if ((png_uint_32)save_size != png_ptr->idat_size)
807 808
            png_error(png_ptr, "save_size overflowed in pngpread");
      }
G
Guy Schalnat 已提交
809 810 811 812
      else
         save_size = png_ptr->current_buffer_size;

      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
813

814
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
G
Guy Schalnat 已提交
815 816 817 818

      png_ptr->idat_size -= save_size;
      png_ptr->buffer_size -= save_size;
      png_ptr->current_buffer_size -= save_size;
A
Andreas Dilger 已提交
819
      png_ptr->current_buffer_ptr += save_size;
G
Guy Schalnat 已提交
820 821 822 823 824 825 826 827 828
   }
   if (!png_ptr->idat_size)
   {
      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
829
      png_crc_finish(png_ptr, 0);
830
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
831
      png_ptr->mode |= PNG_AFTER_IDAT;
G
Guy Schalnat 已提交
832
   }
G
Guy Schalnat 已提交
833 834
}

835
void /* PRIVATE */
G
Guy Schalnat 已提交
836
png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
A
Andreas Dilger 已提交
837
   png_size_t buffer_length)
G
Guy Schalnat 已提交
838
{
839
   /* The caller checks for a non-zero buffer length. */
840
   if (!(buffer_length > 0) || buffer == NULL)
841
      png_error(png_ptr, "No IDAT data (internal error)");
G
Guy Schalnat 已提交
842

843 844
   /* This routine must process all the data it has been given
    * before returning, calling the row callback as required to
845
    * handle the uncompressed results.
846
    */
A
Andreas Dilger 已提交
847 848
   png_ptr->zstream.next_in = buffer;
   png_ptr->zstream.avail_in = (uInt)buffer_length;
849 850 851 852 853

   /* Keep going until the decompressed data is all processed
    * or the stream marked as finished.
    */
   while (png_ptr->zstream.avail_in > 0 &&
854
          !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
G
Guy Schalnat 已提交
855
   {
856 857 858
      int ret;

      /* We have data for zlib, but we must check that zlib
859
       * has someplace to put the results.  It doesn't matter
860
       * if we don't expect any results -- it may be the input
861 862 863
       * data is just the LZ end code.
       */
      if (!(png_ptr->zstream.avail_out > 0))
G
Guy Schalnat 已提交
864
      {
865 866 867
         png_ptr->zstream.avail_out =
             (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
             png_ptr->iwidth) + 1;
868

869 870 871 872
         png_ptr->zstream.next_out = png_ptr->row_buf;
      }

      /* Using Z_SYNC_FLUSH here means that an unterminated
873 874 875 876 877
       * LZ stream (a stream with a missing end code) can still
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
       * implementation might defer output and therefore
       * change the current behavior (see comments in inflate.c
       * for why this doesn't happen at present with zlib 1.2.5).
878 879
       */
      ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
880

881 882 883
      /* Check for any failure before proceeding. */
      if (ret != Z_OK && ret != Z_STREAM_END)
      {
884 885
         /* Terminate the decompression. */
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
886

887
         /* This may be a truncated stream (missing or
888 889
          * damaged end code).  Treat that as a warning.
          */
890
         if (png_ptr->row_number >= png_ptr->num_rows ||
891 892
             png_ptr->pass > 6)
            png_warning(png_ptr, "Truncated compressed data in IDAT");
893

894 895
         else
            png_error(png_ptr, "Decompression error in IDAT");
896

897
         /* Skip the check on unprocessed input */
898
         return;
G
Guy Schalnat 已提交
899
      }
900 901 902

      /* Did inflate output any data? */
      if (png_ptr->zstream.next_out != png_ptr->row_buf)
G
Guy Schalnat 已提交
903
      {
904 905 906 907
         /* Is this unexpected data after the last row?
          * If it is, artificially terminate the LZ output
          * here.
          */
908
         if (png_ptr->row_number >= png_ptr->num_rows ||
909
             png_ptr->pass > 6)
910
         {
911 912
            /* Extra data. */
            png_warning(png_ptr, "Extra compressed data in IDAT");
913
            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
914

915 916 917
            /* Do no more processing; skip the unprocessed
             * input check below.
             */
918
            return;
919
         }
920

921 922 923
         /* Do we have a complete row? */
         if (png_ptr->zstream.avail_out == 0)
            png_push_process_row(png_ptr);
G
Guy Schalnat 已提交
924
      }
925 926 927

      /* And check for the end of the stream. */
      if (ret == Z_STREAM_END)
928
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
A
Andreas Dilger 已提交
929
   }
930 931 932 933 934 935

   /* All the data should have been processed, if anything
    * is left at this point we have bytes of IDAT data
    * after the zlib end code.
    */
   if (png_ptr->zstream.avail_in > 0)
936
      png_warning(png_ptr, "Extra compression data in IDAT");
G
Guy Schalnat 已提交
937 938
}

939
void /* PRIVATE */
G
Guy Schalnat 已提交
940 941
png_push_process_row(png_structp png_ptr)
{
G
Guy Schalnat 已提交
942
   png_ptr->row_info.color_type = png_ptr->color_type;
G
Guy Schalnat 已提交
943 944 945 946
   png_ptr->row_info.width = png_ptr->iwidth;
   png_ptr->row_info.channels = png_ptr->channels;
   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
947

948 949
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
       png_ptr->row_info.width);
G
Guy Schalnat 已提交
950

G
Guy Schalnat 已提交
951
   png_read_filter_row(png_ptr, &(png_ptr->row_info),
952 953
       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
       (int)(png_ptr->row_buf[0]));
G
Guy Schalnat 已提交
954

955
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
G
Guy Schalnat 已提交
956

957
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
G
Guy Schalnat 已提交
958 959
      png_do_read_transformations(png_ptr);

960
#ifdef PNG_READ_INTERLACING_SUPPORTED
961
   /* Blow up interlaced rows to full size */
A
Andreas Dilger 已提交
962
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
G
Guy Schalnat 已提交
963 964
   {
      if (png_ptr->pass < 6)
965
/*       old interface (pre-1.0.9):
G
Guy Schalnat 已提交
966
         png_do_read_interlace(&(png_ptr->row_info),
967
             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
968 969
 */
         png_do_read_interlace(png_ptr);
G
Guy Schalnat 已提交
970

971 972
    switch (png_ptr->pass)
    {
G
Guy Schalnat 已提交
973 974 975 976 977 978
         case 0:
         {
            int i;
            for (i = 0; i < 8 && png_ptr->pass == 0; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
979
               png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
G
Guy Schalnat 已提交
980
            }
981

982
            if (png_ptr->pass == 2) /* Pass 1 might be empty */
983 984 985
            {
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
               {
986
                  png_push_have_row(png_ptr, NULL);
987 988 989
                  png_read_push_finish_row(png_ptr);
               }
            }
990

991 992 993 994
            if (png_ptr->pass == 4 && png_ptr->height <= 4)
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
995
                  png_push_have_row(png_ptr, NULL);
996 997
                  png_read_push_finish_row(png_ptr);
               }
998
            }
999

1000 1001
            if (png_ptr->pass == 6 && png_ptr->height <= 4)
            {
1002
                png_push_have_row(png_ptr, NULL);
1003 1004
                png_read_push_finish_row(png_ptr);
            }
1005

G
Guy Schalnat 已提交
1006 1007
            break;
         }
1008

G
Guy Schalnat 已提交
1009 1010 1011 1012 1013 1014 1015 1016
         case 1:
         {
            int i;
            for (i = 0; i < 8 && png_ptr->pass == 1; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1017

1018
            if (png_ptr->pass == 2) /* Skip top 4 generated rows */
G
Guy Schalnat 已提交
1019 1020 1021
            {
               for (i = 0; i < 4 && png_ptr->pass == 2; i++)
               {
1022
                  png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1023 1024 1025
                  png_read_push_finish_row(png_ptr);
               }
            }
1026

G
Guy Schalnat 已提交
1027 1028
            break;
         }
1029

G
Guy Schalnat 已提交
1030 1031 1032
         case 2:
         {
            int i;
1033

G
Guy Schalnat 已提交
1034 1035 1036 1037 1038
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1039

G
Guy Schalnat 已提交
1040 1041
            for (i = 0; i < 4 && png_ptr->pass == 2; i++)
            {
1042
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1043 1044
               png_read_push_finish_row(png_ptr);
            }
1045

1046
            if (png_ptr->pass == 4) /* Pass 3 might be empty */
1047 1048 1049
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
1050
                  png_push_have_row(png_ptr, NULL);
1051 1052 1053
                  png_read_push_finish_row(png_ptr);
               }
            }
1054

G
Guy Schalnat 已提交
1055 1056
            break;
         }
1057

G
Guy Schalnat 已提交
1058 1059 1060
         case 3:
         {
            int i;
1061

G
Guy Schalnat 已提交
1062 1063 1064 1065 1066
            for (i = 0; i < 4 && png_ptr->pass == 3; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1067

1068
            if (png_ptr->pass == 4) /* Skip top two generated rows */
G
Guy Schalnat 已提交
1069 1070 1071
            {
               for (i = 0; i < 2 && png_ptr->pass == 4; i++)
               {
1072
                  png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1073 1074 1075
                  png_read_push_finish_row(png_ptr);
               }
            }
1076

G
Guy Schalnat 已提交
1077 1078
            break;
         }
1079

G
Guy Schalnat 已提交
1080 1081 1082
         case 4:
         {
            int i;
1083

G
Guy Schalnat 已提交
1084 1085 1086 1087 1088
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1089

G
Guy Schalnat 已提交
1090 1091
            for (i = 0; i < 2 && png_ptr->pass == 4; i++)
            {
1092
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1093 1094
               png_read_push_finish_row(png_ptr);
            }
1095

1096
            if (png_ptr->pass == 6) /* Pass 5 might be empty */
1097
            {
1098
               png_push_have_row(png_ptr, NULL);
1099 1100
               png_read_push_finish_row(png_ptr);
            }
1101

G
Guy Schalnat 已提交
1102 1103
            break;
         }
1104

G
Guy Schalnat 已提交
1105 1106 1107
         case 5:
         {
            int i;
1108

G
Guy Schalnat 已提交
1109 1110 1111 1112 1113
            for (i = 0; i < 2 && png_ptr->pass == 5; i++)
            {
               png_push_have_row(png_ptr, png_ptr->row_buf + 1);
               png_read_push_finish_row(png_ptr);
            }
1114

1115
            if (png_ptr->pass == 6) /* Skip top generated row */
G
Guy Schalnat 已提交
1116
            {
1117
               png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1118 1119
               png_read_push_finish_row(png_ptr);
            }
1120

G
Guy Schalnat 已提交
1121 1122 1123 1124 1125 1126
            break;
         }
         case 6:
         {
            png_push_have_row(png_ptr, png_ptr->row_buf + 1);
            png_read_push_finish_row(png_ptr);
1127

G
Guy Schalnat 已提交
1128 1129
            if (png_ptr->pass != 6)
               break;
1130

1131
            png_push_have_row(png_ptr, NULL);
G
Guy Schalnat 已提交
1132 1133 1134 1135 1136
            png_read_push_finish_row(png_ptr);
         }
      }
   }
   else
G
Guy Schalnat 已提交
1137
#endif
G
Guy Schalnat 已提交
1138 1139 1140 1141
   {
      png_push_have_row(png_ptr, png_ptr->row_buf + 1);
      png_read_push_finish_row(png_ptr);
   }
G
Guy Schalnat 已提交
1142 1143
}

1144
void /* PRIVATE */
G
Guy Schalnat 已提交
1145 1146
png_read_push_finish_row(png_structp png_ptr)
{
1147
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1148

1149
   /* Start of interlace block */
1150
   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1151

1152
   /* Offset to next interlace block */
1153
   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1154

1155
   /* Start of interlace block in the y direction */
1156
   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1157

1158
   /* Offset to next interlace block in the y direction */
1159
   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1160

1161 1162
   /* Height of interlace block.  This is not currently used - if you need
    * it, uncomment it here and in png.h
1163
   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1164
   */
1165

G
Guy Schalnat 已提交
1166 1167
   png_ptr->row_number++;
   if (png_ptr->row_number < png_ptr->num_rows)
G
Guy Schalnat 已提交
1168
      return;
G
Guy Schalnat 已提交
1169

1170
#ifdef PNG_READ_INTERLACING_SUPPORTED
G
Guy Schalnat 已提交
1171 1172 1173
   if (png_ptr->interlaced)
   {
      png_ptr->row_number = 0;
1174 1175
      png_memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);

G
Guy Schalnat 已提交
1176 1177 1178
      do
      {
         png_ptr->pass++;
1179 1180 1181
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
             (png_ptr->pass == 5 && png_ptr->width < 2))
1182
            png_ptr->pass++;
1183

1184 1185
         if (png_ptr->pass > 7)
            png_ptr->pass--;
1186

G
Guy Schalnat 已提交
1187 1188
         if (png_ptr->pass >= 7)
            break;
1189

G
Guy Schalnat 已提交
1190
         png_ptr->iwidth = (png_ptr->width +
1191 1192 1193
             png_pass_inc[png_ptr->pass] - 1 -
             png_pass_start[png_ptr->pass]) /
             png_pass_inc[png_ptr->pass];
1194

G
Guy Schalnat 已提交
1195 1196
         if (png_ptr->transformations & PNG_INTERLACE)
            break;
1197 1198

         png_ptr->num_rows = (png_ptr->height +
1199 1200 1201
             png_pass_yinc[png_ptr->pass] - 1 -
             png_pass_ystart[png_ptr->pass]) /
             png_pass_yinc[png_ptr->pass];
1202 1203

      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
G
Guy Schalnat 已提交
1204
   }
1205
#endif /* PNG_READ_INTERLACING_SUPPORTED */
G
Guy Schalnat 已提交
1206 1207
}

1208
#ifdef PNG_READ_tEXt_SUPPORTED
1209
void /* PRIVATE */
1210
png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1211
    length)
G
Guy Schalnat 已提交
1212
{
1213
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1214 1215
      {
         png_error(png_ptr, "Out of place tEXt");
1216
         info_ptr = info_ptr; /* To quiet some compiler warnings */
1217
      }
A
Andreas Dilger 已提交
1218

A
Andreas Dilger 已提交
1219 1220 1221
#ifdef PNG_MAX_MALLOC_64K
   png_ptr->skip_length = 0;  /* This may not be necessary */

1222
   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
A
Andreas Dilger 已提交
1223 1224
   {
      png_warning(png_ptr, "tEXt chunk too large to fit in memory");
1225 1226
      png_ptr->skip_length = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
A
Andreas Dilger 已提交
1227 1228 1229
   }
#endif

1230
   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1231
       (png_size_t)(length + 1));
A
Andreas Dilger 已提交
1232
   png_ptr->current_text[length] = '\0';
G
Guy Schalnat 已提交
1233
   png_ptr->current_text_ptr = png_ptr->current_text;
1234 1235
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
G
Guy Schalnat 已提交
1236
   png_ptr->process_mode = PNG_READ_tEXt_MODE;
G
Guy Schalnat 已提交
1237 1238
}

1239
void /* PRIVATE */
A
Andreas Dilger 已提交
1240
png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
1241
{
G
Guy Schalnat 已提交
1242 1243
   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
A
Andreas Dilger 已提交
1244
      png_size_t text_size;
G
Guy Schalnat 已提交
1245 1246 1247

      if (png_ptr->buffer_size < png_ptr->current_text_left)
         text_size = png_ptr->buffer_size;
1248

G
Guy Schalnat 已提交
1249 1250
      else
         text_size = png_ptr->current_text_left;
1251

A
Andreas Dilger 已提交
1252
      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
G
Guy Schalnat 已提交
1253
      png_ptr->current_text_left -= text_size;
A
Andreas Dilger 已提交
1254
      png_ptr->current_text_ptr += text_size;
G
Guy Schalnat 已提交
1255 1256 1257
   }
   if (!(png_ptr->current_text_left))
   {
A
Andreas Dilger 已提交
1258
      png_textp text_ptr;
G
Guy Schalnat 已提交
1259 1260
      png_charp text;
      png_charp key;
1261
      int ret;
G
Guy Schalnat 已提交
1262 1263 1264 1265 1266 1267 1268

      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
1269 1270
      png_push_crc_finish(png_ptr);

1271
#ifdef PNG_MAX_MALLOC_64K
A
Andreas Dilger 已提交
1272 1273 1274
      if (png_ptr->skip_length)
         return;
#endif
G
Guy Schalnat 已提交
1275 1276 1277 1278

      key = png_ptr->current_text;

      for (text = key; *text; text++)
1279
         /* Empty loop */ ;
G
Guy Schalnat 已提交
1280

1281
      if (text < key + png_ptr->current_text_size)
G
Guy Schalnat 已提交
1282 1283
         text++;

1284
      text_ptr = (png_textp)png_malloc(png_ptr, png_sizeof(png_text));
A
Andreas Dilger 已提交
1285 1286
      text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
      text_ptr->key = key;
1287
      text_ptr->itxt_length = 0;
1288 1289
      text_ptr->lang = NULL;
      text_ptr->lang_key = NULL;
A
Andreas Dilger 已提交
1290
      text_ptr->text = text;
G
Guy Schalnat 已提交
1291

1292
      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
A
Andreas Dilger 已提交
1293

1294
      png_free(png_ptr, key);
A
Andreas Dilger 已提交
1295
      png_free(png_ptr, text_ptr);
1296
      png_ptr->current_text = NULL;
1297 1298

      if (ret)
1299
         png_warning(png_ptr, "Insufficient memory to store text chunk");
G
Guy Schalnat 已提交
1300
   }
G
Guy Schalnat 已提交
1301 1302 1303
}
#endif

1304
#ifdef PNG_READ_zTXt_SUPPORTED
1305
void /* PRIVATE */
1306 1307
png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
   length)
G
Guy Schalnat 已提交
1308
{
1309
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1310 1311 1312 1313
   {
      png_error(png_ptr, "Out of place zTXt");
      info_ptr = info_ptr; /* To quiet some compiler warnings */
   }
A
Andreas Dilger 已提交
1314

A
Andreas Dilger 已提交
1315 1316 1317 1318 1319
#ifdef PNG_MAX_MALLOC_64K
   /* We can't handle zTXt chunks > 64K, since we don't have enough space
    * to be able to store the uncompressed data.  Actually, the threshold
    * is probably around 32K, but it isn't as definite as 64K is.
    */
1320
   if (length > (png_uint_32)65535L)
A
Andreas Dilger 已提交
1321 1322 1323 1324 1325 1326 1327
   {
      png_warning(png_ptr, "zTXt chunk too large to fit in memory");
      png_push_crc_skip(png_ptr, length);
      return;
   }
#endif

1328
   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1329
       (png_size_t)(length + 1));
A
Andreas Dilger 已提交
1330
   png_ptr->current_text[length] = '\0';
G
Guy Schalnat 已提交
1331
   png_ptr->current_text_ptr = png_ptr->current_text;
1332 1333
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
G
Guy Schalnat 已提交
1334
   png_ptr->process_mode = PNG_READ_zTXt_MODE;
G
Guy Schalnat 已提交
1335 1336
}

1337
void /* PRIVATE */
A
Andreas Dilger 已提交
1338
png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
1339
{
G
Guy Schalnat 已提交
1340 1341
   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
A
Andreas Dilger 已提交
1342
      png_size_t text_size;
G
Guy Schalnat 已提交
1343

A
Andreas Dilger 已提交
1344
      if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left)
G
Guy Schalnat 已提交
1345
         text_size = png_ptr->buffer_size;
1346

G
Guy Schalnat 已提交
1347 1348
      else
         text_size = png_ptr->current_text_left;
1349

A
Andreas Dilger 已提交
1350
      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
G
Guy Schalnat 已提交
1351
      png_ptr->current_text_left -= text_size;
A
Andreas Dilger 已提交
1352
      png_ptr->current_text_ptr += text_size;
G
Guy Schalnat 已提交
1353 1354 1355
   }
   if (!(png_ptr->current_text_left))
   {
A
Andreas Dilger 已提交
1356
      png_textp text_ptr;
G
Guy Schalnat 已提交
1357 1358 1359
      png_charp text;
      png_charp key;
      int ret;
A
Andreas Dilger 已提交
1360
      png_size_t text_size, key_size;
G
Guy Schalnat 已提交
1361 1362 1363 1364 1365 1366 1367

      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

A
Andreas Dilger 已提交
1368
      png_push_crc_finish(png_ptr);
G
Guy Schalnat 已提交
1369 1370 1371 1372

      key = png_ptr->current_text;

      for (text = key; *text; text++)
1373
         /* Empty loop */ ;
G
Guy Schalnat 已提交
1374 1375

      /* zTXt can't have zero text */
1376
      if (text >= key + png_ptr->current_text_size)
G
Guy Schalnat 已提交
1377
      {
1378
         png_ptr->current_text = NULL;
A
Andreas Dilger 已提交
1379
         png_free(png_ptr, key);
G
Guy Schalnat 已提交
1380 1381 1382 1383 1384
         return;
      }

      text++;

1385
      if (*text != PNG_TEXT_COMPRESSION_zTXt) /* Check compression byte */
G
Guy Schalnat 已提交
1386
      {
1387
         png_ptr->current_text = NULL;
A
Andreas Dilger 已提交
1388
         png_free(png_ptr, key);
G
Guy Schalnat 已提交
1389 1390 1391 1392 1393
         return;
      }

      text++;

A
Andreas Dilger 已提交
1394 1395
      png_ptr->zstream.next_in = (png_bytep )text;
      png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size -
1396
          (text - key));
A
Andreas Dilger 已提交
1397
      png_ptr->zstream.next_out = png_ptr->zbuf;
1398
      png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
1399 1400 1401 1402 1403 1404

      key_size = text - key;
      text_size = 0;
      text = NULL;
      ret = Z_STREAM_END;

A
Andreas Dilger 已提交
1405
      while (png_ptr->zstream.avail_in)
G
Guy Schalnat 已提交
1406
      {
A
Andreas Dilger 已提交
1407
         ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
G
Guy Schalnat 已提交
1408 1409
         if (ret != Z_OK && ret != Z_STREAM_END)
         {
A
Andreas Dilger 已提交
1410 1411
            inflateReset(&png_ptr->zstream);
            png_ptr->zstream.avail_in = 0;
1412
            png_ptr->current_text = NULL;
A
Andreas Dilger 已提交
1413 1414
            png_free(png_ptr, key);
            png_free(png_ptr, text);
G
Guy Schalnat 已提交
1415 1416
            return;
         }
1417

A
Andreas Dilger 已提交
1418
         if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END)
G
Guy Schalnat 已提交
1419
         {
A
Andreas Dilger 已提交
1420
            if (text == NULL)
G
Guy Schalnat 已提交
1421
            {
A
Andreas Dilger 已提交
1422
               text = (png_charp)png_malloc(png_ptr,
1423 1424
                   (png_ptr->zbuf_size
                   - png_ptr->zstream.avail_out + key_size + 1));
1425

1426
               png_memcpy(text + key_size, png_ptr->zbuf,
1427
                   png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1428

1429
               png_memcpy(text, key, key_size);
1430

1431
               text_size = key_size + png_ptr->zbuf_size -
1432
                   png_ptr->zstream.avail_out;
1433

1434
               *(text + text_size) = '\0';
G
Guy Schalnat 已提交
1435
            }
1436

G
Guy Schalnat 已提交
1437 1438 1439 1440 1441
            else
            {
               png_charp tmp;

               tmp = text;
1442
               text = (png_charp)png_malloc(png_ptr, text_size +
1443
                   (png_ptr->zbuf_size
1444
                   - png_ptr->zstream.avail_out + 1));
1445

1446 1447
               png_memcpy(text, tmp, text_size);
               png_free(png_ptr, tmp);
1448

1449
               png_memcpy(text + text_size, png_ptr->zbuf,
1450
                   png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1451

1452 1453
               text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
               *(text + text_size) = '\0';
G
Guy Schalnat 已提交
1454
            }
1455

G
Guy Schalnat 已提交
1456 1457
            if (ret != Z_STREAM_END)
            {
A
Andreas Dilger 已提交
1458 1459
               png_ptr->zstream.next_out = png_ptr->zbuf;
               png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
G
Guy Schalnat 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
            }
         }
         else
         {
            break;
         }

         if (ret == Z_STREAM_END)
            break;
      }

A
Andreas Dilger 已提交
1471 1472
      inflateReset(&png_ptr->zstream);
      png_ptr->zstream.avail_in = 0;
G
Guy Schalnat 已提交
1473 1474 1475

      if (ret != Z_STREAM_END)
      {
1476
         png_ptr->current_text = NULL;
A
Andreas Dilger 已提交
1477 1478
         png_free(png_ptr, key);
         png_free(png_ptr, text);
G
Guy Schalnat 已提交
1479 1480 1481
         return;
      }

1482
      png_ptr->current_text = NULL;
A
Andreas Dilger 已提交
1483
      png_free(png_ptr, key);
G
Guy Schalnat 已提交
1484
      key = text;
A
Andreas Dilger 已提交
1485
      text += key_size;
G
Guy Schalnat 已提交
1486

1487 1488
      text_ptr = (png_textp)png_malloc(png_ptr,
          png_sizeof(png_text));
1489 1490
      text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt;
      text_ptr->key = key;
1491
      text_ptr->itxt_length = 0;
1492 1493
      text_ptr->lang = NULL;
      text_ptr->lang_key = NULL;
1494
      text_ptr->text = text;
1495

1496
      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
1497

1498
      png_free(png_ptr, key);
1499
      png_free(png_ptr, text_ptr);
1500 1501

      if (ret)
1502
         png_warning(png_ptr, "Insufficient memory to store text chunk");
1503 1504 1505 1506
   }
}
#endif

1507
#ifdef PNG_READ_iTXt_SUPPORTED
1508
void /* PRIVATE */
1509
png_push_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32
1510
    length)
1511
{
1512
   if (!(png_ptr->mode & PNG_HAVE_IHDR) || (png_ptr->mode & PNG_HAVE_IEND))
1513 1514 1515 1516
   {
      png_error(png_ptr, "Out of place iTXt");
      info_ptr = info_ptr; /* To quiet some compiler warnings */
   }
1517 1518 1519 1520

#ifdef PNG_MAX_MALLOC_64K
   png_ptr->skip_length = 0;  /* This may not be necessary */

1521
   if (length > (png_uint_32)65535L) /* Can't hold entire string in memory */
1522 1523 1524 1525 1526 1527 1528 1529
   {
      png_warning(png_ptr, "iTXt chunk too large to fit in memory");
      png_ptr->skip_length = length - (png_uint_32)65535L;
      length = (png_uint_32)65535L;
   }
#endif

   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
1530
       (png_size_t)(length + 1));
1531 1532 1533 1534 1535
   png_ptr->current_text[length] = '\0';
   png_ptr->current_text_ptr = png_ptr->current_text;
   png_ptr->current_text_size = (png_size_t)length;
   png_ptr->current_text_left = (png_size_t)length;
   png_ptr->process_mode = PNG_READ_iTXt_MODE;
1536 1537
}

1538
void /* PRIVATE */
1539 1540
png_push_read_iTXt(png_structp png_ptr, png_infop info_ptr)
{
1541

1542 1543 1544 1545 1546 1547
   if (png_ptr->buffer_size && png_ptr->current_text_left)
   {
      png_size_t text_size;

      if (png_ptr->buffer_size < png_ptr->current_text_left)
         text_size = png_ptr->buffer_size;
1548

1549 1550
      else
         text_size = png_ptr->current_text_left;
1551

1552 1553 1554 1555
      png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size);
      png_ptr->current_text_left -= text_size;
      png_ptr->current_text_ptr += text_size;
   }
1556

1557 1558 1559 1560
   if (!(png_ptr->current_text_left))
   {
      png_textp text_ptr;
      png_charp key;
1561
      int comp_flag;
1562 1563 1564
      png_charp lang;
      png_charp lang_key;
      png_charp text;
1565
      int ret;
1566 1567 1568 1569 1570 1571 1572 1573 1574

      if (png_ptr->buffer_size < 4)
      {
         png_push_save_buffer(png_ptr);
         return;
      }

      png_push_crc_finish(png_ptr);

1575
#ifdef PNG_MAX_MALLOC_64K
1576 1577 1578 1579
      if (png_ptr->skip_length)
         return;
#endif

1580
      key = png_ptr->current_text;
1581

1582
      for (lang = key; *lang; lang++)
1583
         /* Empty loop */ ;
1584

1585
      if (lang < key + png_ptr->current_text_size - 3)
1586
         lang++;
1587

1588
      comp_flag = *lang++;
1589
      lang++;     /* Skip comp_type, always zero */
1590 1591

      for (lang_key = lang; *lang_key; lang_key++)
1592
         /* Empty loop */ ;
1593

1594
      lang_key++;        /* Skip NUL separator */
1595

1596
      text=lang_key;
1597

1598 1599
      if (lang_key < key + png_ptr->current_text_size - 1)
      {
1600 1601
         for (; *text; text++)
            /* Empty loop */ ;
1602
      }
1603

1604
      if (text < key + png_ptr->current_text_size)
1605 1606
         text++;

1607
      text_ptr = (png_textp)png_malloc(png_ptr,
1608
          png_sizeof(png_text));
1609

1610
      text_ptr->compression = comp_flag + 2;
A
Andreas Dilger 已提交
1611
      text_ptr->key = key;
1612 1613
      text_ptr->lang = lang;
      text_ptr->lang_key = lang_key;
A
Andreas Dilger 已提交
1614
      text_ptr->text = text;
1615 1616
      text_ptr->text_length = 0;
      text_ptr->itxt_length = png_strlen(text);
A
Andreas Dilger 已提交
1617

1618
      ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
A
Andreas Dilger 已提交
1619

1620 1621
      png_ptr->current_text = NULL;

A
Andreas Dilger 已提交
1622
      png_free(png_ptr, text_ptr);
1623
      if (ret)
1624
         png_warning(png_ptr, "Insufficient memory to store iTXt chunk");
G
Guy Schalnat 已提交
1625
   }
G
Guy Schalnat 已提交
1626 1627 1628
}
#endif

A
Andreas Dilger 已提交
1629
/* This function is called when we haven't found a handler for this
1630 1631
 * chunk.  If there isn't a problem with the chunk itself (ie a bad chunk
 * name or a critical chunk), the chunk is (currently) silently ignored.
1632
 */
1633
void /* PRIVATE */
1634
png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32
1635
    length)
A
Andreas Dilger 已提交
1636
{
1637
   png_uint_32 skip = 0;
A
Andreas Dilger 已提交
1638 1639 1640

   if (!(png_ptr->chunk_name[0] & 0x20))
   {
1641
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1642
      if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1643
          PNG_HANDLE_CHUNK_ALWAYS
1644
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1645
          && png_ptr->read_user_chunk_fn == NULL
1646
#endif
1647
          )
1648
#endif
1649 1650
         png_chunk_error(png_ptr, "unknown critical chunk");

1651
      info_ptr = info_ptr; /* To quiet some compiler warnings */
A
Andreas Dilger 已提交
1652 1653
   }

1654
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
1655 1656 1657
   if (png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS)
   {
#ifdef PNG_MAX_MALLOC_64K
1658 1659
      if (length > (png_uint_32)65535L)
      {
1660 1661 1662
         png_warning(png_ptr, "unknown chunk too large to fit in memory");
         skip = length - (png_uint_32)65535L;
         length = (png_uint_32)65535L;
1663
      }
1664
#endif
1665
      png_memcpy((png_charp)png_ptr->unknown_chunk.name,
1666
          (png_charp)png_ptr->chunk_name,
1667
          png_sizeof(png_ptr->unknown_chunk.name));
1668
      png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name) - 1]
1669
          = '\0';
1670 1671

      png_ptr->unknown_chunk.size = (png_size_t)length;
1672

1673 1674
      if (length == 0)
         png_ptr->unknown_chunk.data = NULL;
1675

1676 1677 1678
      else
      {
         png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr,
1679
             (png_size_t)length);
1680 1681
         png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
      }
1682

1683
#ifdef PNG_READ_USER_CHUNKS_SUPPORTED
1684 1685
      if (png_ptr->read_user_chunk_fn != NULL)
      {
1686
         /* Callback to user unknown chunk handler */
1687 1688
         int ret;
         ret = (*(png_ptr->read_user_chunk_fn))
1689
             (png_ptr, &png_ptr->unknown_chunk);
1690

1691 1692
         if (ret < 0)
            png_chunk_error(png_ptr, "error in user chunk");
1693

1694 1695 1696 1697
         if (ret == 0)
         {
            if (!(png_ptr->chunk_name[0] & 0x20))
               if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
1698
                   PNG_HANDLE_CHUNK_ALWAYS)
1699 1700
                  png_chunk_error(png_ptr, "unknown critical chunk");
            png_set_unknown_chunks(png_ptr, info_ptr,
1701
                &png_ptr->unknown_chunk, 1);
1702 1703
         }
      }
1704

1705
      else
1706
#endif
1707
         png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
1708 1709
      png_free(png_ptr, png_ptr->unknown_chunk.data);
      png_ptr->unknown_chunk.data = NULL;
1710
   }
1711

1712 1713 1714 1715
   else
#endif
      skip=length;
   png_push_crc_skip(png_ptr, skip);
A
Andreas Dilger 已提交
1716 1717
}

1718
void /* PRIVATE */
A
Andreas Dilger 已提交
1719
png_push_have_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
1720
{
A
Andreas Dilger 已提交
1721
   if (png_ptr->info_fn != NULL)
A
Andreas Dilger 已提交
1722
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1723 1724
}

1725
void /* PRIVATE */
A
Andreas Dilger 已提交
1726
png_push_have_end(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
1727
{
A
Andreas Dilger 已提交
1728
   if (png_ptr->end_fn != NULL)
A
Andreas Dilger 已提交
1729
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
G
Guy Schalnat 已提交
1730 1731
}

1732
void /* PRIVATE */
G
Guy Schalnat 已提交
1733 1734
png_push_have_row(png_structp png_ptr, png_bytep row)
{
1735
   if (png_ptr->row_fn != NULL)
G
Guy Schalnat 已提交
1736 1737
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
         (int)png_ptr->pass);
G
Guy Schalnat 已提交
1738 1739
}

1740
void PNGAPI
A
Andreas Dilger 已提交
1741
png_progressive_combine_row (png_structp png_ptr,
1742
    png_bytep old_row, png_bytep new_row)
G
Guy Schalnat 已提交
1743
{
1744
   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
1745
      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1746

1747 1748 1749
   if (png_ptr == NULL)
      return;

1750
   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
A
Andreas Dilger 已提交
1751
      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
G
Guy Schalnat 已提交
1752 1753
}

1754
void PNGAPI
G
Guy Schalnat 已提交
1755
png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1756 1757
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
    png_progressive_end_ptr end_fn)
G
Guy Schalnat 已提交
1758
{
1759 1760 1761
   if (png_ptr == NULL)
      return;

G
Guy Schalnat 已提交
1762 1763
   png_ptr->info_fn = info_fn;
   png_ptr->row_fn = row_fn;
G
Guy Schalnat 已提交
1764
   png_ptr->end_fn = end_fn;
G
Guy Schalnat 已提交
1765

1766
   png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer);
G
Guy Schalnat 已提交
1767 1768
}

1769
png_voidp PNGAPI
A
Andreas Dilger 已提交
1770
png_get_progressive_ptr(png_structp png_ptr)
G
Guy Schalnat 已提交
1771
{
1772 1773 1774
   if (png_ptr == NULL)
      return (NULL);

A
Andreas Dilger 已提交
1775
   return png_ptr->io_ptr;
G
Guy Schalnat 已提交
1776
}
G
Guy Schalnat 已提交
1777
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */