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