Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1 | |
| 2 | /* pngerror.c - stub functions for i/o and memory allocation |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 13831bc | 2011-12-21 08:28:28 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.6.0 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2011 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 | b6ce43d | 1998-01-01 07:13:13 -0600 | [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 | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 12 | * |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 13 | * This file provides a location for all error handling. Users who |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 14 | * need special error handling are expected to write replacement functions |
| 15 | * and use png_set_error_fn() to use those functions. See the instructions |
| 16 | * at each function. |
| 17 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 18 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 19 | #include "pngpriv.h" |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 20 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 21 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
| 22 | |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 23 | static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 24 | png_const_charp error_message)),PNG_NORETURN); |
| 25 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 26 | #ifdef PNG_WARNINGS_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 27 | static void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 28 | png_default_warning PNGARG((png_const_structrp png_ptr, |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 29 | png_const_charp warning_message)); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 30 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 31 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 32 | /* This function is called whenever there is a fatal error. This function |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 33 | * should not be changed. If there is a need to handle errors differently, |
| 34 | * you should supply a replacement error function and use png_set_error_fn() |
| 35 | * to replace the error function at run-time. |
| 36 | */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 37 | #ifdef PNG_ERROR_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 38 | PNG_FUNCTION(void,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 39 | png_error,(png_const_structrp png_ptr, png_const_charp error_message), |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 40 | PNG_NORETURN) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 41 | { |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 42 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
| 43 | char msg[16]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 44 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 45 | { |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 46 | if (png_ptr->flags& |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 47 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 48 | { |
| 49 | if (*error_message == PNG_LITERAL_SHARP) |
| 50 | { |
| 51 | /* Strip "#nnnn " from beginning of error message. */ |
| 52 | int offset; |
| 53 | for (offset = 1; offset<15; offset++) |
| 54 | if (error_message[offset] == ' ') |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 55 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 56 | |
| 57 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
| 58 | { |
| 59 | int i; |
| 60 | for (i = 0; i < offset - 1; i++) |
| 61 | msg[i] = error_message[i + 1]; |
| 62 | msg[i - 1] = '\0'; |
| 63 | error_message = msg; |
| 64 | } |
| 65 | |
| 66 | else |
| 67 | error_message += offset; |
| 68 | } |
| 69 | |
| 70 | else |
| 71 | { |
| 72 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
| 73 | { |
| 74 | msg[0] = '0'; |
| 75 | msg[1] = '\0'; |
| 76 | error_message = msg; |
| 77 | } |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 80 | } |
| 81 | #endif |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 82 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 83 | (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), error_message); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 84 | |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 85 | /* If the custom handler doesn't exist, or if it returns, |
| 86 | use the default handler, which will not return. */ |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 87 | png_default_error(png_ptr, error_message); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 88 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 89 | #else |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 90 | PNG_FUNCTION(void,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 91 | png_err,(png_const_structrp png_ptr),PNG_NORETURN) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 92 | { |
Glenn Randers-Pehrson | bb4f77c | 2011-05-16 20:40:59 -0500 | [diff] [blame] | 93 | /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed |
| 94 | * erroneously as '\0', instead of the empty string "". This was |
| 95 | * apparently an error, introduced in libpng-1.2.20, and png_default_error |
| 96 | * will crash in this case. |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 97 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 98 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
Glenn Randers-Pehrson | 0522f26 | 2011-12-29 10:02:24 -0600 | [diff] [blame^] | 99 | (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ""); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 100 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 101 | /* If the custom handler doesn't exist, or if it returns, |
| 102 | use the default handler, which will not return. */ |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 103 | png_default_error(png_ptr, ""); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 104 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 105 | #endif /* PNG_ERROR_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 106 | |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 107 | /* Utility to safely appends strings to a buffer. This never errors out so |
| 108 | * error checking is not required in the caller. |
| 109 | */ |
| 110 | size_t |
| 111 | png_safecat(png_charp buffer, size_t bufsize, size_t pos, |
| 112 | png_const_charp string) |
| 113 | { |
| 114 | if (buffer != NULL && pos < bufsize) |
| 115 | { |
John Bowler | c5bef94 | 2011-05-05 17:35:39 -0500 | [diff] [blame] | 116 | if (string != NULL) |
| 117 | while (*string != '\0' && pos < bufsize-1) |
| 118 | buffer[pos++] = *string++; |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 119 | |
| 120 | buffer[pos] = '\0'; |
| 121 | } |
| 122 | |
| 123 | return pos; |
| 124 | } |
| 125 | |
John Bowler | c5bef94 | 2011-05-05 17:35:39 -0500 | [diff] [blame] | 126 | #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED) |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 127 | /* Utility to dump an unsigned value into a buffer, given a start pointer and |
| 128 | * and end pointer (which should point just *beyond* the end of the buffer!) |
| 129 | * Returns the pointer to the start of the formatted string. |
| 130 | */ |
| 131 | png_charp |
| 132 | png_format_number(png_const_charp start, png_charp end, int format, |
| 133 | png_alloc_size_t number) |
| 134 | { |
| 135 | int count = 0; /* number of digits output */ |
| 136 | int mincount = 1; /* minimum number required */ |
| 137 | int output = 0; /* digit output (for the fixed point format) */ |
| 138 | |
| 139 | *--end = '\0'; |
| 140 | |
| 141 | /* This is written so that the loop always runs at least once, even with |
| 142 | * number zero. |
| 143 | */ |
| 144 | while (end > start && (number != 0 || count < mincount)) |
| 145 | { |
| 146 | |
| 147 | static const char digits[] = "0123456789ABCDEF"; |
| 148 | |
| 149 | switch (format) |
| 150 | { |
| 151 | case PNG_NUMBER_FORMAT_fixed: |
| 152 | /* Needs five digits (the fraction) */ |
| 153 | mincount = 5; |
| 154 | if (output || number % 10 != 0) |
| 155 | { |
| 156 | *--end = digits[number % 10]; |
| 157 | output = 1; |
| 158 | } |
| 159 | number /= 10; |
| 160 | break; |
| 161 | |
| 162 | case PNG_NUMBER_FORMAT_02u: |
| 163 | /* Expects at least 2 digits. */ |
| 164 | mincount = 2; |
| 165 | /* fall through */ |
| 166 | |
| 167 | case PNG_NUMBER_FORMAT_u: |
| 168 | *--end = digits[number % 10]; |
| 169 | number /= 10; |
| 170 | break; |
| 171 | |
| 172 | case PNG_NUMBER_FORMAT_02x: |
| 173 | /* This format expects at least two digits */ |
| 174 | mincount = 2; |
| 175 | /* fall through */ |
| 176 | |
| 177 | case PNG_NUMBER_FORMAT_x: |
| 178 | *--end = digits[number & 0xf]; |
| 179 | number >>= 4; |
| 180 | break; |
| 181 | |
| 182 | default: /* an error */ |
| 183 | number = 0; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | /* Keep track of the number of digits added */ |
| 188 | ++count; |
| 189 | |
| 190 | /* Float a fixed number here: */ |
| 191 | if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start) |
| 192 | { |
| 193 | /* End of the fraction, but maybe nothing was output? In that case |
| 194 | * drop the decimal point. If the number is a true zero handle that |
| 195 | * here. |
| 196 | */ |
| 197 | if (output) |
| 198 | *--end = '.'; |
| 199 | else if (number == 0) /* and !output */ |
| 200 | *--end = '0'; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return end; |
| 205 | } |
| 206 | #endif |
| 207 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 208 | #ifdef PNG_WARNINGS_SUPPORTED |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 209 | /* This function is called whenever there is a non-fatal error. This function |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 210 | * should not be changed. If there is a need to handle warnings differently, |
| 211 | * you should supply a replacement warning function and use |
| 212 | * png_set_error_fn() to replace the warning function at run-time. |
| 213 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 214 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 215 | png_warning(png_const_structrp png_ptr, png_const_charp warning_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 216 | { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 217 | int offset = 0; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 218 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 219 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 220 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
| 221 | if (png_ptr->flags& |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 222 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 223 | #endif |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 224 | { |
| 225 | if (*warning_message == PNG_LITERAL_SHARP) |
| 226 | { |
| 227 | for (offset = 1; offset < 15; offset++) |
| 228 | if (warning_message[offset] == ' ') |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 229 | break; |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 230 | } |
| 231 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 232 | } |
Glenn Randers-Pehrson | 398b5a3 | 2008-11-23 06:48:29 -0600 | [diff] [blame] | 233 | if (png_ptr != NULL && png_ptr->warning_fn != NULL) |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 234 | (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr), |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 235 | warning_message + offset); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 236 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 237 | png_default_warning(png_ptr, warning_message + offset); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 238 | } |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 239 | |
| 240 | /* These functions support 'formatted' warning messages with up to |
| 241 | * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter |
| 242 | * is introduced by @<number>, where 'number' starts at 1. This follows the |
| 243 | * standard established by X/Open for internationalizable error messages. |
| 244 | */ |
| 245 | void |
| 246 | png_warning_parameter(png_warning_parameters p, int number, |
| 247 | png_const_charp string) |
| 248 | { |
| 249 | if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT) |
| 250 | (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string); |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | png_warning_parameter_unsigned(png_warning_parameters p, int number, int format, |
| 255 | png_alloc_size_t value) |
| 256 | { |
| 257 | char buffer[PNG_NUMBER_BUFFER_SIZE]; |
| 258 | png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value)); |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | png_warning_parameter_signed(png_warning_parameters p, int number, int format, |
| 263 | png_int_32 value) |
| 264 | { |
| 265 | png_alloc_size_t u; |
| 266 | png_charp str; |
| 267 | char buffer[PNG_NUMBER_BUFFER_SIZE]; |
| 268 | |
| 269 | /* Avoid overflow by doing the negate in a png_alloc_size_t: */ |
| 270 | u = (png_alloc_size_t)value; |
| 271 | if (value < 0) |
| 272 | u = ~u + 1; |
| 273 | |
| 274 | str = PNG_FORMAT_NUMBER(buffer, format, u); |
| 275 | |
| 276 | if (value < 0 && str > buffer) |
| 277 | *--str = '-'; |
| 278 | |
| 279 | png_warning_parameter(p, number, str); |
| 280 | } |
| 281 | |
| 282 | void |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 283 | png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p, |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 284 | png_const_charp message) |
| 285 | { |
| 286 | /* The internal buffer is just 128 bytes - enough for all our messages, |
| 287 | * overflow doesn't happen because this code checks! |
| 288 | */ |
| 289 | size_t i; |
| 290 | char msg[128]; |
| 291 | |
| 292 | for (i=0; i<(sizeof msg)-1 && *message != '\0'; ++i) |
| 293 | { |
| 294 | if (*message == '@') |
| 295 | { |
| 296 | int parameter = -1; |
| 297 | switch (*++message) |
| 298 | { |
| 299 | case '1': |
| 300 | parameter = 0; |
| 301 | break; |
| 302 | |
| 303 | case '2': |
| 304 | parameter = 1; |
| 305 | break; |
| 306 | |
| 307 | case '\0': |
| 308 | continue; /* To break out of the for loop above. */ |
| 309 | |
| 310 | default: |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | if (parameter >= 0 && parameter < PNG_WARNING_PARAMETER_COUNT) |
| 315 | { |
| 316 | /* Append this parameter */ |
| 317 | png_const_charp parm = p[parameter]; |
| 318 | png_const_charp pend = p[parameter] + (sizeof p[parameter]); |
| 319 | |
| 320 | /* No need to copy the trailing '\0' here, but there is no guarantee |
| 321 | * that parm[] has been initialized, so there is no guarantee of a |
| 322 | * trailing '\0': |
| 323 | */ |
| 324 | for (; i<(sizeof msg)-1 && parm != '\0' && parm < pend; ++i) |
| 325 | msg[i] = *parm++; |
| 326 | |
| 327 | ++message; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | /* else not a parameter and there is a character after the @ sign; just |
| 332 | * copy that. |
| 333 | */ |
| 334 | } |
| 335 | |
| 336 | /* At this point *message can't be '\0', even in the bad parameter case |
| 337 | * above where there is a lone '@' at the end of the message string. |
| 338 | */ |
| 339 | msg[i] = *message++; |
| 340 | } |
| 341 | |
| 342 | /* i is always less than (sizeof msg), so: */ |
| 343 | msg[i] = '\0'; |
| 344 | |
| 345 | /* And this is the formatted message: */ |
| 346 | png_warning(png_ptr, msg); |
| 347 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 348 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 349 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 350 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 351 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 352 | png_benign_error(png_const_structrp png_ptr, png_const_charp error_message) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 353 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 354 | if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 355 | png_warning(png_ptr, error_message); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 356 | else |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 357 | png_error(png_ptr, error_message); |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 358 | } |
| 359 | #endif |
| 360 | |
Glenn Randers-Pehrson | cfbed9b | 2002-05-21 18:06:08 -0500 | [diff] [blame] | 361 | /* These utilities are used internally to build an error message that relates |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 362 | * to the current chunk. The chunk name comes from png_ptr->chunk_name, |
| 363 | * this is used to prefix the message. The message is limited in length |
| 364 | * to 63 bytes, the name characters are output as hex digits wrapped in [] |
| 365 | * if the character is invalid. |
| 366 | */ |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 367 | #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 368 | static PNG_CONST char png_digit[16] = { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 369 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 370 | 'A', 'B', 'C', 'D', 'E', 'F' |
| 371 | }; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 372 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 373 | #define PNG_MAX_ERROR_TEXT 64 |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 374 | #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 375 | static void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 376 | png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 377 | error_message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 378 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 379 | png_uint_32 chunk_name = png_ptr->chunk_name; |
| 380 | int iout = 0, ishift = 24; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 381 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 382 | while (ishift >= 0) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 383 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 384 | int c = (int)(chunk_name >> ishift) & 0xff; |
| 385 | |
| 386 | ishift -= 8; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 387 | if (isnonalpha(c)) |
| 388 | { |
Glenn Randers-Pehrson | 7908421 | 2008-08-04 13:31:41 -0500 | [diff] [blame] | 389 | buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 390 | buffer[iout++] = png_digit[(c & 0xf0) >> 4]; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 391 | buffer[iout++] = png_digit[c & 0x0f]; |
Glenn Randers-Pehrson | 7908421 | 2008-08-04 13:31:41 -0500 | [diff] [blame] | 392 | buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 393 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 394 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 395 | else |
| 396 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 397 | buffer[iout++] = (char)c; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 401 | if (error_message == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 402 | buffer[iout] = '\0'; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 403 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 404 | else |
| 405 | { |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 406 | int iin = 0; |
| 407 | |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 408 | buffer[iout++] = ':'; |
| 409 | buffer[iout++] = ' '; |
Glenn Randers-Pehrson | 07e1d34 | 2011-06-07 14:35:30 -0500 | [diff] [blame] | 410 | |
Glenn Randers-Pehrson | 07e1d34 | 2011-06-07 14:35:30 -0500 | [diff] [blame] | 411 | while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0') |
| 412 | buffer[iout++] = error_message[iin++]; |
| 413 | |
| 414 | /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */ |
| 415 | buffer[iout] = '\0'; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 416 | } |
| 417 | } |
Glenn Randers-Pehrson | 8bdfb47 | 2010-04-17 18:05:01 -0500 | [diff] [blame] | 418 | #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 419 | |
Glenn Randers-Pehrson | 8bdfb47 | 2010-04-17 18:05:01 -0500 | [diff] [blame] | 420 | #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 421 | PNG_FUNCTION(void,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 422 | png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message), |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 423 | PNG_NORETURN) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 424 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 425 | char msg[18+PNG_MAX_ERROR_TEXT]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 426 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 427 | png_error(png_ptr, error_message); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 428 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 429 | else |
| 430 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 431 | png_format_buffer(png_ptr, msg, error_message); |
| 432 | png_error(png_ptr, msg); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 433 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 434 | } |
Glenn Randers-Pehrson | 8bdfb47 | 2010-04-17 18:05:01 -0500 | [diff] [blame] | 435 | #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 436 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 437 | #ifdef PNG_WARNINGS_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 438 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 439 | png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 440 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 441 | char msg[18+PNG_MAX_ERROR_TEXT]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 442 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 443 | png_warning(png_ptr, warning_message); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 444 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 445 | else |
| 446 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 447 | png_format_buffer(png_ptr, msg, warning_message); |
| 448 | png_warning(png_ptr, msg); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 449 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 450 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 451 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 452 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 453 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 454 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 455 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 456 | png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp error_message) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 457 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 458 | if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) |
| 459 | png_chunk_warning(png_ptr, error_message); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 460 | |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 461 | else |
| 462 | png_chunk_error(png_ptr, error_message); |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 463 | } |
| 464 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 465 | #endif /* PNG_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 466 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 467 | #ifdef PNG_ERROR_TEXT_SUPPORTED |
| 468 | #ifdef PNG_FLOATING_POINT_SUPPORTED |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 469 | PNG_FUNCTION(void, |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 470 | png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 471 | { |
| 472 | # define fixed_message "fixed point overflow in " |
| 473 | # define fixed_message_ln ((sizeof fixed_message)-1) |
| 474 | int iin; |
| 475 | char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT]; |
| 476 | png_memcpy(msg, fixed_message, fixed_message_ln); |
| 477 | iin = 0; |
| 478 | if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0) |
| 479 | { |
| 480 | msg[fixed_message_ln + iin] = name[iin]; |
| 481 | ++iin; |
| 482 | } |
| 483 | msg[fixed_message_ln + iin] = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 484 | png_error(png_ptr, msg); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 485 | } |
| 486 | #endif |
| 487 | #endif |
| 488 | |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 489 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | e69b55d | 2010-01-01 10:29:06 -0600 | [diff] [blame] | 490 | /* This API only exists if ANSI-C style error handling is used, |
| 491 | * otherwise it is necessary for png_default_error to be overridden. |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 492 | */ |
| 493 | jmp_buf* PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 494 | png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn, |
Glenn Randers-Pehrson | e69b55d | 2010-01-01 10:29:06 -0600 | [diff] [blame] | 495 | size_t jmp_buf_size) |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 496 | { |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 497 | /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value |
| 498 | * and it must not change after that. Libpng doesn't care how big the |
| 499 | * buffer is, just that it doesn't change. |
| 500 | * |
| 501 | * If the buffer size is no *larger* than the size of jmp_buf when libpng is |
| 502 | * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0 |
| 503 | * semantics that this call will not fail. If the size is larger, however, |
| 504 | * the buffer is allocated and this may fail, causing the function to return |
| 505 | * NULL. |
| 506 | */ |
| 507 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 508 | return NULL; |
| 509 | |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 510 | if (png_ptr->jmp_buf_ptr == NULL) |
| 511 | { |
| 512 | png_ptr->jmp_buf_size = 0; /* not allocated */ |
| 513 | |
| 514 | if (jmp_buf_size <= sizeof png_ptr->jmp_buf_local) |
| 515 | png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; |
| 516 | |
| 517 | else |
| 518 | { |
| 519 | png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *, |
| 520 | png_malloc_warn(png_ptr, jmp_buf_size)); |
| 521 | |
| 522 | if (png_ptr->jmp_buf_ptr == NULL) |
| 523 | return NULL; /* new NULL return on OOM */ |
| 524 | |
| 525 | png_ptr->jmp_buf_size = jmp_buf_size; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | else /* Already allocated: check the size */ |
| 530 | { |
| 531 | size_t size = png_ptr->jmp_buf_size; |
| 532 | |
| 533 | if (size == 0) |
| 534 | { |
| 535 | size = sizeof png_ptr->jmp_buf_local; |
| 536 | if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local) |
| 537 | { |
| 538 | /* This is an internal error in libpng: somehow we have been left |
| 539 | * with a stack allocated jmp_buf when the application regained |
| 540 | * control. It's always possible to fix this up, but for the moment |
| 541 | * this is a png_error because that makes it easy to detect. |
| 542 | */ |
| 543 | png_error(png_ptr, "Libpng jmp_buf still allocated"); |
| 544 | /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */ |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | if (size != jmp_buf_size) |
| 549 | { |
| 550 | png_warning(png_ptr, "Application jmp_buf size changed"); |
| 551 | return NULL; /* caller will probably crash: no choice here */ |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* Finally fill in the function, now we have a satisfactory buffer. It is |
| 556 | * valid to change the function on every call. |
| 557 | */ |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 558 | png_ptr->longjmp_fn = longjmp_fn; |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 559 | return png_ptr->jmp_buf_ptr; |
| 560 | } |
| 561 | |
| 562 | void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 563 | png_free_jmpbuf(png_structrp png_ptr) |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 564 | { |
| 565 | if (png_ptr != NULL) |
| 566 | { |
| 567 | jmp_buf *jb = png_ptr->jmp_buf_ptr; |
| 568 | |
| 569 | /* A size of 0 is used to indicate a local, stack, allocation of the |
| 570 | * pointer; used here and in png.c |
| 571 | */ |
| 572 | if (jb != NULL && png_ptr->jmp_buf_size > 0) |
| 573 | { |
| 574 | |
| 575 | /* This stuff is so that a failure to free the error control structure |
| 576 | * does not leave libpng in a state with no valid error handling: the |
| 577 | * free always succeeds, if there is an error it gets ignored. |
| 578 | */ |
| 579 | if (jb != &png_ptr->jmp_buf_local) |
| 580 | { |
| 581 | /* Make an internal, libpng, jmp_buf to return here */ |
| 582 | jmp_buf free_jmp_buf; |
| 583 | |
| 584 | if (!setjmp(free_jmp_buf)) |
| 585 | { |
| 586 | png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */ |
| 587 | png_ptr->jmp_buf_size = 0; /* stack allocation */ |
| 588 | png_ptr->longjmp_fn = longjmp; |
| 589 | png_free(png_ptr, jb); /* Return to setjmp on error */ |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* *Always* cancel everything out: */ |
| 595 | png_ptr->jmp_buf_size = 0; |
| 596 | png_ptr->jmp_buf_ptr = NULL; |
| 597 | png_ptr->longjmp_fn = 0; |
| 598 | } |
Glenn Randers-Pehrson | f4ea224 | 2009-11-20 21:38:24 -0600 | [diff] [blame] | 599 | } |
| 600 | #endif |
| 601 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 602 | /* This is the default error handling function. Note that replacements for |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 603 | * this function MUST NOT RETURN, or the program will likely crash. This |
| 604 | * function is used by default, or if the program supplies NULL for the |
| 605 | * error function pointer in png_set_error_fn(). |
| 606 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 607 | static PNG_FUNCTION(void /* PRIVATE */, |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 608 | png_default_error,(png_const_structrp png_ptr, png_const_charp error_message), |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 609 | PNG_NORETURN) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 610 | { |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 611 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 612 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | ef217b7 | 2011-06-15 12:58:27 -0500 | [diff] [blame] | 613 | /* Check on NULL only added in 1.5.4 */ |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 614 | if (error_message != NULL && *error_message == PNG_LITERAL_SHARP) |
Glenn Randers-Pehrson | 8fb550c | 2009-03-21 08:15:32 -0500 | [diff] [blame] | 615 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 616 | /* Strip "#nnnn " from beginning of error message. */ |
| 617 | int offset; |
| 618 | char error_number[16]; |
| 619 | for (offset = 0; offset<15; offset++) |
| 620 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 621 | error_number[offset] = error_message[offset + 1]; |
| 622 | if (error_message[offset] == ' ') |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 623 | break; |
| 624 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 625 | |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 626 | if ((offset > 1) && (offset < 15)) |
| 627 | { |
| 628 | error_number[offset - 1] = '\0'; |
| 629 | fprintf(stderr, "libpng error no. %s: %s", |
| 630 | error_number, error_message + offset + 1); |
| 631 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 632 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 633 | |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 634 | else |
| 635 | { |
| 636 | fprintf(stderr, "libpng error: %s, offset=%d", |
| 637 | error_message, offset); |
| 638 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 639 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 640 | } |
| 641 | else |
| 642 | #endif |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 643 | { |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 644 | fprintf(stderr, "libpng error: %s", error_message ? error_message : |
| 645 | "undefined"); |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 646 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 647 | } |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 648 | #else |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 649 | PNG_UNUSED(error_message) /* Make compiler happy */ |
Glenn Randers-Pehrson | f98726a | 2010-02-19 09:54:53 -0600 | [diff] [blame] | 650 | #endif |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 651 | png_longjmp(png_ptr, 1); |
Glenn Randers-Pehrson | f98726a | 2010-02-19 09:54:53 -0600 | [diff] [blame] | 652 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 653 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 654 | PNG_FUNCTION(void,PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 655 | png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN) |
Glenn Randers-Pehrson | f98726a | 2010-02-19 09:54:53 -0600 | [diff] [blame] | 656 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 657 | #ifdef PNG_SETJMP_SUPPORTED |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 658 | if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr) |
| 659 | png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 660 | #endif |
John Bowler | baeb6d1 | 2011-11-26 18:21:02 -0600 | [diff] [blame] | 661 | |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 662 | /* Here if not setjmp support or if png_ptr is null. */ |
| 663 | PNG_ABORT(); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 664 | } |
| 665 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 666 | #ifdef PNG_WARNINGS_SUPPORTED |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 667 | /* This function is called when there is a warning, but the library thinks |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 668 | * it can continue anyway. Replacement functions don't have to do anything |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 669 | * here if you don't want them to. In the default configuration, png_ptr is |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 670 | * not used, but it is passed in case it may be useful. |
| 671 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 672 | static void /* PRIVATE */ |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 673 | png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 674 | { |
Glenn Randers-Pehrson | 2f89d76 | 2009-10-13 18:04:41 -0500 | [diff] [blame] | 675 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 676 | # ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 677 | if (*warning_message == PNG_LITERAL_SHARP) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 678 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 679 | int offset; |
| 680 | char warning_number[16]; |
| 681 | for (offset = 0; offset < 15; offset++) |
| 682 | { |
| 683 | warning_number[offset] = warning_message[offset + 1]; |
| 684 | if (warning_message[offset] == ' ') |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 685 | break; |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 686 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 687 | |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 688 | if ((offset > 1) && (offset < 15)) |
| 689 | { |
| 690 | warning_number[offset + 1] = '\0'; |
| 691 | fprintf(stderr, "libpng warning no. %s: %s", |
| 692 | warning_number, warning_message + offset); |
| 693 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 694 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 695 | |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 696 | else |
| 697 | { |
| 698 | fprintf(stderr, "libpng warning: %s", |
| 699 | warning_message); |
| 700 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 701 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 702 | } |
| 703 | else |
| 704 | # endif |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 705 | |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 706 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 707 | fprintf(stderr, "libpng warning: %s", warning_message); |
| 708 | fprintf(stderr, PNG_STRING_NEWLINE); |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 709 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 710 | #else |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 711 | PNG_UNUSED(warning_message) /* Make compiler happy */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 712 | #endif |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 713 | PNG_UNUSED(png_ptr) /* Make compiler happy */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 714 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 715 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 716 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 717 | /* This function is called when the application wants to use another method |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 718 | * of handling errors and warnings. Note that the error function MUST NOT |
| 719 | * return to the calling routine or serious problems will occur. The return |
John Bowler | d332c67 | 2011-12-21 17:36:12 -0600 | [diff] [blame] | 720 | * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 721 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 722 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 723 | png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr, |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 724 | png_error_ptr error_fn, png_error_ptr warning_fn) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 725 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 726 | if (png_ptr == NULL) |
| 727 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 728 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 729 | png_ptr->error_ptr = error_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 730 | png_ptr->error_fn = error_fn; |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 731 | #ifdef PNG_WARNINGS_SUPPORTED |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 732 | png_ptr->warning_fn = warning_fn; |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 733 | #else |
| 734 | PNG_UNUSED(warning_fn) |
| 735 | #endif |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 739 | /* This function returns a pointer to the error_ptr associated with the user |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 740 | * functions. The application should free any memory associated with this |
| 741 | * pointer before png_write_destroy and png_read_destroy are called. |
| 742 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 743 | png_voidp PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 744 | png_get_error_ptr(png_const_structrp png_ptr) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 745 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 746 | if (png_ptr == NULL) |
| 747 | return NULL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 748 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 749 | return ((png_voidp)png_ptr->error_ptr); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 753 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 754 | void PNGAPI |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 755 | png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 756 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 757 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 758 | { |
Glenn Randers-Pehrson | da30135 | 2010-03-06 06:42:48 -0600 | [diff] [blame] | 759 | png_ptr->flags &= |
| 760 | ((~(PNG_FLAG_STRIP_ERROR_NUMBERS | |
| 761 | PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | #endif |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 765 | |
| 766 | #if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\ |
| 767 | defined PNG_SIMPLIFIED_WRITE_SUPPORTED |
| 768 | /* Currently the above both depend on SETJMP_SUPPORTED, however it would be |
| 769 | * possible to implement without setjmp support just so long as there is some |
| 770 | * way to handle the error return here: |
| 771 | */ |
| 772 | PNG_FUNCTION(void /* PRIVATE */, |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 773 | png_safe_error,(png_structp png_nonconst_ptr, png_const_charp error_message), |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 774 | PNG_NORETURN) |
| 775 | { |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 776 | const png_const_structrp png_ptr = png_nonconst_ptr; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 777 | png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 778 | |
| 779 | /* An error is always logged here, overwriting anything (typically a warning) |
| 780 | * that is already there: |
| 781 | */ |
| 782 | if (image != NULL) |
| 783 | { |
| 784 | png_safecat(image->message, sizeof image->message, 0, error_message); |
| 785 | image->warning_or_error = 1; |
| 786 | |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 787 | /* Retrieve the jmp_buf from within the png_control, making this work for |
| 788 | * C++ compilation too is pretty tricky: C++ wants a pointer to the first |
| 789 | * element of a jmp_buf, but C doesn't tell us the type of that. |
| 790 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 791 | if (image->opaque != NULL && image->opaque->error_buf != NULL) |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 792 | longjmp(png_control_jmp_buf(image->opaque), 1); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 793 | |
| 794 | /* Missing longjmp buffer, the following is to help debugging: */ |
| 795 | { |
| 796 | size_t pos = png_safecat(image->message, sizeof image->message, 0, |
| 797 | "bad longjmp: "); |
| 798 | png_safecat(image->message, sizeof image->message, pos, error_message); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /* Here on an internal programming error. */ |
| 803 | abort(); |
| 804 | } |
| 805 | |
| 806 | #ifdef PNG_WARNINGS_SUPPORTED |
| 807 | void /* PRIVATE */ |
John Bowler | 40b2603 | 2011-12-22 08:09:15 -0600 | [diff] [blame] | 808 | png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message) |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 809 | { |
John Bowler | 5d56786 | 2011-12-24 09:12:00 -0600 | [diff] [blame] | 810 | const png_const_structrp png_ptr = png_nonconst_ptr; |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame] | 811 | png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 812 | |
Glenn Randers-Pehrson | 84b0da9 | 2011-11-10 06:32:19 -0600 | [diff] [blame] | 813 | /* A warning is only logged if there is no prior warning or error. */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 814 | if (image->warning_or_error == 0) |
| 815 | { |
| 816 | png_safecat(image->message, sizeof image->message, 0, warning_message); |
| 817 | image->warning_or_error = 2; |
| 818 | } |
| 819 | } |
| 820 | #endif |
| 821 | |
| 822 | int /* PRIVATE */ |
| 823 | png_safe_execute(png_imagep imageIn, int (*function)(png_voidp), png_voidp arg) |
| 824 | { |
| 825 | volatile png_imagep image = imageIn; |
| 826 | volatile int result; |
| 827 | volatile png_voidp saved_error_buf; |
| 828 | jmp_buf safe_jmpbuf; |
| 829 | |
| 830 | /* Safely execute function(arg) with png_error returning to this function. */ |
| 831 | saved_error_buf = image->opaque->error_buf; |
| 832 | result = setjmp(safe_jmpbuf) == 0; |
| 833 | |
| 834 | if (result) |
| 835 | { |
| 836 | |
| 837 | image->opaque->error_buf = safe_jmpbuf; |
| 838 | result = function(arg); |
| 839 | } |
| 840 | |
| 841 | image->opaque->error_buf = saved_error_buf; |
| 842 | |
| 843 | /* And do the cleanup prior to any failure return. */ |
| 844 | if (!result) |
| 845 | png_image_free(image); |
| 846 | |
| 847 | return result; |
| 848 | } |
| 849 | #endif /* SIMPLIFIED READ/WRITE */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 850 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |