]> git.jsancho.org Git - lugaru.git/blob - Dependencies/libpng/png.c
CMake: Purge all the bundled dependencies
[lugaru.git] / Dependencies / libpng / png.c
1
2 /* png.c - location for general purpose libpng functions
3  *
4  * Last changed in libpng 1.4.1 [February 25, 2010]
5  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8  *
9  * This code is released under the libpng license.
10  * For conditions of distribution and use, see the disclaimer
11  * and license in png.h
12  */
13
14 #define PNG_NO_EXTERN
15 #define PNG_NO_PEDANTIC_WARNINGS
16 #include "png.h"
17 #include "pngpriv.h"
18
19 /* Generate a compiler error if there is an old png.h in the search path. */
20 typedef version_1_4_1 Your_png_h_is_not_version_1_4_1;
21
22 /* Version information for C files.  This had better match the version
23  * string defined in png.h.
24  */
25
26 /* Tells libpng that we have already handled the first "num_bytes" bytes
27  * of the PNG file signature.  If the PNG data is embedded into another
28  * stream we can set num_bytes = 8 so that libpng will not attempt to read
29  * or write any of the magic bytes before it starts on the IHDR.
30  */
31
32 #ifdef PNG_READ_SUPPORTED
33 void PNGAPI
34 png_set_sig_bytes(png_structp png_ptr, int num_bytes)
35 {
36    png_debug(1, "in png_set_sig_bytes");
37
38    if (png_ptr == NULL)
39       return;
40
41    if (num_bytes > 8)
42       png_error(png_ptr, "Too many bytes for PNG signature");
43
44    png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
45 }
46
47 /* Checks whether the supplied bytes match the PNG signature.  We allow
48  * checking less than the full 8-byte signature so that those apps that
49  * already read the first few bytes of a file to determine the file type
50  * can simply check the remaining bytes for extra assurance.  Returns
51  * an integer less than, equal to, or greater than zero if sig is found,
52  * respectively, to be less than, to match, or be greater than the correct
53  * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
54  */
55 int PNGAPI
56 png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
57 {
58    png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
59    if (num_to_check > 8)
60       num_to_check = 8;
61    else if (num_to_check < 1)
62       return (-1);
63
64    if (start > 7)
65       return (-1);
66
67    if (start + num_to_check > 8)
68       num_to_check = 8 - start;
69
70    return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
71 }
72
73 #endif /* PNG_READ_SUPPORTED */
74
75 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
76 /* Function to allocate memory for zlib and clear it to 0. */
77 voidpf /* PRIVATE */
78 png_zalloc(voidpf png_ptr, uInt items, uInt size)
79 {
80    png_voidp ptr;
81    png_structp p=(png_structp)png_ptr;
82    png_uint_32 save_flags=p->flags;
83    png_alloc_size_t num_bytes;
84
85    if (png_ptr == NULL)
86       return (NULL);
87    if (items > PNG_UINT_32_MAX/size)
88    {
89      png_warning (p, "Potential overflow in png_zalloc()");
90      return (NULL);
91    }
92    num_bytes = (png_alloc_size_t)items * size;
93
94    p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
95    ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
96    p->flags=save_flags;
97
98    return ((voidpf)ptr);
99 }
100
101 /* Function to free memory for zlib */
102 void /* PRIVATE */
103 png_zfree(voidpf png_ptr, voidpf ptr)
104 {
105    png_free((png_structp)png_ptr, (png_voidp)ptr);
106 }
107
108 /* Reset the CRC variable to 32 bits of 1's.  Care must be taken
109  * in case CRC is > 32 bits to leave the top bits 0.
110  */
111 void /* PRIVATE */
112 png_reset_crc(png_structp png_ptr)
113 {
114    png_ptr->crc = crc32(0, Z_NULL, 0);
115 }
116
117 /* Calculate the CRC over a section of data.  We can only pass as
118  * much data to this routine as the largest single buffer size.  We
119  * also check that this data will actually be used before going to the
120  * trouble of calculating it.
121  */
122 void /* PRIVATE */
123 png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
124 {
125    int need_crc = 1;
126
127    if (png_ptr->chunk_name[0] & 0x20)                     /* ancillary */
128    {
129       if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
130           (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
131          need_crc = 0;
132    }
133    else                                                    /* critical */
134    {
135       if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
136          need_crc = 0;
137    }
138
139    if (need_crc)
140       png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
141 }
142
143 /* Allocate the memory for an info_struct for the application.  We don't
144  * really need the png_ptr, but it could potentially be useful in the
145  * future.  This should be used in favour of malloc(png_sizeof(png_info))
146  * and png_info_init() so that applications that want to use a shared
147  * libpng don't have to be recompiled if png_info changes size.
148  */
149 png_infop PNGAPI
150 png_create_info_struct(png_structp png_ptr)
151 {
152    png_infop info_ptr;
153
154    png_debug(1, "in png_create_info_struct");
155
156    if (png_ptr == NULL)
157       return (NULL);
158
159 #ifdef PNG_USER_MEM_SUPPORTED
160    info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
161       png_ptr->malloc_fn, png_ptr->mem_ptr);
162 #else
163    info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
164 #endif
165    if (info_ptr != NULL)
166       png_info_init_3(&info_ptr, png_sizeof(png_info));
167
168    return (info_ptr);
169 }
170
171 /* This function frees the memory associated with a single info struct.
172  * Normally, one would use either png_destroy_read_struct() or
173  * png_destroy_write_struct() to free an info struct, but this may be
174  * useful for some applications.
175  */
176 void PNGAPI
177 png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
178 {
179    png_infop info_ptr = NULL;
180
181    png_debug(1, "in png_destroy_info_struct");
182
183    if (png_ptr == NULL)
184       return;
185
186    if (info_ptr_ptr != NULL)
187       info_ptr = *info_ptr_ptr;
188
189    if (info_ptr != NULL)
190    {
191       png_info_destroy(png_ptr, info_ptr);
192
193 #ifdef PNG_USER_MEM_SUPPORTED
194       png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
195           png_ptr->mem_ptr);
196 #else
197       png_destroy_struct((png_voidp)info_ptr);
198 #endif
199       *info_ptr_ptr = NULL;
200    }
201 }
202
203 /* Initialize the info structure.  This is now an internal function (0.89)
204  * and applications using it are urged to use png_create_info_struct()
205  * instead.
206  */
207
208 void PNGAPI
209 png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
210 {
211    png_infop info_ptr = *ptr_ptr;
212
213    png_debug(1, "in png_info_init_3");
214
215    if (info_ptr == NULL)
216       return;
217
218    if (png_sizeof(png_info) > png_info_struct_size)
219    {
220       png_destroy_struct(info_ptr);
221       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
222       *ptr_ptr = info_ptr;
223    }
224
225    /* Set everything to 0 */
226    png_memset(info_ptr, 0, png_sizeof(png_info));
227 }
228
229 void PNGAPI
230 png_data_freer(png_structp png_ptr, png_infop info_ptr,
231    int freer, png_uint_32 mask)
232 {
233    png_debug(1, "in png_data_freer");
234
235    if (png_ptr == NULL || info_ptr == NULL)
236       return;
237
238    if (freer == PNG_DESTROY_WILL_FREE_DATA)
239       info_ptr->free_me |= mask;
240    else if (freer == PNG_USER_WILL_FREE_DATA)
241       info_ptr->free_me &= ~mask;
242    else
243       png_warning(png_ptr,
244          "Unknown freer parameter in png_data_freer");
245 }
246
247 void PNGAPI
248 png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
249    int num)
250 {
251    png_debug(1, "in png_free_data");
252
253    if (png_ptr == NULL || info_ptr == NULL)
254       return;
255
256 #ifdef PNG_TEXT_SUPPORTED
257    /* Free text item num or (if num == -1) all text items */
258    if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
259    {
260       if (num != -1)
261       {
262          if (info_ptr->text && info_ptr->text[num].key)
263          {
264             png_free(png_ptr, info_ptr->text[num].key);
265             info_ptr->text[num].key = NULL;
266          }
267       }
268       else
269       {
270          int i;
271          for (i = 0; i < info_ptr->num_text; i++)
272              png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
273          png_free(png_ptr, info_ptr->text);
274          info_ptr->text = NULL;
275          info_ptr->num_text=0;
276       }
277    }
278 #endif
279
280 #ifdef PNG_tRNS_SUPPORTED
281    /* Free any tRNS entry */
282    if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
283    {
284       png_free(png_ptr, info_ptr->trans_alpha);
285       info_ptr->trans_alpha = NULL;
286       info_ptr->valid &= ~PNG_INFO_tRNS;
287    }
288 #endif
289
290 #ifdef PNG_sCAL_SUPPORTED
291    /* Free any sCAL entry */
292    if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
293    {
294 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
295       png_free(png_ptr, info_ptr->scal_s_width);
296       png_free(png_ptr, info_ptr->scal_s_height);
297       info_ptr->scal_s_width = NULL;
298       info_ptr->scal_s_height = NULL;
299 #endif
300       info_ptr->valid &= ~PNG_INFO_sCAL;
301    }
302 #endif
303
304 #ifdef PNG_pCAL_SUPPORTED
305    /* Free any pCAL entry */
306    if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
307    {
308       png_free(png_ptr, info_ptr->pcal_purpose);
309       png_free(png_ptr, info_ptr->pcal_units);
310       info_ptr->pcal_purpose = NULL;
311       info_ptr->pcal_units = NULL;
312       if (info_ptr->pcal_params != NULL)
313          {
314             int i;
315             for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
316             {
317                png_free(png_ptr, info_ptr->pcal_params[i]);
318                info_ptr->pcal_params[i] = NULL;
319             }
320             png_free(png_ptr, info_ptr->pcal_params);
321             info_ptr->pcal_params = NULL;
322          }
323       info_ptr->valid &= ~PNG_INFO_pCAL;
324    }
325 #endif
326
327 #ifdef PNG_iCCP_SUPPORTED
328    /* Free any iCCP entry */
329    if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
330    {
331       png_free(png_ptr, info_ptr->iccp_name);
332       png_free(png_ptr, info_ptr->iccp_profile);
333       info_ptr->iccp_name = NULL;
334       info_ptr->iccp_profile = NULL;
335       info_ptr->valid &= ~PNG_INFO_iCCP;
336    }
337 #endif
338
339 #ifdef PNG_sPLT_SUPPORTED
340    /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
341    if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
342    {
343       if (num != -1)
344       {
345          if (info_ptr->splt_palettes)
346          {
347             png_free(png_ptr, info_ptr->splt_palettes[num].name);
348             png_free(png_ptr, info_ptr->splt_palettes[num].entries);
349             info_ptr->splt_palettes[num].name = NULL;
350             info_ptr->splt_palettes[num].entries = NULL;
351          }
352       }
353       else
354       {
355          if (info_ptr->splt_palettes_num)
356          {
357             int i;
358             for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
359                png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
360
361             png_free(png_ptr, info_ptr->splt_palettes);
362             info_ptr->splt_palettes = NULL;
363             info_ptr->splt_palettes_num = 0;
364          }
365          info_ptr->valid &= ~PNG_INFO_sPLT;
366       }
367    }
368 #endif
369
370 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
371    if (png_ptr->unknown_chunk.data)
372    {
373       png_free(png_ptr, png_ptr->unknown_chunk.data);
374       png_ptr->unknown_chunk.data = NULL;
375    }
376
377    if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
378    {
379       if (num != -1)
380       {
381           if (info_ptr->unknown_chunks)
382           {
383              png_free(png_ptr, info_ptr->unknown_chunks[num].data);
384              info_ptr->unknown_chunks[num].data = NULL;
385           }
386       }
387       else
388       {
389          int i;
390
391          if (info_ptr->unknown_chunks_num)
392          {
393             for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
394                png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
395
396             png_free(png_ptr, info_ptr->unknown_chunks);
397             info_ptr->unknown_chunks = NULL;
398             info_ptr->unknown_chunks_num = 0;
399          }
400       }
401    }
402 #endif
403
404 #ifdef PNG_hIST_SUPPORTED
405    /* Free any hIST entry */
406    if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
407    {
408       png_free(png_ptr, info_ptr->hist);
409       info_ptr->hist = NULL;
410       info_ptr->valid &= ~PNG_INFO_hIST;
411    }
412 #endif
413
414    /* Free any PLTE entry that was internally allocated */
415    if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
416    {
417       png_zfree(png_ptr, info_ptr->palette);
418       info_ptr->palette = NULL;
419       info_ptr->valid &= ~PNG_INFO_PLTE;
420       info_ptr->num_palette = 0;
421    }
422
423 #ifdef PNG_INFO_IMAGE_SUPPORTED
424    /* Free any image bits attached to the info structure */
425    if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
426    {
427       if (info_ptr->row_pointers)
428       {
429          int row;
430          for (row = 0; row < (int)info_ptr->height; row++)
431          {
432             png_free(png_ptr, info_ptr->row_pointers[row]);
433             info_ptr->row_pointers[row] = NULL;
434          }
435          png_free(png_ptr, info_ptr->row_pointers);
436          info_ptr->row_pointers = NULL;
437       }
438       info_ptr->valid &= ~PNG_INFO_IDAT;
439    }
440 #endif
441
442    if (num == -1)
443       info_ptr->free_me &= ~mask;
444    else
445       info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
446 }
447
448 /* This is an internal routine to free any memory that the info struct is
449  * pointing to before re-using it or freeing the struct itself.  Recall
450  * that png_free() checks for NULL pointers for us.
451  */
452 void /* PRIVATE */
453 png_info_destroy(png_structp png_ptr, png_infop info_ptr)
454 {
455    png_debug(1, "in png_info_destroy");
456
457    png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
458
459 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
460    if (png_ptr->num_chunk_list)
461    {
462       png_free(png_ptr, png_ptr->chunk_list);
463       png_ptr->chunk_list = NULL;
464       png_ptr->num_chunk_list = 0;
465    }
466 #endif
467
468    png_info_init_3(&info_ptr, png_sizeof(png_info));
469 }
470 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
471
472 /* This function returns a pointer to the io_ptr associated with the user
473  * functions.  The application should free any memory associated with this
474  * pointer before png_write_destroy() or png_read_destroy() are called.
475  */
476 png_voidp PNGAPI
477 png_get_io_ptr(png_structp png_ptr)
478 {
479    if (png_ptr == NULL)
480       return (NULL);
481    return (png_ptr->io_ptr);
482 }
483
484 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
485 #ifdef PNG_STDIO_SUPPORTED
486 /* Initialize the default input/output functions for the PNG file.  If you
487  * use your own read or write routines, you can call either png_set_read_fn()
488  * or png_set_write_fn() instead of png_init_io().  If you have defined
489  * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
490  * necessarily available.
491  */
492 void PNGAPI
493 png_init_io(png_structp png_ptr, png_FILE_p fp)
494 {
495    png_debug(1, "in png_init_io");
496
497    if (png_ptr == NULL)
498       return;
499
500    png_ptr->io_ptr = (png_voidp)fp;
501 }
502 #endif
503
504 #ifdef PNG_TIME_RFC1123_SUPPORTED
505 /* Convert the supplied time into an RFC 1123 string suitable for use in
506  * a "Creation Time" or other text-based time string.
507  */
508 png_charp PNGAPI
509 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
510 {
511    static PNG_CONST char short_months[12][4] =
512         {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
513          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
514
515    if (png_ptr == NULL)
516       return (NULL);
517    if (png_ptr->time_buffer == NULL)
518    {
519       png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
520          png_sizeof(char)));
521    }
522
523 #ifdef USE_FAR_KEYWORD
524    {
525       char near_time_buf[29];
526       png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
527           ptime->day % 32, short_months[(ptime->month - 1) % 12],
528           ptime->year, ptime->hour % 24, ptime->minute % 60,
529           ptime->second % 61);
530       png_memcpy(png_ptr->time_buffer, near_time_buf,
531           29*png_sizeof(char));
532    }
533 #else
534    png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
535        ptime->day % 32, short_months[(ptime->month - 1) % 12],
536        ptime->year, ptime->hour % 24, ptime->minute % 60,
537        ptime->second % 61);
538 #endif
539    return ((png_charp)png_ptr->time_buffer);
540 }
541 #endif /* PNG_TIME_RFC1123_SUPPORTED */
542
543 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
544
545 png_charp PNGAPI
546 png_get_copyright(png_structp png_ptr)
547 {
548    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
549 #ifdef PNG_STRING_COPYRIGHT
550       return PNG_STRING_COPYRIGHT
551 #else
552 #ifdef __STDC__
553    return ((png_charp) PNG_STRING_NEWLINE \
554      "libpng version 1.4.1 - February 25, 2010" PNG_STRING_NEWLINE \
555      "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
556      "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
557      "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
558      PNG_STRING_NEWLINE);
559 #else
560       return ((png_charp) "libpng version 1.4.1 - February 25, 2010\
561       Copyright (c) 1998-2010 Glenn Randers-Pehrson\
562       Copyright (c) 1996-1997 Andreas Dilger\
563       Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
564 #endif
565 #endif
566 }
567
568 /* The following return the library version as a short string in the
569  * format 1.0.0 through 99.99.99zz.  To get the version of *.h files
570  * used with your application, print out PNG_LIBPNG_VER_STRING, which
571  * is defined in png.h.
572  * Note: now there is no difference between png_get_libpng_ver() and
573  * png_get_header_ver().  Due to the version_nn_nn_nn typedef guard,
574  * it is guaranteed that png.c uses the correct version of png.h.
575  */
576 png_charp PNGAPI
577 png_get_libpng_ver(png_structp png_ptr)
578 {
579    /* Version of *.c files used when building libpng */
580    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
581    return ((png_charp) PNG_LIBPNG_VER_STRING);
582 }
583
584 png_charp PNGAPI
585 png_get_header_ver(png_structp png_ptr)
586 {
587    /* Version of *.h files used when building libpng */
588    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
589    return ((png_charp) PNG_LIBPNG_VER_STRING);
590 }
591
592 png_charp PNGAPI
593 png_get_header_version(png_structp png_ptr)
594 {
595    /* Returns longer string containing both version and date */
596    png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
597 #ifdef __STDC__
598    return ((png_charp) PNG_HEADER_VERSION_STRING
599 #ifndef PNG_READ_SUPPORTED
600    "     (NO READ SUPPORT)"
601 #endif
602    PNG_STRING_NEWLINE);
603 #else
604    return ((png_charp) PNG_HEADER_VERSION_STRING);
605 #endif
606 }
607
608 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
609 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
610 int PNGAPI
611 png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
612 {
613    /* Check chunk_name and return "keep" value if it's on the list, else 0 */
614    int i;
615    png_bytep p;
616    if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
617       return 0;
618    p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
619    for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
620       if (!png_memcmp(chunk_name, p, 4))
621         return ((int)*(p + 4));
622    return 0;
623 }
624 #endif
625
626 /* This function, added to libpng-1.0.6g, is untested. */
627 int PNGAPI
628 png_reset_zstream(png_structp png_ptr)
629 {
630    if (png_ptr == NULL)
631       return Z_STREAM_ERROR;
632    return (inflateReset(&png_ptr->zstream));
633 }
634 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
635
636 /* This function was added to libpng-1.0.7 */
637 png_uint_32 PNGAPI
638 png_access_version_number(void)
639 {
640    /* Version of *.c files used when building libpng */
641    return((png_uint_32) PNG_LIBPNG_VER);
642 }
643
644
645
646 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
647 #ifdef PNG_SIZE_T
648 /* Added at libpng version 1.2.6 */
649    PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
650 png_size_t PNGAPI
651 png_convert_size(size_t size)
652 {
653    if (size > (png_size_t)-1)
654       PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
655    return ((png_size_t)size);
656 }
657 #endif /* PNG_SIZE_T */
658
659 /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
660 #ifdef PNG_cHRM_SUPPORTED
661 #ifdef PNG_CHECK_cHRM_SUPPORTED
662
663 /*
664  *    Multiply two 32-bit numbers, V1 and V2, using 32-bit
665  *    arithmetic, to produce a 64 bit result in the HI/LO words.
666  *
667  *                  A B
668  *                x C D
669  *               ------
670  *              AD || BD
671  *        AC || CB || 0
672  *
673  *    where A and B are the high and low 16-bit words of V1,
674  *    C and D are the 16-bit words of V2, AD is the product of
675  *    A and D, and X || Y is (X << 16) + Y.
676 */
677
678 void /* PRIVATE */
679 png_64bit_product (long v1, long v2, unsigned long *hi_product,
680    unsigned long *lo_product)
681 {
682    int a, b, c, d;
683    long lo, hi, x, y;
684
685    a = (v1 >> 16) & 0xffff;
686    b = v1 & 0xffff;
687    c = (v2 >> 16) & 0xffff;
688    d = v2 & 0xffff;
689
690    lo = b * d;                   /* BD */
691    x = a * d + c * b;            /* AD + CB */
692    y = ((lo >> 16) & 0xffff) + x;
693
694    lo = (lo & 0xffff) | ((y & 0xffff) << 16);
695    hi = (y >> 16) & 0xffff;
696
697    hi += a * c;                  /* AC */
698
699    *hi_product = (unsigned long)hi;
700    *lo_product = (unsigned long)lo;
701 }
702
703 int /* PRIVATE */
704 png_check_cHRM_fixed(png_structp png_ptr,
705    png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
706    png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
707    png_fixed_point blue_x, png_fixed_point blue_y)
708 {
709    int ret = 1;
710    unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
711
712    png_debug(1, "in function png_check_cHRM_fixed");
713
714    if (png_ptr == NULL)
715       return 0;
716
717    if (white_x < 0 || white_y <= 0 ||
718          red_x < 0 ||   red_y <  0 ||
719        green_x < 0 || green_y <  0 ||
720         blue_x < 0 ||  blue_y <  0)
721    {
722       png_warning(png_ptr,
723         "Ignoring attempt to set negative chromaticity value");
724       ret = 0;
725    }
726    if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
727        white_y > (png_fixed_point) PNG_UINT_31_MAX ||
728          red_x > (png_fixed_point) PNG_UINT_31_MAX ||
729          red_y > (png_fixed_point) PNG_UINT_31_MAX ||
730        green_x > (png_fixed_point) PNG_UINT_31_MAX ||
731        green_y > (png_fixed_point) PNG_UINT_31_MAX ||
732         blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
733         blue_y > (png_fixed_point) PNG_UINT_31_MAX )
734    {
735       png_warning(png_ptr,
736         "Ignoring attempt to set chromaticity value exceeding 21474.83");
737       ret = 0;
738    }
739    if (white_x > 100000L - white_y)
740    {
741       png_warning(png_ptr, "Invalid cHRM white point");
742       ret = 0;
743    }
744    if (red_x > 100000L - red_y)
745    {
746       png_warning(png_ptr, "Invalid cHRM red point");
747       ret = 0;
748    }
749    if (green_x > 100000L - green_y)
750    {
751       png_warning(png_ptr, "Invalid cHRM green point");
752       ret = 0;
753    }
754    if (blue_x > 100000L - blue_y)
755    {
756       png_warning(png_ptr, "Invalid cHRM blue point");
757       ret = 0;
758    }
759
760    png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
761    png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
762
763    if (xy_hi == yx_hi && xy_lo == yx_lo)
764    {
765       png_warning(png_ptr,
766          "Ignoring attempt to set cHRM RGB triangle with zero area");
767       ret = 0;
768    }
769
770    return ret;
771 }
772 #endif /* PNG_CHECK_cHRM_SUPPORTED */
773 #endif /* PNG_cHRM_SUPPORTED */
774
775 void /* PRIVATE */
776 png_check_IHDR(png_structp png_ptr,
777    png_uint_32 width, png_uint_32 height, int bit_depth,
778    int color_type, int interlace_type, int compression_type,
779    int filter_type)
780 {
781    int error = 0;
782
783    /* Check for width and height valid values */
784    if (width == 0)
785    {
786       png_warning(png_ptr, "Image width is zero in IHDR");
787       error = 1;
788    }
789
790    if (height == 0)
791    {
792       png_warning(png_ptr, "Image height is zero in IHDR");
793       error = 1;
794    }
795
796 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
797    if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
798 #else
799    if (width > PNG_USER_WIDTH_MAX)
800 #endif
801    {
802       png_warning(png_ptr, "Image width exceeds user limit in IHDR");
803       error = 1;
804    }
805
806 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
807    if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
808 #else
809    if (height > PNG_USER_HEIGHT_MAX)
810 #endif
811    {
812       png_warning(png_ptr, "Image height exceeds user limit in IHDR");
813       error = 1;
814    }
815
816    if (width > PNG_UINT_31_MAX)
817    {
818       png_warning(png_ptr, "Invalid image width in IHDR");
819       error = 1;
820    }
821
822    if ( height > PNG_UINT_31_MAX)
823    {
824       png_warning(png_ptr, "Invalid image height in IHDR");
825       error = 1;
826    }
827
828    if ( width > (PNG_UINT_32_MAX
829                  >> 3)      /* 8-byte RGBA pixels */
830                  - 64       /* bigrowbuf hack */
831                  - 1        /* filter byte */
832                  - 7*8      /* rounding of width to multiple of 8 pixels */
833                  - 8)       /* extra max_pixel_depth pad */
834       png_warning(png_ptr, "Width is too large for libpng to process pixels");
835
836    /* Check other values */
837    if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
838        bit_depth != 8 && bit_depth != 16)
839    {
840       png_warning(png_ptr, "Invalid bit depth in IHDR");
841       error = 1;
842    }
843
844    if (color_type < 0 || color_type == 1 ||
845        color_type == 5 || color_type > 6)
846    {
847       png_warning(png_ptr, "Invalid color type in IHDR");
848       error = 1;
849    }
850
851    if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
852        ((color_type == PNG_COLOR_TYPE_RGB ||
853          color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
854          color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
855    {
856       png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
857       error = 1;
858    }
859
860    if (interlace_type >= PNG_INTERLACE_LAST)
861    {
862       png_warning(png_ptr, "Unknown interlace method in IHDR");
863       error = 1;
864    }
865
866    if (compression_type != PNG_COMPRESSION_TYPE_BASE)
867    {
868       png_warning(png_ptr, "Unknown compression method in IHDR");
869       error = 1;
870    }
871
872 #ifdef PNG_MNG_FEATURES_SUPPORTED
873    /* Accept filter_method 64 (intrapixel differencing) only if
874     * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
875     * 2. Libpng did not read a PNG signature (this filter_method is only
876     *    used in PNG datastreams that are embedded in MNG datastreams) and
877     * 3. The application called png_permit_mng_features with a mask that
878     *    included PNG_FLAG_MNG_FILTER_64 and
879     * 4. The filter_method is 64 and
880     * 5. The color_type is RGB or RGBA
881     */
882    if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
883        png_ptr->mng_features_permitted)
884       png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
885
886    if (filter_type != PNG_FILTER_TYPE_BASE)
887    {
888       if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
889          (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
890          ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
891          (color_type == PNG_COLOR_TYPE_RGB ||
892          color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
893       {
894          png_warning(png_ptr, "Unknown filter method in IHDR");
895          error = 1;
896       }
897
898       if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
899       {
900          png_warning(png_ptr, "Invalid filter method in IHDR");
901          error = 1;
902       }
903    }
904
905 #else
906    if (filter_type != PNG_FILTER_TYPE_BASE)
907    {
908       png_warning(png_ptr, "Unknown filter method in IHDR");
909       error = 1;
910    }
911 #endif
912
913    if (error == 1)
914       png_error(png_ptr, "Invalid IHDR data");
915 }
916 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */