7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /* pngerror.c - stub functions for i/o and memory allocation
26 *
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file and, per its terms, should not be removed:
31 *
32 * Last changed in libpng 1.6.31 [July 27, 2017]
33 * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
34 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
35 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
36 *
37 * This code is released under the libpng license.
38 * For conditions of distribution and use, see the disclaimer
39 * and license in png.h
40 *
41 * This file provides a location for all error handling. Users who
42 * need special error handling are expected to write replacement functions
43 * and use png_set_error_fn() to use those functions. See the instructions
44 * at each function.
45 */
46
47 #include "pngpriv.h"
48
49 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
50
51 static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
52 png_const_charp error_message)),PNG_NORETURN);
53
54 #ifdef PNG_WARNINGS_SUPPORTED
55 static void /* PRIVATE */
436 png_warning(png_ptr, error_message);
437 else
438 png_error(png_ptr, error_message);
439
440 # ifndef PNG_ERROR_TEXT_SUPPORTED
441 PNG_UNUSED(error_message)
442 # endif
443 }
444 #endif /* BENIGN_ERRORS */
445
446 #define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
447 #if defined(PNG_WARNINGS_SUPPORTED) || \
448 (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
449 /* These utilities are used internally to build an error message that relates
450 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
451 * which is used to prefix the message. The message is limited in length
452 * to 63 bytes. The name characters are output as hex digits wrapped in []
453 * if the character is invalid.
454 */
455 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
456 static PNG_CONST char png_digit[16] = {
457 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
458 'A', 'B', 'C', 'D', 'E', 'F'
459 };
460
461 static void /* PRIVATE */
462 png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
463 error_message)
464 {
465 png_uint_32 chunk_name = png_ptr->chunk_name;
466 int iout = 0, ishift = 24;
467
468 while (ishift >= 0)
469 {
470 int c = (int)(chunk_name >> ishift) & 0xff;
471
472 ishift -= 8;
473 if (isnonalpha(c) != 0)
474 {
475 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
476 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
896 {
897 if (png_ptr != NULL)
898 {
899 png_ptr->flags &=
900 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
901 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
902 }
903 }
904 #endif
905
906 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
907 defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
908 /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
909 * possible to implement without setjmp support just so long as there is some
910 * way to handle the error return here:
911 */
912 PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
913 png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
914 PNG_NORETURN)
915 {
916 const png_const_structrp png_ptr = png_nonconst_ptr;
917 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
918
919 /* An error is always logged here, overwriting anything (typically a warning)
920 * that is already there:
921 */
922 if (image != NULL)
923 {
924 png_safecat(image->message, (sizeof image->message), 0, error_message);
925 image->warning_or_error |= PNG_IMAGE_ERROR;
926
927 /* Retrieve the jmp_buf from within the png_control, making this work for
928 * C++ compilation too is pretty tricky: C++ wants a pointer to the first
929 * element of a jmp_buf, but C doesn't tell us the type of that.
930 */
931 if (image->opaque != NULL && image->opaque->error_buf != NULL)
932 longjmp(png_control_jmp_buf(image->opaque), 1);
933
934 /* Missing longjmp buffer, the following is to help debugging: */
935 {
936 size_t pos = png_safecat(image->message, (sizeof image->message), 0,
937 "bad longjmp: ");
938 png_safecat(image->message, (sizeof image->message), pos,
939 error_message);
940 }
941 }
942
943 /* Here on an internal programming error. */
944 abort();
945 }
946
947 #ifdef PNG_WARNINGS_SUPPORTED
948 void /* PRIVATE */ PNGCBAPI
949 png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
950 {
951 const png_const_structrp png_ptr = png_nonconst_ptr;
952 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
953
954 /* A warning is only logged if there is no prior warning or error. */
955 if (image->warning_or_error == 0)
956 {
957 png_safecat(image->message, (sizeof image->message), 0, warning_message);
958 image->warning_or_error |= PNG_IMAGE_WARNING;
959 }
960 }
961 #endif
962
963 int /* PRIVATE */
964 png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
965 {
966 volatile png_imagep image = image_in;
967 volatile int result;
968 volatile png_voidp saved_error_buf;
969 jmp_buf safe_jmpbuf;
970
971 /* Safely execute function(arg) with png_error returning to this function. */
|
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 */
24
25 /* pngerror.c - stub functions for i/o and memory allocation
26 *
27 * Copyright (c) 2018 Cosmin Truta
28 * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
29 * Copyright (c) 1996-1997 Andreas Dilger
30 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
31 *
32 * This code is released under the libpng license.
33 * For conditions of distribution and use, see the disclaimer
34 * and license in png.h
35 *
36 * This file provides a location for all error handling. Users who
37 * need special error handling are expected to write replacement functions
38 * and use png_set_error_fn() to use those functions. See the instructions
39 * at each function.
40 */
41
42 #include "pngpriv.h"
43
44 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
45
46 static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr,
47 png_const_charp error_message)),PNG_NORETURN);
48
49 #ifdef PNG_WARNINGS_SUPPORTED
50 static void /* PRIVATE */
431 png_warning(png_ptr, error_message);
432 else
433 png_error(png_ptr, error_message);
434
435 # ifndef PNG_ERROR_TEXT_SUPPORTED
436 PNG_UNUSED(error_message)
437 # endif
438 }
439 #endif /* BENIGN_ERRORS */
440
441 #define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */
442 #if defined(PNG_WARNINGS_SUPPORTED) || \
443 (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED))
444 /* These utilities are used internally to build an error message that relates
445 * to the current chunk. The chunk name comes from png_ptr->chunk_name,
446 * which is used to prefix the message. The message is limited in length
447 * to 63 bytes. The name characters are output as hex digits wrapped in []
448 * if the character is invalid.
449 */
450 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
451 static const char png_digit[16] = {
452 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
453 'A', 'B', 'C', 'D', 'E', 'F'
454 };
455
456 static void /* PRIVATE */
457 png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp
458 error_message)
459 {
460 png_uint_32 chunk_name = png_ptr->chunk_name;
461 int iout = 0, ishift = 24;
462
463 while (ishift >= 0)
464 {
465 int c = (int)(chunk_name >> ishift) & 0xff;
466
467 ishift -= 8;
468 if (isnonalpha(c) != 0)
469 {
470 buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET;
471 buffer[iout++] = png_digit[(c & 0xf0) >> 4];
891 {
892 if (png_ptr != NULL)
893 {
894 png_ptr->flags &=
895 ((~(PNG_FLAG_STRIP_ERROR_NUMBERS |
896 PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode);
897 }
898 }
899 #endif
900
901 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
902 defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
903 /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
904 * possible to implement without setjmp support just so long as there is some
905 * way to handle the error return here:
906 */
907 PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI
908 png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message),
909 PNG_NORETURN)
910 {
911 png_const_structrp png_ptr = png_nonconst_ptr;
912 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
913
914 /* An error is always logged here, overwriting anything (typically a warning)
915 * that is already there:
916 */
917 if (image != NULL)
918 {
919 png_safecat(image->message, (sizeof image->message), 0, error_message);
920 image->warning_or_error |= PNG_IMAGE_ERROR;
921
922 /* Retrieve the jmp_buf from within the png_control, making this work for
923 * C++ compilation too is pretty tricky: C++ wants a pointer to the first
924 * element of a jmp_buf, but C doesn't tell us the type of that.
925 */
926 if (image->opaque != NULL && image->opaque->error_buf != NULL)
927 longjmp(png_control_jmp_buf(image->opaque), 1);
928
929 /* Missing longjmp buffer, the following is to help debugging: */
930 {
931 size_t pos = png_safecat(image->message, (sizeof image->message), 0,
932 "bad longjmp: ");
933 png_safecat(image->message, (sizeof image->message), pos,
934 error_message);
935 }
936 }
937
938 /* Here on an internal programming error. */
939 abort();
940 }
941
942 #ifdef PNG_WARNINGS_SUPPORTED
943 void /* PRIVATE */ PNGCBAPI
944 png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message)
945 {
946 png_const_structrp png_ptr = png_nonconst_ptr;
947 png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr);
948
949 /* A warning is only logged if there is no prior warning or error. */
950 if (image->warning_or_error == 0)
951 {
952 png_safecat(image->message, (sizeof image->message), 0, warning_message);
953 image->warning_or_error |= PNG_IMAGE_WARNING;
954 }
955 }
956 #endif
957
958 int /* PRIVATE */
959 png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg)
960 {
961 volatile png_imagep image = image_in;
962 volatile int result;
963 volatile png_voidp saved_error_buf;
964 jmp_buf safe_jmpbuf;
965
966 /* Safely execute function(arg) with png_error returning to this function. */
|