VirtualBox

source: vbox/trunk/src/libs/libpng-1.2.54/png.c@ 74912

Last change on this file since 74912 was 58796, checked in by vboxsync, 9 years ago

libpng 1.2.54 unmodified

  • Property svn:eol-style set to native
File size: 30.4 KB
Line 
1
2/* png.c - location for general purpose libpng functions
3 *
4 * Last changed in libpng 1.2.54 [November 12, 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_54 Your_png_h_is_not_version_1_2_54;
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 if (info_ptr == NULL)
336 return;
337 }
338
339 /* Set everything to 0 */
340 png_memset(info_ptr, 0, png_sizeof(png_info));
341}
342
343#ifdef PNG_FREE_ME_SUPPORTED
344void PNGAPI
345png_data_freer(png_structp png_ptr, png_infop info_ptr,
346 int freer, png_uint_32 mask)
347{
348 png_debug(1, "in png_data_freer");
349
350 if (png_ptr == NULL || info_ptr == NULL)
351 return;
352
353 if (freer == PNG_DESTROY_WILL_FREE_DATA)
354 info_ptr->free_me |= mask;
355 else if (freer == PNG_USER_WILL_FREE_DATA)
356 info_ptr->free_me &= ~mask;
357 else
358 png_warning(png_ptr,
359 "Unknown freer parameter in png_data_freer.");
360}
361#endif
362
363void PNGAPI
364png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
365 int num)
366{
367 png_debug(1, "in png_free_data");
368
369 if (png_ptr == NULL || info_ptr == NULL)
370 return;
371
372#ifdef PNG_TEXT_SUPPORTED
373 /* Free text item num or (if num == -1) all text items */
374#ifdef PNG_FREE_ME_SUPPORTED
375 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
376#else
377 if (mask & PNG_FREE_TEXT)
378#endif
379 {
380 if (num != -1)
381 {
382 if (info_ptr->text && info_ptr->text[num].key)
383 {
384 png_free(png_ptr, info_ptr->text[num].key);
385 info_ptr->text[num].key = NULL;
386 }
387 }
388 else
389 {
390 int i;
391 for (i = 0; i < info_ptr->num_text; i++)
392 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
393 png_free(png_ptr, info_ptr->text);
394 info_ptr->text = NULL;
395 info_ptr->num_text=0;
396 }
397 }
398#endif
399
400#ifdef PNG_tRNS_SUPPORTED
401 /* Free any tRNS entry */
402#ifdef PNG_FREE_ME_SUPPORTED
403 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
404#else
405 if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
406#endif
407 {
408 png_free(png_ptr, info_ptr->trans);
409 info_ptr->trans = NULL;
410 info_ptr->valid &= ~PNG_INFO_tRNS;
411#ifndef PNG_FREE_ME_SUPPORTED
412 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
413#endif
414 }
415#endif
416
417#ifdef PNG_sCAL_SUPPORTED
418 /* Free any sCAL entry */
419#ifdef PNG_FREE_ME_SUPPORTED
420 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
421#else
422 if (mask & PNG_FREE_SCAL)
423#endif
424 {
425#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
426 png_free(png_ptr, info_ptr->scal_s_width);
427 png_free(png_ptr, info_ptr->scal_s_height);
428 info_ptr->scal_s_width = NULL;
429 info_ptr->scal_s_height = NULL;
430#endif
431 info_ptr->valid &= ~PNG_INFO_sCAL;
432 }
433#endif
434
435#ifdef PNG_pCAL_SUPPORTED
436 /* Free any pCAL entry */
437#ifdef PNG_FREE_ME_SUPPORTED
438 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
439#else
440 if (mask & PNG_FREE_PCAL)
441#endif
442 {
443 png_free(png_ptr, info_ptr->pcal_purpose);
444 png_free(png_ptr, info_ptr->pcal_units);
445 info_ptr->pcal_purpose = NULL;
446 info_ptr->pcal_units = NULL;
447 if (info_ptr->pcal_params != NULL)
448 {
449 int i;
450 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
451 {
452 png_free(png_ptr, info_ptr->pcal_params[i]);
453 info_ptr->pcal_params[i] = NULL;
454 }
455 png_free(png_ptr, info_ptr->pcal_params);
456 info_ptr->pcal_params = NULL;
457 }
458 info_ptr->valid &= ~PNG_INFO_pCAL;
459 }
460#endif
461
462#ifdef PNG_iCCP_SUPPORTED
463 /* Free any iCCP entry */
464#ifdef PNG_FREE_ME_SUPPORTED
465 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
466#else
467 if (mask & PNG_FREE_ICCP)
468#endif
469 {
470 png_free(png_ptr, info_ptr->iccp_name);
471 png_free(png_ptr, info_ptr->iccp_profile);
472 info_ptr->iccp_name = NULL;
473 info_ptr->iccp_profile = NULL;
474 info_ptr->valid &= ~PNG_INFO_iCCP;
475 }
476#endif
477
478#ifdef PNG_sPLT_SUPPORTED
479 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
480#ifdef PNG_FREE_ME_SUPPORTED
481 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
482#else
483 if (mask & PNG_FREE_SPLT)
484#endif
485 {
486 if (num != -1)
487 {
488 if (info_ptr->splt_palettes)
489 {
490 png_free(png_ptr, info_ptr->splt_palettes[num].name);
491 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
492 info_ptr->splt_palettes[num].name = NULL;
493 info_ptr->splt_palettes[num].entries = NULL;
494 }
495 }
496 else
497 {
498 if (info_ptr->splt_palettes_num)
499 {
500 int i;
501 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
502 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
503
504 png_free(png_ptr, info_ptr->splt_palettes);
505 info_ptr->splt_palettes = NULL;
506 info_ptr->splt_palettes_num = 0;
507 }
508 info_ptr->valid &= ~PNG_INFO_sPLT;
509 }
510 }
511#endif
512
513#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
514 if (png_ptr->unknown_chunk.data)
515 {
516 png_free(png_ptr, png_ptr->unknown_chunk.data);
517 png_ptr->unknown_chunk.data = NULL;
518 }
519
520#ifdef PNG_FREE_ME_SUPPORTED
521 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
522#else
523 if (mask & PNG_FREE_UNKN)
524#endif
525 {
526 if (num != -1)
527 {
528 if (info_ptr->unknown_chunks)
529 {
530 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
531 info_ptr->unknown_chunks[num].data = NULL;
532 }
533 }
534 else
535 {
536 int i;
537
538 if (info_ptr->unknown_chunks_num)
539 {
540 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
541 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
542
543 png_free(png_ptr, info_ptr->unknown_chunks);
544 info_ptr->unknown_chunks = NULL;
545 info_ptr->unknown_chunks_num = 0;
546 }
547 }
548 }
549#endif
550
551#ifdef PNG_hIST_SUPPORTED
552 /* Free any hIST entry */
553#ifdef PNG_FREE_ME_SUPPORTED
554 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
555#else
556 if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
557#endif
558 {
559 png_free(png_ptr, info_ptr->hist);
560 info_ptr->hist = NULL;
561 info_ptr->valid &= ~PNG_INFO_hIST;
562#ifndef PNG_FREE_ME_SUPPORTED
563 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
564#endif
565 }
566#endif
567
568 /* Free any PLTE entry that was internally allocated */
569#ifdef PNG_FREE_ME_SUPPORTED
570 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
571#else
572 if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
573#endif
574 {
575 png_zfree(png_ptr, info_ptr->palette);
576 info_ptr->palette = NULL;
577 info_ptr->valid &= ~PNG_INFO_PLTE;
578#ifndef PNG_FREE_ME_SUPPORTED
579 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
580#endif
581 info_ptr->num_palette = 0;
582 }
583
584#ifdef PNG_INFO_IMAGE_SUPPORTED
585 /* Free any image bits attached to the info structure */
586#ifdef PNG_FREE_ME_SUPPORTED
587 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
588#else
589 if (mask & PNG_FREE_ROWS)
590#endif
591 {
592 if (info_ptr->row_pointers)
593 {
594 int row;
595 for (row = 0; row < (int)info_ptr->height; row++)
596 {
597 png_free(png_ptr, info_ptr->row_pointers[row]);
598 info_ptr->row_pointers[row] = NULL;
599 }
600 png_free(png_ptr, info_ptr->row_pointers);
601 info_ptr->row_pointers = NULL;
602 }
603 info_ptr->valid &= ~PNG_INFO_IDAT;
604 }
605#endif
606
607#ifdef PNG_FREE_ME_SUPPORTED
608 if (num == -1)
609 info_ptr->free_me &= ~mask;
610 else
611 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
612#endif
613}
614
615/* This is an internal routine to free any memory that the info struct is
616 * pointing to before re-using it or freeing the struct itself. Recall
617 * that png_free() checks for NULL pointers for us.
618 */
619void /* PRIVATE */
620png_info_destroy(png_structp png_ptr, png_infop info_ptr)
621{
622 png_debug(1, "in png_info_destroy");
623
624 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
625
626#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
627 if (png_ptr->num_chunk_list)
628 {
629 png_free(png_ptr, png_ptr->chunk_list);
630 png_ptr->chunk_list = NULL;
631 png_ptr->num_chunk_list = 0;
632 }
633#endif
634
635 png_info_init_3(&info_ptr, png_sizeof(png_info));
636}
637#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
638
639/* This function returns a pointer to the io_ptr associated with the user
640 * functions. The application should free any memory associated with this
641 * pointer before png_write_destroy() or png_read_destroy() are called.
642 */
643png_voidp PNGAPI
644png_get_io_ptr(png_structp png_ptr)
645{
646 if (png_ptr == NULL)
647 return (NULL);
648 return (png_ptr->io_ptr);
649}
650
651#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
652#ifdef PNG_STDIO_SUPPORTED
653/* Initialize the default input/output functions for the PNG file. If you
654 * use your own read or write routines, you can call either png_set_read_fn()
655 * or png_set_write_fn() instead of png_init_io(). If you have defined
656 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
657 * necessarily available.
658 */
659void PNGAPI
660png_init_io(png_structp png_ptr, png_FILE_p fp)
661{
662 png_debug(1, "in png_init_io");
663
664 if (png_ptr == NULL)
665 return;
666
667 png_ptr->io_ptr = (png_voidp)fp;
668}
669#endif
670
671#ifdef PNG_TIME_RFC1123_SUPPORTED
672/* Convert the supplied time into an RFC 1123 string suitable for use in
673 * a "Creation Time" or other text-based time string.
674 */
675png_charp PNGAPI
676png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
677{
678 static PNG_CONST char short_months[12][4] =
679 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
680 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
681
682 if (png_ptr == NULL)
683 return (NULL);
684
685 if (png_ptr->time_buffer == NULL)
686 {
687 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
688 png_sizeof(char)));
689 }
690
691#ifdef _WIN32_WCE
692 {
693 wchar_t time_buf[29];
694 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
695 ptime->day % 32, short_months[(ptime->month - 1U) % 12],
696 ptime->year, ptime->hour % 24, ptime->minute % 60,
697 ptime->second % 61);
698 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer,
699 29, NULL, NULL);
700 }
701#else
702#ifdef USE_FAR_KEYWORD
703 {
704 char near_time_buf[29];
705 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
706 ptime->day % 32, short_months[(ptime->month - 1U) % 12],
707 ptime->year, ptime->hour % 24, ptime->minute % 60,
708 ptime->second % 61);
709 png_memcpy(png_ptr->time_buffer, near_time_buf,
710 29*png_sizeof(char));
711 }
712#else
713 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
714 ptime->day % 32, short_months[(ptime->month - 1U) % 12],
715 ptime->year, ptime->hour % 24, ptime->minute % 60,
716 ptime->second % 61);
717#endif
718#endif /* _WIN32_WCE */
719 return ((png_charp)png_ptr->time_buffer);
720}
721#endif /* PNG_TIME_RFC1123_SUPPORTED */
722
723#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
724
725png_charp PNGAPI
726png_get_copyright(png_structp png_ptr)
727{
728 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
729#ifdef PNG_STRING_COPYRIGHT
730 return PNG_STRING_COPYRIGHT
731#else
732#ifdef __STDC__
733 return ((png_charp) PNG_STRING_NEWLINE \
734 "libpng version 1.2.54 - November 12, 2015" PNG_STRING_NEWLINE \
735 "Copyright (c) 1998-2015 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
736 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
737 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
738 PNG_STRING_NEWLINE);
739#else
740 return ((png_charp) "libpng version 1.2.54 - November 12, 2015\
741 Copyright (c) 1998-2015 Glenn Randers-Pehrson\
742 Copyright (c) 1996-1997 Andreas Dilger\
743 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
744#endif
745#endif
746}
747
748/* The following return the library version as a short string in the
749 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
750 * used with your application, print out PNG_LIBPNG_VER_STRING, which
751 * is defined in png.h.
752 * Note: now there is no difference between png_get_libpng_ver() and
753 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
754 * it is guaranteed that png.c uses the correct version of png.h.
755 */
756png_charp PNGAPI
757png_get_libpng_ver(png_structp png_ptr)
758{
759 /* Version of *.c files used when building libpng */
760 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
761 return ((png_charp) PNG_LIBPNG_VER_STRING);
762}
763
764png_charp PNGAPI
765png_get_header_ver(png_structp png_ptr)
766{
767 /* Version of *.h files used when building libpng */
768 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
769 return ((png_charp) PNG_LIBPNG_VER_STRING);
770}
771
772png_charp PNGAPI
773png_get_header_version(png_structp png_ptr)
774{
775 /* Returns longer string containing both version and date */
776 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
777#ifdef __STDC__
778 return ((png_charp) PNG_HEADER_VERSION_STRING
779#ifndef PNG_READ_SUPPORTED
780 " (NO READ SUPPORT)"
781#endif
782 PNG_STRING_NEWLINE);
783#else
784 return ((png_charp) PNG_HEADER_VERSION_STRING);
785#endif
786}
787
788#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
789#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
790int PNGAPI
791png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
792{
793 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
794 int i;
795 png_bytep p;
796 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
797 return 0;
798 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
799 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
800 if (!png_memcmp(chunk_name, p, 4))
801 return ((int)*(p + 4));
802 return 0;
803}
804#endif
805
806/* This function, added to libpng-1.0.6g, is untested. */
807int PNGAPI
808png_reset_zstream(png_structp png_ptr)
809{
810 if (png_ptr == NULL)
811 return Z_STREAM_ERROR;
812 return (inflateReset(&png_ptr->zstream));
813}
814#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
815
816/* This function was added to libpng-1.0.7 */
817png_uint_32 PNGAPI
818png_access_version_number(void)
819{
820 /* Version of *.c files used when building libpng */
821 return((png_uint_32) PNG_LIBPNG_VER);
822}
823
824
825#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
826#ifndef PNG_1_0_X
827/* This function was added to libpng 1.2.0 */
828int PNGAPI
829png_mmx_support(void)
830{
831 /* Obsolete, to be removed from libpng-1.4.0 */
832 return -1;
833}
834#endif /* PNG_1_0_X */
835#endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
836
837#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
838#ifdef PNG_SIZE_T
839/* Added at libpng version 1.2.6 */
840 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
841png_size_t PNGAPI
842png_convert_size(size_t size)
843{
844 if (size > (png_size_t)-1)
845 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
846 return ((png_size_t)size);
847}
848#endif /* PNG_SIZE_T */
849
850/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
851#ifdef PNG_cHRM_SUPPORTED
852#ifdef PNG_CHECK_cHRM_SUPPORTED
853
854/*
855 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
856 * arithmetic, to produce a 64 bit result in the HI/LO words.
857 *
858 * A B
859 * x C D
860 * ------
861 * AD || BD
862 * AC || CB || 0
863 *
864 * where A and B are the high and low 16-bit words of V1,
865 * C and D are the 16-bit words of V2, AD is the product of
866 * A and D, and X || Y is (X << 16) + Y.
867*/
868
869void /* PRIVATE */
870png_64bit_product (long v1, long v2, unsigned long *hi_product,
871 unsigned long *lo_product)
872{
873 int a, b, c, d;
874 long lo, hi, x, y;
875
876 a = (v1 >> 16) & 0xffff;
877 b = v1 & 0xffff;
878 c = (v2 >> 16) & 0xffff;
879 d = v2 & 0xffff;
880
881 lo = b * d; /* BD */
882 x = a * d + c * b; /* AD + CB */
883 y = ((lo >> 16) & 0xffff) + x;
884
885 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
886 hi = (y >> 16) & 0xffff;
887
888 hi += a * c; /* AC */
889
890 *hi_product = (unsigned long)hi;
891 *lo_product = (unsigned long)lo;
892}
893
894int /* PRIVATE */
895png_check_cHRM_fixed(png_structp png_ptr,
896 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
897 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
898 png_fixed_point blue_x, png_fixed_point blue_y)
899{
900 int ret = 1;
901 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
902
903 png_debug(1, "in function png_check_cHRM_fixed");
904
905 if (png_ptr == NULL)
906 return 0;
907
908 if (white_x < 0 || white_y <= 0 ||
909 red_x < 0 || red_y < 0 ||
910 green_x < 0 || green_y < 0 ||
911 blue_x < 0 || blue_y < 0)
912 {
913 png_warning(png_ptr,
914 "Ignoring attempt to set negative chromaticity value");
915 ret = 0;
916 }
917 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
918 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
919 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
920 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
921 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
922 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
923 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
924 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
925 {
926 png_warning(png_ptr,
927 "Ignoring attempt to set chromaticity value exceeding 21474.83");
928 ret = 0;
929 }
930 if (white_x > 100000L - white_y)
931 {
932 png_warning(png_ptr, "Invalid cHRM white point");
933 ret = 0;
934 }
935 if (red_x > 100000L - red_y)
936 {
937 png_warning(png_ptr, "Invalid cHRM red point");
938 ret = 0;
939 }
940 if (green_x > 100000L - green_y)
941 {
942 png_warning(png_ptr, "Invalid cHRM green point");
943 ret = 0;
944 }
945 if (blue_x > 100000L - blue_y)
946 {
947 png_warning(png_ptr, "Invalid cHRM blue point");
948 ret = 0;
949 }
950
951 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
952 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
953
954 if (xy_hi == yx_hi && xy_lo == yx_lo)
955 {
956 png_warning(png_ptr,
957 "Ignoring attempt to set cHRM RGB triangle with zero area");
958 ret = 0;
959 }
960
961 return ret;
962}
963#endif /* PNG_CHECK_cHRM_SUPPORTED */
964#endif /* PNG_cHRM_SUPPORTED */
965
966void /* PRIVATE */
967png_check_IHDR(png_structp png_ptr,
968 png_uint_32 width, png_uint_32 height, int bit_depth,
969 int color_type, int interlace_type, int compression_type,
970 int filter_type)
971{
972 int error = 0;
973
974 /* Check for width and height valid values */
975 if (width == 0)
976 {
977 png_warning(png_ptr, "Image width is zero in IHDR");
978 error = 1;
979 }
980
981 if (height == 0)
982 {
983 png_warning(png_ptr, "Image height is zero in IHDR");
984 error = 1;
985 }
986
987#ifdef PNG_SET_USER_LIMITS_SUPPORTED
988 if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
989#else
990 if (width > PNG_USER_WIDTH_MAX)
991#endif
992 {
993 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
994 error = 1;
995 }
996
997#ifdef PNG_SET_USER_LIMITS_SUPPORTED
998 if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
999#else
1000 if (height > PNG_USER_HEIGHT_MAX)
1001#endif
1002 {
1003 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
1004 error = 1;
1005 }
1006
1007 if (width > PNG_UINT_31_MAX)
1008 {
1009 png_warning(png_ptr, "Invalid image width in IHDR");
1010 error = 1;
1011 }
1012
1013 if ( height > PNG_UINT_31_MAX)
1014 {
1015 png_warning(png_ptr, "Invalid image height in IHDR");
1016 error = 1;
1017 }
1018
1019 /* Check other values */
1020 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
1021 bit_depth != 8 && bit_depth != 16)
1022 {
1023 png_warning(png_ptr, "Invalid bit depth in IHDR");
1024 error = 1;
1025 }
1026
1027 if (color_type < 0 || color_type == 1 ||
1028 color_type == 5 || color_type > 6)
1029 {
1030 png_warning(png_ptr, "Invalid color type in IHDR");
1031 error = 1;
1032 }
1033
1034 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
1035 ((color_type == PNG_COLOR_TYPE_RGB ||
1036 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
1037 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
1038 {
1039 png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
1040 error = 1;
1041 }
1042
1043 if (interlace_type >= PNG_INTERLACE_LAST)
1044 {
1045 png_warning(png_ptr, "Unknown interlace method in IHDR");
1046 error = 1;
1047 }
1048
1049 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
1050 {
1051 png_warning(png_ptr, "Unknown compression method in IHDR");
1052 error = 1;
1053 }
1054
1055#ifdef PNG_MNG_FEATURES_SUPPORTED
1056 /* Accept filter_method 64 (intrapixel differencing) only if
1057 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
1058 * 2. Libpng did not read a PNG signature (this filter_method is only
1059 * used in PNG datastreams that are embedded in MNG datastreams) and
1060 * 3. The application called png_permit_mng_features with a mask that
1061 * included PNG_FLAG_MNG_FILTER_64 and
1062 * 4. The filter_method is 64 and
1063 * 5. The color_type is RGB or RGBA
1064 */
1065 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
1066 png_ptr->mng_features_permitted)
1067 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
1068
1069 if (filter_type != PNG_FILTER_TYPE_BASE)
1070 {
1071 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1072 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
1073 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
1074 (color_type == PNG_COLOR_TYPE_RGB ||
1075 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
1076 {
1077 png_warning(png_ptr, "Unknown filter method in IHDR");
1078 error = 1;
1079 }
1080
1081 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
1082 {
1083 png_warning(png_ptr, "Invalid filter method in IHDR");
1084 error = 1;
1085 }
1086 }
1087
1088#else
1089 if (filter_type != PNG_FILTER_TYPE_BASE)
1090 {
1091 png_warning(png_ptr, "Unknown filter method in IHDR");
1092 error = 1;
1093 }
1094#endif
1095
1096 if (error == 1)
1097 png_error(png_ptr, "Invalid IHDR data");
1098}
1099#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