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