1 /* PDFlib GmbH cvsid: $Id: pngread.c,v 1.9.6.1 2009/06/03 08:53:24 rjs Exp $ */
2 
3 /* pngread.c - read a PNG file
4  *
5  * Last changed in libpng 1.2.35 [February 14, 2009]
6  * For conditions of distribution and use, see copyright notice in png.h
7  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
8  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
9  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10  *
11  * This file contains routines that an application calls directly to
12  * read a PNG file or stream.
13  */
14 
15 #define PNG_INTERNAL
16 #include "png.h"
17 #if defined(PNG_READ_SUPPORTED)
18 
19 /* PDFlib GmbH */
20 #if defined(__ia64__) && defined (__linux__)
21 #define PDFLIB_ALIGN16
22 #endif
23 
24 /* Create a PNG structure for reading, and allocate any memory needed. */
25 png_structp PNGAPI
png_create_read_struct(png_const_charp user_png_ver,png_voidp error_ptr,png_error_ptr error_fn,png_error_ptr warn_fn)26 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
27    png_error_ptr error_fn, png_error_ptr warn_fn)
28 {
29 
30 #ifdef PNG_USER_MEM_SUPPORTED
31    return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
32       warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
33 }
34 
35 /* Alternate create PNG structure for reading, and allocate any memory needed.
36 */
37 png_structp PNGAPI
png_create_read_struct_2(png_const_charp user_png_ver,png_voidp error_ptr,png_error_ptr error_fn,png_error_ptr warn_fn,png_voidp mem_ptr,png_malloc_ptr malloc_fn,png_free_ptr free_fn)38 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
39    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
40    png_malloc_ptr malloc_fn, png_free_ptr free_fn)
41 {
42 #endif /* PNG_USER_MEM_SUPPORTED */
43 
44 #ifdef PNG_SETJMP_SUPPORTED
45    volatile
46 #endif
47    png_structp png_ptr;
48 
49 #ifdef PNG_SETJMP_SUPPORTED
50 #if defined(USE_FAR_KEYWORD) || defined (PDFLIB_ALIGN16)
51    jmp_buf jmpbuf;
52 #endif
53 #endif
54 
55    int i;
56 
57    png_debug(1, "in png_create_read_struct");
58 #ifdef PNG_USER_MEM_SUPPORTED
59    png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
60       (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
61 #else
62    png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
63 #endif
64    if (png_ptr == NULL)
65       return (NULL);
66 
67    /* added at libpng-1.2.6 */
68 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
69    png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
70    png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
71 #endif
72 
73 #ifdef PNG_SETJMP_SUPPORTED
74 #if defined (USE_FAR_KEYWORD) || defined (PDFLIB_ALIGN16)
75    if (setjmp(jmpbuf))
76 #else
77    if (setjmp(png_ptr->jmpbuf))
78 #endif
79    {
80       png_free(png_ptr, png_ptr->zbuf);
81       png_ptr->zbuf = NULL;
82 #ifdef PNG_USER_MEM_SUPPORTED
83       png_destroy_struct_2((png_voidp)png_ptr,
84          (png_free_ptr)free_fn, (png_voidp)mem_ptr);
85 #else
86       png_destroy_struct((png_voidp)png_ptr);
87 #endif
88       return (NULL);
89    }
90 #if defined (USE_FAR_KEYWORD) || defined (PDFLIB_ALIGN16)
91    png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
92 #endif
93 #endif
94 
95 #ifdef PNG_USER_MEM_SUPPORTED
96    png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
97 #endif
98 
99    png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
100 
101    if (user_png_ver)
102    {
103      i = 0;
104      do
105      {
106        if (user_png_ver[i] != png_libpng_ver[i])
107           png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
108      } while (png_libpng_ver[i++]);
109    }
110    else
111         png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
112 
113 
114    if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
115    {
116      /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
117       * we must recompile any applications that use any older library version.
118       * For versions after libpng 1.0, we will be compatible, so we need
119       * only check the first digit.
120       */
121      if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
122          (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
123          (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
124      {
125 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
126         char msg[80];
127         if (user_png_ver)
128         {
129           png_snprintf(msg, 80,
130              "Application was compiled with png.h from libpng-%.20s",
131              user_png_ver);
132           png_warning(png_ptr, msg);
133         }
134         png_snprintf(msg, 80,
135              "Application  is  running with png.c from libpng-%.20s",
136            png_libpng_ver);
137         png_warning(png_ptr, msg);
138 #endif
139 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
140         png_ptr->flags = 0;
141 #endif
142         png_error(png_ptr,
143            "Incompatible libpng version in application and library");
144      }
145    }
146 
147    /* initialize zbuf - compression buffer */
148    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
149    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
150      (png_uint_32)png_ptr->zbuf_size);
151    png_ptr->zstream.zalloc = png_zalloc;
152    png_ptr->zstream.zfree = png_zfree;
153    png_ptr->zstream.opaque = (voidpf)png_ptr;
154 
155    switch (inflateInit(&png_ptr->zstream))
156    {
157      case Z_OK: /* Do nothing */ break;
158      case Z_MEM_ERROR:
159      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
160      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
161      default: png_error(png_ptr, "Unknown zlib error");
162    }
163 
164    png_ptr->zstream.next_out = png_ptr->zbuf;
165    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
166 
167    png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
168 
169 #ifdef PNG_SETJMP_SUPPORTED
170 /* Applications that neglect to set up their own setjmp() and then encounter
171    a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
172    abort instead of returning. */
173 #if defined (USE_FAR_KEYWORD) || defined (PDFLIB_ALIGN16)
174    if (setjmp(jmpbuf))
175       PNG_ABORT();
176    png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
177 #else
178    if (setjmp(png_ptr->jmpbuf))
179       PNG_ABORT();
180 #endif
181 #endif
182    return (png_ptr);
183 }
184 
185 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
186 /* Initialize PNG structure for reading, and allocate any memory needed.
187    This interface is deprecated in favour of the png_create_read_struct(),
188    and it will disappear as of libpng-1.3.0. */
189 /* PDFlib GmbH: the messy hack below would fool our pdf_ prefixes. */
190 #if 0
191 #undef png_read_init
192 void PNGAPI
193 png_read_init(png_structp png_ptr)
194 {
195    /* We only come here via pre-1.0.7-compiled applications */
196    png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
197 }
198 #endif
199 
200 void PNGAPI
png_read_init_2(png_structp png_ptr,png_const_charp user_png_ver,png_size_t png_struct_size,png_size_t png_info_size)201 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
202    png_size_t png_struct_size, png_size_t png_info_size)
203 {
204    /* We only come here via pre-1.0.12-compiled applications */
205    if (png_ptr == NULL) return;
206 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
207    if (png_sizeof(png_struct) > png_struct_size ||
208       png_sizeof(png_info) > png_info_size)
209    {
210       char msg[80];
211       png_ptr->warning_fn = NULL;
212       if (user_png_ver)
213       {
214         png_snprintf(msg, 80,
215            "Application was compiled with png.h from libpng-%.20s",
216            user_png_ver);
217         png_warning(png_ptr, msg);
218       }
219       png_snprintf(msg, 80,
220          "Application  is  running with png.c from libpng-%.20s",
221          png_libpng_ver);
222       png_warning(png_ptr, msg);
223    }
224 #endif
225    if (png_sizeof(png_struct) > png_struct_size)
226      {
227        png_ptr->error_fn = NULL;
228 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
229        png_ptr->flags = 0;
230 #endif
231        png_error(png_ptr,
232        "The png struct allocated by the application for reading is too small.");
233      }
234    if (png_sizeof(png_info) > png_info_size)
235      {
236        png_ptr->error_fn = NULL;
237 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
238        png_ptr->flags = 0;
239 #endif
240        png_error(png_ptr,
241          "The info struct allocated by application for reading is too small.");
242      }
243    png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
244 }
245 #endif /* PNG_1_0_X || PNG_1_2_X */
246 
247 void PNGAPI
png_read_init_3(png_structpp ptr_ptr,png_const_charp user_png_ver,png_size_t png_struct_size)248 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
249    png_size_t png_struct_size)
250 {
251 #ifdef PNG_SETJMP_SUPPORTED
252    jmp_buf tmp_jmp;  /* to save current jump buffer */
253 #endif
254 
255    int i = 0;
256 
257    png_structp png_ptr=*ptr_ptr;
258 
259    if (png_ptr == NULL) return;
260 
261    do
262    {
263      if (user_png_ver[i] != png_libpng_ver[i])
264      {
265 #ifdef PNG_LEGACY_SUPPORTED
266        png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
267 #else
268        png_ptr->warning_fn = NULL;
269        png_warning(png_ptr,
270        "Application uses deprecated png_read_init() and should be recompiled.");
271        break;
272 #endif
273      }
274    } while (png_libpng_ver[i++]);
275 
276    png_debug(1, "in png_read_init_3");
277 
278 #ifdef PNG_SETJMP_SUPPORTED
279    /* save jump buffer and error functions */
280    png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
281 #endif
282 
283    if (png_sizeof(png_struct) > png_struct_size)
284    {
285       png_destroy_struct(png_ptr);
286       *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
287       png_ptr = *ptr_ptr;
288    }
289 
290    /* reset all variables to 0 */
291    png_memset(png_ptr, 0, png_sizeof(png_struct));
292 
293 #ifdef PNG_SETJMP_SUPPORTED
294    /* restore jump buffer */
295    png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
296 #endif
297 
298    /* added at libpng-1.2.6 */
299 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
300    png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
301    png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
302 #endif
303 
304    /* initialize zbuf - compression buffer */
305    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
306    png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
307      (png_uint_32)png_ptr->zbuf_size);
308    png_ptr->zstream.zalloc = png_zalloc;
309    png_ptr->zstream.zfree = png_zfree;
310    png_ptr->zstream.opaque = (voidpf)png_ptr;
311 
312    switch (inflateInit(&png_ptr->zstream))
313    {
314      case Z_OK: /* Do nothing */ break;
315      case Z_MEM_ERROR:
316      case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
317      case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
318      default: png_error(png_ptr, "Unknown zlib error");
319    }
320 
321    png_ptr->zstream.next_out = png_ptr->zbuf;
322    png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
323 
324    png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
325 }
326 
327 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
328 /* Read the information before the actual image data.  This has been
329  * changed in v0.90 to allow reading a file that already has the magic
330  * bytes read from the stream.  You can tell libpng how many bytes have
331  * been read from the beginning of the stream (up to the maximum of 8)
332  * via png_set_sig_bytes(), and we will only check the remaining bytes
333  * here.  The application can then have access to the signature bytes we
334  * read if it is determined that this isn't a valid PNG file.
335  */
336 void PNGAPI
png_read_info(png_structp png_ptr,png_infop info_ptr)337 png_read_info(png_structp png_ptr, png_infop info_ptr)
338 {
339    if (png_ptr == NULL || info_ptr == NULL) return;
340    png_debug(1, "in png_read_info");
341    /* If we haven't checked all of the PNG signature bytes, do so now. */
342    if (png_ptr->sig_bytes < 8)
343    {
344       png_size_t num_checked = png_ptr->sig_bytes,
345                  num_to_check = 8 - num_checked;
346 
347       png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
348       png_ptr->sig_bytes = 8;
349 
350       if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
351       {
352          if (num_checked < 4 &&
353              png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
354             png_error(png_ptr, "Not a PNG file");
355          else
356             png_error(png_ptr, "PNG file corrupted by ASCII conversion");
357       }
358       if (num_checked < 3)
359          png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
360    }
361 
362    for (;;)
363    {
364 #ifdef PNG_USE_LOCAL_ARRAYS
365       PNG_CONST PNG_IHDR;
366       PNG_CONST PNG_IDAT;
367       PNG_CONST PNG_IEND;
368       PNG_CONST PNG_PLTE;
369 #if defined(PNG_READ_bKGD_SUPPORTED)
370       PNG_CONST PNG_bKGD;
371 #endif
372 #if defined(PNG_READ_cHRM_SUPPORTED)
373       PNG_CONST PNG_cHRM;
374 #endif
375 #if defined(PNG_READ_gAMA_SUPPORTED)
376       PNG_CONST PNG_gAMA;
377 #endif
378 #if defined(PNG_READ_hIST_SUPPORTED)
379       PNG_CONST PNG_hIST;
380 #endif
381 #if defined(PNG_READ_iCCP_SUPPORTED)
382       PNG_CONST PNG_iCCP;
383 #endif
384 #if defined(PNG_READ_iTXt_SUPPORTED)
385       PNG_CONST PNG_iTXt;
386 #endif
387 #if defined(PNG_READ_oFFs_SUPPORTED)
388       PNG_CONST PNG_oFFs;
389 #endif
390 #if defined(PNG_READ_pCAL_SUPPORTED)
391       PNG_CONST PNG_pCAL;
392 #endif
393 #if defined(PNG_READ_pHYs_SUPPORTED)
394       PNG_CONST PNG_pHYs;
395 #endif
396 #if defined(PNG_READ_sBIT_SUPPORTED)
397       PNG_CONST PNG_sBIT;
398 #endif
399 #if defined(PNG_READ_sCAL_SUPPORTED)
400       PNG_CONST PNG_sCAL;
401 #endif
402 #if defined(PNG_READ_sPLT_SUPPORTED)
403       PNG_CONST PNG_sPLT;
404 #endif
405 #if defined(PNG_READ_sRGB_SUPPORTED)
406       PNG_CONST PNG_sRGB;
407 #endif
408 #if defined(PNG_READ_tEXt_SUPPORTED)
409       PNG_CONST PNG_tEXt;
410 #endif
411 #if defined(PNG_READ_tIME_SUPPORTED)
412       PNG_CONST PNG_tIME;
413 #endif
414 #if defined(PNG_READ_tRNS_SUPPORTED)
415       PNG_CONST PNG_tRNS;
416 #endif
417 #if defined(PNG_READ_zTXt_SUPPORTED)
418       PNG_CONST PNG_zTXt;
419 #endif
420 #endif /* PNG_USE_LOCAL_ARRAYS */
421       png_uint_32 length = png_read_chunk_header(png_ptr);
422       PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
423 
424       /* This should be a binary subdivision search or a hash for
425        * matching the chunk name rather than a linear search.
426        */
427       if (!png_memcmp(chunk_name, png_IDAT, 4))
428         if (png_ptr->mode & PNG_AFTER_IDAT)
429           png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
430 
431       if (!png_memcmp(chunk_name, png_IHDR, 4))
432          png_handle_IHDR(png_ptr, info_ptr, length);
433       else if (!png_memcmp(chunk_name, png_IEND, 4))
434          png_handle_IEND(png_ptr, info_ptr, length);
435 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
436       else if (png_handle_as_unknown(png_ptr, chunk_name))
437       {
438          if (!png_memcmp(chunk_name, png_IDAT, 4))
439             png_ptr->mode |= PNG_HAVE_IDAT;
440          png_handle_unknown(png_ptr, info_ptr, length);
441          if (!png_memcmp(chunk_name, png_PLTE, 4))
442             png_ptr->mode |= PNG_HAVE_PLTE;
443          else if (!png_memcmp(chunk_name, png_IDAT, 4))
444          {
445             if (!(png_ptr->mode & PNG_HAVE_IHDR))
446                png_error(png_ptr, "Missing IHDR before IDAT");
447             else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
448                      !(png_ptr->mode & PNG_HAVE_PLTE))
449                png_error(png_ptr, "Missing PLTE before IDAT");
450             break;
451          }
452       }
453 #endif
454       else if (!png_memcmp(chunk_name, png_PLTE, 4))
455          png_handle_PLTE(png_ptr, info_ptr, length);
456       else if (!png_memcmp(chunk_name, png_IDAT, 4))
457       {
458          if (!(png_ptr->mode & PNG_HAVE_IHDR))
459             png_error(png_ptr, "Missing IHDR before IDAT");
460          else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
461                   !(png_ptr->mode & PNG_HAVE_PLTE))
462             png_error(png_ptr, "Missing PLTE before IDAT");
463 
464          png_ptr->idat_size = length;
465          png_ptr->mode |= PNG_HAVE_IDAT;
466          break;
467       }
468 #if defined(PNG_READ_bKGD_SUPPORTED)
469       else if (!png_memcmp(chunk_name, png_bKGD, 4))
470          png_handle_bKGD(png_ptr, info_ptr, length);
471 #endif
472 #if defined(PNG_READ_cHRM_SUPPORTED)
473       else if (!png_memcmp(chunk_name, png_cHRM, 4))
474          png_handle_cHRM(png_ptr, info_ptr, length);
475 #endif
476 #if defined(PNG_READ_gAMA_SUPPORTED)
477       else if (!png_memcmp(chunk_name, png_gAMA, 4))
478          png_handle_gAMA(png_ptr, info_ptr, length);
479 #endif
480 #if defined(PNG_READ_hIST_SUPPORTED)
481       else if (!png_memcmp(chunk_name, png_hIST, 4))
482          png_handle_hIST(png_ptr, info_ptr, length);
483 #endif
484 #if defined(PNG_READ_oFFs_SUPPORTED)
485       else if (!png_memcmp(chunk_name, png_oFFs, 4))
486          png_handle_oFFs(png_ptr, info_ptr, length);
487 #endif
488 #if defined(PNG_READ_pCAL_SUPPORTED)
489       else if (!png_memcmp(chunk_name, png_pCAL, 4))
490          png_handle_pCAL(png_ptr, info_ptr, length);
491 #endif
492 #if defined(PNG_READ_sCAL_SUPPORTED)
493       else if (!png_memcmp(chunk_name, png_sCAL, 4))
494          png_handle_sCAL(png_ptr, info_ptr, length);
495 #endif
496 #if defined(PNG_READ_pHYs_SUPPORTED)
497       else if (!png_memcmp(chunk_name, png_pHYs, 4))
498          png_handle_pHYs(png_ptr, info_ptr, length);
499 #endif
500 #if defined(PNG_READ_sBIT_SUPPORTED)
501       else if (!png_memcmp(chunk_name, png_sBIT, 4))
502          png_handle_sBIT(png_ptr, info_ptr, length);
503 #endif
504 #if defined(PNG_READ_sRGB_SUPPORTED)
505       else if (!png_memcmp(chunk_name, png_sRGB, 4))
506          png_handle_sRGB(png_ptr, info_ptr, length);
507 #endif
508 #if defined(PNG_READ_iCCP_SUPPORTED)
509       else if (!png_memcmp(chunk_name, png_iCCP, 4))
510          png_handle_iCCP(png_ptr, info_ptr, length);
511 #endif
512 #if defined(PNG_READ_sPLT_SUPPORTED)
513       else if (!png_memcmp(chunk_name, png_sPLT, 4))
514          png_handle_sPLT(png_ptr, info_ptr, length);
515 #endif
516 #if defined(PNG_READ_tEXt_SUPPORTED)
517       else if (!png_memcmp(chunk_name, png_tEXt, 4))
518          png_handle_tEXt(png_ptr, info_ptr, length);
519 #endif
520 #if defined(PNG_READ_tIME_SUPPORTED)
521       else if (!png_memcmp(chunk_name, png_tIME, 4))
522          png_handle_tIME(png_ptr, info_ptr, length);
523 #endif
524 #if defined(PNG_READ_tRNS_SUPPORTED)
525       else if (!png_memcmp(chunk_name, png_tRNS, 4))
526          png_handle_tRNS(png_ptr, info_ptr, length);
527 #endif
528 #if defined(PNG_READ_zTXt_SUPPORTED)
529       else if (!png_memcmp(chunk_name, png_zTXt, 4))
530          png_handle_zTXt(png_ptr, info_ptr, length);
531 #endif
532 #if defined(PNG_READ_iTXt_SUPPORTED)
533       else if (!png_memcmp(chunk_name, png_iTXt, 4))
534          png_handle_iTXt(png_ptr, info_ptr, length);
535 #endif
536       else
537          png_handle_unknown(png_ptr, info_ptr, length);
538    }
539 }
540 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
541 
542 /* optional call to update the users info_ptr structure */
543 void PNGAPI
png_read_update_info(png_structp png_ptr,png_infop info_ptr)544 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
545 {
546    png_debug(1, "in png_read_update_info");
547    if (png_ptr == NULL) return;
548    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
549       png_read_start_row(png_ptr);
550    else
551       png_warning(png_ptr,
552       "Ignoring extra png_read_update_info() call; row buffer not reallocated");
553    png_read_transform_info(png_ptr, info_ptr);
554 }
555 
556 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
557 /* Initialize palette, background, etc, after transformations
558  * are set, but before any reading takes place.  This allows
559  * the user to obtain a gamma-corrected palette, for example.
560  * If the user doesn't call this, we will do it ourselves.
561  */
562 void PNGAPI
png_start_read_image(png_structp png_ptr)563 png_start_read_image(png_structp png_ptr)
564 {
565    png_debug(1, "in png_start_read_image");
566    if (png_ptr == NULL) return;
567    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
568       png_read_start_row(png_ptr);
569 }
570 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
571 
572 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
573 void PNGAPI
png_read_row(png_structp png_ptr,png_bytep row,png_bytep dsp_row)574 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
575 {
576 #ifdef PNG_USE_LOCAL_ARRAYS
577    PNG_CONST PNG_IDAT;
578    PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
579       0xff};
580    PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
581 #endif
582    int ret;
583    if (png_ptr == NULL) return;
584    png_debug2(1, "in png_read_row (row %lu, pass %d)",
585       png_ptr->row_number, png_ptr->pass);
586    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
587       png_read_start_row(png_ptr);
588    if (png_ptr->row_number == 0 && png_ptr->pass == 0)
589    {
590    /* check for transforms that have been set but were defined out */
591 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
592    if (png_ptr->transformations & PNG_INVERT_MONO)
593       png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
594 #endif
595 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
596    if (png_ptr->transformations & PNG_FILLER)
597       png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
598 #endif
599 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) \
600     && !defined(PNG_READ_PACKSWAP_SUPPORTED)
601    if (png_ptr->transformations & PNG_PACKSWAP)
602       png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
603 #endif
604 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
605    if (png_ptr->transformations & PNG_PACK)
606       png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
607 #endif
608 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
609    if (png_ptr->transformations & PNG_SHIFT)
610       png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
611 #endif
612 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
613    if (png_ptr->transformations & PNG_BGR)
614       png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
615 #endif
616 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
617    if (png_ptr->transformations & PNG_SWAP_BYTES)
618       png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
619 #endif
620    }
621 
622 #if defined(PNG_READ_INTERLACING_SUPPORTED)
623    /* if interlaced and we do not need a new row, combine row and return */
624    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
625    {
626       switch (png_ptr->pass)
627       {
628          case 0:
629             if (png_ptr->row_number & 0x07)
630             {
631                if (dsp_row != NULL)
632                   png_combine_row(png_ptr, dsp_row,
633                      png_pass_dsp_mask[png_ptr->pass]);
634                png_read_finish_row(png_ptr);
635                return;
636             }
637             break;
638          case 1:
639             if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
640             {
641                if (dsp_row != NULL)
642                   png_combine_row(png_ptr, dsp_row,
643                      png_pass_dsp_mask[png_ptr->pass]);
644                png_read_finish_row(png_ptr);
645                return;
646             }
647             break;
648          case 2:
649             if ((png_ptr->row_number & 0x07) != 4)
650             {
651                if (dsp_row != NULL && (png_ptr->row_number & 4))
652                   png_combine_row(png_ptr, dsp_row,
653                      png_pass_dsp_mask[png_ptr->pass]);
654                png_read_finish_row(png_ptr);
655                return;
656             }
657             break;
658          case 3:
659             if ((png_ptr->row_number & 3) || png_ptr->width < 3)
660             {
661                if (dsp_row != NULL)
662                   png_combine_row(png_ptr, dsp_row,
663                      png_pass_dsp_mask[png_ptr->pass]);
664                png_read_finish_row(png_ptr);
665                return;
666             }
667             break;
668          case 4:
669             if ((png_ptr->row_number & 3) != 2)
670             {
671                if (dsp_row != NULL && (png_ptr->row_number & 2))
672                   png_combine_row(png_ptr, dsp_row,
673                      png_pass_dsp_mask[png_ptr->pass]);
674                png_read_finish_row(png_ptr);
675                return;
676             }
677             break;
678          case 5:
679             if ((png_ptr->row_number & 1) || png_ptr->width < 2)
680             {
681                if (dsp_row != NULL)
682                   png_combine_row(png_ptr, dsp_row,
683                      png_pass_dsp_mask[png_ptr->pass]);
684                png_read_finish_row(png_ptr);
685                return;
686             }
687             break;
688          case 6:
689             if (!(png_ptr->row_number & 1))
690             {
691                png_read_finish_row(png_ptr);
692                return;
693             }
694             break;
695       }
696    }
697 #endif
698 
699    if (!(png_ptr->mode & PNG_HAVE_IDAT))
700       png_error(png_ptr, "Invalid attempt to read row data");
701 
702    png_ptr->zstream.next_out = png_ptr->row_buf;
703    png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
704    do
705    {
706       if (!(png_ptr->zstream.avail_in))
707       {
708          while (!png_ptr->idat_size)
709          {
710             png_crc_finish(png_ptr, 0);
711 
712             png_ptr->idat_size = png_read_chunk_header(png_ptr);
713             if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
714                png_error(png_ptr, "Not enough image data");
715          }
716          png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
717          png_ptr->zstream.next_in = png_ptr->zbuf;
718          if (png_ptr->zbuf_size > png_ptr->idat_size)
719             png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
720          png_crc_read(png_ptr, png_ptr->zbuf,
721             (png_size_t)png_ptr->zstream.avail_in);
722          png_ptr->idat_size -= png_ptr->zstream.avail_in;
723       }
724       ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
725       if (ret == Z_STREAM_END)
726       {
727          if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
728             png_ptr->idat_size)
729             png_error(png_ptr, "Extra compressed data");
730          png_ptr->mode |= PNG_AFTER_IDAT;
731          png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
732          break;
733       }
734       if (ret != Z_OK)
735          png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
736                    "Decompression error");
737 
738    } while (png_ptr->zstream.avail_out);
739 
740    png_ptr->row_info.color_type = png_ptr->color_type;
741    png_ptr->row_info.width = png_ptr->iwidth;
742    png_ptr->row_info.channels = png_ptr->channels;
743    png_ptr->row_info.bit_depth = png_ptr->bit_depth;
744    png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
745    png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
746        png_ptr->row_info.width);
747 
748    if (png_ptr->row_buf[0])
749    png_read_filter_row(png_ptr, &(png_ptr->row_info),
750       png_ptr->row_buf + 1, png_ptr->prev_row + 1,
751       (int)(png_ptr->row_buf[0]));
752 
753    png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
754       png_ptr->rowbytes + 1);
755 
756 #if defined(PNG_MNG_FEATURES_SUPPORTED)
757    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
758       (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
759    {
760       /* Intrapixel differencing */
761       png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
762    }
763 #endif
764 
765 
766    if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
767       png_do_read_transformations(png_ptr);
768 
769 #if defined(PNG_READ_INTERLACING_SUPPORTED)
770    /* blow up interlaced rows to full size */
771    if (png_ptr->interlaced &&
772       (png_ptr->transformations & PNG_INTERLACE))
773    {
774       if (png_ptr->pass < 6)
775 /*       old interface (pre-1.0.9):
776          png_do_read_interlace(&(png_ptr->row_info),
777             png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
778  */
779          png_do_read_interlace(png_ptr);
780 
781       if (dsp_row != NULL)
782          png_combine_row(png_ptr, dsp_row,
783             png_pass_dsp_mask[png_ptr->pass]);
784       if (row != NULL)
785          png_combine_row(png_ptr, row,
786             png_pass_mask[png_ptr->pass]);
787    }
788    else
789 #endif
790    {
791       if (row != NULL)
792          png_combine_row(png_ptr, row, 0xff);
793       if (dsp_row != NULL)
794          png_combine_row(png_ptr, dsp_row, 0xff);
795    }
796    png_read_finish_row(png_ptr);
797 
798    if (png_ptr->read_row_fn != NULL)
799       (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
800 }
801 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
802 
803 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
804 /* Read one or more rows of image data.  If the image is interlaced,
805  * and png_set_interlace_handling() has been called, the rows need to
806  * contain the contents of the rows from the previous pass.  If the
807  * image has alpha or transparency, and png_handle_alpha()[*] has been
808  * called, the rows contents must be initialized to the contents of the
809  * screen.
810  *
811  * "row" holds the actual image, and pixels are placed in it
812  * as they arrive.  If the image is displayed after each pass, it will
813  * appear to "sparkle" in.  "display_row" can be used to display a
814  * "chunky" progressive image, with finer detail added as it becomes
815  * available.  If you do not want this "chunky" display, you may pass
816  * NULL for display_row.  If you do not want the sparkle display, and
817  * you have not called png_handle_alpha(), you may pass NULL for rows.
818  * If you have called png_handle_alpha(), and the image has either an
819  * alpha channel or a transparency chunk, you must provide a buffer for
820  * rows.  In this case, you do not have to provide a display_row buffer
821  * also, but you may.  If the image is not interlaced, or if you have
822  * not called png_set_interlace_handling(), the display_row buffer will
823  * be ignored, so pass NULL to it.
824  *
825  * [*] png_handle_alpha() does not exist yet, as of this version of libpng
826  */
827 
828 void PNGAPI
png_read_rows(png_structp png_ptr,png_bytepp row,png_bytepp display_row,png_uint_32 num_rows)829 png_read_rows(png_structp png_ptr, png_bytepp row,
830    png_bytepp display_row, png_uint_32 num_rows)
831 {
832    png_uint_32 i;
833    png_bytepp rp;
834    png_bytepp dp;
835 
836    png_debug(1, "in png_read_rows");
837    if (png_ptr == NULL) return;
838    rp = row;
839    dp = display_row;
840    if (rp != NULL && dp != NULL)
841       for (i = 0; i < num_rows; i++)
842       {
843          png_bytep rptr = *rp++;
844          png_bytep dptr = *dp++;
845 
846          png_read_row(png_ptr, rptr, dptr);
847       }
848    else if (rp != NULL)
849       for (i = 0; i < num_rows; i++)
850       {
851          png_bytep rptr = *rp;
852          png_read_row(png_ptr, rptr, png_bytep_NULL);
853          rp++;
854       }
855    else if (dp != NULL)
856       for (i = 0; i < num_rows; i++)
857       {
858          png_bytep dptr = *dp;
859          png_read_row(png_ptr, png_bytep_NULL, dptr);
860          dp++;
861       }
862 }
863 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
864 
865 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
866 /* Read the entire image.  If the image has an alpha channel or a tRNS
867  * chunk, and you have called png_handle_alpha()[*], you will need to
868  * initialize the image to the current image that PNG will be overlaying.
869  * We set the num_rows again here, in case it was incorrectly set in
870  * png_read_start_row() by a call to png_read_update_info() or
871  * png_start_read_image() if png_set_interlace_handling() wasn't called
872  * prior to either of these functions like it should have been.  You can
873  * only call this function once.  If you desire to have an image for
874  * each pass of a interlaced image, use png_read_rows() instead.
875  *
876  * [*] png_handle_alpha() does not exist yet, as of this version of libpng
877  */
878 void PNGAPI
png_read_image(png_structp png_ptr,png_bytepp image)879 png_read_image(png_structp png_ptr, png_bytepp image)
880 {
881    png_uint_32 i, image_height;
882    int pass, j;
883    png_bytepp rp;
884 
885    png_debug(1, "in png_read_image");
886    if (png_ptr == NULL) return;
887 
888 #ifdef PNG_READ_INTERLACING_SUPPORTED
889    pass = png_set_interlace_handling(png_ptr);
890 #else
891    if (png_ptr->interlaced)
892       png_error(png_ptr,
893         "Cannot read interlaced image -- interlace handler disabled.");
894    pass = 1;
895 #endif
896 
897 
898    image_height=png_ptr->height;
899    png_ptr->num_rows = image_height; /* Make sure this is set correctly */
900 
901    for (j = 0; j < pass; j++)
902    {
903       rp = image;
904       for (i = 0; i < image_height; i++)
905       {
906          png_read_row(png_ptr, *rp, png_bytep_NULL);
907          rp++;
908       }
909    }
910 }
911 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
912 
913 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
914 /* Read the end of the PNG file.  Will not read past the end of the
915  * file, will verify the end is accurate, and will read any comments
916  * or time information at the end of the file, if info is not NULL.
917  */
918 void PNGAPI
png_read_end(png_structp png_ptr,png_infop info_ptr)919 png_read_end(png_structp png_ptr, png_infop info_ptr)
920 {
921    png_debug(1, "in png_read_end");
922    if (png_ptr == NULL) return;
923    png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
924 
925    do
926    {
927 #ifdef PNG_USE_LOCAL_ARRAYS
928       PNG_CONST PNG_IHDR;
929       PNG_CONST PNG_IDAT;
930       PNG_CONST PNG_IEND;
931       PNG_CONST PNG_PLTE;
932 #if defined(PNG_READ_bKGD_SUPPORTED)
933       PNG_CONST PNG_bKGD;
934 #endif
935 #if defined(PNG_READ_cHRM_SUPPORTED)
936       PNG_CONST PNG_cHRM;
937 #endif
938 #if defined(PNG_READ_gAMA_SUPPORTED)
939       PNG_CONST PNG_gAMA;
940 #endif
941 #if defined(PNG_READ_hIST_SUPPORTED)
942       PNG_CONST PNG_hIST;
943 #endif
944 #if defined(PNG_READ_iCCP_SUPPORTED)
945       PNG_CONST PNG_iCCP;
946 #endif
947 #if defined(PNG_READ_iTXt_SUPPORTED)
948       PNG_CONST PNG_iTXt;
949 #endif
950 #if defined(PNG_READ_oFFs_SUPPORTED)
951       PNG_CONST PNG_oFFs;
952 #endif
953 #if defined(PNG_READ_pCAL_SUPPORTED)
954       PNG_CONST PNG_pCAL;
955 #endif
956 #if defined(PNG_READ_pHYs_SUPPORTED)
957       PNG_CONST PNG_pHYs;
958 #endif
959 #if defined(PNG_READ_sBIT_SUPPORTED)
960       PNG_CONST PNG_sBIT;
961 #endif
962 #if defined(PNG_READ_sCAL_SUPPORTED)
963       PNG_CONST PNG_sCAL;
964 #endif
965 #if defined(PNG_READ_sPLT_SUPPORTED)
966       PNG_CONST PNG_sPLT;
967 #endif
968 #if defined(PNG_READ_sRGB_SUPPORTED)
969       PNG_CONST PNG_sRGB;
970 #endif
971 #if defined(PNG_READ_tEXt_SUPPORTED)
972       PNG_CONST PNG_tEXt;
973 #endif
974 #if defined(PNG_READ_tIME_SUPPORTED)
975       PNG_CONST PNG_tIME;
976 #endif
977 #if defined(PNG_READ_tRNS_SUPPORTED)
978       PNG_CONST PNG_tRNS;
979 #endif
980 #if defined(PNG_READ_zTXt_SUPPORTED)
981       PNG_CONST PNG_zTXt;
982 #endif
983 #endif /* PNG_USE_LOCAL_ARRAYS */
984       png_uint_32 length = png_read_chunk_header(png_ptr);
985       PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
986 
987       if (!png_memcmp(chunk_name, png_IHDR, 4))
988          png_handle_IHDR(png_ptr, info_ptr, length);
989       else if (!png_memcmp(chunk_name, png_IEND, 4))
990          png_handle_IEND(png_ptr, info_ptr, length);
991 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
992       else if (png_handle_as_unknown(png_ptr, chunk_name))
993       {
994          if (!png_memcmp(chunk_name, png_IDAT, 4))
995          {
996             if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
997                png_error(png_ptr, "Too many IDAT's found");
998          }
999          png_handle_unknown(png_ptr, info_ptr, length);
1000          if (!png_memcmp(chunk_name, png_PLTE, 4))
1001             png_ptr->mode |= PNG_HAVE_PLTE;
1002       }
1003 #endif
1004       else if (!png_memcmp(chunk_name, png_IDAT, 4))
1005       {
1006          /* Zero length IDATs are legal after the last IDAT has been
1007           * read, but not after other chunks have been read.
1008           */
1009          if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
1010             png_error(png_ptr, "Too many IDAT's found");
1011          png_crc_finish(png_ptr, length);
1012       }
1013       else if (!png_memcmp(chunk_name, png_PLTE, 4))
1014          png_handle_PLTE(png_ptr, info_ptr, length);
1015 #if defined(PNG_READ_bKGD_SUPPORTED)
1016       else if (!png_memcmp(chunk_name, png_bKGD, 4))
1017          png_handle_bKGD(png_ptr, info_ptr, length);
1018 #endif
1019 #if defined(PNG_READ_cHRM_SUPPORTED)
1020       else if (!png_memcmp(chunk_name, png_cHRM, 4))
1021          png_handle_cHRM(png_ptr, info_ptr, length);
1022 #endif
1023 #if defined(PNG_READ_gAMA_SUPPORTED)
1024       else if (!png_memcmp(chunk_name, png_gAMA, 4))
1025          png_handle_gAMA(png_ptr, info_ptr, length);
1026 #endif
1027 #if defined(PNG_READ_hIST_SUPPORTED)
1028       else if (!png_memcmp(chunk_name, png_hIST, 4))
1029          png_handle_hIST(png_ptr, info_ptr, length);
1030 #endif
1031 #if defined(PNG_READ_oFFs_SUPPORTED)
1032       else if (!png_memcmp(chunk_name, png_oFFs, 4))
1033          png_handle_oFFs(png_ptr, info_ptr, length);
1034 #endif
1035 #if defined(PNG_READ_pCAL_SUPPORTED)
1036       else if (!png_memcmp(chunk_name, png_pCAL, 4))
1037          png_handle_pCAL(png_ptr, info_ptr, length);
1038 #endif
1039 #if defined(PNG_READ_sCAL_SUPPORTED)
1040       else if (!png_memcmp(chunk_name, png_sCAL, 4))
1041          png_handle_sCAL(png_ptr, info_ptr, length);
1042 #endif
1043 #if defined(PNG_READ_pHYs_SUPPORTED)
1044       else if (!png_memcmp(chunk_name, png_pHYs, 4))
1045          png_handle_pHYs(png_ptr, info_ptr, length);
1046 #endif
1047 #if defined(PNG_READ_sBIT_SUPPORTED)
1048       else if (!png_memcmp(chunk_name, png_sBIT, 4))
1049          png_handle_sBIT(png_ptr, info_ptr, length);
1050 #endif
1051 #if defined(PNG_READ_sRGB_SUPPORTED)
1052       else if (!png_memcmp(chunk_name, png_sRGB, 4))
1053          png_handle_sRGB(png_ptr, info_ptr, length);
1054 #endif
1055 #if defined(PNG_READ_iCCP_SUPPORTED)
1056       else if (!png_memcmp(chunk_name, png_iCCP, 4))
1057          png_handle_iCCP(png_ptr, info_ptr, length);
1058 #endif
1059 #if defined(PNG_READ_sPLT_SUPPORTED)
1060       else if (!png_memcmp(chunk_name, png_sPLT, 4))
1061          png_handle_sPLT(png_ptr, info_ptr, length);
1062 #endif
1063 #if defined(PNG_READ_tEXt_SUPPORTED)
1064       else if (!png_memcmp(chunk_name, png_tEXt, 4))
1065          png_handle_tEXt(png_ptr, info_ptr, length);
1066 #endif
1067 #if defined(PNG_READ_tIME_SUPPORTED)
1068       else if (!png_memcmp(chunk_name, png_tIME, 4))
1069          png_handle_tIME(png_ptr, info_ptr, length);
1070 #endif
1071 #if defined(PNG_READ_tRNS_SUPPORTED)
1072       else if (!png_memcmp(chunk_name, png_tRNS, 4))
1073          png_handle_tRNS(png_ptr, info_ptr, length);
1074 #endif
1075 #if defined(PNG_READ_zTXt_SUPPORTED)
1076       else if (!png_memcmp(chunk_name, png_zTXt, 4))
1077          png_handle_zTXt(png_ptr, info_ptr, length);
1078 #endif
1079 #if defined(PNG_READ_iTXt_SUPPORTED)
1080       else if (!png_memcmp(chunk_name, png_iTXt, 4))
1081          png_handle_iTXt(png_ptr, info_ptr, length);
1082 #endif
1083       else
1084          png_handle_unknown(png_ptr, info_ptr, length);
1085    } while (!(png_ptr->mode & PNG_HAVE_IEND));
1086 }
1087 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1088 
1089 /* free all memory used by the read */
1090 void PNGAPI
png_destroy_read_struct(png_structpp png_ptr_ptr,png_infopp info_ptr_ptr,png_infopp end_info_ptr_ptr)1091 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1092    png_infopp end_info_ptr_ptr)
1093 {
1094    png_structp png_ptr = NULL;
1095    png_infop info_ptr = NULL, end_info_ptr = NULL;
1096 #ifdef PNG_USER_MEM_SUPPORTED
1097    png_free_ptr free_fn = NULL;
1098    png_voidp mem_ptr = NULL;
1099 #endif
1100 
1101    png_debug(1, "in png_destroy_read_struct");
1102    if (png_ptr_ptr != NULL)
1103       png_ptr = *png_ptr_ptr;
1104    if (png_ptr == NULL)
1105       return;
1106 
1107 #ifdef PNG_USER_MEM_SUPPORTED
1108    free_fn = png_ptr->free_fn;
1109    mem_ptr = png_ptr->mem_ptr;
1110 #endif
1111 
1112    if (info_ptr_ptr != NULL)
1113       info_ptr = *info_ptr_ptr;
1114 
1115    if (end_info_ptr_ptr != NULL)
1116       end_info_ptr = *end_info_ptr_ptr;
1117 
1118    png_read_destroy(png_ptr, info_ptr, end_info_ptr);
1119 
1120    if (info_ptr != NULL)
1121    {
1122 #if defined(PNG_TEXT_SUPPORTED)
1123       png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1124 #endif
1125 
1126 #ifdef PNG_USER_MEM_SUPPORTED
1127       png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1128           (png_voidp)mem_ptr);
1129 #else
1130       png_destroy_struct((png_voidp)info_ptr);
1131 #endif
1132       *info_ptr_ptr = NULL;
1133    }
1134 
1135    if (end_info_ptr != NULL)
1136    {
1137 #if defined(PNG_READ_TEXT_SUPPORTED)
1138       png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1139 #endif
1140 #ifdef PNG_USER_MEM_SUPPORTED
1141       png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1142          (png_voidp)mem_ptr);
1143 #else
1144       png_destroy_struct((png_voidp)end_info_ptr);
1145 #endif
1146       *end_info_ptr_ptr = NULL;
1147    }
1148 
1149    if (png_ptr != NULL)
1150    {
1151 #ifdef PNG_USER_MEM_SUPPORTED
1152       png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1153           (png_voidp)mem_ptr);
1154 #else
1155       png_destroy_struct((png_voidp)png_ptr);
1156 #endif
1157       *png_ptr_ptr = NULL;
1158    }
1159 }
1160 
1161 /* free all memory used by the read (old method) */
1162 void /* PRIVATE */
png_read_destroy(png_structp png_ptr,png_infop info_ptr,png_infop end_info_ptr)1163 png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1164     png_infop end_info_ptr)
1165 {
1166 #ifdef PNG_SETJMP_SUPPORTED
1167    jmp_buf tmp_jmp;
1168 #endif
1169    png_error_ptr error_fn;
1170    png_error_ptr warning_fn;
1171    png_voidp error_ptr;
1172 #ifdef PNG_USER_MEM_SUPPORTED
1173    png_free_ptr free_fn;
1174    png_voidp mem_ptr; /* PDFlib GmbH: was missing */
1175 #endif
1176 
1177    png_debug(1, "in png_read_destroy");
1178    if (info_ptr != NULL)
1179       png_info_destroy(png_ptr, info_ptr);
1180 
1181    if (end_info_ptr != NULL)
1182       png_info_destroy(png_ptr, end_info_ptr);
1183 
1184    png_free(png_ptr, png_ptr->zbuf);
1185    png_free(png_ptr, png_ptr->big_row_buf);
1186    png_free(png_ptr, png_ptr->prev_row);
1187    png_free(png_ptr, png_ptr->chunkdata);
1188 #if defined(PNG_READ_DITHER_SUPPORTED)
1189    png_free(png_ptr, png_ptr->palette_lookup);
1190    png_free(png_ptr, png_ptr->dither_index);
1191 #endif
1192 #if defined(PNG_READ_GAMMA_SUPPORTED)
1193    png_free(png_ptr, png_ptr->gamma_table);
1194 #endif
1195 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1196    png_free(png_ptr, png_ptr->gamma_from_1);
1197    png_free(png_ptr, png_ptr->gamma_to_1);
1198 #endif
1199 #ifdef PNG_FREE_ME_SUPPORTED
1200    if (png_ptr->free_me & PNG_FREE_PLTE)
1201       png_zfree(png_ptr, png_ptr->palette);
1202    png_ptr->free_me &= ~PNG_FREE_PLTE;
1203 #else
1204    if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1205       png_zfree(png_ptr, png_ptr->palette);
1206    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1207 #endif
1208 #if defined(PNG_tRNS_SUPPORTED) || \
1209     defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1210 #ifdef PNG_FREE_ME_SUPPORTED
1211    if (png_ptr->free_me & PNG_FREE_TRNS)
1212       png_free(png_ptr, png_ptr->trans);
1213    png_ptr->free_me &= ~PNG_FREE_TRNS;
1214 #else
1215    if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1216       png_free(png_ptr, png_ptr->trans);
1217    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1218 #endif
1219 #endif
1220 #if defined(PNG_READ_hIST_SUPPORTED)
1221 #ifdef PNG_FREE_ME_SUPPORTED
1222    if (png_ptr->free_me & PNG_FREE_HIST)
1223       png_free(png_ptr, png_ptr->hist);
1224    png_ptr->free_me &= ~PNG_FREE_HIST;
1225 #else
1226    if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1227       png_free(png_ptr, png_ptr->hist);
1228    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1229 #endif
1230 #endif
1231 #if defined(PNG_READ_GAMMA_SUPPORTED)
1232    if (png_ptr->gamma_16_table != NULL)
1233    {
1234       int i;
1235       int istop = (1 << (8 - png_ptr->gamma_shift));
1236       for (i = 0; i < istop; i++)
1237       {
1238          png_free(png_ptr, png_ptr->gamma_16_table[i]);
1239       }
1240    png_free(png_ptr, png_ptr->gamma_16_table);
1241    }
1242 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1243    if (png_ptr->gamma_16_from_1 != NULL)
1244    {
1245       int i;
1246       int istop = (1 << (8 - png_ptr->gamma_shift));
1247       for (i = 0; i < istop; i++)
1248       {
1249          png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
1250       }
1251    png_free(png_ptr, png_ptr->gamma_16_from_1);
1252    }
1253    if (png_ptr->gamma_16_to_1 != NULL)
1254    {
1255       int i;
1256       int istop = (1 << (8 - png_ptr->gamma_shift));
1257       for (i = 0; i < istop; i++)
1258       {
1259          png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
1260       }
1261    png_free(png_ptr, png_ptr->gamma_16_to_1);
1262    }
1263 #endif
1264 #endif
1265 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1266    png_free(png_ptr, png_ptr->time_buffer);
1267 #endif
1268 
1269    inflateEnd(&png_ptr->zstream);
1270 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1271    png_free(png_ptr, png_ptr->save_buffer);
1272 #endif
1273 
1274 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1275 #ifdef PNG_TEXT_SUPPORTED
1276    png_free(png_ptr, png_ptr->current_text);
1277 #endif /* PNG_TEXT_SUPPORTED */
1278 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1279 
1280    /* Save the important info out of the png_struct, in case it is
1281     * being used again.
1282     */
1283 #ifdef PNG_SETJMP_SUPPORTED
1284    png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1285 #endif
1286 
1287    error_fn = png_ptr->error_fn;
1288    warning_fn = png_ptr->warning_fn;
1289    error_ptr = png_ptr->error_ptr;
1290 #ifdef PNG_USER_MEM_SUPPORTED
1291    free_fn = png_ptr->free_fn;
1292    mem_ptr = png_ptr->mem_ptr; /* PDFlib GmbH: was missing */
1293 #endif
1294 
1295    png_memset(png_ptr, 0, png_sizeof(png_struct));
1296 
1297    png_ptr->error_fn = error_fn;
1298    png_ptr->warning_fn = warning_fn;
1299    png_ptr->error_ptr = error_ptr;
1300 #ifdef PNG_USER_MEM_SUPPORTED
1301    png_ptr->free_fn = free_fn;
1302    png_ptr->mem_ptr = mem_ptr; /* PDFlib GmbH: was missing */
1303 #endif
1304 
1305 #ifdef PNG_SETJMP_SUPPORTED
1306    png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1307 #endif
1308 
1309 }
1310 
1311 void PNGAPI
png_set_read_status_fn(png_structp png_ptr,png_read_status_ptr read_row_fn)1312 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1313 {
1314    if (png_ptr == NULL) return;
1315    png_ptr->read_row_fn = read_row_fn;
1316 }
1317 
1318 
1319 #ifndef PNG_NO_SEQUENTIAL_READ_SUPPORTED
1320 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1321 void PNGAPI
png_read_png(png_structp png_ptr,png_infop info_ptr,int transforms,voidp params)1322 png_read_png(png_structp png_ptr, png_infop info_ptr,
1323                            int transforms,
1324                            voidp params)
1325 {
1326    int row;
1327 
1328    if (png_ptr == NULL) return;
1329 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1330    /* invert the alpha channel from opacity to transparency
1331     */
1332    if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1333        png_set_invert_alpha(png_ptr);
1334 #endif
1335 
1336    /* png_read_info() gives us all of the information from the
1337     * PNG file before the first IDAT (image data chunk).
1338     */
1339    png_read_info(png_ptr, info_ptr);
1340    if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1341       png_error(png_ptr, "Image is too high to process with png_read_png()");
1342 
1343    /* -------------- image transformations start here ------------------- */
1344 
1345 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1346    /* tell libpng to strip 16 bit/color files down to 8 bits per color
1347     */
1348    if (transforms & PNG_TRANSFORM_STRIP_16)
1349        png_set_strip_16(png_ptr);
1350 #endif
1351 
1352 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1353    /* Strip alpha bytes from the input data without combining with
1354     * the background (not recommended).
1355     */
1356    if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1357        png_set_strip_alpha(png_ptr);
1358 #endif
1359 
1360 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1361    /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1362     * byte into separate bytes (useful for paletted and grayscale images).
1363     */
1364    if (transforms & PNG_TRANSFORM_PACKING)
1365        png_set_packing(png_ptr);
1366 #endif
1367 
1368 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1369    /* Change the order of packed pixels to least significant bit first
1370     * (not useful if you are using png_set_packing).
1371     */
1372    if (transforms & PNG_TRANSFORM_PACKSWAP)
1373        png_set_packswap(png_ptr);
1374 #endif
1375 
1376 #if defined(PNG_READ_EXPAND_SUPPORTED)
1377    /* Expand paletted colors into true RGB triplets
1378     * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1379     * Expand paletted or RGB images with transparency to full alpha
1380     * channels so the data will be available as RGBA quartets.
1381     */
1382    if (transforms & PNG_TRANSFORM_EXPAND)
1383        if ((png_ptr->bit_depth < 8) ||
1384            (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1385            (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1386          png_set_expand(png_ptr);
1387 #endif
1388 
1389    /* We don't handle background color or gamma transformation or dithering.
1390     */
1391 
1392 #if defined(PNG_READ_INVERT_SUPPORTED)
1393    /* invert monochrome files to have 0 as white and 1 as black
1394     */
1395    if (transforms & PNG_TRANSFORM_INVERT_MONO)
1396        png_set_invert_mono(png_ptr);
1397 #endif
1398 
1399 #if defined(PNG_READ_SHIFT_SUPPORTED)
1400    /* If you want to shift the pixel values from the range [0,255] or
1401     * [0,65535] to the original [0,7] or [0,31], or whatever range the
1402     * colors were originally in:
1403     */
1404    if ((transforms & PNG_TRANSFORM_SHIFT)
1405        && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1406    {
1407       png_color_8p sig_bit;
1408 
1409       png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1410       png_set_shift(png_ptr, sig_bit);
1411    }
1412 #endif
1413 
1414 #if defined(PNG_READ_BGR_SUPPORTED)
1415    /* flip the RGB pixels to BGR (or RGBA to BGRA)
1416     */
1417    if (transforms & PNG_TRANSFORM_BGR)
1418        png_set_bgr(png_ptr);
1419 #endif
1420 
1421 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1422    /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1423     */
1424    if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1425        png_set_swap_alpha(png_ptr);
1426 #endif
1427 
1428 #if defined(PNG_READ_SWAP_SUPPORTED)
1429    /* swap bytes of 16 bit files to least significant byte first
1430     */
1431    if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1432        png_set_swap(png_ptr);
1433 #endif
1434 
1435    /* We don't handle adding filler bytes */
1436 
1437    /* Optional call to gamma correct and add the background to the palette
1438     * and update info structure.  REQUIRED if you are expecting libpng to
1439     * update the palette for you (i.e., you selected such a transform above).
1440     */
1441    png_read_update_info(png_ptr, info_ptr);
1442 
1443    /* -------------- image transformations end here ------------------- */
1444 
1445 #ifdef PNG_FREE_ME_SUPPORTED
1446    png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1447 #endif
1448    if (info_ptr->row_pointers == NULL)
1449    {
1450       info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1451          info_ptr->height * png_sizeof(png_bytep));
1452       png_memset(info_ptr->row_pointers, 0, info_ptr->height
1453          * png_sizeof(png_bytep));
1454 #ifdef PNG_FREE_ME_SUPPORTED
1455       info_ptr->free_me |= PNG_FREE_ROWS;
1456 #endif
1457       for (row = 0; row < (int)info_ptr->height; row++)
1458          info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1459             png_get_rowbytes(png_ptr, info_ptr));
1460    }
1461 
1462    png_read_image(png_ptr, info_ptr->row_pointers);
1463    info_ptr->valid |= PNG_INFO_IDAT;
1464 
1465    /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1466    png_read_end(png_ptr, info_ptr);
1467 
1468    transforms = transforms; /* quiet compiler warnings */
1469    params = params;
1470 
1471 }
1472 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1473 #endif /* PNG_NO_SEQUENTIAL_READ_SUPPORTED */
1474 #endif /* PNG_READ_SUPPORTED */
1475