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