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