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