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 | 311c847 | 2009-11-20 09:45:08 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.4.0 [November 20, 2009] |
Glenn Randers-Pehrson | 79134c6 | 2009-02-14 10:32:18 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2009 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 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 19 | #include "png.h" |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 20 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 21 | #include "pngpriv.h" |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 22 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 23 | static void /* PRIVATE */ |
| 24 | png_default_error PNGARG((png_structp png_ptr, |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 25 | png_const_charp error_message)) PNG_NORETURN; |
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 */ |
| 28 | png_default_warning PNGARG((png_structp png_ptr, |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -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 | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 38 | void PNGAPI |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 39 | png_error(png_structp png_ptr, png_const_charp error_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 40 | { |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 41 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
| 42 | char msg[16]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 43 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 44 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 45 | if (png_ptr->flags& |
| 46 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
| 47 | { |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 48 | if (*error_message == PNG_LITERAL_SHARP) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 49 | { |
| 50 | /* Strip "#nnnn " from beginning of error message. */ |
| 51 | int offset; |
| 52 | for (offset = 1; offset<15; offset++) |
| 53 | if (error_message[offset] == ' ') |
| 54 | break; |
| 55 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
| 56 | { |
| 57 | int i; |
| 58 | for (i = 0; i < offset - 1; i++) |
| 59 | msg[i] = error_message[i + 1]; |
| 60 | msg[i - 1] = '\0'; |
| 61 | error_message = msg; |
| 62 | } |
| 63 | else |
| 64 | error_message += offset; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) |
| 69 | { |
| 70 | msg[0] = '0'; |
| 71 | msg[1] = '\0'; |
| 72 | error_message = msg; |
| 73 | } |
| 74 | } |
| 75 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 76 | } |
| 77 | #endif |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 78 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 79 | (*(png_ptr->error_fn))(png_ptr, error_message); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 80 | |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 81 | /* If the custom handler doesn't exist, or if it returns, |
| 82 | use the default handler, which will not return. */ |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 83 | png_default_error(png_ptr, error_message); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 84 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 85 | #else |
| 86 | void PNGAPI |
| 87 | png_err(png_structp png_ptr) |
| 88 | { |
| 89 | if (png_ptr != NULL && png_ptr->error_fn != NULL) |
| 90 | (*(png_ptr->error_fn))(png_ptr, '\0'); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 91 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 92 | /* If the custom handler doesn't exist, or if it returns, |
| 93 | use the default handler, which will not return. */ |
| 94 | png_default_error(png_ptr, '\0'); |
| 95 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 96 | #endif /* PNG_ERROR_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 97 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 98 | #ifdef PNG_WARNINGS_SUPPORTED |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 99 | /* 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] | 100 | * should not be changed. If there is a need to handle warnings differently, |
| 101 | * you should supply a replacement warning function and use |
| 102 | * png_set_error_fn() to replace the warning function at run-time. |
| 103 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 104 | void PNGAPI |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 105 | png_warning(png_structp png_ptr, png_const_charp warning_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 106 | { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 107 | int offset = 0; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 108 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 109 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 110 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
| 111 | if (png_ptr->flags& |
| 112 | (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) |
| 113 | #endif |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 114 | { |
| 115 | if (*warning_message == PNG_LITERAL_SHARP) |
| 116 | { |
| 117 | for (offset = 1; offset < 15; offset++) |
| 118 | if (warning_message[offset] == ' ') |
| 119 | break; |
| 120 | } |
| 121 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 122 | } |
Glenn Randers-Pehrson | 398b5a3 | 2008-11-23 06:48:29 -0600 | [diff] [blame] | 123 | if (png_ptr != NULL && png_ptr->warning_fn != NULL) |
| 124 | (*(png_ptr->warning_fn))(png_ptr, warning_message + offset); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 125 | else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 126 | png_default_warning(png_ptr, warning_message + offset); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 127 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 128 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 129 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 130 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 131 | void PNGAPI |
| 132 | png_benign_error(png_structp png_ptr, png_const_charp error_message) |
| 133 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 134 | if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) |
| 135 | png_warning(png_ptr, error_message); |
| 136 | else |
| 137 | png_error(png_ptr, error_message); |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 138 | } |
| 139 | #endif |
| 140 | |
Glenn Randers-Pehrson | cfbed9b | 2002-05-21 18:06:08 -0500 | [diff] [blame] | 141 | /* 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] | 142 | * to the current chunk. The chunk name comes from png_ptr->chunk_name, |
| 143 | * this is used to prefix the message. The message is limited in length |
| 144 | * to 63 bytes, the name characters are output as hex digits wrapped in [] |
| 145 | * if the character is invalid. |
| 146 | */ |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 147 | #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) |
Glenn Randers-Pehrson | 7cd899c | 1998-03-07 16:17:42 -0600 | [diff] [blame] | 148 | static PNG_CONST char png_digit[16] = { |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 149 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 150 | 'A', 'B', 'C', 'D', 'E', 'F' |
| 151 | }; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 152 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 153 | #define PNG_MAX_ERROR_TEXT 64 |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 154 | #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 155 | static void /* PRIVATE */ |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 156 | png_format_buffer(png_structp png_ptr, png_charp buffer, png_const_charp |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 157 | error_message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 158 | { |
| 159 | int iout = 0, iin = 0; |
| 160 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 161 | while (iin < 4) |
| 162 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 163 | int c = png_ptr->chunk_name[iin++]; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 164 | if (isnonalpha(c)) |
| 165 | { |
Glenn Randers-Pehrson | 7908421 | 2008-08-04 13:31:41 -0500 | [diff] [blame] | 166 | buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 167 | buffer[iout++] = png_digit[(c & 0xf0) >> 4]; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 168 | buffer[iout++] = png_digit[c & 0x0f]; |
Glenn Randers-Pehrson | 7908421 | 2008-08-04 13:31:41 -0500 | [diff] [blame] | 169 | buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 173 | buffer[iout++] = (png_byte)c; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 177 | if (error_message == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 178 | buffer[iout] = '\0'; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 179 | else |
| 180 | { |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 181 | buffer[iout++] = ':'; |
| 182 | buffer[iout++] = ' '; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 183 | png_memcpy(buffer + iout, error_message, PNG_MAX_ERROR_TEXT); |
| 184 | buffer[iout + PNG_MAX_ERROR_TEXT - 1] = '\0'; |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 188 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 189 | void PNGAPI |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 190 | png_chunk_error(png_structp png_ptr, png_const_charp error_message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 191 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 192 | char msg[18+PNG_MAX_ERROR_TEXT]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 193 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 194 | png_error(png_ptr, error_message); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 195 | else |
| 196 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 197 | png_format_buffer(png_ptr, msg, error_message); |
| 198 | png_error(png_ptr, msg); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 199 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 200 | } |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 201 | #endif /* PNG_READ_SUPPORTED */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 202 | #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 203 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 204 | #ifdef PNG_WARNINGS_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 205 | void PNGAPI |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 206 | png_chunk_warning(png_structp png_ptr, png_const_charp warning_message) |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 207 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 208 | char msg[18+PNG_MAX_ERROR_TEXT]; |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 209 | if (png_ptr == NULL) |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 210 | png_warning(png_ptr, warning_message); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 211 | else |
| 212 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 213 | png_format_buffer(png_ptr, msg, warning_message); |
| 214 | png_warning(png_ptr, msg); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 215 | } |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 216 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 217 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 218 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 219 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 220 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 221 | void PNGAPI |
| 222 | png_chunk_benign_error(png_structp png_ptr, png_const_charp error_message) |
| 223 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 224 | if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) |
| 225 | png_chunk_warning(png_ptr, error_message); |
| 226 | else |
| 227 | png_chunk_error(png_ptr, error_message); |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 228 | } |
| 229 | #endif |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 230 | #endif /* PNG_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 231 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 232 | /* This is the default error handling function. Note that replacements for |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 233 | * this function MUST NOT RETURN, or the program will likely crash. This |
| 234 | * function is used by default, or if the program supplies NULL for the |
| 235 | * error function pointer in png_set_error_fn(). |
| 236 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 237 | static void /* PRIVATE */ |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 238 | png_default_error(png_structp png_ptr, png_const_charp error_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 239 | { |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 240 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 241 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 242 | if (*error_message == PNG_LITERAL_SHARP) |
Glenn Randers-Pehrson | 8fb550c | 2009-03-21 08:15:32 -0500 | [diff] [blame] | 243 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 244 | /* Strip "#nnnn " from beginning of error message. */ |
| 245 | int offset; |
| 246 | char error_number[16]; |
| 247 | for (offset = 0; offset<15; offset++) |
| 248 | { |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 249 | error_number[offset] = error_message[offset + 1]; |
| 250 | if (error_message[offset] == ' ') |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 251 | break; |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 252 | } |
| 253 | if ((offset > 1) && (offset < 15)) |
| 254 | { |
| 255 | error_number[offset - 1] = '\0'; |
| 256 | fprintf(stderr, "libpng error no. %s: %s", |
| 257 | error_number, error_message + offset + 1); |
| 258 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | fprintf(stderr, "libpng error: %s, offset=%d", |
| 263 | error_message, offset); |
| 264 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 265 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 266 | } |
| 267 | else |
| 268 | #endif |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 269 | { |
| 270 | fprintf(stderr, "libpng error: %s", error_message); |
| 271 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 272 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 273 | #endif |
| 274 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 275 | #ifdef PNG_SETJMP_SUPPORTED |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 276 | if (png_ptr) |
| 277 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 278 | # ifdef USE_FAR_KEYWORD |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 279 | { |
| 280 | jmp_buf jmpbuf; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 281 | png_memcpy(jmpbuf, png_ptr->jmpbuf, png_sizeof(jmp_buf)); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 282 | longjmp(jmpbuf, 1); |
| 283 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 284 | # else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 285 | longjmp(png_ptr->jmpbuf, 1); |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 286 | # endif |
| 287 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 288 | #endif |
Glenn Randers-Pehrson | d29033f | 2009-11-07 10:46:42 -0600 | [diff] [blame] | 289 | /* Here if not setjmp support or if png_ptr is null. */ |
| 290 | PNG_ABORT(); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 291 | #ifndef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 292 | error_message = error_message; /* Make compiler happy */ |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 293 | #endif |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 294 | } |
| 295 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 296 | #ifdef PNG_WARNINGS_SUPPORTED |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 297 | /* 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] | 298 | * 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] | 299 | * 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] | 300 | * not used, but it is passed in case it may be useful. |
| 301 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 302 | static void /* PRIVATE */ |
Glenn Randers-Pehrson | 837a3d1 | 2002-05-10 20:19:58 -0500 | [diff] [blame] | 303 | png_default_warning(png_structp png_ptr, png_const_charp warning_message) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 304 | { |
Glenn Randers-Pehrson | 2f89d76 | 2009-10-13 18:04:41 -0500 | [diff] [blame] | 305 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 306 | # ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 307 | if (*warning_message == PNG_LITERAL_SHARP) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 308 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 309 | int offset; |
| 310 | char warning_number[16]; |
| 311 | for (offset = 0; offset < 15; offset++) |
| 312 | { |
| 313 | warning_number[offset] = warning_message[offset + 1]; |
| 314 | if (warning_message[offset] == ' ') |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 315 | break; |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 316 | } |
| 317 | if ((offset > 1) && (offset < 15)) |
| 318 | { |
| 319 | warning_number[offset + 1] = '\0'; |
| 320 | fprintf(stderr, "libpng warning no. %s: %s", |
| 321 | warning_number, warning_message + offset); |
| 322 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | fprintf(stderr, "libpng warning: %s", |
| 327 | warning_message); |
| 328 | fprintf(stderr, PNG_STRING_NEWLINE); |
| 329 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 330 | } |
| 331 | else |
| 332 | # endif |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 333 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 334 | fprintf(stderr, "libpng warning: %s", warning_message); |
| 335 | fprintf(stderr, PNG_STRING_NEWLINE); |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 336 | } |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 337 | #else |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 338 | warning_message = warning_message; /* Make compiler happy */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 339 | #endif |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 340 | png_ptr = png_ptr; /* Make compiler happy */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 341 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 342 | #endif /* PNG_WARNINGS_SUPPORTED */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 343 | |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 344 | /* 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] | 345 | * of handling errors and warnings. Note that the error function MUST NOT |
| 346 | * return to the calling routine or serious problems will occur. The return |
| 347 | * method used in the default routine calls longjmp(png_ptr->jmpbuf, 1) |
| 348 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 349 | void PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 350 | png_set_error_fn(png_structp png_ptr, png_voidp error_ptr, |
| 351 | png_error_ptr error_fn, png_error_ptr warning_fn) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 352 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 353 | if (png_ptr == NULL) |
| 354 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 355 | png_ptr->error_ptr = error_ptr; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 356 | png_ptr->error_fn = error_fn; |
| 357 | png_ptr->warning_fn = warning_fn; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 361 | /* 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] | 362 | * functions. The application should free any memory associated with this |
| 363 | * pointer before png_write_destroy and png_read_destroy are called. |
| 364 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 365 | png_voidp PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 366 | png_get_error_ptr(png_structp png_ptr) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 367 | { |
Glenn Randers-Pehrson | 170b70c | 2006-03-10 10:19:04 -0600 | [diff] [blame] | 368 | if (png_ptr == NULL) |
| 369 | return NULL; |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 370 | return ((png_voidp)png_ptr->error_ptr); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 374 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 375 | void PNGAPI |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 376 | png_set_strip_error_numbers(png_structp png_ptr, png_uint_32 strip_mode) |
| 377 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 378 | if (png_ptr != NULL) |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 379 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 380 | png_ptr->flags &= |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 381 | ((~(PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); |
| 382 | } |
| 383 | } |
| 384 | #endif |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 385 | #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |