VirtualBox

source: vbox/trunk/src/libs/libpng-1.2.53/png.c@ 58414

Last change on this file since 58414 was 58100, checked in by vboxsync, 9 years ago

libpng 1.2.53 export fix

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette