michael@0: michael@0: /* pngerror.c - stub functions for i/o and memory allocation michael@0: * michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file provides a location for all error handling. Users who michael@0: * need special error handling are expected to write replacement functions michael@0: * and use png_set_error_fn() to use those functions. See the instructions michael@0: * at each function. michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) michael@0: michael@0: static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr, michael@0: png_const_charp error_message)),PNG_NORETURN); michael@0: michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: static void /* PRIVATE */ michael@0: png_default_warning PNGARG((png_const_structrp png_ptr, michael@0: png_const_charp warning_message)); michael@0: #endif /* PNG_WARNINGS_SUPPORTED */ michael@0: michael@0: /* This function is called whenever there is a fatal error. This function michael@0: * should not be changed. If there is a need to handle errors differently, michael@0: * you should supply a replacement error function and use png_set_error_fn() michael@0: * to replace the error function at run-time. michael@0: */ michael@0: #ifdef PNG_ERROR_TEXT_SUPPORTED michael@0: PNG_FUNCTION(void,PNGAPI michael@0: png_error,(png_const_structrp png_ptr, png_const_charp error_message), michael@0: PNG_NORETURN) michael@0: { michael@0: #ifdef PNG_ERROR_NUMBERS_SUPPORTED michael@0: char msg[16]; michael@0: if (png_ptr != NULL) michael@0: { michael@0: if (png_ptr->flags& michael@0: (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) michael@0: { michael@0: if (*error_message == PNG_LITERAL_SHARP) michael@0: { michael@0: /* Strip "#nnnn " from beginning of error message. */ michael@0: int offset; michael@0: for (offset = 1; offset<15; offset++) michael@0: if (error_message[offset] == ' ') michael@0: break; michael@0: michael@0: if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) michael@0: { michael@0: int i; michael@0: for (i = 0; i < offset - 1; i++) michael@0: msg[i] = error_message[i + 1]; michael@0: msg[i - 1] = '\0'; michael@0: error_message = msg; michael@0: } michael@0: michael@0: else michael@0: error_message += offset; michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (png_ptr->flags&PNG_FLAG_STRIP_ERROR_TEXT) michael@0: { michael@0: msg[0] = '0'; michael@0: msg[1] = '\0'; michael@0: error_message = msg; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: if (png_ptr != NULL && png_ptr->error_fn != NULL) michael@0: (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), michael@0: error_message); michael@0: michael@0: /* If the custom handler doesn't exist, or if it returns, michael@0: use the default handler, which will not return. */ michael@0: png_default_error(png_ptr, error_message); michael@0: } michael@0: #else michael@0: PNG_FUNCTION(void,PNGAPI michael@0: png_err,(png_const_structrp png_ptr),PNG_NORETURN) michael@0: { michael@0: /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed michael@0: * erroneously as '\0', instead of the empty string "". This was michael@0: * apparently an error, introduced in libpng-1.2.20, and png_default_error michael@0: * will crash in this case. michael@0: */ michael@0: if (png_ptr != NULL && png_ptr->error_fn != NULL) michael@0: (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ""); michael@0: michael@0: /* If the custom handler doesn't exist, or if it returns, michael@0: use the default handler, which will not return. */ michael@0: png_default_error(png_ptr, ""); michael@0: } michael@0: #endif /* PNG_ERROR_TEXT_SUPPORTED */ michael@0: michael@0: /* Utility to safely appends strings to a buffer. This never errors out so michael@0: * error checking is not required in the caller. michael@0: */ michael@0: size_t michael@0: png_safecat(png_charp buffer, size_t bufsize, size_t pos, michael@0: png_const_charp string) michael@0: { michael@0: if (buffer != NULL && pos < bufsize) michael@0: { michael@0: if (string != NULL) michael@0: while (*string != '\0' && pos < bufsize-1) michael@0: buffer[pos++] = *string++; michael@0: michael@0: buffer[pos] = '\0'; michael@0: } michael@0: michael@0: return pos; michael@0: } michael@0: michael@0: #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED) michael@0: /* Utility to dump an unsigned value into a buffer, given a start pointer and michael@0: * and end pointer (which should point just *beyond* the end of the buffer!) michael@0: * Returns the pointer to the start of the formatted string. michael@0: */ michael@0: png_charp michael@0: png_format_number(png_const_charp start, png_charp end, int format, michael@0: png_alloc_size_t number) michael@0: { michael@0: int count = 0; /* number of digits output */ michael@0: int mincount = 1; /* minimum number required */ michael@0: int output = 0; /* digit output (for the fixed point format) */ michael@0: michael@0: *--end = '\0'; michael@0: michael@0: /* This is written so that the loop always runs at least once, even with michael@0: * number zero. michael@0: */ michael@0: while (end > start && (number != 0 || count < mincount)) michael@0: { michael@0: michael@0: static const char digits[] = "0123456789ABCDEF"; michael@0: michael@0: switch (format) michael@0: { michael@0: case PNG_NUMBER_FORMAT_fixed: michael@0: /* Needs five digits (the fraction) */ michael@0: mincount = 5; michael@0: if (output || number % 10 != 0) michael@0: { michael@0: *--end = digits[number % 10]; michael@0: output = 1; michael@0: } michael@0: number /= 10; michael@0: break; michael@0: michael@0: case PNG_NUMBER_FORMAT_02u: michael@0: /* Expects at least 2 digits. */ michael@0: mincount = 2; michael@0: /* FALL THROUGH */ michael@0: michael@0: case PNG_NUMBER_FORMAT_u: michael@0: *--end = digits[number % 10]; michael@0: number /= 10; michael@0: break; michael@0: michael@0: case PNG_NUMBER_FORMAT_02x: michael@0: /* This format expects at least two digits */ michael@0: mincount = 2; michael@0: /* FALL THROUGH */ michael@0: michael@0: case PNG_NUMBER_FORMAT_x: michael@0: *--end = digits[number & 0xf]; michael@0: number >>= 4; michael@0: break; michael@0: michael@0: default: /* an error */ michael@0: number = 0; michael@0: break; michael@0: } michael@0: michael@0: /* Keep track of the number of digits added */ michael@0: ++count; michael@0: michael@0: /* Float a fixed number here: */ michael@0: if (format == PNG_NUMBER_FORMAT_fixed) if (count == 5) if (end > start) michael@0: { michael@0: /* End of the fraction, but maybe nothing was output? In that case michael@0: * drop the decimal point. If the number is a true zero handle that michael@0: * here. michael@0: */ michael@0: if (output) michael@0: *--end = '.'; michael@0: else if (number == 0) /* and !output */ michael@0: *--end = '0'; michael@0: } michael@0: } michael@0: michael@0: return end; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: /* This function is called whenever there is a non-fatal error. This function michael@0: * should not be changed. If there is a need to handle warnings differently, michael@0: * you should supply a replacement warning function and use michael@0: * png_set_error_fn() to replace the warning function at run-time. michael@0: */ michael@0: void PNGAPI michael@0: png_warning(png_const_structrp png_ptr, png_const_charp warning_message) michael@0: { michael@0: int offset = 0; michael@0: if (png_ptr != NULL) michael@0: { michael@0: #ifdef PNG_ERROR_NUMBERS_SUPPORTED michael@0: if (png_ptr->flags& michael@0: (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) michael@0: #endif michael@0: { michael@0: if (*warning_message == PNG_LITERAL_SHARP) michael@0: { michael@0: for (offset = 1; offset < 15; offset++) michael@0: if (warning_message[offset] == ' ') michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (png_ptr != NULL && png_ptr->warning_fn != NULL) michael@0: (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr), michael@0: warning_message + offset); michael@0: else michael@0: png_default_warning(png_ptr, warning_message + offset); michael@0: } michael@0: michael@0: /* These functions support 'formatted' warning messages with up to michael@0: * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter michael@0: * is introduced by @, where 'number' starts at 1. This follows the michael@0: * standard established by X/Open for internationalizable error messages. michael@0: */ michael@0: void michael@0: png_warning_parameter(png_warning_parameters p, int number, michael@0: png_const_charp string) michael@0: { michael@0: if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT) michael@0: (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string); michael@0: } michael@0: michael@0: void michael@0: png_warning_parameter_unsigned(png_warning_parameters p, int number, int format, michael@0: png_alloc_size_t value) michael@0: { michael@0: char buffer[PNG_NUMBER_BUFFER_SIZE]; michael@0: png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value)); michael@0: } michael@0: michael@0: void michael@0: png_warning_parameter_signed(png_warning_parameters p, int number, int format, michael@0: png_int_32 value) michael@0: { michael@0: png_alloc_size_t u; michael@0: png_charp str; michael@0: char buffer[PNG_NUMBER_BUFFER_SIZE]; michael@0: michael@0: /* Avoid overflow by doing the negate in a png_alloc_size_t: */ michael@0: u = (png_alloc_size_t)value; michael@0: if (value < 0) michael@0: u = ~u + 1; michael@0: michael@0: str = PNG_FORMAT_NUMBER(buffer, format, u); michael@0: michael@0: if (value < 0 && str > buffer) michael@0: *--str = '-'; michael@0: michael@0: png_warning_parameter(p, number, str); michael@0: } michael@0: michael@0: void michael@0: png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p, michael@0: png_const_charp message) michael@0: { michael@0: /* The internal buffer is just 192 bytes - enough for all our messages, michael@0: * overflow doesn't happen because this code checks! If someone figures michael@0: * out how to send us a message longer than 192 bytes, all that will michael@0: * happen is that the message will be truncated appropriately. michael@0: */ michael@0: size_t i = 0; /* Index in the msg[] buffer: */ michael@0: char msg[192]; michael@0: michael@0: /* Each iteration through the following loop writes at most one character michael@0: * to msg[i++] then returns here to validate that there is still space for michael@0: * the trailing '\0'. It may (in the case of a parameter) read more than michael@0: * one character from message[]; it must check for '\0' and continue to the michael@0: * test if it finds the end of string. michael@0: */ michael@0: while (i<(sizeof msg)-1 && *message != '\0') michael@0: { michael@0: /* '@' at end of string is now just printed (previously it was skipped); michael@0: * it is an error in the calling code to terminate the string with @. michael@0: */ michael@0: if (p != NULL && *message == '@' && message[1] != '\0') michael@0: { michael@0: int parameter_char = *++message; /* Consume the '@' */ michael@0: static const char valid_parameters[] = "123456789"; michael@0: int parameter = 0; michael@0: michael@0: /* Search for the parameter digit, the index in the string is the michael@0: * parameter to use. michael@0: */ michael@0: while (valid_parameters[parameter] != parameter_char && michael@0: valid_parameters[parameter] != '\0') michael@0: ++parameter; michael@0: michael@0: /* If the parameter digit is out of range it will just get printed. */ michael@0: if (parameter < PNG_WARNING_PARAMETER_COUNT) michael@0: { michael@0: /* Append this parameter */ michael@0: png_const_charp parm = p[parameter]; michael@0: png_const_charp pend = p[parameter] + (sizeof p[parameter]); michael@0: michael@0: /* No need to copy the trailing '\0' here, but there is no guarantee michael@0: * that parm[] has been initialized, so there is no guarantee of a michael@0: * trailing '\0': michael@0: */ michael@0: while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend) michael@0: msg[i++] = *parm++; michael@0: michael@0: /* Consume the parameter digit too: */ michael@0: ++message; michael@0: continue; michael@0: } michael@0: michael@0: /* else not a parameter and there is a character after the @ sign; just michael@0: * copy that. This is known not to be '\0' because of the test above. michael@0: */ michael@0: } michael@0: michael@0: /* At this point *message can't be '\0', even in the bad parameter case michael@0: * above where there is a lone '@' at the end of the message string. michael@0: */ michael@0: msg[i++] = *message++; michael@0: } michael@0: michael@0: /* i is always less than (sizeof msg), so: */ michael@0: msg[i] = '\0'; michael@0: michael@0: /* And this is the formatted message. It may be larger than michael@0: * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these michael@0: * are not (currently) formatted. michael@0: */ michael@0: png_warning(png_ptr, msg); michael@0: } michael@0: #endif /* PNG_WARNINGS_SUPPORTED */ michael@0: michael@0: #ifdef PNG_BENIGN_ERRORS_SUPPORTED michael@0: void PNGAPI michael@0: png_benign_error(png_const_structrp png_ptr, png_const_charp error_message) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) michael@0: { michael@0: # ifdef PNG_READ_SUPPORTED michael@0: if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && michael@0: png_ptr->chunk_name != 0) michael@0: png_chunk_warning(png_ptr, error_message); michael@0: else michael@0: # endif michael@0: png_warning(png_ptr, error_message); michael@0: } michael@0: michael@0: else michael@0: { michael@0: # ifdef PNG_READ_SUPPORTED michael@0: if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && michael@0: png_ptr->chunk_name != 0) michael@0: png_chunk_error(png_ptr, error_message); michael@0: else michael@0: # endif michael@0: png_error(png_ptr, error_message); michael@0: } michael@0: michael@0: # ifndef PNG_ERROR_TEXT_SUPPORTED michael@0: PNG_UNUSED(error_message) michael@0: # endif michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_app_warning(png_const_structrp png_ptr, png_const_charp error_message) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) michael@0: png_warning(png_ptr, error_message); michael@0: else michael@0: png_error(png_ptr, error_message); michael@0: michael@0: # ifndef PNG_ERROR_TEXT_SUPPORTED michael@0: PNG_UNUSED(error_message) michael@0: # endif michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_app_error(png_const_structrp png_ptr, png_const_charp error_message) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) michael@0: png_warning(png_ptr, error_message); michael@0: else michael@0: png_error(png_ptr, error_message); michael@0: michael@0: # ifndef PNG_ERROR_TEXT_SUPPORTED michael@0: PNG_UNUSED(error_message) michael@0: # endif michael@0: } michael@0: #endif /* BENIGN_ERRORS */ michael@0: michael@0: /* These utilities are used internally to build an error message that relates michael@0: * to the current chunk. The chunk name comes from png_ptr->chunk_name, michael@0: * this is used to prefix the message. The message is limited in length michael@0: * to 63 bytes, the name characters are output as hex digits wrapped in [] michael@0: * if the character is invalid. michael@0: */ michael@0: #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) michael@0: static PNG_CONST char png_digit[16] = { michael@0: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', michael@0: 'A', 'B', 'C', 'D', 'E', 'F' michael@0: }; michael@0: michael@0: #define PNG_MAX_ERROR_TEXT 196 /* Currently limited be profile_error in png.c */ michael@0: #if defined(PNG_WARNINGS_SUPPORTED) || \ michael@0: (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)) michael@0: static void /* PRIVATE */ michael@0: png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp michael@0: error_message) michael@0: { michael@0: png_uint_32 chunk_name = png_ptr->chunk_name; michael@0: int iout = 0, ishift = 24; michael@0: michael@0: while (ishift >= 0) michael@0: { michael@0: int c = (int)(chunk_name >> ishift) & 0xff; michael@0: michael@0: ishift -= 8; michael@0: if (isnonalpha(c)) michael@0: { michael@0: buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; michael@0: buffer[iout++] = png_digit[(c & 0xf0) >> 4]; michael@0: buffer[iout++] = png_digit[c & 0x0f]; michael@0: buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET; michael@0: } michael@0: michael@0: else michael@0: { michael@0: buffer[iout++] = (char)c; michael@0: } michael@0: } michael@0: michael@0: if (error_message == NULL) michael@0: buffer[iout] = '\0'; michael@0: michael@0: else michael@0: { michael@0: int iin = 0; michael@0: michael@0: buffer[iout++] = ':'; michael@0: buffer[iout++] = ' '; michael@0: michael@0: while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0') michael@0: buffer[iout++] = error_message[iin++]; michael@0: michael@0: /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */ michael@0: buffer[iout] = '\0'; michael@0: } michael@0: } michael@0: #endif /* PNG_WARNINGS_SUPPORTED || PNG_ERROR_TEXT_SUPPORTED */ michael@0: michael@0: #if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) michael@0: PNG_FUNCTION(void,PNGAPI michael@0: png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message), michael@0: PNG_NORETURN) michael@0: { michael@0: char msg[18+PNG_MAX_ERROR_TEXT]; michael@0: if (png_ptr == NULL) michael@0: png_error(png_ptr, error_message); michael@0: michael@0: else michael@0: { michael@0: png_format_buffer(png_ptr, msg, error_message); michael@0: png_error(png_ptr, msg); michael@0: } michael@0: } michael@0: #endif /* PNG_READ_SUPPORTED && PNG_ERROR_TEXT_SUPPORTED */ michael@0: michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: void PNGAPI michael@0: png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message) michael@0: { michael@0: char msg[18+PNG_MAX_ERROR_TEXT]; michael@0: if (png_ptr == NULL) michael@0: png_warning(png_ptr, warning_message); michael@0: michael@0: else michael@0: { michael@0: png_format_buffer(png_ptr, msg, warning_message); michael@0: png_warning(png_ptr, msg); michael@0: } michael@0: } michael@0: #endif /* PNG_WARNINGS_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: #ifdef PNG_BENIGN_ERRORS_SUPPORTED michael@0: void PNGAPI michael@0: png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp michael@0: error_message) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) michael@0: png_chunk_warning(png_ptr, error_message); michael@0: michael@0: else michael@0: png_chunk_error(png_ptr, error_message); michael@0: michael@0: # ifndef PNG_ERROR_TEXT_SUPPORTED michael@0: PNG_UNUSED(error_message) michael@0: # endif michael@0: } michael@0: #endif michael@0: #endif /* PNG_READ_SUPPORTED */ michael@0: michael@0: void /* PRIVATE */ michael@0: png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error) michael@0: { michael@0: # ifndef PNG_WARNINGS_SUPPORTED michael@0: PNG_UNUSED(message) michael@0: # endif michael@0: michael@0: /* This is always supported, but for just read or just write it michael@0: * unconditionally does the right thing. michael@0: */ michael@0: # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) michael@0: if (png_ptr->mode & PNG_IS_READ_STRUCT) michael@0: # endif michael@0: michael@0: # ifdef PNG_READ_SUPPORTED michael@0: { michael@0: if (error < PNG_CHUNK_ERROR) michael@0: png_chunk_warning(png_ptr, message); michael@0: michael@0: else michael@0: png_chunk_benign_error(png_ptr, message); michael@0: } michael@0: # endif michael@0: michael@0: # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) michael@0: else if (!(png_ptr->mode & PNG_IS_READ_STRUCT)) michael@0: # endif michael@0: michael@0: # ifdef PNG_WRITE_SUPPORTED michael@0: { michael@0: if (error < PNG_CHUNK_WRITE_ERROR) michael@0: png_app_warning(png_ptr, message); michael@0: michael@0: else michael@0: png_app_error(png_ptr, message); michael@0: } michael@0: # endif michael@0: } michael@0: michael@0: #ifdef PNG_ERROR_TEXT_SUPPORTED michael@0: #ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: PNG_FUNCTION(void, michael@0: png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN) michael@0: { michael@0: # define fixed_message "fixed point overflow in " michael@0: # define fixed_message_ln ((sizeof fixed_message)-1) michael@0: int iin; michael@0: char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT]; michael@0: memcpy(msg, fixed_message, fixed_message_ln); michael@0: iin = 0; michael@0: if (name != NULL) while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0) michael@0: { michael@0: msg[fixed_message_ln + iin] = name[iin]; michael@0: ++iin; michael@0: } michael@0: msg[fixed_message_ln + iin] = 0; michael@0: png_error(png_ptr, msg); michael@0: } michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef PNG_SETJMP_SUPPORTED michael@0: /* This API only exists if ANSI-C style error handling is used, michael@0: * otherwise it is necessary for png_default_error to be overridden. michael@0: */ michael@0: jmp_buf* PNGAPI michael@0: png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn, michael@0: size_t jmp_buf_size) michael@0: { michael@0: /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value michael@0: * and it must not change after that. Libpng doesn't care how big the michael@0: * buffer is, just that it doesn't change. michael@0: * michael@0: * If the buffer size is no *larger* than the size of jmp_buf when libpng is michael@0: * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0 michael@0: * semantics that this call will not fail. If the size is larger, however, michael@0: * the buffer is allocated and this may fail, causing the function to return michael@0: * NULL. michael@0: */ michael@0: if (png_ptr == NULL) michael@0: return NULL; michael@0: michael@0: if (png_ptr->jmp_buf_ptr == NULL) michael@0: { michael@0: png_ptr->jmp_buf_size = 0; /* not allocated */ michael@0: michael@0: if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local)) michael@0: png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; michael@0: michael@0: else michael@0: { michael@0: png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *, michael@0: png_malloc_warn(png_ptr, jmp_buf_size)); michael@0: michael@0: if (png_ptr->jmp_buf_ptr == NULL) michael@0: return NULL; /* new NULL return on OOM */ michael@0: michael@0: png_ptr->jmp_buf_size = jmp_buf_size; michael@0: } michael@0: } michael@0: michael@0: else /* Already allocated: check the size */ michael@0: { michael@0: size_t size = png_ptr->jmp_buf_size; michael@0: michael@0: if (size == 0) michael@0: { michael@0: size = (sizeof png_ptr->jmp_buf_local); michael@0: if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local) michael@0: { michael@0: /* This is an internal error in libpng: somehow we have been left michael@0: * with a stack allocated jmp_buf when the application regained michael@0: * control. It's always possible to fix this up, but for the moment michael@0: * this is a png_error because that makes it easy to detect. michael@0: */ michael@0: png_error(png_ptr, "Libpng jmp_buf still allocated"); michael@0: /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */ michael@0: } michael@0: } michael@0: michael@0: if (size != jmp_buf_size) michael@0: { michael@0: png_warning(png_ptr, "Application jmp_buf size changed"); michael@0: return NULL; /* caller will probably crash: no choice here */ michael@0: } michael@0: } michael@0: michael@0: /* Finally fill in the function, now we have a satisfactory buffer. It is michael@0: * valid to change the function on every call. michael@0: */ michael@0: png_ptr->longjmp_fn = longjmp_fn; michael@0: return png_ptr->jmp_buf_ptr; michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_free_jmpbuf(png_structrp png_ptr) michael@0: { michael@0: if (png_ptr != NULL) michael@0: { michael@0: jmp_buf *jb = png_ptr->jmp_buf_ptr; michael@0: michael@0: /* A size of 0 is used to indicate a local, stack, allocation of the michael@0: * pointer; used here and in png.c michael@0: */ michael@0: if (jb != NULL && png_ptr->jmp_buf_size > 0) michael@0: { michael@0: michael@0: /* This stuff is so that a failure to free the error control structure michael@0: * does not leave libpng in a state with no valid error handling: the michael@0: * free always succeeds, if there is an error it gets ignored. michael@0: */ michael@0: if (jb != &png_ptr->jmp_buf_local) michael@0: { michael@0: /* Make an internal, libpng, jmp_buf to return here */ michael@0: jmp_buf free_jmp_buf; michael@0: michael@0: if (!setjmp(free_jmp_buf)) michael@0: { michael@0: png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */ michael@0: png_ptr->jmp_buf_size = 0; /* stack allocation */ michael@0: png_ptr->longjmp_fn = longjmp; michael@0: png_free(png_ptr, jb); /* Return to setjmp on error */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* *Always* cancel everything out: */ michael@0: png_ptr->jmp_buf_size = 0; michael@0: png_ptr->jmp_buf_ptr = NULL; michael@0: png_ptr->longjmp_fn = 0; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* This is the default error handling function. Note that replacements for michael@0: * this function MUST NOT RETURN, or the program will likely crash. This michael@0: * function is used by default, or if the program supplies NULL for the michael@0: * error function pointer in png_set_error_fn(). michael@0: */ michael@0: static PNG_FUNCTION(void /* PRIVATE */, michael@0: png_default_error,(png_const_structrp png_ptr, png_const_charp error_message), michael@0: PNG_NORETURN) michael@0: { michael@0: #ifdef PNG_CONSOLE_IO_SUPPORTED michael@0: #ifdef PNG_ERROR_NUMBERS_SUPPORTED michael@0: /* Check on NULL only added in 1.5.4 */ michael@0: if (error_message != NULL && *error_message == PNG_LITERAL_SHARP) michael@0: { michael@0: /* Strip "#nnnn " from beginning of error message. */ michael@0: int offset; michael@0: char error_number[16]; michael@0: for (offset = 0; offset<15; offset++) michael@0: { michael@0: error_number[offset] = error_message[offset + 1]; michael@0: if (error_message[offset] == ' ') michael@0: break; michael@0: } michael@0: michael@0: if ((offset > 1) && (offset < 15)) michael@0: { michael@0: error_number[offset - 1] = '\0'; michael@0: fprintf(stderr, "libpng error no. %s: %s", michael@0: error_number, error_message + offset + 1); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: michael@0: else michael@0: { michael@0: fprintf(stderr, "libpng error: %s, offset=%d", michael@0: error_message, offset); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: fprintf(stderr, "libpng error: %s", error_message ? error_message : michael@0: "undefined"); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: #else michael@0: PNG_UNUSED(error_message) /* Make compiler happy */ michael@0: #endif michael@0: png_longjmp(png_ptr, 1); michael@0: } michael@0: michael@0: PNG_FUNCTION(void,PNGAPI michael@0: png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN) michael@0: { michael@0: #ifdef PNG_SETJMP_SUPPORTED michael@0: if (png_ptr && png_ptr->longjmp_fn && png_ptr->jmp_buf_ptr) michael@0: png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val); michael@0: #endif michael@0: michael@0: /* If control reaches this point, png_longjmp() must not return. The only michael@0: * choice is to terminate the whole process (or maybe the thread); to do michael@0: * this the ANSI-C abort() function is used unless a different method is michael@0: * implemented by overriding the default configuration setting for michael@0: * PNG_ABORT(). michael@0: */ michael@0: PNG_ABORT(); michael@0: } michael@0: michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: /* This function is called when there is a warning, but the library thinks michael@0: * it can continue anyway. Replacement functions don't have to do anything michael@0: * here if you don't want them to. In the default configuration, png_ptr is michael@0: * not used, but it is passed in case it may be useful. michael@0: */ michael@0: static void /* PRIVATE */ michael@0: png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message) michael@0: { michael@0: #ifdef PNG_CONSOLE_IO_SUPPORTED michael@0: # ifdef PNG_ERROR_NUMBERS_SUPPORTED michael@0: if (*warning_message == PNG_LITERAL_SHARP) michael@0: { michael@0: int offset; michael@0: char warning_number[16]; michael@0: for (offset = 0; offset < 15; offset++) michael@0: { michael@0: warning_number[offset] = warning_message[offset + 1]; michael@0: if (warning_message[offset] == ' ') michael@0: break; michael@0: } michael@0: michael@0: if ((offset > 1) && (offset < 15)) michael@0: { michael@0: warning_number[offset + 1] = '\0'; michael@0: fprintf(stderr, "libpng warning no. %s: %s", michael@0: warning_number, warning_message + offset); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: michael@0: else michael@0: { michael@0: fprintf(stderr, "libpng warning: %s", michael@0: warning_message); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: } michael@0: else michael@0: # endif michael@0: michael@0: { michael@0: fprintf(stderr, "libpng warning: %s", warning_message); michael@0: fprintf(stderr, PNG_STRING_NEWLINE); michael@0: } michael@0: #else michael@0: PNG_UNUSED(warning_message) /* Make compiler happy */ michael@0: #endif michael@0: PNG_UNUSED(png_ptr) /* Make compiler happy */ michael@0: } michael@0: #endif /* PNG_WARNINGS_SUPPORTED */ michael@0: michael@0: /* This function is called when the application wants to use another method michael@0: * of handling errors and warnings. Note that the error function MUST NOT michael@0: * return to the calling routine or serious problems will occur. The return michael@0: * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1) michael@0: */ michael@0: void PNGAPI michael@0: png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr, michael@0: png_error_ptr error_fn, png_error_ptr warning_fn) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: png_ptr->error_ptr = error_ptr; michael@0: png_ptr->error_fn = error_fn; michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: png_ptr->warning_fn = warning_fn; michael@0: #else michael@0: PNG_UNUSED(warning_fn) michael@0: #endif michael@0: } michael@0: michael@0: michael@0: /* This function returns a pointer to the error_ptr associated with the user michael@0: * functions. The application should free any memory associated with this michael@0: * pointer before png_write_destroy and png_read_destroy are called. michael@0: */ michael@0: png_voidp PNGAPI michael@0: png_get_error_ptr(png_const_structrp png_ptr) michael@0: { michael@0: if (png_ptr == NULL) michael@0: return NULL; michael@0: michael@0: return ((png_voidp)png_ptr->error_ptr); michael@0: } michael@0: michael@0: michael@0: #ifdef PNG_ERROR_NUMBERS_SUPPORTED michael@0: void PNGAPI michael@0: png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode) michael@0: { michael@0: if (png_ptr != NULL) michael@0: { michael@0: png_ptr->flags &= michael@0: ((~(PNG_FLAG_STRIP_ERROR_NUMBERS | michael@0: PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ michael@0: defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) michael@0: /* Currently the above both depend on SETJMP_SUPPORTED, however it would be michael@0: * possible to implement without setjmp support just so long as there is some michael@0: * way to handle the error return here: michael@0: */ michael@0: PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI michael@0: png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message), michael@0: PNG_NORETURN) michael@0: { michael@0: const png_const_structrp png_ptr = png_nonconst_ptr; michael@0: png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); michael@0: michael@0: /* An error is always logged here, overwriting anything (typically a warning) michael@0: * that is already there: michael@0: */ michael@0: if (image != NULL) michael@0: { michael@0: png_safecat(image->message, (sizeof image->message), 0, error_message); michael@0: image->warning_or_error |= PNG_IMAGE_ERROR; michael@0: michael@0: /* Retrieve the jmp_buf from within the png_control, making this work for michael@0: * C++ compilation too is pretty tricky: C++ wants a pointer to the first michael@0: * element of a jmp_buf, but C doesn't tell us the type of that. michael@0: */ michael@0: if (image->opaque != NULL && image->opaque->error_buf != NULL) michael@0: longjmp(png_control_jmp_buf(image->opaque), 1); michael@0: michael@0: /* Missing longjmp buffer, the following is to help debugging: */ michael@0: { michael@0: size_t pos = png_safecat(image->message, (sizeof image->message), 0, michael@0: "bad longjmp: "); michael@0: png_safecat(image->message, (sizeof image->message), pos, michael@0: error_message); michael@0: } michael@0: } michael@0: michael@0: /* Here on an internal programming error. */ michael@0: abort(); michael@0: } michael@0: michael@0: #ifdef PNG_WARNINGS_SUPPORTED michael@0: void /* PRIVATE */ PNGCBAPI michael@0: png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message) michael@0: { michael@0: const png_const_structrp png_ptr = png_nonconst_ptr; michael@0: png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); michael@0: michael@0: /* A warning is only logged if there is no prior warning or error. */ michael@0: if (image->warning_or_error == 0) michael@0: { michael@0: png_safecat(image->message, (sizeof image->message), 0, warning_message); michael@0: image->warning_or_error |= PNG_IMAGE_WARNING; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: int /* PRIVATE */ michael@0: png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg) michael@0: { michael@0: volatile png_imagep image = image_in; michael@0: volatile int result; michael@0: volatile png_voidp saved_error_buf; michael@0: jmp_buf safe_jmpbuf; michael@0: michael@0: /* Safely execute function(arg) with png_error returning to this function. */ michael@0: saved_error_buf = image->opaque->error_buf; michael@0: result = setjmp(safe_jmpbuf) == 0; michael@0: michael@0: if (result) michael@0: { michael@0: michael@0: image->opaque->error_buf = safe_jmpbuf; michael@0: result = function(arg); michael@0: } michael@0: michael@0: image->opaque->error_buf = saved_error_buf; michael@0: michael@0: /* And do the cleanup prior to any failure return. */ michael@0: if (!result) michael@0: png_image_free(image); michael@0: michael@0: return result; michael@0: } michael@0: #endif /* SIMPLIFIED READ/WRITE */ michael@0: #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */