~random-stuff/libpng/libpng-1.6.x

« back to all changes in this revision

Viewing changes to pngpread.c

  • Committer: Sérgio Benjamim
  • Date: 2015-10-10 23:00:20 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20151010230020-gdtmmv30zn25396n
Update to 1.6.18.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngpread.c - read a png file in push mode
3
3
 *
4
 
 * Last changed in libpng 1.4.10 [March 8, 2012]
5
 
 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
 
4
 * Last changed in libpng 1.6.18 [July 23, 2015]
 
5
 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
6
6
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7
7
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8
8
 *
11
11
 * and license in png.h
12
12
 */
13
13
 
14
 
#define PNG_NO_PEDANTIC_WARNINGS
15
 
#include "png.h"
 
14
#include "pngpriv.h"
 
15
 
16
16
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
17
 
#include "pngpriv.h"
18
17
 
19
18
/* Push model modes */
20
19
#define PNG_READ_SIG_MODE   0
21
20
#define PNG_READ_CHUNK_MODE 1
22
21
#define PNG_READ_IDAT_MODE  2
23
 
#define PNG_SKIP_MODE       3
24
22
#define PNG_READ_tEXt_MODE  4
25
23
#define PNG_READ_zTXt_MODE  5
26
24
#define PNG_READ_DONE_MODE  6
27
25
#define PNG_READ_iTXt_MODE  7
28
26
#define PNG_ERROR_MODE      8
29
27
 
 
28
#define PNG_PUSH_SAVE_BUFFER_IF_FULL \
 
29
if (png_ptr->push_length + 4 > png_ptr->buffer_size) \
 
30
   { png_push_save_buffer(png_ptr); return; }
 
31
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \
 
32
if (png_ptr->buffer_size < N) \
 
33
   { png_push_save_buffer(png_ptr); return; }
 
34
 
30
35
void PNGAPI
31
 
png_process_data(png_structp png_ptr, png_infop info_ptr,
32
 
   png_bytep buffer, png_size_t buffer_size)
 
36
png_process_data(png_structrp png_ptr, png_inforp info_ptr,
 
37
    png_bytep buffer, png_size_t buffer_size)
33
38
{
34
39
   if (png_ptr == NULL || info_ptr == NULL)
35
40
      return;
42
47
   }
43
48
}
44
49
 
 
50
png_size_t PNGAPI
 
51
png_process_data_pause(png_structrp png_ptr, int save)
 
52
{
 
53
   if (png_ptr != NULL)
 
54
   {
 
55
      /* It's easiest for the caller if we do the save; then the caller doesn't
 
56
       * have to supply the same data again:
 
57
       */
 
58
      if (save != 0)
 
59
         png_push_save_buffer(png_ptr);
 
60
      else
 
61
      {
 
62
         /* This includes any pending saved bytes: */
 
63
         png_size_t remaining = png_ptr->buffer_size;
 
64
         png_ptr->buffer_size = 0;
 
65
 
 
66
         /* So subtract the saved buffer size, unless all the data
 
67
          * is actually 'saved', in which case we just return 0
 
68
          */
 
69
         if (png_ptr->save_buffer_size < remaining)
 
70
            return remaining - png_ptr->save_buffer_size;
 
71
      }
 
72
   }
 
73
 
 
74
   return 0;
 
75
}
 
76
 
 
77
png_uint_32 PNGAPI
 
78
png_process_data_skip(png_structrp png_ptr)
 
79
{
 
80
  /* TODO: Deprecate and remove this API.
 
81
   * Somewhere the implementation of this seems to have been lost,
 
82
   * or abandoned.  It was only to support some internal back-door access
 
83
   * to png_struct) in libpng-1.4.x.
 
84
   */
 
85
   png_app_warning(png_ptr,
 
86
"png_process_data_skip is not implemented in any current version of libpng");
 
87
   return 0;
 
88
}
 
89
 
45
90
/* What we do with the incoming data depends on what we were previously
46
91
 * doing before we ran out of data...
47
92
 */
48
93
void /* PRIVATE */
49
 
png_process_some_data(png_structp png_ptr, png_infop info_ptr)
 
94
png_process_some_data(png_structrp png_ptr, png_inforp info_ptr)
50
95
{
51
96
   if (png_ptr == NULL)
52
97
      return;
71
116
         break;
72
117
      }
73
118
 
74
 
      case PNG_SKIP_MODE:
75
 
      {
76
 
         png_push_crc_finish(png_ptr);
77
 
         break;
78
 
      }
79
 
 
80
119
      default:
81
120
      {
82
121
         png_ptr->buffer_size = 0;
92
131
 * routine.
93
132
 */
94
133
void /* PRIVATE */
95
 
png_push_read_sig(png_structp png_ptr, png_infop info_ptr)
 
134
png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
96
135
{
97
 
   png_size_t num_checked = png_ptr->sig_bytes,
98
 
             num_to_check = 8 - num_checked;
 
136
   png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */ 
 
137
       num_to_check = 8 - num_checked;
99
138
 
100
139
   if (png_ptr->buffer_size < num_to_check)
101
140
   {
103
142
   }
104
143
 
105
144
   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
106
 
      num_to_check);
 
145
       num_to_check);
107
146
   png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
108
147
 
109
148
   if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
111
150
      if (num_checked < 4 &&
112
151
          png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
113
152
         png_error(png_ptr, "Not a PNG file");
 
153
 
114
154
      else
115
155
         png_error(png_ptr, "PNG file corrupted by ASCII conversion");
116
156
   }
124
164
}
125
165
 
126
166
void /* PRIVATE */
127
 
png_push_read_chunk(png_structp png_ptr, png_infop info_ptr)
 
167
png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
128
168
{
129
 
      PNG_IHDR;
130
 
      PNG_IDAT;
131
 
      PNG_IEND;
132
 
      PNG_PLTE;
133
 
#ifdef PNG_READ_bKGD_SUPPORTED
134
 
      PNG_bKGD;
135
 
#endif
136
 
#ifdef PNG_READ_cHRM_SUPPORTED
137
 
      PNG_cHRM;
138
 
#endif
139
 
#ifdef PNG_READ_gAMA_SUPPORTED
140
 
      PNG_gAMA;
141
 
#endif
142
 
#ifdef PNG_READ_hIST_SUPPORTED
143
 
      PNG_hIST;
144
 
#endif
145
 
#ifdef PNG_READ_iCCP_SUPPORTED
146
 
      PNG_iCCP;
147
 
#endif
148
 
#ifdef PNG_READ_iTXt_SUPPORTED
149
 
      PNG_iTXt;
150
 
#endif
151
 
#ifdef PNG_READ_oFFs_SUPPORTED
152
 
      PNG_oFFs;
153
 
#endif
154
 
#ifdef PNG_READ_pCAL_SUPPORTED
155
 
      PNG_pCAL;
156
 
#endif
157
 
#ifdef PNG_READ_pHYs_SUPPORTED
158
 
      PNG_pHYs;
159
 
#endif
160
 
#ifdef PNG_READ_sBIT_SUPPORTED
161
 
      PNG_sBIT;
162
 
#endif
163
 
#ifdef PNG_READ_sCAL_SUPPORTED
164
 
      PNG_sCAL;
165
 
#endif
166
 
#ifdef PNG_READ_sRGB_SUPPORTED
167
 
      PNG_sRGB;
168
 
#endif
169
 
#ifdef PNG_READ_sPLT_SUPPORTED
170
 
      PNG_sPLT;
171
 
#endif
172
 
#ifdef PNG_READ_tEXt_SUPPORTED
173
 
      PNG_tEXt;
174
 
#endif
175
 
#ifdef PNG_READ_tIME_SUPPORTED
176
 
      PNG_tIME;
177
 
#endif
178
 
#ifdef PNG_READ_tRNS_SUPPORTED
179
 
      PNG_tRNS;
180
 
#endif
181
 
#ifdef PNG_READ_zTXt_SUPPORTED
182
 
      PNG_zTXt;
 
169
   png_uint_32 chunk_name;
 
170
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
 
171
   int keep; /* unknown handling method */
183
172
#endif
184
173
 
185
 
   /* First we make sure we have enough data for the 4 byte chunk name
186
 
    * and the 4 byte chunk length before proceeding with decoding the
 
174
   /* First we make sure we have enough data for the 4-byte chunk name
 
175
    * and the 4-byte chunk length before proceeding with decoding the
187
176
    * chunk data.  To fully decode each of these chunks, we also make
188
 
    * sure we have enough data in the buffer for the 4 byte CRC at the
 
177
    * sure we have enough data in the buffer for the 4-byte CRC at the
189
178
    * end of every chunk (except IDAT, which is handled separately).
190
179
    */
191
 
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
 
180
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
192
181
   {
193
182
      png_byte chunk_length[4];
194
 
 
195
 
      if (png_ptr->buffer_size < 8)
196
 
      {
197
 
         png_push_save_buffer(png_ptr);
198
 
         return;
199
 
      }
200
 
 
 
183
      png_byte chunk_tag[4];
 
184
 
 
185
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
201
186
      png_push_fill_buffer(png_ptr, chunk_length, 4);
202
187
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
203
188
      png_reset_crc(png_ptr);
204
 
      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
 
189
      png_crc_read(png_ptr, chunk_tag, 4);
 
190
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
205
191
      png_check_chunk_name(png_ptr, png_ptr->chunk_name);
206
192
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
207
193
   }
208
194
 
209
 
   if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
210
 
     if (png_ptr->mode & PNG_AFTER_IDAT)
211
 
        png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
212
 
 
213
 
   if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
 
195
   chunk_name = png_ptr->chunk_name;
 
196
 
 
197
   if (chunk_name == png_IDAT)
 
198
   {
 
199
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
 
200
         png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
 
201
 
 
202
      /* If we reach an IDAT chunk, this means we have read all of the
 
203
       * header chunks, and we can start reading the image (or if this
 
204
       * is called after the image has been read - we have an error).
 
205
       */
 
206
      if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
 
207
         png_error(png_ptr, "Missing IHDR before IDAT");
 
208
 
 
209
      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
 
210
          (png_ptr->mode & PNG_HAVE_PLTE) == 0)
 
211
         png_error(png_ptr, "Missing PLTE before IDAT");
 
212
 
 
213
      png_ptr->mode |= PNG_HAVE_IDAT;
 
214
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
 
215
 
 
216
      if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
 
217
         if (png_ptr->push_length == 0)
 
218
            return;
 
219
 
 
220
      if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
 
221
         png_benign_error(png_ptr, "Too many IDATs found");
 
222
   }
 
223
 
 
224
   if (chunk_name == png_IHDR)
214
225
   {
215
226
      if (png_ptr->push_length != 13)
216
227
         png_error(png_ptr, "Invalid IHDR length");
217
228
 
218
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
219
 
      {
220
 
         png_push_save_buffer(png_ptr);
221
 
         return;
222
 
      }
223
 
 
 
229
      PNG_PUSH_SAVE_BUFFER_IF_FULL
224
230
      png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
225
231
   }
226
232
 
227
 
   else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
 
233
   else if (chunk_name == png_IEND)
228
234
   {
229
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
230
 
      {
231
 
         png_push_save_buffer(png_ptr);
232
 
         return;
233
 
      }
234
 
 
 
235
      PNG_PUSH_SAVE_BUFFER_IF_FULL
235
236
      png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
236
237
 
237
238
      png_ptr->process_mode = PNG_READ_DONE_MODE;
239
240
   }
240
241
 
241
242
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
242
 
   else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
 
243
   else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
243
244
   {
244
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
245
 
      {
246
 
         png_push_save_buffer(png_ptr);
247
 
         return;
248
 
      }
249
 
 
250
 
      if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
251
 
         png_ptr->mode |= PNG_HAVE_IDAT;
252
 
 
253
 
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
254
 
 
255
 
      if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
 
245
      PNG_PUSH_SAVE_BUFFER_IF_FULL
 
246
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
 
247
 
 
248
      if (chunk_name == png_PLTE)
256
249
         png_ptr->mode |= PNG_HAVE_PLTE;
257
 
 
258
 
      else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
259
 
      {
260
 
         if (!(png_ptr->mode & PNG_HAVE_IHDR))
261
 
            png_error(png_ptr, "Missing IHDR before IDAT");
262
 
 
263
 
         else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
264
 
                  !(png_ptr->mode & PNG_HAVE_PLTE))
265
 
            png_error(png_ptr, "Missing PLTE before IDAT");
266
 
      }
267
250
   }
268
 
 
269
251
#endif
270
 
   else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
 
252
 
 
253
   else if (chunk_name == png_PLTE)
271
254
   {
272
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
273
 
      {
274
 
         png_push_save_buffer(png_ptr);
275
 
         return;
276
 
      }
 
255
      PNG_PUSH_SAVE_BUFFER_IF_FULL
277
256
      png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
278
257
   }
279
258
 
280
 
   else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
 
259
   else if (chunk_name == png_IDAT)
281
260
   {
282
 
      /* If we reach an IDAT chunk, this means we have read all of the
283
 
       * header chunks, and we can start reading the image (or if this
284
 
       * is called after the image has been read - we have an error).
285
 
       */
286
 
 
287
 
      if (!(png_ptr->mode & PNG_HAVE_IHDR))
288
 
         png_error(png_ptr, "Missing IHDR before IDAT");
289
 
 
290
 
      else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
291
 
          !(png_ptr->mode & PNG_HAVE_PLTE))
292
 
         png_error(png_ptr, "Missing PLTE before IDAT");
293
 
 
294
 
      if (png_ptr->mode & PNG_HAVE_IDAT)
295
 
      {
296
 
         if (!(png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
297
 
            if (png_ptr->push_length == 0)
298
 
               return;
299
 
 
300
 
         if (png_ptr->mode & PNG_AFTER_IDAT)
301
 
            png_benign_error(png_ptr, "Too many IDATs found");
302
 
      }
303
 
 
304
261
      png_ptr->idat_size = png_ptr->push_length;
305
 
      png_ptr->mode |= PNG_HAVE_IDAT;
306
262
      png_ptr->process_mode = PNG_READ_IDAT_MODE;
307
263
      png_push_have_info(png_ptr, info_ptr);
308
264
      png_ptr->zstream.avail_out =
313
269
   }
314
270
 
315
271
#ifdef PNG_READ_gAMA_SUPPORTED
316
 
   else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
 
272
   else if (png_ptr->chunk_name == png_gAMA)
317
273
   {
318
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
319
 
      {
320
 
         png_push_save_buffer(png_ptr);
321
 
         return;
322
 
      }
323
 
 
 
274
      PNG_PUSH_SAVE_BUFFER_IF_FULL
324
275
      png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
325
276
   }
326
277
 
327
278
#endif
328
279
#ifdef PNG_READ_sBIT_SUPPORTED
329
 
   else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
 
280
   else if (png_ptr->chunk_name == png_sBIT)
330
281
   {
331
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
332
 
      {
333
 
         png_push_save_buffer(png_ptr);
334
 
         return;
335
 
      }
336
 
 
 
282
      PNG_PUSH_SAVE_BUFFER_IF_FULL
337
283
      png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
338
284
   }
339
285
 
340
286
#endif
341
287
#ifdef PNG_READ_cHRM_SUPPORTED
342
 
   else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
 
288
   else if (png_ptr->chunk_name == png_cHRM)
343
289
   {
344
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
345
 
      {
346
 
         png_push_save_buffer(png_ptr);
347
 
         return;
348
 
      }
349
 
 
 
290
      PNG_PUSH_SAVE_BUFFER_IF_FULL
350
291
      png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
351
292
   }
352
293
 
353
294
#endif
354
295
#ifdef PNG_READ_sRGB_SUPPORTED
355
 
   else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
 
296
   else if (chunk_name == png_sRGB)
356
297
   {
357
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
358
 
      {
359
 
         png_push_save_buffer(png_ptr);
360
 
         return;
361
 
      }
362
 
 
 
298
      PNG_PUSH_SAVE_BUFFER_IF_FULL
363
299
      png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
364
300
   }
365
301
 
366
302
#endif
367
303
#ifdef PNG_READ_iCCP_SUPPORTED
368
 
   else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
 
304
   else if (png_ptr->chunk_name == png_iCCP)
369
305
   {
370
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
371
 
      {
372
 
         png_push_save_buffer(png_ptr);
373
 
         return;
374
 
      }
375
 
 
 
306
      PNG_PUSH_SAVE_BUFFER_IF_FULL
376
307
      png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
377
308
   }
378
309
 
379
310
#endif
380
311
#ifdef PNG_READ_sPLT_SUPPORTED
381
 
   else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
 
312
   else if (chunk_name == png_sPLT)
382
313
   {
383
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
384
 
      {
385
 
         png_push_save_buffer(png_ptr);
386
 
         return;
387
 
      }
388
 
 
 
314
      PNG_PUSH_SAVE_BUFFER_IF_FULL
389
315
      png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
390
316
   }
391
317
 
392
318
#endif
393
319
#ifdef PNG_READ_tRNS_SUPPORTED
394
 
   else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
 
320
   else if (chunk_name == png_tRNS)
395
321
   {
396
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
397
 
      {
398
 
         png_push_save_buffer(png_ptr);
399
 
         return;
400
 
      }
401
 
 
 
322
      PNG_PUSH_SAVE_BUFFER_IF_FULL
402
323
      png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
403
324
   }
404
325
 
405
326
#endif
406
327
#ifdef PNG_READ_bKGD_SUPPORTED
407
 
   else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
 
328
   else if (chunk_name == png_bKGD)
408
329
   {
409
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
410
 
      {
411
 
         png_push_save_buffer(png_ptr);
412
 
         return;
413
 
      }
414
 
 
 
330
      PNG_PUSH_SAVE_BUFFER_IF_FULL
415
331
      png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
416
332
   }
417
333
 
418
334
#endif
419
335
#ifdef PNG_READ_hIST_SUPPORTED
420
 
   else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
 
336
   else if (chunk_name == png_hIST)
421
337
   {
422
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
423
 
      {
424
 
         png_push_save_buffer(png_ptr);
425
 
         return;
426
 
      }
427
 
 
 
338
      PNG_PUSH_SAVE_BUFFER_IF_FULL
428
339
      png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
429
340
   }
430
341
 
431
342
#endif
432
343
#ifdef PNG_READ_pHYs_SUPPORTED
433
 
   else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
 
344
   else if (chunk_name == png_pHYs)
434
345
   {
435
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
436
 
      {
437
 
         png_push_save_buffer(png_ptr);
438
 
         return;
439
 
      }
440
 
 
 
346
      PNG_PUSH_SAVE_BUFFER_IF_FULL
441
347
      png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
442
348
   }
443
349
 
444
350
#endif
445
351
#ifdef PNG_READ_oFFs_SUPPORTED
446
 
   else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
 
352
   else if (chunk_name == png_oFFs)
447
353
   {
448
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
449
 
      {
450
 
         png_push_save_buffer(png_ptr);
451
 
         return;
452
 
      }
453
 
 
 
354
      PNG_PUSH_SAVE_BUFFER_IF_FULL
454
355
      png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
455
356
   }
456
357
#endif
457
358
 
458
359
#ifdef PNG_READ_pCAL_SUPPORTED
459
 
   else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
 
360
   else if (chunk_name == png_pCAL)
460
361
   {
461
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
462
 
      {
463
 
         png_push_save_buffer(png_ptr);
464
 
         return;
465
 
      }
466
 
 
 
362
      PNG_PUSH_SAVE_BUFFER_IF_FULL
467
363
      png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
468
364
   }
469
365
 
470
366
#endif
471
367
#ifdef PNG_READ_sCAL_SUPPORTED
472
 
   else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
 
368
   else if (chunk_name == png_sCAL)
473
369
   {
474
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
475
 
      {
476
 
         png_push_save_buffer(png_ptr);
477
 
         return;
478
 
      }
479
 
 
 
370
      PNG_PUSH_SAVE_BUFFER_IF_FULL
480
371
      png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
481
372
   }
482
373
 
483
374
#endif
484
375
#ifdef PNG_READ_tIME_SUPPORTED
485
 
   else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
 
376
   else if (chunk_name == png_tIME)
486
377
   {
487
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
488
 
      {
489
 
         png_push_save_buffer(png_ptr);
490
 
         return;
491
 
      }
492
 
 
 
378
      PNG_PUSH_SAVE_BUFFER_IF_FULL
493
379
      png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
494
380
   }
495
381
 
496
382
#endif
497
383
#ifdef PNG_READ_tEXt_SUPPORTED
498
 
   else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
 
384
   else if (chunk_name == png_tEXt)
499
385
   {
500
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
501
 
      {
502
 
         png_push_save_buffer(png_ptr);
503
 
         return;
504
 
      }
505
 
 
 
386
      PNG_PUSH_SAVE_BUFFER_IF_FULL
506
387
      png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
507
388
   }
508
389
 
509
390
#endif
510
391
#ifdef PNG_READ_zTXt_SUPPORTED
511
 
   else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
 
392
   else if (chunk_name == png_zTXt)
512
393
   {
513
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
514
 
      {
515
 
         png_push_save_buffer(png_ptr);
516
 
         return;
517
 
      }
518
 
 
 
394
      PNG_PUSH_SAVE_BUFFER_IF_FULL
519
395
      png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
520
396
   }
521
397
 
522
398
#endif
523
399
#ifdef PNG_READ_iTXt_SUPPORTED
524
 
   else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
 
400
   else if (chunk_name == png_iTXt)
525
401
   {
526
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
527
 
      {
528
 
         png_push_save_buffer(png_ptr);
529
 
         return;
530
 
      }
531
 
 
 
402
      PNG_PUSH_SAVE_BUFFER_IF_FULL
532
403
      png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
533
404
   }
534
 
 
535
405
#endif
 
406
 
536
407
   else
537
408
   {
538
 
      if (png_ptr->push_length + 4 > png_ptr->buffer_size)
539
 
      {
540
 
         png_push_save_buffer(png_ptr);
541
 
         return;
542
 
      }
543
 
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length);
 
409
      PNG_PUSH_SAVE_BUFFER_IF_FULL
 
410
      png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
 
411
         PNG_HANDLE_CHUNK_AS_DEFAULT);
544
412
   }
545
413
 
546
414
   png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
547
415
}
548
416
 
549
 
void /* PRIVATE */
550
 
png_push_crc_skip(png_structp png_ptr, png_uint_32 skip)
551
 
{
552
 
   png_ptr->process_mode = PNG_SKIP_MODE;
553
 
   png_ptr->skip_length = skip;
554
 
}
555
 
 
556
 
void /* PRIVATE */
557
 
png_push_crc_finish(png_structp png_ptr)
558
 
{
559
 
   if (png_ptr->skip_length && png_ptr->save_buffer_size)
560
 
   {
561
 
      png_size_t save_size;
562
 
 
563
 
      if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size)
564
 
         save_size = (png_size_t)png_ptr->skip_length;
565
 
      else
566
 
         save_size = png_ptr->save_buffer_size;
567
 
 
568
 
      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
569
 
 
570
 
      png_ptr->skip_length -= save_size;
571
 
      png_ptr->buffer_size -= save_size;
572
 
      png_ptr->save_buffer_size -= save_size;
573
 
      png_ptr->save_buffer_ptr += save_size;
574
 
   }
575
 
   if (png_ptr->skip_length && png_ptr->current_buffer_size)
576
 
   {
577
 
      png_size_t save_size;
578
 
 
579
 
      if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size)
580
 
         save_size = (png_size_t)png_ptr->skip_length;
581
 
      else
582
 
         save_size = png_ptr->current_buffer_size;
583
 
 
584
 
      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
585
 
 
586
 
      png_ptr->skip_length -= save_size;
587
 
      png_ptr->buffer_size -= save_size;
588
 
      png_ptr->current_buffer_size -= save_size;
589
 
      png_ptr->current_buffer_ptr += save_size;
590
 
   }
591
 
   if (!png_ptr->skip_length)
592
 
   {
593
 
      if (png_ptr->buffer_size < 4)
594
 
      {
595
 
         png_push_save_buffer(png_ptr);
596
 
         return;
597
 
      }
598
 
 
599
 
      png_crc_finish(png_ptr, 0);
600
 
      png_ptr->process_mode = PNG_READ_CHUNK_MODE;
601
 
   }
602
 
}
603
 
 
604
 
void PNGAPI
 
417
void PNGCBAPI
605
418
png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
606
419
{
607
420
   png_bytep ptr;
610
423
      return;
611
424
 
612
425
   ptr = buffer;
613
 
   if (png_ptr->save_buffer_size)
 
426
   if (png_ptr->save_buffer_size != 0)
614
427
   {
615
428
      png_size_t save_size;
616
429
 
617
430
      if (length < png_ptr->save_buffer_size)
618
431
         save_size = length;
 
432
 
619
433
      else
620
434
         save_size = png_ptr->save_buffer_size;
621
435
 
622
 
      png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
 
436
      memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
623
437
      length -= save_size;
624
438
      ptr += save_size;
625
439
      png_ptr->buffer_size -= save_size;
626
440
      png_ptr->save_buffer_size -= save_size;
627
441
      png_ptr->save_buffer_ptr += save_size;
628
442
   }
629
 
   if (length && png_ptr->current_buffer_size)
 
443
   if (length != 0 && png_ptr->current_buffer_size != 0)
630
444
   {
631
445
      png_size_t save_size;
632
446
 
636
450
      else
637
451
         save_size = png_ptr->current_buffer_size;
638
452
 
639
 
      png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
 
453
      memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
640
454
      png_ptr->buffer_size -= save_size;
641
455
      png_ptr->current_buffer_size -= save_size;
642
456
      png_ptr->current_buffer_ptr += save_size;
644
458
}
645
459
 
646
460
void /* PRIVATE */
647
 
png_push_save_buffer(png_structp png_ptr)
 
461
png_push_save_buffer(png_structrp png_ptr)
648
462
{
649
 
   if (png_ptr->save_buffer_size)
 
463
   if (png_ptr->save_buffer_size != 0)
650
464
   {
651
465
      if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
652
466
      {
656
470
 
657
471
         istop = png_ptr->save_buffer_size;
658
472
         for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
659
 
            i < istop; i++, sp++, dp++)
 
473
             i < istop; i++, sp++, dp++)
660
474
         {
661
475
            *dp = *sp;
662
476
         }
663
477
      }
664
478
   }
665
479
   if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
666
 
      png_ptr->save_buffer_max)
 
480
       png_ptr->save_buffer_max)
667
481
   {
668
482
      png_size_t new_max;
669
483
      png_bytep old_buffer;
670
484
 
671
485
      if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
672
 
         (png_ptr->current_buffer_size + 256))
 
486
          (png_ptr->current_buffer_size + 256))
673
487
      {
674
 
        png_error(png_ptr, "Potential overflow of save_buffer");
 
488
         png_error(png_ptr, "Potential overflow of save_buffer");
675
489
      }
676
490
 
677
491
      new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
678
492
      old_buffer = png_ptr->save_buffer;
679
493
      png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
680
 
         new_max);
 
494
          (png_size_t)new_max);
 
495
 
681
496
      if (png_ptr->save_buffer == NULL)
682
497
      {
683
 
        png_free(png_ptr, old_buffer);
684
 
        png_error(png_ptr, "Insufficient memory for save_buffer");
 
498
         png_free(png_ptr, old_buffer);
 
499
         png_error(png_ptr, "Insufficient memory for save_buffer");
685
500
      }
686
 
      png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
 
501
 
 
502
      memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
687
503
      png_free(png_ptr, old_buffer);
688
504
      png_ptr->save_buffer_max = new_max;
689
505
   }
690
506
   if (png_ptr->current_buffer_size)
691
507
   {
692
 
      png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
 
508
      memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
693
509
         png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
694
510
      png_ptr->save_buffer_size += png_ptr->current_buffer_size;
695
511
      png_ptr->current_buffer_size = 0;
699
515
}
700
516
 
701
517
void /* PRIVATE */
702
 
png_push_restore_buffer(png_structp png_ptr, png_bytep buffer,
 
518
png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer,
703
519
   png_size_t buffer_length)
704
520
{
705
521
   png_ptr->current_buffer = buffer;
709
525
}
710
526
 
711
527
void /* PRIVATE */
712
 
png_push_read_IDAT(png_structp png_ptr)
 
528
png_push_read_IDAT(png_structrp png_ptr)
713
529
{
714
 
   PNG_IDAT;
715
 
   if (!(png_ptr->mode & PNG_HAVE_CHUNK_HEADER))
 
530
   if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
716
531
   {
717
532
      png_byte chunk_length[4];
718
 
 
719
 
      if (png_ptr->buffer_size < 8)
720
 
      {
721
 
         png_push_save_buffer(png_ptr);
722
 
         return;
723
 
      }
724
 
 
 
533
      png_byte chunk_tag[4];
 
534
 
 
535
      /* TODO: this code can be commoned up with the same code in push_read */
 
536
      PNG_PUSH_SAVE_BUFFER_IF_LT(8)
725
537
      png_push_fill_buffer(png_ptr, chunk_length, 4);
726
538
      png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
727
539
      png_reset_crc(png_ptr);
728
 
      png_crc_read(png_ptr, png_ptr->chunk_name, 4);
 
540
      png_crc_read(png_ptr, chunk_tag, 4);
 
541
      png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
729
542
      png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
730
543
 
731
 
      if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
 
544
      if (png_ptr->chunk_name != png_IDAT)
732
545
      {
733
546
         png_ptr->process_mode = PNG_READ_CHUNK_MODE;
734
 
         if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
 
547
 
 
548
         if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
735
549
            png_error(png_ptr, "Not enough compressed data");
 
550
 
736
551
         return;
737
552
      }
738
553
 
739
554
      png_ptr->idat_size = png_ptr->push_length;
740
555
   }
741
 
   if (png_ptr->idat_size && png_ptr->save_buffer_size)
 
556
 
 
557
   if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
742
558
   {
743
 
      png_size_t save_size;
744
 
 
745
 
      if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size)
746
 
      {
747
 
         save_size = (png_size_t)png_ptr->idat_size;
748
 
 
749
 
         /* Check for overflow */
750
 
         if ((png_uint_32)save_size != png_ptr->idat_size)
751
 
            png_error(png_ptr, "save_size overflowed in pngpread");
752
 
      }
 
559
      png_size_t save_size = png_ptr->save_buffer_size;
 
560
      png_uint_32 idat_size = png_ptr->idat_size;
 
561
 
 
562
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
 
563
       * are of different types and we don't know which variable has the fewest
 
564
       * bits.  Carefully select the smaller and cast it to the type of the
 
565
       * larger - this cannot overflow.  Do not cast in the following test - it
 
566
       * will break on either 16 or 64 bit platforms.
 
567
       */
 
568
      if (idat_size < save_size)
 
569
         save_size = (png_size_t)idat_size;
 
570
 
753
571
      else
754
 
         save_size = png_ptr->save_buffer_size;
 
572
         idat_size = (png_uint_32)save_size;
755
573
 
756
574
      png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
757
575
 
758
576
      png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
759
577
 
760
 
      png_ptr->idat_size -= save_size;
 
578
      png_ptr->idat_size -= idat_size;
761
579
      png_ptr->buffer_size -= save_size;
762
580
      png_ptr->save_buffer_size -= save_size;
763
581
      png_ptr->save_buffer_ptr += save_size;
764
582
   }
765
 
   if (png_ptr->idat_size && png_ptr->current_buffer_size)
 
583
 
 
584
   if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
766
585
   {
767
 
      png_size_t save_size;
768
 
 
769
 
      if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size)
770
 
      {
771
 
         save_size = (png_size_t)png_ptr->idat_size;
772
 
 
773
 
         /* Check for overflow */
774
 
         if ((png_uint_32)save_size != png_ptr->idat_size)
775
 
            png_error(png_ptr, "save_size overflowed in pngpread");
776
 
      }
 
586
      png_size_t save_size = png_ptr->current_buffer_size;
 
587
      png_uint_32 idat_size = png_ptr->idat_size;
 
588
 
 
589
      /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
 
590
       * are of different types and we don't know which variable has the fewest
 
591
       * bits.  Carefully select the smaller and cast it to the type of the
 
592
       * larger - this cannot overflow.
 
593
       */
 
594
      if (idat_size < save_size)
 
595
         save_size = (png_size_t)idat_size;
 
596
 
777
597
      else
778
 
         save_size = png_ptr->current_buffer_size;
 
598
         idat_size = (png_uint_32)save_size;
779
599
 
780
600
      png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
781
601
 
782
602
      png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
783
603
 
784
 
      png_ptr->idat_size -= save_size;
 
604
      png_ptr->idat_size -= idat_size;
785
605
      png_ptr->buffer_size -= save_size;
786
606
      png_ptr->current_buffer_size -= save_size;
787
607
      png_ptr->current_buffer_ptr += save_size;
788
608
   }
789
 
   if (!png_ptr->idat_size)
 
609
 
 
610
   if (png_ptr->idat_size == 0)
790
611
   {
791
 
      if (png_ptr->buffer_size < 4)
792
 
      {
793
 
         png_push_save_buffer(png_ptr);
794
 
         return;
795
 
      }
796
 
 
 
612
      PNG_PUSH_SAVE_BUFFER_IF_LT(4)
797
613
      png_crc_finish(png_ptr, 0);
798
614
      png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
799
615
      png_ptr->mode |= PNG_AFTER_IDAT;
 
616
      png_ptr->zowner = 0;
800
617
   }
801
618
}
802
619
 
803
620
void /* PRIVATE */
804
 
png_process_IDAT_data(png_structp png_ptr, png_bytep buffer,
 
621
png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer,
805
622
   png_size_t buffer_length)
806
623
{
807
624
   /* The caller checks for a non-zero buffer length. */
813
630
    * handle the uncompressed results.
814
631
    */
815
632
   png_ptr->zstream.next_in = buffer;
 
633
   /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
816
634
   png_ptr->zstream.avail_in = (uInt)buffer_length;
817
635
 
818
636
   /* Keep going until the decompressed data is all processed
819
637
    * or the stream marked as finished.
820
638
    */
821
639
   while (png_ptr->zstream.avail_in > 0 &&
822
 
      !(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
 
640
      (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
823
641
   {
824
642
      int ret;
825
643
 
826
644
      /* We have data for zlib, but we must check that zlib
827
 
       * has somewhere to put the results.  It doesn't matter
 
645
       * has someplace to put the results.  It doesn't matter
828
646
       * if we don't expect any results -- it may be the input
829
647
       * data is just the LZ end code.
830
648
       */
831
649
      if (!(png_ptr->zstream.avail_out > 0))
832
650
      {
833
 
         png_ptr->zstream.avail_out =
834
 
             (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
835
 
             png_ptr->iwidth) + 1;
 
651
         /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
 
652
         png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
 
653
             png_ptr->iwidth) + 1);
 
654
 
836
655
         png_ptr->zstream.next_out = png_ptr->row_buf;
837
656
      }
838
657
 
839
658
      /* Using Z_SYNC_FLUSH here means that an unterminated
840
 
       * LZ stream can still be handled (a stream with a missing
841
 
       * end code), otherwise (Z_NO_FLUSH) a future zlib
842
 
       * implementation might defer output and, therefore,
843
 
       * change the current behavior.  (See comments in inflate.c
844
 
       * for why this doesn't happen at present with zlib 1.2.5.)
 
659
       * LZ stream (a stream with a missing end code) can still
 
660
       * be handled, otherwise (Z_NO_FLUSH) a future zlib
 
661
       * implementation might defer output and therefore
 
662
       * change the current behavior (see comments in inflate.c
 
663
       * for why this doesn't happen at present with zlib 1.2.5).
845
664
       */
846
665
      ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
847
666
 
849
668
      if (ret != Z_OK && ret != Z_STREAM_END)
850
669
      {
851
670
         /* Terminate the decompression. */
852
 
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 
671
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
 
672
         png_ptr->zowner = 0;
853
673
 
854
674
         /* This may be a truncated stream (missing or
855
675
          * damaged end code).  Treat that as a warning.
857
677
         if (png_ptr->row_number >= png_ptr->num_rows ||
858
678
             png_ptr->pass > 6)
859
679
            png_warning(png_ptr, "Truncated compressed data in IDAT");
 
680
 
860
681
         else
861
682
            png_error(png_ptr, "Decompression error in IDAT");
862
683
 
876
697
         {
877
698
            /* Extra data. */
878
699
            png_warning(png_ptr, "Extra compressed data in IDAT");
879
 
            png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 
700
            png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
 
701
            png_ptr->zowner = 0;
 
702
 
880
703
            /* Do no more processing; skip the unprocessed
881
704
             * input check below.
882
705
             */
890
713
 
891
714
      /* And check for the end of the stream. */
892
715
      if (ret == Z_STREAM_END)
893
 
         png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
 
716
         png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
894
717
   }
895
718
 
896
719
   /* All the data should have been processed, if anything
898
721
    * after the zlib end code.
899
722
    */
900
723
   if (png_ptr->zstream.avail_in > 0)
901
 
      png_warning(png_ptr, "Extra compression data");
 
724
      png_warning(png_ptr, "Extra compression data in IDAT");
902
725
}
903
726
 
904
727
void /* PRIVATE */
905
 
png_push_process_row(png_structp png_ptr)
 
728
png_push_process_row(png_structrp png_ptr)
906
729
{
907
 
   png_ptr->row_info.color_type = png_ptr->color_type;
908
 
   png_ptr->row_info.width = png_ptr->iwidth;
909
 
   png_ptr->row_info.channels = png_ptr->channels;
910
 
   png_ptr->row_info.bit_depth = png_ptr->bit_depth;
911
 
   png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
912
 
 
913
 
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
914
 
       png_ptr->row_info.width);
915
 
 
916
 
   png_read_filter_row(png_ptr, &(png_ptr->row_info),
917
 
       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
918
 
       (int)(png_ptr->row_buf[0]));
919
 
 
920
 
   png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
921
 
 
922
 
   if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
923
 
      png_do_read_transformations(png_ptr);
 
730
   /* 1.5.6: row_info moved out of png_struct to a local here. */
 
731
   png_row_info row_info;
 
732
 
 
733
   row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
 
734
   row_info.color_type = png_ptr->color_type;
 
735
   row_info.bit_depth = png_ptr->bit_depth;
 
736
   row_info.channels = png_ptr->channels;
 
737
   row_info.pixel_depth = png_ptr->pixel_depth;
 
738
   row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
 
739
 
 
740
   if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
 
741
   {
 
742
      if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
 
743
         png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
 
744
            png_ptr->prev_row + 1, png_ptr->row_buf[0]);
 
745
      else
 
746
         png_error(png_ptr, "bad adaptive filter value");
 
747
   }
 
748
 
 
749
   /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
 
750
    * 1.5.6, while the buffer really is this big in current versions of libpng
 
751
    * it may not be in the future, so this was changed just to copy the
 
752
    * interlaced row count:
 
753
    */
 
754
   memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
 
755
 
 
756
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
 
757
   if (png_ptr->transformations != 0)
 
758
      png_do_read_transformations(png_ptr, &row_info);
 
759
#endif
 
760
 
 
761
   /* The transformed pixel depth should match the depth now in row_info. */
 
762
   if (png_ptr->transformed_pixel_depth == 0)
 
763
   {
 
764
      png_ptr->transformed_pixel_depth = row_info.pixel_depth;
 
765
      if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
 
766
         png_error(png_ptr, "progressive row overflow");
 
767
   }
 
768
 
 
769
   else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
 
770
      png_error(png_ptr, "internal progressive row size calculation error");
 
771
 
924
772
 
925
773
#ifdef PNG_READ_INTERLACING_SUPPORTED
926
 
   /* Blow up interlaced rows to full size */
927
 
   if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
 
774
   /* Expand interlaced rows to full size */
 
775
   if (png_ptr->interlaced != 0 &&
 
776
       (png_ptr->transformations & PNG_INTERLACE) != 0)
928
777
   {
929
778
      if (png_ptr->pass < 6)
930
 
/*       old interface (pre-1.0.9):
931
 
         png_do_read_interlace(&(png_ptr->row_info),
932
 
             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
933
 
 */
934
 
         png_do_read_interlace(png_ptr);
 
779
         png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
 
780
            png_ptr->transformations);
935
781
 
936
 
    switch (png_ptr->pass)
937
 
    {
 
782
      switch (png_ptr->pass)
 
783
      {
938
784
         case 0:
939
785
         {
940
786
            int i;
1109
955
}
1110
956
 
1111
957
void /* PRIVATE */
1112
 
png_read_push_finish_row(png_structp png_ptr)
 
958
png_read_push_finish_row(png_structrp png_ptr)
1113
959
{
 
960
#ifdef PNG_READ_INTERLACING_SUPPORTED
1114
961
   /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1115
962
 
1116
963
   /* Start of interlace block */
1117
 
   PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
 
964
   static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1118
965
 
1119
966
   /* Offset to next interlace block */
1120
 
   PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
 
967
   static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1121
968
 
1122
969
   /* Start of interlace block in the y direction */
1123
 
   PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
 
970
   static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1124
971
 
1125
972
   /* Offset to next interlace block in the y direction */
1126
 
   PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
 
973
   static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1127
974
 
1128
975
   /* Height of interlace block.  This is not currently used - if you need
1129
976
    * it, uncomment it here and in png.h
1130
 
   PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
 
977
   static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1131
978
   */
 
979
#endif
1132
980
 
1133
981
   png_ptr->row_number++;
1134
982
   if (png_ptr->row_number < png_ptr->num_rows)
1135
983
      return;
1136
984
 
1137
985
#ifdef PNG_READ_INTERLACING_SUPPORTED
1138
 
   if (png_ptr->interlaced)
 
986
   if (png_ptr->interlaced != 0)
1139
987
   {
1140
988
      png_ptr->row_number = 0;
1141
 
      png_memset(png_ptr->prev_row, 0,
1142
 
         png_ptr->rowbytes + 1);
 
989
      memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
 
990
 
1143
991
      do
1144
992
      {
1145
993
         png_ptr->pass++;
1146
994
         if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1147
995
             (png_ptr->pass == 3 && png_ptr->width < 3) ||
1148
996
             (png_ptr->pass == 5 && png_ptr->width < 2))
1149
 
           png_ptr->pass++;
 
997
            png_ptr->pass++;
1150
998
 
1151
999
         if (png_ptr->pass > 7)
1152
1000
            png_ptr->pass--;
1155
1003
            break;
1156
1004
 
1157
1005
         png_ptr->iwidth = (png_ptr->width +
1158
 
            png_pass_inc[png_ptr->pass] - 1 -
1159
 
            png_pass_start[png_ptr->pass]) /
1160
 
            png_pass_inc[png_ptr->pass];
 
1006
             png_pass_inc[png_ptr->pass] - 1 -
 
1007
             png_pass_start[png_ptr->pass]) /
 
1008
             png_pass_inc[png_ptr->pass];
1161
1009
 
1162
 
         if (png_ptr->transformations & PNG_INTERLACE)
 
1010
         if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1163
1011
            break;
1164
1012
 
1165
1013
         png_ptr->num_rows = (png_ptr->height +
1166
 
            png_pass_yinc[png_ptr->pass] - 1 -
1167
 
            png_pass_ystart[png_ptr->pass]) /
1168
 
            png_pass_yinc[png_ptr->pass];
 
1014
             png_pass_yinc[png_ptr->pass] - 1 -
 
1015
             png_pass_ystart[png_ptr->pass]) /
 
1016
             png_pass_yinc[png_ptr->pass];
1169
1017
 
1170
1018
      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1171
1019
   }
1172
 
#endif /* PNG_READ_INTERLACING_SUPPORTED */
 
1020
#endif /* READ_INTERLACING */
1173
1021
}
1174
1022
 
1175
1023
void /* PRIVATE */
1176
 
png_push_have_info(png_structp png_ptr, png_infop info_ptr)
 
1024
png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
1177
1025
{
1178
1026
   if (png_ptr->info_fn != NULL)
1179
1027
      (*(png_ptr->info_fn))(png_ptr, info_ptr);
1180
1028
}
1181
1029
 
1182
1030
void /* PRIVATE */
1183
 
png_push_have_end(png_structp png_ptr, png_infop info_ptr)
 
1031
png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
1184
1032
{
1185
1033
   if (png_ptr->end_fn != NULL)
1186
1034
      (*(png_ptr->end_fn))(png_ptr, info_ptr);
1187
1035
}
1188
1036
 
1189
1037
void /* PRIVATE */
1190
 
png_push_have_row(png_structp png_ptr, png_bytep row)
 
1038
png_push_have_row(png_structrp png_ptr, png_bytep row)
1191
1039
{
1192
1040
   if (png_ptr->row_fn != NULL)
1193
1041
      (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1194
1042
         (int)png_ptr->pass);
1195
1043
}
1196
1044
 
 
1045
#ifdef PNG_READ_INTERLACING_SUPPORTED
1197
1046
void PNGAPI
1198
 
png_progressive_combine_row(png_structp png_ptr,
1199
 
   png_bytep old_row, png_bytep new_row)
 
1047
png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row,
 
1048
    png_const_bytep new_row)
1200
1049
{
1201
 
   PNG_CONST int FARDATA png_pass_dsp_mask[7] =
1202
 
      {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
1203
 
 
1204
1050
   if (png_ptr == NULL)
1205
1051
      return;
1206
1052
 
1207
 
   if (new_row != NULL)    /* new_row must == png_ptr->row_buf here. */
1208
 
      png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
 
1053
   /* new_row is a flag here - if it is NULL then the app callback was called
 
1054
    * from an empty row (see the calls to png_struct::row_fn below), otherwise
 
1055
    * it must be png_ptr->row_buf+1
 
1056
    */
 
1057
   if (new_row != NULL)
 
1058
      png_combine_row(png_ptr, old_row, 1/*blocky display*/);
1209
1059
}
 
1060
#endif /* READ_INTERLACING */
1210
1061
 
1211
1062
void PNGAPI
1212
 
png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr,
1213
 
   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
1214
 
   png_progressive_end_ptr end_fn)
 
1063
png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr,
 
1064
    png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
 
1065
    png_progressive_end_ptr end_fn)
1215
1066
{
1216
1067
   if (png_ptr == NULL)
1217
1068
      return;
1224
1075
}
1225
1076
 
1226
1077
png_voidp PNGAPI
1227
 
png_get_progressive_ptr(png_const_structp png_ptr)
 
1078
png_get_progressive_ptr(png_const_structrp png_ptr)
1228
1079
{
1229
1080
   if (png_ptr == NULL)
1230
1081
      return (NULL);
1231
1082
 
1232
1083
   return png_ptr->io_ptr;
1233
1084
}
1234
 
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 
1085
#endif /* PROGRESSIVE_READ */